normalizeEmail β GTM Variable Template for String
normalizeEmail EXTENDED String
Normalizes an email address following the Google Ads / Campaign Manager 360 / Meta CAPI Enhanced Conversions specification. Four-step normalization: 1. Trim leading/trailing whitespace 2. Lowercase the entire string 3. Gmail/Googlemail only: remove dots from local part 4. Gmail/Googlemail only: remove +suffix from local part
When to Use This
Section titled βWhen to Use ThisβString Manipulation
Transform, clean, and normalize text data for consistent downstream processing.
Formatting
Normalize casing, spacing, encoding, and presentation of data values.
GA4 Ecommerce
Build and transform ecommerce data structures for GA4 event tracking.
Examples
Section titled βExamplesβGmail dot and plus removal
INPUT
Email Address: [email protected]
OUTPUT
Non-Gmail unchanged
INPUT
Email Address: [email protected]
OUTPUT
Non-string returns undefined
INPUT
Email Address: 12345
OUTPUT
undefined
Live Sandbox
Section titled βLive SandboxβThis is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
normalizeEmail
Email Address
πΎ The email address to normalize before hashing.
Supported formats:
β String
Supported formats:
β String
Input Setup
Input Function (optional)
βοΈ Optional pre-processing function applied to the input before internal logic (e.g., extract email from an object). Internal transformations such as trimming and lowercasing will still apply afterward.
Result Handling
Output Function (optional)
βοΈ Optional function to apply to the result before returning it (e.g., pipe into a SHA-256 hash function). Useful for chaining transformations on the output.
Email Address string
π‘ Type any text to see the result update live
π― Using special value β click input to type instead
Test with:
Falsy
Truthy
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
normalizeEmail()
Related Variables
Section titled βRelated VariablesβSame category: String
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/*** Normalizes an email address for Enhanced Conversions hashing.** Applies the four-step normalization sequence required by Google Ads,* Campaign Manager 360, and Meta CAPI before SHA-256 hashing:* 1. Trim leading and trailing whitespace* 2. Lowercase the entire string* 3. For gmail.com / googlemail.com: remove all dots from the local part* 4. For gmail.com / googlemail.com: remove the + and everything after it in the local part** @param {string} data.src - The email address to normalize.* @param {Function|string} [data.out] - Optional output handler: function to transform result or string with format.** Direct-mode specific parameters:* @param {Function} [data.pre] - Optional pre-processor function to transform src before normalizing.** @returns {string|undefined} The normalized email address, or undefined if input is not a valid email string.** @framework ggLowCodeGTMKit*/const normalizeEmail = function(email) { if (typeof email !== 'string') { return undefined; }
let i = 0; while ( i < email.length && (email.charAt(i) === ' ' || email.charAt(i) === '\t' || email.charAt(i) === '\n' || email.charAt(i) === '\r') ) { i++; }
let j = email.length - 1; while ( j >= 0 && (email.charAt(j) === ' ' || email.charAt(j) === '\t' || email.charAt(j) === '\n' || email.charAt(j) === '\r') ) { j--; }
email = email.slice(i, j + 1);
email = email.toLowerCase();
const atIdx = email.indexOf('@'); if (atIdx === -1 || atIdx === 0 || atIdx === email.length - 1) { return undefined; } if (email.indexOf('@', atIdx + 1) !== -1) { return undefined; }
var local = email.slice(0, atIdx); var domain = email.slice(atIdx + 1);
// Gmail / Googlemail normalization if (domain === 'gmail.com' || domain === 'googlemail.com') { var plusIdx = local.indexOf('+'); if (plusIdx !== -1) { local = local.slice(0, plusIdx); }
local = local.split('.').join(''); }
return local + '@' + domain;};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);
// ===============================================================================// normalizeEmail - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(normalizeEmail(value));// ===============================================================================// normalizeEmail() β Apply Mode// ===============================================================================/*return function(value) { return out(normalizeEmail(value));};*/π§ͺ View Test Scenarios (10 tests)
β
'[example] Gmail dot and plus removal'β
'[example] Non-Gmail unchanged'β
Gmail dot com with dots only - removes dotsβ
Gmail dot com with plus only - removes plus suffixβ
Gmail dot com with dots and plus combined - full normalizationβ
Non-Gmail with dots and plus - preserves dots and plusβ
Mixed case domain and local - lowercases bothβ
'[example] Non-string returns undefined'β
No @ sign - returns undefinedβ
Multiple @ signs - returns undefined