Skip to content

Instantly share code, notes, and snippets.

@vvastdev
Created October 20, 2021 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vvastdev/b1598ea35da031a672089787ee758a3a to your computer and use it in GitHub Desktop.
Save vvastdev/b1598ea35da031a672089787ee758a3a to your computer and use it in GitHub Desktop.
Callback to Shopify privacy API on OneTrust banner interaction, passing boolean based on OT tracking consent codes
<script>
const performanceCookieCategory = '2,';
function waitForOneTrust() {
hasOneTrustLoaded();
let attempts = 0;
const interval = setInterval(function () {
if (hasOneTrustLoaded() || attempts > 100){
clearInterval(interval);
}
attempts++;
}, 100)
}
function hasOneTrustLoaded() {
if(typeof window.OnetrustActiveGroups === 'string'){
//check now
optanonWrapper()
// and wrap and trigger after cookie opt-in
window.OptanonWrapper = optanonWrapper;
return true;
}
return false;
}
function sendConsent(trackingConsent) {
window.Shopify.customerPrivacy.setTrackingConsent(trackingConsent, () => {
console.log('Happy Days')
});
}
function optanonWrapper() {
const trackingConsent = !!window.OnetrustActiveGroups.includes(performanceCookieCategory);
window.Shopify.loadFeatures(
[
{
name: 'consent-tracking-api',
version: '0.1',
},
],
error => {
if (error) {
throw error;
}
sendConsent(trackingConsent)
},
);
}
(function () {
waitForOneTrust();
})();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment