window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
/**
* Function to update Google Consent Mode
* @param {string} status - "granted" or "denied"
*/
function updateConsent(status) {
const consentConfig = status === "granted" ? {
'analytics_storage': 'granted',
'ad_storage': 'granted',
'ad_user_data': 'granted',
'ad_personalization': 'granted'
} : {
'analytics_storage': 'denied',
'ad_storage': 'denied',
'ad_user_data': 'denied',
'ad_personalization': 'denied',
'region': ['AT','BE','BG','CY','CZ','DE','DK','EE','ES','FI','FR','GR','HR','HU','IE','IS','IT','LI','LT','LU','LV','MT','NL','NO','PL','PT','RO','SE','SI','SK']
};
gtag('consent', 'update', consentConfig);
}
// Check Shopify's privacy API for tracking consent
if (window.Shopify && Shopify.customerPrivacy) {
Shopify.customerPrivacy.getTrackingConsent().then(consent => {
if (consent === true) {
updateConsent("granted");
} else {
updateConsent("denied");
}
});
}
// Check if the `_gcl_au` cookie exists
const cookies = document.cookie.split("; ");
const gclCookie = cookies.find(c => c.startsWith("_gcl_au="));
if (!gclCookie) {
gtag('consent', 'default', {
'analytics_storage': 'denied',
'ad_storage': 'denied',
'ad_user_data': 'denied',
'ad_personalization': 'denied',
'region': ['AT','BE','BG','CY','CZ','DE','DK','EE','ES','FI','FR','GR','HR','HU','IE','IS','IT','LI','LT','LU','LV','MT','NL','NO','PL','PT','RO','SE','SI','SK']
});
} else {
updateConsent("granted");
}
// Listen for user clicks on "Accept" or "Decline" buttons
document.addEventListener('click', function(e) {
if (e.target.innerText.includes("Accept")) {
Shopify.customerPrivacy.setTrackingConsent(true);
updateConsent("granted");
} else if (e.target.innerText.includes("Decline")) {
Shopify.customerPrivacy.setTrackingConsent(false);
updateConsent("denied");
}
});
Shopify Customer Events实现Google意见征求模式
杂谈489
未经允许不得转载:迷你笔记 » Shopify Customer Events实现Google意见征求模式

