Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
Hi everyone,
I'm working on integrating OneTrust’s cookie consent banner into our Hydrogen-based store and syncing the user’s choices with Shopify’s Customer Privacy API. My goal is to have the third‑party consent preferences (from OneTrust) automatically update Shopify’s tracking consent state using the customerPrivacy.setTrackingConsent method.
What I’ve Observed:
OneTrust Behavior:
The OneTrust banner loads correctly, and when the user accepts. I am able to create a consent object as such,
{ "marketing": true, "analytics": true, "preferences": true, "sale_of_data": true }
Shopify’s Response:
I call customerPrivacy.setTrackingConsent() method, after loading it via useAnalytics() However, immediately after the banner is dismissed, when i console.log customerPrivacy.currentVisitorConsent(), it shows empty strings for each property. Even after a full page reload (or subsequent route navigation) it is also empty so is the _tracking_consent cookie.
Cookies:
The OptanonConsent cookie is updated correctly by OneTrust, and I also see Shopify’s _tracking_consent cookie being set, but its contents (e.g., "con":{"CMP":{"a":"","m":"","p":"","s":""}}) indicate that the consent values aren’t being registered as expected on the initial call.
Relevant Code Snippet:
import {
useAnalytics,
useLoadScript,
useCustomerPrivacy,
VisitorConsentCollected,
} from '@shopify/hydrogen';
import {useEffect, useCallback} from 'react';
export function OneTrustConsent() {
const {register, customerPrivacy} = useAnalytics();
const {ready} = register('OneTrustConsent');
const oneTrustConsentStatus = useLoadScript(
`https://cdn.cookielaw.org/scripttemplates/otSDKStub.js`,
{
in: 'head',
attributes: {
id: 'OneTrust',
type: 'text/javascript',
'data-domain-script': '',
charset: 'UTF-8',
},
},
);
const oneTrustSync = useCallback(() => {
if (!customerPrivacy || !customerPrivacy?.setTrackingConsent) {
return;
}
const activeGroups = window.OnetrustActiveGroups;
if (!activeGroups) return;
const analyticsCategory = 'C0002';
const preferencesCategory = 'C0003';
const sale_of_Data = 'C0004';
const trackingConsent = {
marketing: activeGroups.includes(sale_of_Data),
analytics: activeGroups.includes(analyticsCategory),
preferences: activeGroups.includes(preferencesCategory),
sale_of_data: activeGroups.includes(sale_of_Data),
};
console.log('OneTrust Banner Consent', trackingConsent);
customerPrivacy.setTrackingConsent(
trackingConsent,
(result) => {
if (result?.error) {
console.error('Error syncing with Shopify customer privacy:', result);
} else {
console.log('Shopify Customer Privacy updated:', result);
}
ready();
setTimeout(() => {
console.log(
'Current Visitor Consent:',
customerPrivacy.currentVisitorConsent(),
);
const trackingCookie = document.cookie.match(
/_tracking_consent=([^;]+)/,
);
console.log(
'Tracking Consent Cookie:',
trackingCookie ? trackingCookie[1] : 'Not found',
);
}, 2000);
},
);
console.log(
'customerPrivacy consent',
customerPrivacy.currentVisitorConsent(),
);
}, [customerPrivacy, ready]);
useEffect(() => {
if (oneTrustConsentStatus !== 'done') return;
if (
window.OneTrust &&
typeof window.OneTrust.OnConsentChanged === 'function'
) {
window.OneTrust.OnConsentChanged(oneTrustSync);
}
oneTrustSync();
}, [oneTrustConsentStatus, oneTrustSync]);
return null;
}
My Questions:
Store Info:
"@shopify/cli": "^3.74.1",
Any insights or recommendations would be greatly appreciated. Thanks in advance for your help!
Hello, have you been able to solve it?
@ChrisWilTTIHow are you importing this component(OneTrustConsent) in main component?
Discover how to increase customer engagement on your store with articles from Shopify A...
By Jacqui Apr 23, 2025Hey Community 👋 Did you know that March 15th is National Everything You Think Is W...
By JasonH Apr 1, 2025Discover how to increase the efficiency of commerce operations with Shopify Academy's l...
By Jacqui Mar 26, 2025