@primarix/easy-consent
Version:
A lightweight consent management solution for Google Analytics and related services. This is a beta version and should be used with caution.
175 lines (169 loc) • 6.12 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
EasyConsent: () => EasyConsent
});
module.exports = __toCommonJS(index_exports);
// src/consent.ts
var EasyConsent = class {
constructor(id) {
this.id = id;
const config = this.getCookie("consentConfig");
if (config) {
this.state = config;
this.isNewUser = false;
} else {
this.isNewUser = true;
this.state = {
"ad_storage": "denied",
"analytics_storage": "denied",
"functionality_storage": "denied",
"personalization_storage": "denied",
"ad_user_data": "denied",
"ad_personalization": "denied",
"security_storage": "denied"
};
this.setCookie("consentConfig", this.state, this.consentConfigDuration);
}
this.head = document.head;
this.analytics_lib = document.createElement("script");
this.init_consent = document.createElement("script");
this.init_GA = document.createElement("script");
this.analytics_lib.setAttribute("src", `https://www.googletagmanager.com/gtag/js?id=${this.id}`);
this.analytics_lib.setAttribute("async", "true");
this.init_consent.setAttribute("data-cookieconsent", "ignore");
this.init_consent.textContent = `
window.dataLayer = window.dataLayer || [];
function gtag(){ dataLayer.push(arguments); }
gtag('consent', 'default', ${JSON.stringify(this.state)});
gtag('set', 'ads_data_redaction', true);
gtag('set', 'url_passthrough', true);
`;
this.init_GA.textContent = `
gtag('js', new Date());
gtag('config', '${this.id}', { send_page_view: false });
`;
this.head.appendChild(this.init_consent);
this.analytics_lib.onload = () => {
this.head.appendChild(this.init_GA);
};
this.head.appendChild(this.analytics_lib);
}
isNewUser;
state;
head;
analytics_lib;
init_consent;
init_GA;
consentConfigDuration = 180;
getCookie(name) {
const cookies = document.cookie.split("; ");
for (let cookie of cookies) {
const [key, value] = cookie.split("=");
if (key === name) {
try {
return JSON.parse(decodeURIComponent(value));
} catch (error) {
console.error("Error al parsear el contenido de la cookie:", error);
return null;
}
}
}
return null;
}
setCookie(name, value, days) {
const jsonValue = JSON.stringify(value);
const encodedValue = encodeURIComponent(jsonValue);
const expires = days ? "; expires=" + new Date(Date.now() + days * 864e5).toUTCString() : "";
document.cookie = `${name}=${encodedValue}${expires}; path=/; SameSite=Lax; Secure`;
}
pageView() {
if (this.state.analytics_storage === "granted") {
window.gtag("event", "page_view", {
page_path: window.location.pathname,
page_title: document.title
});
}
}
dispatchCustomEvent(key, mode) {
const event = new CustomEvent("consent-updated", {
detail: { key, mode, state: this.state, timestamp: Date.now().toString() }
});
window.dispatchEvent(event);
}
update(key, mode) {
if (!window.dataLayer) throw new Error("The gtag function is not defined");
this.state = { ...this.state, [key]: mode };
this.setCookie("consentConfig", this.state, this.consentConfigDuration);
window.gtag("consent", "update", { [key]: mode });
this.pageView();
this.dispatchCustomEvent(key, mode);
}
acceptAll() {
try {
if (!window.dataLayer) throw new Error("The gtag function is not defined");
const v1 = Object.keys(this.state);
const v2 = v1.map((key) => [key, "granted"]);
this.state = Object.fromEntries(v2);
window.gtag("consent", "update", this.state);
this.setCookie("consentConfig", this.state, this.consentConfigDuration);
this.pageView();
this.dispatchCustomEvent("all", "accept-all");
} catch (error) {
console.error("Error in acceptAll:", error);
}
}
rejectAll() {
try {
if (!window.dataLayer) throw new Error("The gtag function is not defined");
const v1 = Object.keys(this.state);
const v2 = v1.map((key) => [key, "denied"]);
this.state = Object.fromEntries(v2);
window.gtag("consent", "update", this.state);
this.setCookie("consentConfig", this.state, this.consentConfigDuration);
this.dispatchCustomEvent("all", "reject-all");
} catch (error) {
console.error("Error in rejectAll:", error);
}
}
isAllConsented() {
return Object.values(this.state).every((value) => value === "granted");
}
isAllDenied() {
return Object.values(this.state).every((value) => value === "denied");
}
updateMultiple(new_state) {
try {
this.state = { ...this.state, ...new_state };
window.gtag("consent", "update", new_state);
this.setCookie("consentConfig", this.state, this.consentConfigDuration);
Object.entries(new_state).forEach(([key, mode]) => {
this.dispatchCustomEvent(key, mode);
});
this.pageView();
} catch (error) {
console.error("Error in updateMultiple:", error);
}
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
EasyConsent
});