@consentry/core
Version:
Core consent management logic for Consentry SDK — handles preferences, script filtering, and integration with analytics tools.
64 lines (61 loc) • 1.9 kB
JavaScript
// src/storage.ts
import Cookies from "js-cookie";
var STORAGE_KEY = "cookiePreferences";
var getConsentPreferences = () => {
if (typeof window === "undefined") return null;
try {
const raw = localStorage.getItem(STORAGE_KEY) || Cookies.get(STORAGE_KEY) || "";
if (!raw) return null;
return JSON.parse(raw);
} catch (e) {
console.warn("Failed to parse consent preferences:", e);
return null;
}
};
var setConsentPreferences = (prefs) => {
if (typeof window === "undefined") return;
const json = JSON.stringify(prefs);
try {
localStorage.setItem(STORAGE_KEY, json);
} catch (e) {
console.warn("localStorage set failed, falling back to cookies", e);
Cookies.set(STORAGE_KEY, json, { expires: 365 });
}
};
// src/defaults.ts
var fallbackDefaults = {
functional: true,
performance: false,
advertising: false,
social: false
};
// src/updateConsentSettings.ts
var updateConsentSettings = (type, settings) => {
if (typeof window !== "undefined" && typeof window.gtag === "function") {
window.gtag("consent", type, settings);
} else {
console.warn("Google Analytics is not loaded yet.");
}
};
// src/getAllowedScripts.ts
function getAllowedScripts(config, prefs, debug = false) {
return config.scripts.filter((script) => {
const requiresConsent = script.consentRequired ?? true;
const userConsented = prefs[script.category];
const shouldLoad = requiresConsent ? !!userConsented : true;
if (debug || config.debug) {
console.debug(
`[consentry] Script "${script.id}" (${script.vendor || "no vendor"}) - category "${script.category}" \u2192 ${shouldLoad ? "\u2705 ALLOWED" : "\u{1F6AB} BLOCKED"}`
);
}
return shouldLoad;
});
}
export {
fallbackDefaults,
getAllowedScripts,
getConsentPreferences,
setConsentPreferences,
updateConsentSettings
};
//# sourceMappingURL=index.mjs.map