diff --git a/example.js b/example.js index 76e8ae1..2d4304b 100644 --- a/example.js +++ b/example.js @@ -1,4 +1,4 @@ -import Crumbs from "./lib"; +import Crumbs, { parseAccepted } from "./lib"; document.addEventListener("DOMContentLoaded", () => { const editCookieButton = document.querySelector(".js-edit"); diff --git a/lib/index.js b/lib/index.js index 99b7bf9..691aaac 100644 --- a/lib/index.js +++ b/lib/index.js @@ -3,14 +3,6 @@ import { editScreen } from "./components/editScreen"; import { cookieBanner } from "./components/cookieBanner"; import { TYPES } from "./constants"; -window.dataLayer = window.dataLayer || []; - -window.gtag = - window.gtag || - function () { - dataLayer.push(arguments); - }; - /** * Formats an array of types by adding a summary to each type. * If a type does not have a summary, it uses the corresponding default consent type summary. @@ -92,6 +84,18 @@ export default class Crumbs extends EventEmitter { this.render(); } + gtag = (...params) => { + window.dataLayer = window.dataLayer || []; + + window.gtag = + window.gtag || + function () { + dataLayer.push(arguments); + }; + + window.gtag(...params); + }; + outdated() { return this.cookie.version < this.version; } @@ -135,7 +139,7 @@ export default class Crumbs extends EventEmitter { const accepted = migration.call(this, this.cookie.value); // The migration returned null rather than a collection of consent, let's clear the cookie - if (accepted === null || !Array.isArray(accepted)) { + if (accepted == null || !Array.isArray(accepted)) { this.accepted = []; this.clear(); return; @@ -156,7 +160,7 @@ export default class Crumbs extends EventEmitter { }; broadcast(type = "default") { - window.gtag( + this.gtag( "consent", type, this.types.reduce( @@ -532,7 +536,10 @@ export default class Crumbs extends EventEmitter { ...this.accepted, ].join("|")};domain=${this.domain}${expiry}; path=/;SameSite=None; Secure`; - this.cookie = value; + this.cookie = { + value, + version: this.version, + }; } /**