Skip to content

normalizeEmail β€” GTM Variable Template for String

VARIABLES β€Ί STRING
normalizeEmail EXTENDED String
Direct (.tpl) Apply (.tpl)

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


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.


Gmail dot and plus removal
INPUT
Email Address: [email protected]
Non-Gmail unchanged
INPUT
Email Address: [email protected]
Non-string returns undefined
INPUT
Email Address: 12345
OUTPUT
undefined

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
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
normalizeEmail()


πŸ“œ 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