refactor(bf): hold the Google Ads id/label in the page, keep ads-consent.js generic

Tag id + conversion label now live as data-ads-id / data-ads-send-to on the banner
element, so the served HTML carries the tag config and the script stays reusable
(missing config -> inert). Re-verified: no cookie/request before consent, cookies +
tag after Accept, conversion + form submit on click, 700 ms fallback submit with the
tag blocked.
This commit is contained in:
webmaster committed 2026-11-02 09:13:16 +00:00
1 parent ae97f4f5eb
commit 6d486ad954
2 files changed
+18 -8

No files matched your search

+16 -8
View File
@@ -1,6 +1,9 @@
/* ads-consent.js — Google Ads tag, gated behind a consent banner. Page-side only.
*
* Page: /odoo-black-friday/. Companion to /static/black-friday.js (which is untouched).
* The tag ID and the conversion label are read from the banner element's data-* attributes
* (#adsConsent[data-ads-id][data-ads-send-to]), so this file stays generic and reusable and
* the page holds the one copy of the config. Without those attributes the script stays inert.
*
* WHY A GATE: the Google Ads tag is NOT cookieless — it sets the advertising cookies
* _gcl_au / _gcl_aw. So nothing loads and no request fires until the visitor accepts.
@@ -17,10 +20,9 @@
(function () {
"use strict";
var AW_ID = "AW-18065522009";
var SEND_TO = "AW-18065522009/vnDUCPDxt7AdENn6p6ZD";
var STORE_KEY = "o4p_ads_consent"; // "granted" | "denied"
var SUBMIT_FALLBACK_MS = 700; // hard cap: submit even if the tag never answers
var cfg = null; // { id, sendTo } — read from the banner, never hard-coded
/* ---- the visitor's stored choice ---- */
function readChoice() {
@@ -61,7 +63,7 @@
/* ---- the tag: loaded ONLY on consent ---- */
function loadGtag() {
if (window.__o4pAdsLoaded) return;
if (!cfg || window.__o4pAdsLoaded) return;
window.__o4pAdsLoaded = true;
window.dataLayer = window.dataLayer || [];
window.gtag =
@@ -70,10 +72,10 @@
window.dataLayer.push(arguments);
};
window.gtag("js", new Date());
window.gtag("config", AW_ID);
window.gtag("config", cfg.id);
var s = document.createElement("script");
s.async = true;
s.src = "https://www.googletagmanager.com/gtag/js?id=" + AW_ID;
s.src = "https://www.googletagmanager.com/gtag/js?id=" + cfg.id;
document.head.appendChild(s);
}
@@ -84,8 +86,8 @@
var action = form.getAttribute("action") || "";
if (action.indexOf("/shop/post_basket") === -1) return;
// No consent, or no tag: do nothing, let the native POST go through untouched.
if (!isGranted() || typeof window.gtag !== "function") return;
// Not configured, no consent, or no tag: do nothing, let the native POST go through.
if (!cfg || !isGranted() || typeof window.gtag !== "function") return;
e.preventDefault();
var submitted = false;
@@ -96,7 +98,7 @@
}
try {
window.gtag("event", "conversion", {
send_to: SEND_TO,
send_to: cfg.sendTo,
event_callback: submitNow,
});
} catch (err) {
@@ -135,6 +137,12 @@
var banner = bannerEl();
if (!banner) return;
// Config lives in the page. Missing config -> stay inert (no tag, no conversion).
var id = banner.getAttribute("data-ads-id");
var sendTo = banner.getAttribute("data-ads-send-to");
if (!id || !sendTo) return;
cfg = { id: id, sendTo: sendTo };
var choice = readChoice();
if (choice === "granted") {
loadGtag(); // returning visitor who already accepted: load straight away