Fix Google Consent Mode integration for GTM / dataLayer
Fall back to pushing consent commands onto window.dataLayer when window.gtag is not available, enabling compatibility with Google Tag Manager setups that don't define a global gtag function. Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
@@ -22,18 +22,26 @@ export class GoogleConsentModeIntegration implements ConsentIntegration {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private getGtag(): ((...args: unknown[]) => void) | null {
|
private getConsentFn(): ((...args: unknown[]) => void) | null {
|
||||||
const w = window as unknown as Record<string, unknown>;
|
const w = window as unknown as Record<string, unknown>;
|
||||||
const gtag = w.gtag as ((...args: unknown[]) => void) | undefined;
|
|
||||||
if (typeof gtag !== "function") return null;
|
if (typeof w.gtag === "function") {
|
||||||
return gtag;
|
return w.gtag as (...args: unknown[]) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Array.isArray(w.dataLayer)) return null;
|
||||||
|
|
||||||
|
const dataLayer = w.dataLayer as unknown[];
|
||||||
|
return function () {
|
||||||
|
dataLayer.push(arguments);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
setDefaults(categories: Category[]): void {
|
setDefaults(categories: Category[]): void {
|
||||||
if (!this.hasMapping(categories)) return;
|
if (!this.hasMapping(categories)) return;
|
||||||
|
|
||||||
const gtag = this.getGtag();
|
const consentFn = this.getConsentFn();
|
||||||
if (!gtag) return;
|
if (!consentFn) return;
|
||||||
|
|
||||||
const defaults: Record<string, string> = {};
|
const defaults: Record<string, string> = {};
|
||||||
for (const cat of categories) {
|
for (const cat of categories) {
|
||||||
@@ -44,7 +52,7 @@ export class GoogleConsentModeIntegration implements ConsentIntegration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (Object.keys(defaults).length > 0) {
|
if (Object.keys(defaults).length > 0) {
|
||||||
gtag("consent", "default", defaults);
|
consentFn("consent", "default", defaults);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,8 +62,8 @@ export class GoogleConsentModeIntegration implements ConsentIntegration {
|
|||||||
): void {
|
): void {
|
||||||
if (!this.hasMapping(categories)) return;
|
if (!this.hasMapping(categories)) return;
|
||||||
|
|
||||||
const gtag = this.getGtag();
|
const consentFn = this.getConsentFn();
|
||||||
if (!gtag) return;
|
if (!consentFn) return;
|
||||||
|
|
||||||
const update: Record<string, string> = {};
|
const update: Record<string, string> = {};
|
||||||
for (const cat of categories) {
|
for (const cat of categories) {
|
||||||
@@ -67,7 +75,7 @@ export class GoogleConsentModeIntegration implements ConsentIntegration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (Object.keys(update).length > 0) {
|
if (Object.keys(update).length > 0) {
|
||||||
gtag("consent", "update", update);
|
consentFn("consent", "update", update);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user