Skip to content

Instantly share code, notes, and snippets.

@stefanatcroud
Last active July 8, 2024 13:14
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 = [
"test.t.test+testing@gmail.com",
"zaphod.beeblebrox@yahoo.com",
"arthur.dent@googlemail.com",
"ford.prefect+earth@planningoffice.org"
]
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