Skip to content

Instantly share code, notes, and snippets.

@stefanatcroud
Last active September 6, 2024 09:50
Show Gist options
  • Save stefanatcroud/d42f9783aca0ec2121a82d64e7e911e4 to your computer and use it in GitHub Desktop.
Save stefanatcroud/d42f9783aca0ec2121a82d64e7e911e4 to your computer and use it in GitHub Desktop.
Demonstrate the email transform required for Enhanced Conversions
// Add the below as a Custom JavaScript variable in GTM and it will use
// the Data Layer Variable "DLV - user_email" as the plaintext email input.
//
// Output will be a preformatted email address adhering to Google's
// requirements for Enhanced Conversions.
function() {
email = {{DLV - user_email}}.trim().toLowerCase().split('@');
return (email[1] === 'gmail.com' || email[1] === 'googlemail.com')
? email[0].replaceAll('.','').split('+')[0] + '@' + email[1]
: email[0].split('+')[0] + '@' + email[1]
}
// Demo of the email transform required for Enhanced Conversions
// Copy & paste this into e.g. Chrome dev console
const userEmails = [
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]"
]
for (email of userEmails) {
console.log(email + " -> " + transformEmailForEC(email));
}
function transformEmailForEC(email) {
email = email.trim().toLowerCase().split('@');
return (email[1] === 'gmail.com' || email[1] === 'googlemail.com')
? email[0].replaceAll('.', '').split('+')[0] + '@' + email[1]
: email[0].split('+')[0] + '@' + email[1];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment