Published · 2 min read
Tracking that waits for a yes
ZONACARS Sound Lab, a car audio shop, was about to start paying for ads on Instagram and Google. Ads are only worth what you can measure, so every WhatsApp click had to reach Google, Meta and TikTok with the campaign that produced it. Colombian data-protection law also asks for consent before tracking cookies are set. The page is plain HTML with no server, so all of this happens in about 270 lines of JavaScript.
Denied by default, from the first line
Before any tag loads, the page tells Google that every kind of storage is denied except what the site needs to work. Ad data is redacted, and URL passthrough keeps the click identifiers in links instead of cookies.
window.dataLayer = window.dataLayer || [];
window.gtag = function () { window.dataLayer.push(arguments); };
gtag("consent", "default", {
ad_storage: "denied",
ad_user_data: "denied",
ad_personalization: "denied",
analytics_storage: "denied",
functionality_storage: "granted",
security_storage: "granted",
wait_for_update: 500
});
gtag("set", "ads_data_redaction", true);
gtag("set", "url_passthrough", true);With that in place, Google's tag loads on every visit. It does not set cookies until the visitor agrees, but it still sends cookieless pings, which Google uses to model the conversions it cannot observe. Measurement does not start at zero for the people who never touch the banner.
The pixels are not even downloaded without a yes
Meta and TikTok have no equivalent of Consent Mode. The only way to keep them from setting cookies is not to load them. Their snippets live inside functions that run only when the visitor accepts advertising cookies, and each one returns early if its ID is empty.
function applyConsent(c) {
gtag("consent", "update", {
analytics_storage: c.analytics ? "granted" : "denied",
ad_storage: c.marketing ? "granted" : "denied",
ad_user_data: c.marketing ? "granted" : "denied",
ad_personalization: c.marketing ? "granted" : "denied"
});
if (c.marketing) { loadMeta(); loadTikTok(); }
}
// Google se carga siempre en modo sin cookies; las cookies solo se usan con consentimiento.
loadGoogle();The banner offers Accept, Reject and Choose, and a button in the footer reopens it with the current choices ticked. The decision is saved in the browser with a date, and is applied again on the next visit as soon as the script runs.
Remember where the visitor came from
A visitor from an ad lands with utm_campaign and a click ID in the address. If they scroll, read the FAQ and click WhatsApp three minutes later, the event would lose that unless something kept it. On arrival, the page saves those parameters in session storage.
(function captureUtm() {
var params = new URLSearchParams(location.search);
var keys = ["utm_source", "utm_medium", "utm_campaign", "utm_content", "utm_term", "gclid", "fbclid", "ttclid"];
var found = {};
keys.forEach(function (k) { if (params.get(k)) found[k] = params.get(k); });
if (Object.keys(found).length) setJSON("sessionStorage", UTM_KEY, found);
})();Session storage, not local storage: the attribution belongs to this visit. A person who comes back next week through Google Maps should not be credited to last week's ad.
One function, three dialects
The buttons know nothing about ad platforms. Every WhatsApp link calls track with contact_whatsapp, the service and where on the page it was; the quote form sends generate_lead. One function merges in the campaign parameters and translates the event for each platform that is loaded.
function track(name, params) {
params = Object.assign({}, utm(), params || {});
gtag("event", name, params);
if (window.fbq) {
if (name === "generate_lead") fbq("track", "Lead", { content_name: params.service || "" });
else if (name === "contact_whatsapp") fbq("track", "Contact", { content_name: params.service || "" });
else fbq("trackCustom", name, params);
}
if (window.ttq) {
if (name === "generate_lead") ttq.track("SubmitForm");
else if (name === "contact_whatsapp") ttq.track("Contact");
}
if ((name === "contact_whatsapp" || name === "generate_lead") && C.googleAdsLeadLabel) {
gtag("event", "conversion", { send_to: C.googleAdsLeadLabel });
}
}Adding a platform means editing this function and the config file — nothing in the HTML. Because the pixels only exist after consent, the window.fbq and window.ttq checks double as the consent check: without a yes there is nothing to call.
What is still missing
- The measurement IDs are empty until the business creates its accounts, so today nothing is sent anywhere.
- The consent choice lives only in the visitor's browser; there is no server-side record of who accepted what.
- Conversions are counted at the click. Whether the WhatsApp chat became a sale happens outside the page and is not measured.