use Customer Privacyhook
hook
A hook that loads the Customer Privacy API.
You can also listen to a document
event for . It will be emitted when the Customer Privacy API is loaded.
Anchor to useCustomerPrivacy-parametersParameters
- Anchor to propspropsCustomerPrivacyApiPropsrequired
CustomerPrivacyApiProps
- checkoutDomain
The production shop checkout domain url.
string
- country
Country code for the shop.
CountryCode
- locale
Language code for the shop.
LanguageCode
- onReady
Callback to be call when customer privacy api is ready.
() => void
- onVisitorConsentCollected
Callback to be called when visitor consent is collected.
(consent: VisitorConsentCollected) => void
- storefrontAccessToken
The storefront access token for the shop.
string
- withPrivacyBanner
Whether to load the Shopify privacy banner as configured in Shopify admin. Defaults to true.
boolean
{
/** The production shop checkout domain url. */
checkoutDomain: string;
/** The storefront access token for the shop. */
storefrontAccessToken: string;
/** Whether to load the Shopify privacy banner as configured in Shopify admin. Defaults to true. */
withPrivacyBanner?: boolean;
/** Country code for the shop. */
country?: CountryCode;
/** Language code for the shop. */
locale?: LanguageCode;
/** Callback to be called when visitor consent is collected. */
onVisitorConsentCollected?: (consent: VisitorConsentCollected) => void;
/** Callback to be call when customer privacy api is ready. */
onReady?: () => void;
}
VisitorConsentCollected
- analyticsAllowed
boolean
- firstPartyMarketingAllowed
boolean
- marketingAllowed
boolean
- preferencesAllowed
boolean
- saleOfDataAllowed
boolean
- thirdPartyMarketingAllowed
boolean
{
analyticsAllowed: boolean;
firstPartyMarketingAllowed: boolean;
marketingAllowed: boolean;
preferencesAllowed: boolean;
saleOfDataAllowed: boolean;
thirdPartyMarketingAllowed: boolean;
}
Was this section helpful?
Example
import {useCustomerPrivacy} from '@shopify/hydrogen';
import {useEffect} from 'react';
export function MyComponent() {
const {customerPrivacy, privacyBanner = null} = useCustomerPrivacy({
storefrontAccessToken: '12345',
checkoutDomain: 'checkout.example.com',
onVisitorConsentCollected: (consent) => {
console.log('Visitor consent collected:', consent);
},
});
useEffect(() => {
if (customerPrivacy) {
// check if user has marketing consent
console.log(
'User marketing consent:',
customerPrivacy.analyticsProcessingAllowed(),
);
// or set tracking consent
customerPrivacy.setTrackingConsent(
{
marketing: true,
analytics: true,
preferences: true,
sale_of_data: true,
},
(data) => {
if (data?.error) {
console.error('Error setting tracking consent:', data.error);
return;
}
console.log('Tracking consent set');
},
);
}
if (privacyBanner) {
privacyBanner.loadBanner();
// or show banner with specific locale and country
// privacyBanner.loadBanner({locale: 'FR', country: 'CA'});
// or show consent preferences banner
// privacyBanner.showPreferences()
// or show consent preferences banner with specific locale and country
// privacyBanner.showPreferences({locale: 'FR', country: 'CA'});
}
}, [customerPrivacy, privacyBanner]);
}
Examples
example
Description
This is the default example
JavaScript
import {useCustomerPrivacy} from '@shopify/hydrogen'; import {useEffect} from 'react'; export function MyComponent() { const {customerPrivacy, privacyBanner = null} = useCustomerPrivacy({ storefrontAccessToken: '12345', checkoutDomain: 'checkout.example.com', onVisitorConsentCollected: (consent) => { console.log('Visitor consent collected:', consent); }, }); useEffect(() => { if (customerPrivacy) { // check if user has marketing consent console.log( 'User marketing consent:', customerPrivacy.analyticsProcessingAllowed(), ); // or set tracking consent customerPrivacy.setTrackingConsent( { marketing: true, analytics: true, preferences: true, sale_of_data: true, }, (data) => { if (data?.error) { console.error('Error setting tracking consent:', data.error); return; } console.log('Tracking consent set'); }, ); } if (privacyBanner) { privacyBanner.loadBanner(); // or show banner with specific locale and country // privacyBanner.loadBanner({locale: 'FR', country: 'CA'}); // or show consent preferences banner // privacyBanner.showPreferences() // or show consent preferences banner with specific locale and country // privacyBanner.showPreferences({locale: 'FR', country: 'CA'}); } }, [customerPrivacy, privacyBanner]); }
TypeScript
import { type VisitorConsentCollected, useCustomerPrivacy, } from '@shopify/hydrogen'; import {useEffect} from 'react'; export function MyComponent() { const {customerPrivacy, privacyBanner = null} = useCustomerPrivacy({ storefrontAccessToken: '12345', checkoutDomain: 'checkout.example.com', onVisitorConsentCollected: (consent: VisitorConsentCollected) => { console.log('Visitor consent collected:', consent); }, }); useEffect(() => { if (customerPrivacy) { // check if user has marketing consent console.log( 'User marketing consent:', customerPrivacy.analyticsProcessingAllowed(), ); // or set tracking consent customerPrivacy.setTrackingConsent( { marketing: true, analytics: true, preferences: true, sale_of_data: true, }, (data) => { if (data?.error) { console.error('Error setting tracking consent:', data.error); return; } console.log('Tracking consent set'); }, ); } if (privacyBanner) { privacyBanner.loadBanner(); // or show banner with specific locale and country // privacyBanner.loadBanner({locale: 'FR', country: 'CA'}); // or show consent preferences banner // privacyBanner.showPreferences() // or show consent preferences banner with specific locale and country // privacyBanner.showPreferences({locale: 'FR', country: 'CA'}); } }, [customerPrivacy, privacyBanner]); }
Anchor to examplesExamples
Example usage with :
Anchor to example-usecustomerprivacyuseCustomerPrivacy
Returns the value of if it exists.
Was this section helpful?
JavaScript
import {getCustomerPrivacy} from '@shopify/hydrogen';
import {useEffect} from 'react';
export function MyComponent() {
useEffect(() => {
const customerPrivacy = getCustomerPrivacy();
if (customerPrivacy) {
console.log('Customer privacy:', customerPrivacy);
}
}, []);
}
Examples
Description
Returns the value of `window.Shopify.customerPrivacy` if it exists.
JavaScript
import {getCustomerPrivacy} from '@shopify/hydrogen'; import {useEffect} from 'react'; export function MyComponent() { useEffect(() => { const customerPrivacy = getCustomerPrivacy(); if (customerPrivacy) { console.log('Customer privacy:', customerPrivacy); } }, []); }