Instantly share code, notes, and snippets.
Created
October 20, 2023 16:33
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save elephantsneverforget/9cc9ce4cfb3412403ff46c8cc0974e90 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const injectScript = require('injectScript'); | |
const encodeUriComponent = require('encodeUriComponent'); | |
const queryPermission = require('queryPermission'); | |
const setDefaultConsentState =require('setDefaultConsentState'); | |
const getCookieValues = require("getCookieValues"); | |
const logToConsole = require("logToConsole"); | |
const isConsentGranted = require('isConsentGranted'); | |
const updateConsentState = require("updateConsentState"); | |
const getContainerVersion = require('getContainerVersion'); | |
const addConsentListener = require('addConsentListener'); | |
const gtagSet = require('gtagSet'); | |
const cookiebotSerial = data.serial; | |
const IABEnabled = data.iabFramework; | |
const language = data.language; | |
const consentNames = ["functionality_storage", "ad_storage", "security_storage", "personalization_storage", "analytics_storage"]; | |
const logConsentValues = (prefix) => { | |
consentNames.forEach((name) => { | |
logToConsole("Cookiebot CMP tag template: " + prefix + " consent status for " + name + ": " + isConsentGranted(name)); | |
}); | |
}; | |
gtagSet('url_passthrough', true); | |
// Listen for consent changes. | |
// This only fires when switched from denied to granted, or from granted to denied | |
// A consent type with no state is considered granted, so the listener won't be called if an unset consent type is updated to granted. | |
consentNames.forEach((name) => { | |
addConsentListener(name, (consentType, granted) => { | |
logToConsole("Cookiebot CMP tag template: Consent state updated, consent state listener called, consent type: " + consentType + " = " + granted); | |
}); | |
}); | |
let scriptUrl = 'https://consent.cookiebot.com/uc.js?cbid=' + encodeUriComponent(cookiebotSerial); | |
if (language === 'variable') | |
{ | |
scriptUrl += '&culture=' + encodeUriComponent(data.languageVariable); | |
} | |
if(IABEnabled) | |
{ | |
scriptUrl += '&framework=IAB'; | |
} | |
if (queryPermission('inject_script', scriptUrl)) { | |
injectScript(scriptUrl, data.gtmOnSuccess, data.gtmOnFailure); | |
} else { | |
data.gtmOnFailure(); | |
} | |
// BEGIN ELEVAR MODIFICATION | |
// UTILS START --------------------------------------------------------- | |
const runFailedCookieGetSequence = () => { | |
logToConsole( | |
"Cookiebot CMP tag template: CookieConsent cookie was not found. Setting all consent types to defaults based on Cookiebot CMP tag template settings" | |
); | |
// Extracted from default template | |
setDefaultConsentState({ | |
'functionality_storage': data.defaultConsentPreferences, | |
'personalization_storage': data.defaultConsentPreferences, | |
'analytics_storage': data.defaultConsentStatistics, | |
'ad_storage': data.defaultConsentMarketing, | |
'security_storage': 'denied', | |
'wait_for_update': 5000 | |
}); | |
logToConsole("Cookiebot CMP tag template: Consent state updated with default consent settings from tag template"); | |
data.gtmOnSuccess(); | |
}; | |
const runSuccessfullCookieGetSequence = (cookieBotCookie) => { | |
// Split the string up by commas | |
const cookieElements = cookieBotCookie[0].split(","); | |
const necessary = getBoolean(cookieElements, "necessary"); | |
const preferences = getBoolean(cookieElements, "preferences"); | |
const statistics = getBoolean(cookieElements, "statistics"); | |
const marketing = getBoolean(cookieElements, "marketing"); | |
setDefaultConsentState({ | |
ad_storage: marketing ? "granted" : "denied", | |
analytics_storage: statistics ? "granted" : "denied", | |
functionality_storage: preferences ? "granted" : "denied", | |
personalization_storage: preferences ? "granted" : "denied", | |
security_storage: "granted", | |
'wait_for_update': data.waitForUpdate | |
}); | |
logToConsole("Cookiebot CMP tag template: Consent state updated with consent values from cookie"); | |
data.gtmOnSuccess(); | |
}; | |
// Get the value and turn it into true false or undefined | |
const getBoolean = (cookieElements, valueName) => { | |
const stringPair = cookieElements.filter(function (item) { | |
return item.indexOf(valueName) >= 0; | |
}); | |
// If we can't find anything return undefined | |
if (stringPair.length === 0) { | |
logToConsole("Cookiebot CMP tag template: ERROR: Could not find value for: " + valueName); | |
return undefined; | |
} | |
const booleanPair = stringPair[0]; | |
if (booleanPair.indexOf("true") > 0) return true; | |
if (booleanPair.indexOf("false") > 0) return false; | |
logToConsole( | |
"Cookiebot CMP tag template: Could not find 'true' or 'false' in string for: " + valueName | |
); | |
return undefined; | |
}; | |
// UTILS END --------------------------------------------------------- | |
const run = (cookieBotCookieArray) => { | |
if (cookieBotCookieArray.length === 0) { | |
// If there is no cookie, set default consent types based on tag template | |
logToConsole("Cookiebot CMP tag template: No cookie found, defaulting to consent settings from tag template"); | |
runFailedCookieGetSequence(cookieBotCookieArray); | |
} else { | |
logToConsole("Cookiebot CMP tag template: Cookie found, parsing cookie for consent settings"); | |
// If cookie value present set consent types accordingly | |
runSuccessfullCookieGetSequence(cookieBotCookieArray); | |
} | |
}; | |
// RUNS SEQUENCE | |
// Get the cookie that stores the consentTypes (returns an array) | |
// logConsentValues("At tag start:"); | |
const cookieBotCookieArray = getCookieValues("CookieConsent"); | |
run(cookieBotCookieArray); | |
logConsentValues("At tag completion"); | |
// END ELEVAR MODIFICATION |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment