I'm using OneTrust for my cookie banner on a website. I'm trying to determine how, using Javascript, to tell if a user has opted in to permit cookies.
I have been able to detect changes to cookie opt-in using this code:
window.OneTrust.OnConsentChanged(() => {
// cookie consent change
})
However, after navigating around my website - or if I reload my site - I'd like to know if the user has at any time in the past opted in (or out) of the OneTrust cookies. For example, if opted out, I can display a subtle message about the benefits of opting in.
I didn't see anything obvious in the OneTrust web sdk:
https://developer.onetrust.com/onetrust/docs/javascript-api
I'm looking for something like window.OneTrust.hasOptedIn()
(of course, that doesn't exist, but you get the idea!).
Or, perhaps there is some other way outside of the OneTrust SDK to achieve this?
This is the solution that I ultimately went with:
const OTCookieName = 'OptanonConsent';
const OTCookieMatch = document.cookie.match(new RegExp('(^| )' + OTCookieName + '=([^;]+)'));
if (OTCookieMatch) {
const OTCookieValue = OTCookieMatch[2];
// replaxe C0003 with whatever OT value we need to check for
if (OTCookieValue && OTCookieValue.includes('C0003')) {
// user has opted-in
}
}