How to Hash Customer Data for Facebook & GA4
October 23, 2024
Why Hash Customer Data?
Hashing is a cryptographic method that converts plain text data into a unique, fixed-length string. It’s used primarily for security purposes, ensuring sensitive customer information like emails or phone numbers is not directly exposed.
For platforms like Facebook and GA4, hashing customer data before transmitting it enables compliance with privacy laws like GDPR and CCPA. It provides a secure way to match events without compromising users’ personal information, thus maintaining user trust and fulfilling legal obligations.
Using SHA-256 for Hashing
SHA-256 (Secure Hash Algorithm 256-bit) is one of the most widely used hashing algorithms today, providing a strong level of security. Here is a JavaScript function you can use to hash customer data, such as email addresses, before sending it to Facebook or Google Analytics.
function() {
return sha256(customerEmail); //replace “customerEmail” with your variable
/**
* Author: Ashiqur Rahman
* Linkedin:https://www.linkedin.com/in/ashiiq61
* Email: [email protected]
*/
function sha256(input){
if(input){
input = input.toString();
for (var $, _, o = Math.pow, f = o(2, 32), t = “length”, a = “”, n = [], h = 8 * input[t], i = sha256.h = sha256.h || [], c = sha256.k = sha256.k || [], e = c[t], s = {}, u = 2; e < 64; u++) {
if (!s[u]) {
for ($ = 0; $ < 313; $ += u) s[$] = u;
i[e] = o(u, .5) * f | 0, c[e++] = o(u, 1 / 3) * f | 0;
}
}
for (input += “\x80”; input[t] % 64 – 56;) input += “\0”;
for ($ = 0; $ < input[t]; $++) {
if (( _ = input.charCodeAt($)) >> 8) return;
n[$ >> 2] |= _ << (3 – $) % 4 * 8;
}
for (_ = 0, n[n[t]] = h / f | 0, n[n[t]] = h; _ < n[t];) {
var v = n.slice(_, _ += 16), g = i;
for ($ = 0, i = i.slice(0, 8); $ < 64; $++) {
var l = v[$ – 15], k = v[$ – 2], x = i[0], S = i[4], d = i[7] + (A(S, 6) ^ A(S, 11) ^ A(S, 25)) + (S & i[5] ^ ~S & i[6]) + c[$] + (v[$] = $ < 16 ? v[$] : v[$ – 16] + (A(l, 7) ^ A(l, 18) ^ l >>> 3) + v[$ – 7] + (A(k, 17) ^ A(k, 19) ^ k >>> 10) | 0), p = (A(x, 2) ^ A(x, 13) ^ A(x, 22)) + (x & i[1] ^ x & i[2] ^ i[1] & i[2]);
(i = [d + p | 0].concat(i))[4] = i[4] + d | 0;
}
for ($ = 0; $ < 8; $++) i[$] = i[$] + g[$] | 0;
}
for ($ = 0; $ < 8; $++) for (_ = 3; _ + 1; _–) {
var w = i[$] >> 8 * _ & 255;
a += (w < 16 ? 0 : “”) + w.toString(16);
}
return a;
}
}
function A(r, $) {
return r >>> $ | r << 32 – $;
}
}
How to Use This Code
The provided JavaScript function uses the SHA-256 hashing algorithm to hash customer data. Replace the customerEmail variable with the appropriate variable you are working with, such as a customer email, phone number, or any other PII you wish to secure.
Example Use Case:
- Tracking Leads with Facebook: Before sending an email address to Facebook for lead tracking, use the sha256(customerEmail) function to hash it.
- GA4 User Matching: Hash customer IDs or emails before sending them to Google Analytics 4 for user matching and custom audiences.
Setting Up in Google Tag Manager (GTM)
- Create a Variable in GTM: Create a variable of any customer data that you want to hash.
- Add Custom JavaScript Variable: Then, add another variable with custom JavaScript using the provided code. Replace customerEmail with the variable you created in the first step.
Hashing in Action
- Client-Side Hashing: The JavaScript function should be executed before transmitting data to any platform. For example, if you’re collecting lead form submissions on your website, hash the email immediately before triggering the event to Facebook or GA4.
- Server-Side Hashing: For more control and security, you can implement this hashing mechanism server-side. Popular languages like Python or Node.js have libraries that can also perform SHA-256 hashing.
Final Thoughts
Data privacy is not just a best practice—it’s a necessity. Hashing customer data is an essential step to ensure compliance, build trust, and responsibly use the power of analytics to grow your business. With the above JavaScript function, you can securely hash sensitive information before sharing it with third-party platforms like Facebook and GA4.
Let’s keep our customer data secure, transparent, and effective for business growth.
Have questions about implementing customer data hashing? Connect with me on LinkedIn or drop me an email at [email protected].