Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC-4329: Ketch footer and Segment updates #179

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
9fee0e0
Add consent management link to footer
eric-schneider Jan 9, 2025
35bacac
dynamic footer text and consent context added to segment calls
colegoldsmith Jan 9, 2025
c0dd2bc
fix consent object
colegoldsmith Jan 9, 2025
e00c57d
Make footer link conditional
eric-schneider Jan 9, 2025
bbca953
Shorten data-track attribute
eric-schneider Jan 9, 2025
55e9c9f
Squash merge remove-gtm-scripts into DOC-4329-footer
colegoldsmith Jan 15, 2025
b5697a9
fix track events so they use consent context correctly. Only load seg…
colegoldsmith Jan 16, 2025
a2de33e
Merge branch 'main' into DOC-4329-footer
colegoldsmith Jan 16, 2025
0cf2a0f
manual ketch url for testing purposes, revert this soon
colegoldsmith Jan 16, 2025
6a7cfaf
test showing ketch preferences on load
colegoldsmith Jan 16, 2025
23e91e0
fix segment object
colegoldsmith Jan 16, 2025
ecabb4f
load segment initially, update ketch scripts
colegoldsmith Jan 16, 2025
9213d6a
hard code keys
colegoldsmith Jan 16, 2025
a363a1c
undo hard coded site keys
colegoldsmith Jan 17, 2025
3c2eea0
add ketch logic to js file and let middleware handle context
colegoldsmith Jan 21, 2025
ec60950
update to latest changes from marketing without gtag scripts
colegoldsmith Jan 24, 2025
89d00a4
UNDO THIS - hard coding segment and ketch site keys
colegoldsmith Jan 24, 2025
0350d78
use segment prod source
colegoldsmith Jan 24, 2025
459745d
force a rebuild
colegoldsmith Jan 24, 2025
0663bb2
use dev segment source
colegoldsmith Jan 24, 2025
2652537
add gtag script and use staging www site footer link
colegoldsmith Jan 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions src/js/04-segment-analytics.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,55 @@
;(function () {
'use strict'

const ketchConsentContext = {
context: {
consent: {
categoryPreferences: {
...(window.ketchConsent?.purposes || {
analytics: false,
essential_services: false,
targeted_advertising: false,
}),
},
},
},
}

const trackEvent = (name, payload) => {
if (window.analytics) {
window.analytics.track(name, {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just found that this is wrong. This sends the context as an event property and not as a context.

The correct way is to send it as a third param. window.analytics.track(eventName, eventProperties, eventContext)

      window.analytics.track(name, payload || {}, ketchConsentContext)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah lol yep good catch, thanks!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok that's updated

...(payload || {}),
...ketchConsentContext,
})
}
}

const trackLinkEvent = (element, name, payload) => {
if (window.analytics) {
window.analytics.trackLink(element, name, {
...(payload || {}),
...ketchConsentContext,
})
}
}

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.
// All tracking events should be done through these functions because they handle the Ketch consent context.
window.trackEvent = trackEvent
window.trackLinkEvent = trackLinkEvent
})()
4 changes: 2 additions & 2 deletions src/js/05-feedback-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/js/07-copy-to-clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions src/partials/footer.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
target="_blank"
data-track="Footer Terms of Use Link Clicked"
>Terms of use</a>
{{#with site.keys.ketchSmartTagUrl}}
|
<a
id="preferenceCenterLink"
href="https://www.datastax.com/preferences"
data-track="Footer Consent Preference Link Clicked"
>Manage Privacy Choices</a>
{{/with}}
</span>
</p>
<p class="text-tertiary !m-0 max-w-[640px] text-xs">Apache, Apache
Expand Down
3 changes: 0 additions & 3 deletions src/partials/head-prelude.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
{{#with site.keys.ketchSmartTagUrl}}
<script>!function(){window.semaphore=window.semaphore||[],window.ketch=function(){window.semaphore.push(arguments)};var e=document.createElement("script");e.type="text/javascript",e.src="{{this}}",e.defer=e.async=!0,document.getElementsByTagName("head")[0].appendChild(e)}();</script>
{{/with}}
37 changes: 31 additions & 6 deletions src/partials/head-scripts.hbs
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
{{#with site.keys.segment}}
<script>
!function(){var i="analytics",analytics=window[i]=window[i]||[];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","screen","once","off","on","addSourceMiddleware","addIntegrationMiddleware","setAnonymousId","addDestinationMiddleware","register"];analytics.factory=function(e){return function(){if(window[i].initialized)return window[i][e].apply(window[i],arguments);var n=Array.prototype.slice.call(arguments);if(["track","screen","alias","group","page","identify"].indexOf(e)>-1){var c=document.querySelector("link[rel='canonical']");n.push({__t:"bpc",c:c&&c.getAttribute("href")||void 0,p:location.pathname,u:location.href,s:location.search,t:document.title,r:document.referrer})}n.unshift(e);analytics.push(n);return analytics}};for(var n=0;n<analytics.methods.length;n++){var key=analytics.methods[n];analytics[key]=analytics.factory(key)}analytics.load=function(key,n){var t=document.createElement("script");t.type="text/javascript";t.async=!0;t.setAttribute("data-global-segment-analytics-key",i);t.src="https://cdn.segment.com/analytics.js/v1/" + key + "/analytics.min.js";var r=document.getElementsByTagName("script")[0];r.parentNode.insertBefore(t,r);analytics._loadOptions=n};analytics._writeKey="{{this}}";;analytics.SNIPPET_VERSION="5.2.1";
analytics.load("{{this}}");
analytics.page();
}}();
</script>
<script>
!function(){var i="analytics",analytics=window[i]=window[i]||[];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","screen","once","off","on","addSourceMiddleware","addIntegrationMiddleware","setAnonymousId","addDestinationMiddleware","register"];analytics.factory=function(e){return function(){if(window[i].initialized)return window[i][e].apply(window[i],arguments);var n=Array.prototype.slice.call(arguments);if(["track","screen","alias","group","page","identify"].indexOf(e)>-1){var c=document.querySelector("link[rel='canonical']");n.push({__t:"bpc",c:c&&c.getAttribute("href")||void 0,p:location.pathname,u:location.href,s:location.search,t:document.title,r:document.referrer})}n.unshift(e);analytics.push(n);return analytics}};for(var n=0;n<analytics.methods.length;n++){var key=analytics.methods[n];analytics[key]=analytics.factory(key)}analytics.load=function(key,n){var t=document.createElement("script");t.type="text/javascript";t.async=!0;t.setAttribute("data-global-segment-analytics-key",i);t.src="https://cdn.segment.com/analytics.js/v1/" + key + "/analytics.min.js";var r=document.getElementsByTagName("script")[0];r.parentNode.insertBefore(t,r);analytics._loadOptions=n};analytics._writeKey="{{this}}";;analytics.SNIPPET_VERSION="5.2.1";
analytics.page();
}}();
</script>
{{/with}}

{{!-- {{#with site.keys.ketchSmartTagUrl}} --}}
<script>!function(){window.semaphore=window.semaphore||[],window.ketch=function(){window.semaphore.push(arguments)};var e=document.createElement("script");e.type="text/javascript",e.src="https://global.ketchcdn.com/web/v3/config/datastax/website_smart_tag/boot.js",e.defer=e.async=!0,document.getElementsByTagName("head")[0].appendChild(e)}();</script>
<script>
if (window.ketch) {
function saveConsent(consent) {
window.ketchConsent = consent;
};
window.ketch("on", "consent", (consent) => {
saveConsent(consent);
// Only load Segment analytics if the user has consented to analytics
if (consent?.purposes?.analytics && window.analytics) {
window.analytics.load("{{../site.keys.segment}}");
Copy link

@EladioGaldamez EladioGaldamez Jan 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we also add the following script:

    window.analytics.addSourceMiddleware(({ payload, next }) => {
      if (window.ketchConsent) {
        payload.obj.context.consent = {
          categoryPreferences: window.ketchConsent?.purposes,
        };
      }
      next(payload);
    });

This will enable the context on all the events and not only track events.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nice! Will do

}
});
window.ketch("on", "userConsentUpdated", saveConsent);
window.ketch("on", "regionInfo", regionInfo => {
var customTextRegions = ["US-CA"];
if (customTextRegions.includes(regionInfo)) {
var preferenceCenterLinkElement = document.getElementById("preferenceCenterLink");
preferenceCenterLinkElement.textContent = "Do Not Sell My Personal Information";
}
})
}
</script>
{{!-- {{/with}} --}}

<script>var uiRootPath = '{{{uiRootPath}}}'</script>

<script>
Expand Down
Loading