Before you begin
- A server container connected to a BonicBD tracking hostname.
- Access to Google Tag Manager Variables.
1. The ten variables
Create a Custom JavaScript variable for each of the ten names below, copying the function code into the variable body. The variable name is what the server tag references, so the name must match exactly — the code and the name are two separate things in GTM, and a correct function under a wrong name silently resolves to undefined.
Each variable is a Custom JavaScript variable: GTM → Variables → New → Custom JavaScript. Paste the function, then save it under the exact name in the label. The copy button beside each block copies the function body only.
function() {
// Priority 1: BBD Context
var ctx = window.BBD_CONTEXT && window.BBD_CONTEXT.user_data;
if (ctx && ctx.em) return ctx.em;
// Priority 2: Enricher events + dataLayer
var dl = window.dataLayer || [];
for (var i = dl.length - 1; i >= 0; i--) {
var e = dl[i];
if (!e) continue;
if (e.bbd_em) return e.bbd_em;
if (e.user_data && e.user_data.em) return e.user_data.em;
if (e.user_data && e.user_data.sha256_email_address) return e.user_data.sha256_email_address;
}
// Priority 3: Cookie fallback
try {
var cookies = ['bbd_pii', '_bbd_ppi', 'bbd_id_cache'];
for (var j = 0; j < cookies.length; j++) {
var match = document.cookie.match(new RegExp('(^|;\\s*)' + cookies[j] + '=([^;]+)'));
if (match) {
var decoded = JSON.parse(atob(decodeURIComponent(match[2])));
if (decoded && decoded.em) return decoded.em;
}
}
} catch (e) {}
return '';
}
function() {
// Google normalization for RAW emails (pre-hashed values pass through untouched)
function normalizeGoogle(v) {
v = String(v || '').trim().toLowerCase().replace(/\s+/g, '');
if (!/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(v)) return '';
var at = v.lastIndexOf('@');
var local = v.slice(0, at), domain = v.slice(at + 1);
if (domain === 'gmail.com' || domain === 'googlemail.com') {
var plus = local.indexOf('+');
if (plus !== -1) local = local.slice(0, plus);
local = local.split('.').join('');
if (!local) return '';
v = local + '@' + domain;
}
return v;
}
// Priority 1: BBD Context — pre-hashed Google variant (emg)
var ctx = window.BBD_CONTEXT && window.BBD_CONTEXT.user_data;
if (ctx) {
if (ctx.emg) return ctx.emg;
if (ctx.em) return ctx.em;
var ctxRaw = normalizeGoogle(ctx.email || '');
if (ctxRaw) return ctxRaw;
}
// Priority 2: Enricher events + dataLayer
var dl = window.dataLayer || [];
for (var i = dl.length - 1; i >= 0; i--) {
var e = dl[i];
if (!e) continue;
if (e.bbd_emg) return e.bbd_emg;
if (e.user_data) {
if (e.user_data.emg) return e.user_data.emg;
if (e.user_data.sha256_email_address_google) return e.user_data.sha256_email_address_google;
if (e.user_data.email) {
var g1 = normalizeGoogle(e.user_data.email);
if (g1) return g1;
}
if (e.user_data.em) return e.user_data.em;
if (e.user_data.sha256_email_address) return e.user_data.sha256_email_address;
}
if (e.email) {
var g2 = normalizeGoogle(e.email);
if (g2) return g2;
}
if (e.billing_email) {
var g3 = normalizeGoogle(e.billing_email);
if (g3) return g3;
}
}
// Priority 3: Cookie fallback
try {
var cookies = ['bbd_pii', '_bbd_ppi', 'bbd_id_cache'];
for (var j = 0; j < cookies.length; j++) {
var match = document.cookie.match(new RegExp('(^|;\\s*)' + cookies[j] + '=([^;]+)'));
if (match) {
var decoded = JSON.parse(atob(decodeURIComponent(match[2])));
if (decoded) {
if (decoded.emg) return decoded.emg;
if (decoded.em) return decoded.em;
}
}
}
} catch (e) {}
return '';
}
function() {
// Priority 1: BBD Context
var ctx = window.BBD_CONTEXT && window.BBD_CONTEXT.user_data;
if (ctx && ctx.ph) return ctx.ph;
// Priority 2: Enricher events + dataLayer
var dl = window.dataLayer || [];
for (var i = dl.length - 1; i >= 0; i--) {
var e = dl[i];
if (!e) continue;
if (e.bbd_ph) return e.bbd_ph;
if (e.user_data && e.user_data.ph) return e.user_data.ph;
if (e.user_data && e.user_data.sha256_phone_number) return e.user_data.sha256_phone_number;
}
// Priority 3: Cookie fallback
try {
var cookies = ['bbd_pii', '_bbd_ppi', 'bbd_id_cache'];
for (var j = 0; j < cookies.length; j++) {
var match = document.cookie.match(new RegExp('(^|;\\s*)' + cookies[j] + '=([^;]+)'));
if (match) {
var decoded = JSON.parse(atob(decodeURIComponent(match[2])));
if (decoded && decoded.ph) return decoded.ph;
}
}
} catch (e) {}
return '';
}
function() {
// Priority 1: BBD Context
var ctx = window.BBD_CONTEXT && window.BBD_CONTEXT.user_data;
if (ctx && ctx.fn) return ctx.fn;
// Priority 2: Enricher events + dataLayer
var dl = window.dataLayer || [];
for (var i = dl.length - 1; i >= 0; i--) {
var e = dl[i];
if (!e) continue;
if (e.bbd_fn) return e.bbd_fn;
if (e.user_data && e.user_data.fn) return e.user_data.fn;
if (e.user_data && e.user_data.sha256_first_name) return e.user_data.sha256_first_name;
if (e.user_data && e.user_data.address && e.user_data.address.sha256_first_name) {
return e.user_data.address.sha256_first_name;
}
}
// Priority 3: Cookie fallback
try {
var cookies = ['bbd_pii', '_bbd_ppi', 'bbd_id_cache'];
for (var j = 0; j < cookies.length; j++) {
var match = document.cookie.match(new RegExp('(^|;\\s*)' + cookies[j] + '=([^;]+)'));
if (match) {
var decoded = JSON.parse(atob(decodeURIComponent(match[2])));
if (decoded && decoded.fn) return decoded.fn;
}
}
} catch (e) {}
return '';
}
function() {
// Priority 1: BBD Context
var ctx = window.BBD_CONTEXT && window.BBD_CONTEXT.user_data;
if (ctx && ctx.ln) return ctx.ln;
// Priority 2: Enricher events + dataLayer
var dl = window.dataLayer || [];
for (var i = dl.length - 1; i >= 0; i--) {
var e = dl[i];
if (!e) continue;
if (e.bbd_ln) return e.bbd_ln;
if (e.user_data && e.user_data.ln) return e.user_data.ln;
if (e.user_data && e.user_data.sha256_last_name) return e.user_data.sha256_last_name;
if (e.user_data && e.user_data.address && e.user_data.address.sha256_last_name) {
return e.user_data.address.sha256_last_name;
}
}
// Priority 3: Cookie fallback
try {
var cookies = ['bbd_pii', '_bbd_ppi', 'bbd_id_cache'];
for (var j = 0; j < cookies.length; j++) {
var match = document.cookie.match(new RegExp('(^|;\\s*)' + cookies[j] + '=([^;]+)'));
if (match) {
var decoded = JSON.parse(atob(decodeURIComponent(match[2])));
if (decoded && decoded.ln) return decoded.ln;
}
}
} catch (e) {}
return '';
}
function() {
// Priority 1: BBD Context
var ctx = window.BBD_CONTEXT && window.BBD_CONTEXT.user_data;
if (ctx && ctx.ct) return ctx.ct;
// Priority 2: Enricher events + dataLayer
var dl = window.dataLayer || [];
for (var i = dl.length - 1; i >= 0; i--) {
var e = dl[i];
if (!e) continue;
if (e.bbd_ct) return e.bbd_ct;
if (e.user_data && e.user_data.ct) return e.user_data.ct;
if (e.user_data && e.user_data.address && e.user_data.address.city) {
return e.user_data.address.city;
}
}
// Priority 3: Cookie fallback
try {
var cookies = ['bbd_pii', '_bbd_ppi', 'bbd_id_cache'];
for (var j = 0; j < cookies.length; j++) {
var match = document.cookie.match(new RegExp('(^|;\\s*)' + cookies[j] + '=([^;]+)'));
if (match) {
var decoded = JSON.parse(atob(decodeURIComponent(match[2])));
if (decoded && decoded.ct) return decoded.ct;
}
}
} catch (e) {}
return '';
}
function() {
// Priority 1: BBD Context
var ctx = window.BBD_CONTEXT && window.BBD_CONTEXT.user_data;
if (ctx && ctx.st) return ctx.st;
// Priority 2: Enricher events + dataLayer
var dl = window.dataLayer || [];
for (var i = dl.length - 1; i >= 0; i--) {
var e = dl[i];
if (!e) continue;
if (e.bbd_st) return e.bbd_st;
if (e.user_data && e.user_data.st) return e.user_data.st;
if (e.user_data && e.user_data.address && e.user_data.address.region) {
return e.user_data.address.region;
}
if (e.user_data && e.user_data.address && e.user_data.address.state) {
return e.user_data.address.state;
}
}
// Priority 3: Cookie fallback
try {
var cookies = ['bbd_pii', '_bbd_ppi', 'bbd_id_cache'];
for (var j = 0; j < cookies.length; j++) {
var match = document.cookie.match(new RegExp('(^|;\\s*)' + cookies[j] + '=([^;]+)'));
if (match) {
var decoded = JSON.parse(atob(decodeURIComponent(match[2])));
if (decoded && decoded.st) return decoded.st;
}
}
} catch (e) {}
return '';
}
function() {
// Priority 1: BBD Context
var ctx = window.BBD_CONTEXT && window.BBD_CONTEXT.user_data;
if (ctx && ctx.zp) return ctx.zp;
// Priority 2: Enricher events + dataLayer
var dl = window.dataLayer || [];
for (var i = dl.length - 1; i >= 0; i--) {
var e = dl[i];
if (!e) continue;
if (e.bbd_zp) return e.bbd_zp;
if (e.user_data && e.user_data.zp) return e.user_data.zp;
if (e.user_data && e.user_data.address && e.user_data.address.postal_code) {
return e.user_data.address.postal_code;
}
if (e.user_data && e.user_data.address && e.user_data.address.zip) {
return e.user_data.address.zip;
}
}
// Priority 3: Cookie fallback
try {
var cookies = ['bbd_pii', '_bbd_ppi', 'bbd_id_cache'];
for (var j = 0; j < cookies.length; j++) {
var match = document.cookie.match(new RegExp('(^|;\\s*)' + cookies[j] + '=([^;]+)'));
if (match) {
var decoded = JSON.parse(atob(decodeURIComponent(match[2])));
if (decoded && decoded.zp) return decoded.zp;
}
}
} catch (e) {}
return '';
}
function() {
// Priority 1: BBD Context
var ctx = window.BBD_CONTEXT && window.BBD_CONTEXT.user_data;
if (ctx && ctx.country) return ctx.country;
// Priority 2: Enricher events + dataLayer
var dl = window.dataLayer || [];
for (var i = dl.length - 1; i >= 0; i--) {
var e = dl[i];
if (!e) continue;
if (e.bbd_country) return e.bbd_country;
if (e.user_data && e.user_data.country) return e.user_data.country;
if (e.user_data && e.user_data.address && e.user_data.address.country) {
return e.user_data.address.country;
}
}
// Priority 3: Cookie fallback
try {
var cookies = ['bbd_pii', '_bbd_ppi', 'bbd_id_cache'];
for (var j = 0; j < cookies.length; j++) {
var match = document.cookie.match(new RegExp('(^|;\\s*)' + cookies[j] + '=([^;]+)'));
if (match) {
var decoded = JSON.parse(atob(decodeURIComponent(match[2])));
if (decoded && decoded.country) return decoded.country;
}
}
} catch (e) {}
return '';
}
function() {
var address = {};
function isHash(v) { return /^[a-f0-9]{64}$/i.test(v); }
function joinStreet(a, b) {
var parts = [];
if (a && String(a).trim()) parts.push(String(a).trim());
if (b && String(b).trim()) parts.push(String(b).trim());
return parts.join(' ');
}
// Raw text lands in address.street (Google hashes it);
// pre-hashed values land in address.sha256_street_address.
function setStreet(v) {
v = String(v || '').trim();
if (!v) return;
if (isHash(v)) {
if (!address.sha256_street_address) address.sha256_street_address = v;
} else if (!address.street) {
address.street = v;
}
}
// Priority 1: BBD Context (hashed values)
var ctx = window.BBD_CONTEXT && window.BBD_CONTEXT.user_data;
if (ctx) {
if (ctx.ct) address.city = ctx.ct;
if (ctx.st) address.region = ctx.st;
if (ctx.st) address.state = ctx.st;
if (ctx.zp) address.postal_code = ctx.zp;
if (ctx.country) address.country = ctx.country;
if (ctx.fn) address.sha256_first_name = ctx.fn;
if (ctx.ln) address.sha256_last_name = ctx.ln;
setStreet(ctx.street);
setStreet(ctx.street_hashed);
setStreet(ctx.sha256_street_address);
}
// Priority 2: Enricher events + dataLayer
var dl = window.dataLayer || [];
for (var i = dl.length - 1; i >= 0; i--) {
var e = dl[i];
if (!e) continue;
// Check bbd_* fields
if (e.bbd_ct && !address.city) address.city = e.bbd_ct;
if (e.bbd_st && !address.region) address.region = e.bbd_st;
if (e.bbd_st && !address.state) address.state = e.bbd_st;
if (e.bbd_zp && !address.postal_code) address.postal_code = e.bbd_zp;
if (e.bbd_country && !address.country) address.country = e.bbd_country;
if (e.bbd_fn && !address.sha256_first_name) address.sha256_first_name = e.bbd_fn;
if (e.bbd_ln && !address.sha256_last_name) address.sha256_last_name = e.bbd_ln;
if (e.bbd_street) setStreet(e.bbd_street);
// Street: billing address line 1 + 2 (WooCommerce convention)
setStreet(joinStreet(e.billing_address_1, e.billing_address_2));
setStreet(e.billing_address);
setStreet(joinStreet(e.address_1, e.address_2));
setStreet(e.street);
setStreet(e.street_address);
if (e.customerBillingAddress) setStreet(e.customerBillingAddress);
setStreet(joinStreet(e.customerBillingAddress1, e.customerBillingAddress2));
if (e.billing && typeof e.billing === 'object') {
setStreet(joinStreet(e.billing.address_1 || e.billing.address1,
e.billing.address_2 || e.billing.address2));
}
// Check user_data
if (e.user_data) {
setStreet(e.user_data.street);
setStreet(e.user_data.street_hashed);
setStreet(e.user_data.billing_address);
setStreet(joinStreet(e.user_data.billing_address_1, e.user_data.billing_address_2));
if (e.user_data.address) {
var addr = e.user_data.address;
if (addr.city && !address.city) address.city = addr.city;
if (addr.region && !address.region) address.region = addr.region;
if (addr.state && !address.state) address.state = addr.state;
if (addr.postal_code && !address.postal_code) address.postal_code = addr.postal_code;
if (addr.country && !address.country) address.country = addr.country;
if (addr.sha256_first_name && !address.sha256_first_name) address.sha256_first_name = addr.sha256_first_name;
if (addr.sha256_last_name && !address.sha256_last_name) address.sha256_last_name = addr.sha256_last_name;
setStreet(addr.street);
setStreet(addr.street_address);
setStreet(addr.sha256_street_address);
setStreet(addr.sha256_street);
}
}
}
// Priority 3: Cookie fallback
try {
var cookies = ['bbd_pii', '_bbd_ppi', 'bbd_id_cache'];
for (var j = 0; j < cookies.length; j++) {
var match = document.cookie.match(new RegExp('(^|;\\s*)' + cookies[j] + '=([^;]+)'));
if (match) {
var decoded = JSON.parse(atob(decodeURIComponent(match[2])));
if (decoded) {
if (decoded.ct && !address.city) address.city = decoded.ct;
if (decoded.st && !address.region) address.region = decoded.st;
if (decoded.st && !address.state) address.state = decoded.st;
if (decoded.zp && !address.postal_code) address.postal_code = decoded.zp;
if (decoded.country && !address.country) address.country = decoded.country;
if (decoded.fn && !address.sha256_first_name) address.sha256_first_name = decoded.fn;
if (decoded.ln && !address.sha256_last_name) address.sha256_last_name = decoded.ln;
if (decoded.street) setStreet(decoded.street);
}
}
}
} catch (e) {}
// Return empty object if no address data
return Object.keys(address).length > 0 ? address : {};
}
2. Where each piece goes
The function code and the GTM variable name are two different things, and each has its own place. The function goes inside the variable body. The variable name goes inside your server tag, where the tag refers to it.
- In GTM, open Variables, then New, then choose Custom JavaScript as the type.
- Paste the function code into the JavaScript field of the variable.
- Set the Name field to the exact variable name, for example UserData-Email. The name is what the server tag references.
- Save the variable, then create the next one the same way.
- In the server container, reference the variable by its name inside the tag. In a Custom JavaScript tag, {{UserData-Email}} reads the variable of that name.
- Run the setup once per container. A variable lives in the container it was created in, so a second server container needs its own copy.
3. The source priority
Each variable checks its sources in the same order: BBD Context first, then the data layer, then cookies.
- BBD Context — window.BBD_CONTEXT.user_data
- The data layer — Enricher events and user_data objects, including bbd_-prefixed fields
- Cookies — bbd_pii, _bbd_ppi, bbd_id_cache
4. Meta normalisation and Google normalisation differ
Meta normalisation lowercases and trims the address and keeps the local part intact. Google normalisation is stricter for gmail.com and googlemail.com: every dot in the local part and any plus alias are removed before hashing, so jane.doe+shop@gmail.com becomes janedoe@gmail.com. Other domains keep their dots.
The tracker emits both forms as emg and bbd_emg. Use UserData-Email-Google for Google Ads user-provided data and UserData-Email for Meta. Swapping them produces a hash the receiving platform cannot match, and the symptom is low match quality rather than an error.
5. The address object and street
The address variable builds a single object from every source. For WooCommerce it reads billing_address_1 and billing_address_2 as the street, and it accepts the same street under billing_address, address_1, address_2, street, street_address, and the equivalent user_data fields.
A raw street value lands in address.street, which the platform hashes itself. A pre-hashed street lands in address.sha256_street_address. Sending a raw street into the hashed field, or the reverse, produces a value the platform will not accept.
6. Using the variables in a tag
Inside a server tag, each variable is referenced by its name. The example below shows the user_data block for a Meta conversion tag and for a Google Ads user-provided data tag.
Use UserData-Email for Meta and UserData-Email-Google for Google Ads. Swapping them produces a hash the receiving platform cannot match, and the symptom is low match quality rather than an error.
// Facebook CAPI Tag
fbq('track', 'Purchase', {
user_data: {
em: {{UserData-Email}},
ph: {{UserData-Phone}},
fn: {{UserData-Fname}},
ln: {{UserData-Lname}},
ct: {{UserData-City}},
st: {{UserData-State}},
zp: {{UserData-Zip}},
country: {{UserData-Country}},
address: {{UserData-Address}}
}
});
// Google Ads Enhanced Conversions (sGTM user-provided data)
// Use UserData-Email-Google here — Gmail/Googlemail dots + aliases are
// already stripped before hashing, which Google's match requires.
gtag('set', 'user_data', {
sha256_email_address: {{UserData-Email-Google}},
sha256_phone_number: {{UserData-Phone}},
address: {{UserData-Address}}
});
7. Testing the variables
Enable GTM Preview Mode, run an action that carries user data, and read the variable values in the debug panel. Confirm each value came from the source you expected.
Check the normalisation on the Google variable specifically. A correct-looking email that hashes to a different result than the platform expects is the failure that is hardest to spot, because the value is present and plausible.
Reviewed against the BonicBD documentation source on 2026-11-07. Dashboard labels can change; follow the current setup guide shown in your account.