Skip to content

Instantly share code, notes, and snippets.

@nedimdragic
Last active December 1, 2020 02:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save nedimdragic/a6e590d6622be2083f2a4e63d319d304 to your computer and use it in GitHub Desktop.
Save nedimdragic/a6e590d6622be2083f2a4e63d319d304 to your computer and use it in GitHub Desktop.
;(function(u){
/**
* @desc Map your OneTrust tag IDs here to Tealium Template IDs found in utag.loader.cfg object like this:
* @param "OneTrust_tag_id": "Tealium_tid",
* @return You can add more lines to suit your tag library in both tools
*/
// Begin Config
var oneTrustTagIdToTealiumTemplateIdConfig = {
"81878": "7110", // e.g.: Google Analytics
"81879": "19004", // e.g.: Adobe Analytics
"81876": "2011", // e.g.: BlueKai
"106": "13099" // e.g.: Maxymiser
};
// End Config
/**
* @desc allows only the OneTrust Consent Manager tag to load, set all other tags to no-load
* @param object u - the utag object
* @return - no return value
*/
var resetLoader = function(u){
for(var uid in u.loader.cfg) {
if(uid !== '18') {
u.loader.cfg[uid].load = 0;
}
}
};
/**
* @desc listener for custom event triggered by OneTrust consent change
* @param object element, string event, function callback - HTML DOM element, custom event name and the handler function
* @return - no return value
*/
var addListener = function(element, event, callback) {
if(element.addEventListener) {
element.addEventListener(event, callback, false);
}
else if(element.attachEvent) {
element.attachEvent('on' + event, callback);
}
};
/**
* @desc get cookie function
* @param string cookie_name - name of the cookie
* @return string cookie value - return cookie value or empty string if no cookie found
*/
var getCookie = function(cookie_name) {
var name = cookie_name + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
return "";
};
/**
* @desc extract cookie information from OneTrust cookie and build object with OneTrust tag IDs as keys and 1|0 as value depening on consent given or not
* @param string cookie_name - name of the cookie
* @return object groupObj - return object with OneTrust tag IDs as keys and 1|0 as value depening on consent given or not
*/
var readCookieAndCreateObj = function(cookie_name){
var groups = "",
groupArr = [],
cookie = "",
groupObj = {},
elem = [];
cookie = getCookie(cookie_name);
if(cookie && cookie !== "") {
cookie = cookie.split('&');
} else {
return "";
}
for(var i = 0; i < cookie.length; i++) {
if(/groups/gi.test(cookie[i])) {
groups = cookie[i].split('=')[1].replace(/0_/g, '');
}
}
groupArr = groups.split(',');
for(var j = 0; j < groupArr.length; j++) {
elem = groupArr[j].split(':');
groupObj[elem[0]] = elem[1];
}
return groupObj;
};
/**
* @desc maps the OneTrust tag IDs to Tealium's template IDs (tid) using oneTrustTagIdToTealiumTemplateIdConfig object and enable loading of tags which received consent in OneTrust
* @param none
* @return none
*/
var compareIds = function(){
var groupObj = readCookieAndCreateObj('OptanonConsent');
if(groupObj === "") {
return;
}
for(var oneTrustTagId in groupObj) {
if(groupObj[oneTrustTagId] === '1') {
for(var key in u.loader.cfg) {
if(oneTrustTagIdToTealiumTemplateIdConfig[oneTrustTagId] == u.loader.cfg[key].tid) {
u.loader.cfg[key].load = 1;
}
}
}
}
};
/**
* @desc custom event handler for consent.onetrust event
* @param none
* @return none
*/
var readCookieAndSetConsentPref = function(){
resetLoader(u);
compareIds();
u.loader.ol = 0;
u.loader.INIT();
};
/**
* @desc init on extension execution during page load, add listener
* @param none
* @return none
*/
resetLoader(u);
addListener(window, "consent.onetrust", readCookieAndSetConsentPref);
compareIds();
})(window.utag);
@nedimdragic
Copy link
Author

comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment