///Set keys based on hostname const SEGMENT_WRITE_KEY = "euWPpq9nroYVrG1wcyneg5rfRfDXZX8k"; /// START SEGMENT SDK CONFIGURATION !function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice."); else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","debug","page","once","off","on","addSourceMiddleware","addIntegrationMiddleware","setAnonymousId","addDestinationMiddleware"];analytics.factory=function(e){return function(){var t=Array.prototype.slice.call(arguments);t.unshift(e);analytics.push(t);return analytics}};for(var e=0;e e.startsWith("C")); previousOneTrustGroupIds = oneTrustActiveGroupArray; localStorage.setItem('onetrust_active_groups', JSON.stringify(oneTrustActiveGroupArray)) return oneTrustActiveGroupArray; } async function getConsentedIntegrations(enabledIntegrations, oneTrustGroupIds) { // Get consented segment categories. const segmentCategories = oneTrustGroupIds .map(oneTrustGroupId => ONE_TRUST_SEGMENT_MAPPING[oneTrustGroupId]) .filter(segmentCategory => segmentCategory) .flat(1); // Filter out `null` mappings. // Filter enabled integrations by consented segment categories. const consentedIntegrations = enabledIntegrations.filter( enabledIntegration => { const isConsented = segmentCategories.includes( enabledIntegration.category ); return isConsented; } ); return consentedIntegrations; } //helper function determines whether user has given consent for any cookie groups OTHER than strictly necessary. Returns boolean function shouldLoadAjs(oneTrustActiveGroups) { return ( oneTrustActiveGroups.includes("C0002") || oneTrustActiveGroups.includes("C0003") || oneTrustActiveGroups.includes("C0004") || oneTrustActiveGroups.includes("C0005") ); } //Calls Segment API to get all destinations connected to source write key. Returns object of destination info async function fetchDestinationForWriteKey(writeKey) { if (!writeKey) { return []; } // To get currently enabled integrations, execute HTTP get to: https://codepen.io/samuelkahr/pen/gOpWyEG const res = await window.fetch( `https://cdn.segment.com/v1/projects/${writeKey}/integrations` ); if (!res.ok) { throw new Error( `Failed to fetch integrations for write key ${writeKey}: HTTP ${res.status} ${res.statusText}` ); } const destinations = await res.json(); // Rename creationName to id to abstract default data model for (const destination of destinations) { destination.id = destination.creationName; delete destination.creationName; } localStorage.setItem('segment_destinations', JSON.stringify(destinations)) return destinations; } //Main executable that calls other helper functions. If user has consented to active group //other than strictly necessary, loads A.js with destination preferences async function loadAnalytics() { const enabledIntegrations = await fetchDestinationForWriteKey( SEGMENT_WRITE_KEY ); const oneTrustGroupIds = getConsentGroups(); const consentedIntegrations = await getConsentedIntegrations( enabledIntegrations, oneTrustGroupIds ); //If All:false is added to integrations object, you need to be sure to add Segment.io:true or else data will not be sent to Segment + cloud mode destinations const destinationPreferences = DISABLE_DESTINATIONS_BY_DEFAULT ? {"All": false, "Segment.io": true, "Webhooks": true} : {}; //setting consented destination to truthy in destinationPreferences consentedIntegrations.forEach(consentedIntegration => { destinationPreferences[consentedIntegration.id] = true; }); //creating array with names of destinations consented to const consentedIntegrationArray = []; for (const value in consentedIntegrations) { consentedIntegrationArray.push(consentedIntegrations[value].name); //console.log('consentedIntegrationArray' + ' '+ consentedIntegrationArray) } //setting destinations w/o consent to false in destinationPreferences //console.log('enabledIntegrations' + ' ' + JSON.stringify(enabledIntegrations)); enabledIntegrations.forEach(integration => { //console.log('integrations' + ' ' + JSON.stringify(integration)); if (!consentedIntegrationArray.includes(integration.name)) { destinationPreferences[integration.id] = false; //console.log('destinationPreferences[integration.id]' + ' ' + JSON.stringify(destinationPreferences[integration.id])) //console.log('destinationPreferences[integration.name]' + ' ' + JSON.stringify(destinationPreferences[integration.name])) } }); //calling helper funciton to determine whether any categories other than strictly necessary have been consented to const loadAjs = shouldLoadAjs(oneTrustGroupIds); if (getConsentGroupChange()){ //localStorage.setItem('onetrust_active_groups', JSON.stringify(oneTrustActiveGroupArray)) console.log("fire") } //Segment integrations object https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/#load-options if (loadAjs) { //console.log('destinationPreferences' + ' ' + destinationPreferences); //Build the Segment Options object that will be pass in every Segment API Call window.destinationPreferences = destinationPreferences //console.log(destinationPreferences); if(!window.analytics.initialized ){ window.analytics.load(SEGMENT_WRITE_KEY, { "integrations": destinationPreferences }); } else { SEGMENT_OPTIONS.integrations = window.destinationPreferences } } else { //Build the Segment Options object that will be pass in every Segment API Call console.log("No groups other than strictly necessary consented to"); if(!window.analytics.initialized ){ window.analytics.load(SEGMENT_WRITE_KEY, { "integrations": destinationPreferences }); } else { SEGMENT_OPTIONS.integrations = window.destinationPreferences } } } function getConsentGroupChange() { let consentGroupChange; if (localStorage.getItem("onetrust_active_groups")) { let arrayA = getConsentGroups(); let arrayB = JSON.parse(localStorage.getItem("onetrust_active_groups")); let difference = arrayA .filter(x => !arrayB.includes(x)) .concat(arrayB.filter(x => !arrayA.includes(x))); console.log(difference); consentGroupChange = difference.length > 0 ? true : false; } else { consentGroupChange = false; } return consentGroupChange; } loadAnalytics(); }