Public Access
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:
1 parent
ae97f4f5eb
commit
6d486ad954
2 files changed
+18
-8
No files matched your search
@@ -1070,6 +1070,8 @@
|
|||||||
class="ads-consent"
|
class="ads-consent"
|
||||||
role="region"
|
role="region"
|
||||||
aria-label="Advertising cookie consent"
|
aria-label="Advertising cookie consent"
|
||||||
|
data-ads-id="AW-18065522009"
|
||||||
|
data-ads-send-to="AW-18065522009/vnDUCPDxt7AdENn6p6ZD"
|
||||||
hidden
|
hidden
|
||||||
>
|
>
|
||||||
<div class="ads-consent-inner">
|
<div class="ads-consent-inner">
|
||||||
|
|||||||
+16
-8
@@ -1,6 +1,9 @@
|
|||||||
/* ads-consent.js — Google Ads tag, gated behind a consent banner. Page-side only.
|
/* 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).
|
* 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
|
* 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.
|
* _gcl_au / _gcl_aw. So nothing loads and no request fires until the visitor accepts.
|
||||||
@@ -17,10 +20,9 @@
|
|||||||
(function () {
|
(function () {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var AW_ID = "AW-18065522009";
|
|
||||||
var SEND_TO = "AW-18065522009/vnDUCPDxt7AdENn6p6ZD";
|
|
||||||
var STORE_KEY = "o4p_ads_consent"; // "granted" | "denied"
|
var STORE_KEY = "o4p_ads_consent"; // "granted" | "denied"
|
||||||
var SUBMIT_FALLBACK_MS = 700; // hard cap: submit even if the tag never answers
|
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 ---- */
|
/* ---- the visitor's stored choice ---- */
|
||||||
function readChoice() {
|
function readChoice() {
|
||||||
@@ -61,7 +63,7 @@
|
|||||||
|
|
||||||
/* ---- the tag: loaded ONLY on consent ---- */
|
/* ---- the tag: loaded ONLY on consent ---- */
|
||||||
function loadGtag() {
|
function loadGtag() {
|
||||||
if (window.__o4pAdsLoaded) return;
|
if (!cfg || window.__o4pAdsLoaded) return;
|
||||||
window.__o4pAdsLoaded = true;
|
window.__o4pAdsLoaded = true;
|
||||||
window.dataLayer = window.dataLayer || [];
|
window.dataLayer = window.dataLayer || [];
|
||||||
window.gtag =
|
window.gtag =
|
||||||
@@ -70,10 +72,10 @@
|
|||||||
window.dataLayer.push(arguments);
|
window.dataLayer.push(arguments);
|
||||||
};
|
};
|
||||||
window.gtag("js", new Date());
|
window.gtag("js", new Date());
|
||||||
window.gtag("config", AW_ID);
|
window.gtag("config", cfg.id);
|
||||||
var s = document.createElement("script");
|
var s = document.createElement("script");
|
||||||
s.async = true;
|
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);
|
document.head.appendChild(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,8 +86,8 @@
|
|||||||
var action = form.getAttribute("action") || "";
|
var action = form.getAttribute("action") || "";
|
||||||
if (action.indexOf("/shop/post_basket") === -1) return;
|
if (action.indexOf("/shop/post_basket") === -1) return;
|
||||||
|
|
||||||
// No consent, or no tag: do nothing, let the native POST go through untouched.
|
// Not configured, no consent, or no tag: do nothing, let the native POST go through.
|
||||||
if (!isGranted() || typeof window.gtag !== "function") return;
|
if (!cfg || !isGranted() || typeof window.gtag !== "function") return;
|
||||||
|
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
var submitted = false;
|
var submitted = false;
|
||||||
@@ -96,7 +98,7 @@
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
window.gtag("event", "conversion", {
|
window.gtag("event", "conversion", {
|
||||||
send_to: SEND_TO,
|
send_to: cfg.sendTo,
|
||||||
event_callback: submitNow,
|
event_callback: submitNow,
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -135,6 +137,12 @@
|
|||||||
var banner = bannerEl();
|
var banner = bannerEl();
|
||||||
if (!banner) return;
|
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();
|
var choice = readChoice();
|
||||||
if (choice === "granted") {
|
if (choice === "granted") {
|
||||||
loadGtag(); // returning visitor who already accepted: load straight away
|
loadGtag(); // returning visitor who already accepted: load straight away
|
||||||
|
|||||||
Reference in new issue
Block a user