Created
May 24, 2024 10:56
-
-
Save stevehanlon/5a6017bdfeefdf9f50c9c687654e183a to your computer and use it in GitHub Desktop.
Tag manager variable for consent mode video
This file contains 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
// this is the code used in the consent mode video at https://youtu.be/Z29FThETSug | |
// This example is for the 'necessary' cookie. | |
// You'll enter this JS into three variables corresponding to | |
// analytics, marketing and necessary | |
function () { | |
const cookietype = 'necessary'; // change this to analytics, marketing or necessary | |
// Function to get cookie by name | |
function getCookie(name) { | |
var nameEQ = name + "="; | |
var ca = document.cookie.split(';'); | |
for(var i=0;i < ca.length;i++) { | |
var c = ca[i]; | |
while (c.charAt(0)==' ') c = c.substring(1,c.length); | |
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); | |
} | |
return null; | |
} | |
// Decode and parse the cc_consent cookie | |
var ccConsentRaw = getCookie('cc_cookie'); | |
if (ccConsentRaw) { | |
try { | |
var ccConsent = JSON.parse(ccConsentRaw); | |
// Check if consent is granted | |
if (ccConsent.level && ccConsent.level.includes(cookietype)) { | |
return "granted"; | |
} | |
} catch(e) { | |
console.error('Error parsing cc_consent cookie:', e); | |
} | |
} | |
// Return "denied" if necessary consent isn't found or the cookie doesn't exist | |
return "denied"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment