@chordcommerce/analytics
Version:
Chord Commerce event tracking
59 lines (58 loc) • 1.7 kB
JavaScript
/**
* Returns Shopify's Customer Privacy API.
*/
const getApi = () => { var _a; return (_a = window.Shopify) === null || _a === void 0 ? void 0 : _a.customerPrivacy; };
/**
* Returns whether the user can currently be tracked.
*/
const userCanBeTracked = () => {
const api = getApi();
if (!api)
return false;
return !!(api.getRegion() &&
api.analyticsProcessingAllowed() &&
api.marketingAllowed());
};
/**
* Returns the user's current consent configuration.
*/
const getCurrentConsent = () => {
const api = getApi();
if (!api)
return {};
return {
consentCategories: {
analytics: api.analyticsProcessingAllowed(),
marketing: api.marketingAllowed(),
preferences: api.preferencesProcessingAllowed(),
sale_of_data: api.saleOfDataAllowed(),
},
};
};
/**
* Handles subsequent changes to consent configuration.
*/
const handleConsentUpdates = (updateCdpConsent) => {
document.addEventListener('visitorConsentCollected', () => {
updateCdpConsent(getCurrentConsent());
});
};
export const shopifyConsentAdapter = () => {
return {
getCurrentConsent,
handleConsentUpdates,
userCanBeTracked,
getCategories: () => {
const api = getApi();
if (!api)
return {};
return {
analytics: api.analyticsProcessingAllowed(),
marketing: api.marketingAllowed(),
preferences: api.preferencesProcessingAllowed(),
sale_of_data: api.saleOfDataAllowed(),
};
},
isConsentReady: () => !!getApi(),
};
};