diff --git a/src/js/04-segment-analytics.js b/src/js/04-segment-analytics.js index 04791bbf..fffa0e30 100644 --- a/src/js/04-segment-analytics.js +++ b/src/js/04-segment-analytics.js @@ -1,18 +1,35 @@ ;(function () { 'use strict' + const trackEvent = (name, payload) => { + if (window.analytics) { + window.analytics.track(name, payload || {}) + } + } + + const trackLinkEvent = (element, name, payload) => { + if (window.analytics) { + window.analytics.trackLink(element, name, payload || {}) + } + } + + // Add click event listeners to all elements with a data-track attribute. if (window.analytics) { const trackedLinkElements = document.querySelectorAll('a[data-track]') const trackedElements = document.querySelectorAll('[data-track]:not(a)') trackedLinkElements.forEach((element) => { - window.analytics.trackLink(element, element.dataset.track) + trackLinkEvent(element, element.dataset.track) }) trackedElements.forEach((element) => { element.addEventListener('click', (e) => { - window.analytics.track(element.dataset.track) + trackEvent(element.dataset.track) }) }) } + + // Expose trackEvent and trackLinkEvent to the global scope. + window.trackEvent = trackEvent + window.trackLinkEvent = trackLinkEvent })() diff --git a/src/js/05-feedback-dialog.js b/src/js/05-feedback-dialog.js index d5a40003..a10d6fcc 100644 --- a/src/js/05-feedback-dialog.js +++ b/src/js/05-feedback-dialog.js @@ -40,8 +40,8 @@ form.onsubmit = (e) => { e.preventDefault() const message = form.elements.message.value - if (message && window.analytics) { - window.analytics.track('Feedback Form', { + if (message && window.trackEvent) { + window.trackEvent('Feedback Form', { message, }) } diff --git a/src/js/07-copy-to-clipboard.js b/src/js/07-copy-to-clipboard.js index 559e9d29..9b7ada5c 100644 --- a/src/js/07-copy-to-clipboard.js +++ b/src/js/07-copy-to-clipboard.js @@ -92,9 +92,9 @@ } function trackCopy (language, title, text) { - if (window.analytics) { + if (window.trackEvent) { var sample = text.slice(0, 50).replace(/\s+/g, ' ').trim() - window.analytics.track('Code Snippet Copied', { + window.trackEvent('Code Snippet Copied', { snippetLanguage: language, snippetTitle: title, snippetSample: sample, diff --git a/src/js/10-ketch-consent.js b/src/js/10-ketch-consent.js new file mode 100644 index 00000000..844fe40e --- /dev/null +++ b/src/js/10-ketch-consent.js @@ -0,0 +1,85 @@ +;(function () { + 'use strict' + + window.analytics.ready(() => { + window.ketch('once', 'consent', onKetchConsent) + window.ketch('on', 'consent', onKetchConsentGtagTrack) + window.ketch('on', 'userConsentUpdated', onKetchConsentUpdated) + }) + + // If the user is in the US-CA region, change the text of the preference center link + window.ketch('on', 'regionInfo', (regionInfo) => { + const customTextRegions = ['US-CA'] + if (customTextRegions.includes(regionInfo)) { + const preferenceCenterLinkElement = document.getElementById('preferenceCenterLink') + if (preferenceCenterLinkElement) { + preferenceCenterLinkElement.textContent = 'Do Not Sell My Personal Information' + } + } + }) + + // If the user is in the default jurisdiction, remove the preference center link + window.ketch('on', 'jurisdiction', (jurisdiction) => { + if (jurisdiction.includes('default')) { + const preferenceCenterContainerElement = document.getElementById('preferenceCenterContainer') + if (preferenceCenterContainerElement) { + preferenceCenterContainerElement.remove() + } + } + }) + + // Once - This will be fired only one time, will initialize all the main features. + const onKetchConsent = (consent) => { + window.ketchConsent = consent + addKetchConsentToContextMiddleware() + window.analytics.page() + // loadScripts(); // Load any script if we have scripts to fire after ketch consent is fired. + } + + // on - Each time the user changes the preferences, save them to the global variable + const onKetchConsentUpdated = (consent) => { + window.ketchConsent = consent + } + + // On - each time the consent is loaded, track it to the gtag event + const onKetchConsentGtagTrack = (consent) => { + if (window.gtag && + consent.purposes && + 'analytics' in consent.purposes && + 'targeted_advertising' in consent.purposes + ) { + const analyticsString = consent.purposes.analytics === true ? 'granted' : 'denied' + const targetedAdsString = consent.purposes.targeted_advertising === true ? 'granted' : 'denied' + + const gtagObject = { + analytics_storage: analyticsString, + ad_personalization: targetedAdsString, + ad_storage: targetedAdsString, + ad_user_data: targetedAdsString, + } + window.gtag('consent', 'update', gtagObject) + } + } + + // Use the analytics.addSourceMiddleware function to include the consent on all the events + const addKetchConsentToContextMiddleware = () => { + window.analytics.addSourceMiddleware(({ payload, next }) => { + if (window.ketchConsent) { + const analyticsString = window.ketchConsent.purposes.analytics === true ? 'granted' : 'denied' + const targetedAdsString = window.ketchConsent.purposes.targeted_advertising === true ? 'granted' : 'denied' + + payload.obj.properties = { + ...(payload.obj.properties || {}), + analyticsStorageConsentState: analyticsString, + adsStorageConsentState: targetedAdsString, + adUserDataConsentState: targetedAdsString, + adPersonalizationConsentState: targetedAdsString, + } + payload.obj.context.consent = { + categoryPreferences: window.ketchConsent?.purposes, + } + } + next(payload) + }) + } +})() diff --git a/src/partials/footer.hbs b/src/partials/footer.hbs index 694938a5..27718253 100644 --- a/src/partials/footer.hbs +++ b/src/partials/footer.hbs @@ -24,6 +24,16 @@ target="_blank" data-track="Footer Terms of Use Link Clicked" >Terms of use + {{#with site.keys.ketchSmartTagUrl}} + + | + Manage Privacy Choices + + {{/with}}

Apache, Apache diff --git a/src/partials/head-prelude.hbs b/src/partials/head-prelude.hbs index f0f73b16..a8b267d3 100644 --- a/src/partials/head-prelude.hbs +++ b/src/partials/head-prelude.hbs @@ -1,5 +1,2 @@ - {{#with site.keys.ketchSmartTagUrl}} - - {{/with}} diff --git a/src/partials/head-scripts.hbs b/src/partials/head-scripts.hbs index 7bea401f..9e543ddd 100644 --- a/src/partials/head-scripts.hbs +++ b/src/partials/head-scripts.hbs @@ -1,10 +1,13 @@ {{#with site.keys.segment}} - + +{{/with}} + +{{#with site.keys.ketchSmartTagUrl}} + {{/with}}