gdpr-consent
Version:
GDPR banner to comply with the European cookie law. Inspired by tarteaucitronjs.
49 lines (48 loc) • 1.79 kB
JavaScript
import { addScript } from "../utils/index.js";
export const googleanalytics = ((user) => {
const googleIdentifier = user.googleanalyticsUa;
let tagUaCookie = "_gat_gtag_" + googleIdentifier;
let tagGCookie = "_ga_" + googleIdentifier;
tagUaCookie = tagUaCookie.replace(/-/g, "_");
tagGCookie = tagGCookie.replace(/G-/g, "");
const isGoogleAdsLinked = user.googleanalyticsWithEnabledAdsLink === true;
return {
key: "googleanalytics",
type: "analytic",
name: isGoogleAdsLinked ? "Google Analytics (GA4) & Google Ads" : "Google Analytics (GA4)",
uri: "https://policies.google.com/privacy",
needConsent: true,
lazyConsent: false,
cookies: [
"_ga",
"_gat",
"_gid",
"__utma",
"__utmb",
"__utmc",
"__utmt",
"__utmz",
tagUaCookie,
tagGCookie,
"_gcl_au",
],
js: function () {
window.dataLayer = window.dataLayer || [];
addScript("https://www.googletagmanager.com/gtag/js?id=" + user.googleanalyticsUa, {}, function () {
window.gtag = function gtag() {
window.dataLayer.push(arguments);
};
window.gtag("js", new Date());
if (user.googleanalyticsInitOptions) {
window.gtag("config", user.googleanalyticsUa, user.googleanalyticsInitOptions);
}
else {
window.gtag("config", user.googleanalyticsUa);
}
if (typeof user.googleanalyticsMore === "function") {
user.googleanalyticsMore();
}
});
},
};
});