// Store the previous consent state on window object window.didomiPreviousGeoConsentEnabled = null; // Externalized consent handler function function handleConsentChange(event, shouldReload) { console.log("Consent changed", event); // Get current status const currentStatus = window.Didomi.getUserStatus(); // Check current geo consent state const currentGeoConsentEnabled = currentStatus.vendors.global_consent.enabled.indexOf("c:sitecore-geopersonalisation") > -1; // Set cookie based on consent with Secure and SameSite attributes if (currentGeoConsentEnabled) { document.cookie = 'MSCB2C_ANALYTICS_CONSENT="true"; Secure; SameSite=Strict; path=/'; } else { document.cookie = 'MSCB2C_ANALYTICS_CONSENT="false"; Secure; SameSite=Strict; path=/'; } console.info(currentStatus); // Only reload if triggered by event AND the consent state actually changed if (shouldReload && window.didomiPreviousGeoConsentEnabled !== null && window.didomiPreviousGeoConsentEnabled !== currentGeoConsentEnabled) { console.log("Geo consent changed from", window.didomiPreviousGeoConsentEnabled, "to", currentGeoConsentEnabled, "- reloading"); window.didomiPreviousGeoConsentEnabled = currentGeoConsentEnabled; window.location = window.location + '' ; //force redirect with params } else { // Update stored state without reloading window.didomiPreviousGeoConsentEnabled = currentGeoConsentEnabled; } } // Register the event listener with externalized function window.didomiEventListeners.push({ event: "consent.changed", listener: function(event) { handleConsentChange(event, true); // Reload only if changed } }); // Execute once at initialization (without reload) handleConsentChange({ type: 'init' }, false);