Created
September 22, 2022 14:27
-
-
Save jdjuan/d26f30773323a3009dfeab97b2811e3c to your computer and use it in GitHub Desktop.
Read OneTrust Cookies
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 cookie = | |
"'WATRC=2D0FDD6D-6F29-49E6-AEE0-42080FAA8120; _ga_6NJJBYQCBV=GS1.1.1658756504.1.1.1658756505.59; _gcl_au=1.1.2054335917.1659447846; _ga_Y9TTEFYVZC=GS1.1.1662476698.1.1.1662478229.0.0.0; _ga_TW3TH3JW1S=GS1.1.1662477516.1.1.1662478229.0.0.0; _ga_M0YDYEWKG9=GS1.1.1662477395.1.1.1662478229.0.0.0; _ga_MF7T4Q7V2G=GS1.1.1662477370.1.1.1662478229.0.0.0; _ga_ZE6FQL0YJS=GS1.1.1663668306.2.1.1663668449.0.0.0; _ga=GA1.2.402987297.1658756505; _gid=GA1.2.534896851.1663742869; WANV=AA2512FF-2F73-448D-8675-6A3DA6184C9F; OptanonAlertBoxClosed=2022-09-22T07:22:21.804Z; OptanonConsent=isGpcEnabled=0&datestamp=Thu+Sep+22+2022+09%3A22%3A22+GMT%2B0200+(Mitteleurop%C3%A4ische+Sommerzeit)&version=6.36.0&isIABGlobal=false&hosts=&consentId=f51c839a-cd58-489e-9f9c-e8a6e3931a0d&interactionCount=8&landingPath=NotLandingPage&groups=C0001%3A1%2CC0002%3A0%2CC0004%3A0&geolocation=%3B&AwaitingReconsent=false'"; | |
function getOneTrustGrants(cookies) { | |
const escapedCookies = decodeURIComponent(cookies); | |
const result = escapedCookies.split('; ').map((cookie) => { | |
const [prop, ...value] = cookie.split('='); | |
return [prop, value.join('=')]; | |
}); | |
const oneTrustCookie = Object.fromEntries(result)['OptanonConsent']; | |
const oneTrustProps = Object.fromEntries( | |
oneTrustCookie.split('&').map((prop) => prop.split('=')) | |
); | |
console.log(oneTrustProps['groups'].split(',')); | |
const grants = oneTrustProps['groups'].split(',').reduce((acc, group) => { | |
const [groupName, isActivated] = group.split(':'); | |
acc[groupName] = isActivated === '1'; | |
return acc; | |
}, {}); | |
return grants; | |
} | |
console.log(getOneTrustGrants(cookie)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment