Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save juanpprieto/51265f9ac1526f61b770c30180859892 to your computer and use it in GitHub Desktop.
Save juanpprieto/51265f9ac1526f61b770c30180859892 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