// ── Extract customer email for advanced matching ───────────
// Booqable doesn't expose email in cartData, so we check the DOM.
// The email input is still present in the (hidden) checkout form.
var email = null;
var emailSelectors = [
'input[name="email"]',
'input[type="email"]',
'[data-customer-email]',
'[data-email]',
'.customer-email'
];
for (var i = 0; i < emailSelectors.length; i++) {
var el = document.querySelector(emailSelectors[i]);
if (el) {
var raw = (el.value || el.getAttribute('data-customer-email') || el.textContent || '').trim().toLowerCase();
if (/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(raw)) {
email = raw;
console.log('[AAB FB] Email found via: ' + emailSelectors[i]);
break;
}
}
}
// Fallback: scan visible page text for an email address
if (!email) {
var match = (document.body.innerText || '').match(/[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}/);
if (match) {
email = match[0].trim().toLowerCase();
console.log('[AAB FB] Email found via text scan → ' + email);
}
}
if (!email) {
console.warn('[AAB FB] Email not found — Purchase will fire without advanced matching.');
}
// ── SHA-256 hash the email (required by Facebook) ─────────
function sha256(str) {
return crypto.subtle.digest('SHA-256', new TextEncoder().encode(str))
.then(function (buf) {
return Array.from(new Uint8Array(buf))
.map(function (b) { return b.toString(16).padStart(2, '0'); })
.join('');
});
}
// ── Fire the Purchase event ────────────────────────────────
function doFire(hashedEmail) {
if (typeof fbq === 'undefined') {
console.warn('[AAB FB] fbq not found. Check that the Meta Pixel is initialised.');
return;
}
if (hashedEmail) {
fbq('init', '4488114761516320', { em: hashedEmail });
console.log('[AAB FB] Advanced matching set with hashed email.');
}
fbq('track', 'Purchase', {
value: value,
currency: 'CAD'
}, {
eventID: eventId
});
console.log('[AAB FB] Purchase fired — $' + value + ' CAD | eventID: ' + eventId);
}
if (email) {
sha256(email).then(doFire);
} else {
doFire(null);
}
});
})