Skip to content

Instantly share code, notes, and snippets.

@townivan
Created May 22, 2024 20:51
Show Gist options
  • Save townivan/6be4ddf6104ec98938c2c5a529342c5c to your computer and use it in GitHub Desktop.
Save townivan/6be4ddf6104ec98938c2c5a529342c5c to your computer and use it in GitHub Desktop.
Acoustic Event Tracking - Downloads
/*
A. Acoustic "event" tracking
This is used to update the DOM of a button or link with an Acoustic web tracking click event.
This script uses the url of a button/link to target and tag it. Each object in the "matches" array represents a url that will be tracked with a particular "nameValue" and "typeValue". This script will tag all buttons/links that have a substring that matches the "urlfragment" value.
If you want to track multiple buttons or links, create an object for each unique url.
- urlfragment: part of a button/link url that will uniquely match only the button/link that you want to track.
- nameValue: a string provided by Digital Marketing than Acoustic will use to track the web event.
- typeValue: a string provided by Digital Marketing (but usually not given because it's assumed to be the most common option which is "content-download". Sometimes it will be "button-click". This is used to categorize within Acoustic.
With the new CSP changes in Sitecore, put the following script into "Script Body End Snippet" > "Script Inline". (no <script> tags)
*/
// Acoustic web event tracking. Add tracking to all buttons/links that contain urlfragment in their url....
let matches = [
{urlfragment:'Guide-Fall-2021/Enrollment-Guide-2021', nameValue:'benefits-enrollment-ungated-download', typeValue:'content-download'},
{urlfragment:'Guide-Fall-2021/Enrollment-Guide-2025', nameValue:'benefits-enrollment-gated-download', typeValue:'content-download'},
]
let buttons = Array.prototype.slice.call(document.querySelectorAll('.button, a')); // only scan buttons and links
buttons.map(function (button) {
matches.map(function (match){
if (button.href.indexOf(match.urlfragment) > -1) {
button.setAttribute("onclick", "return ewt.trackLink({name:'"+match.nameValue+"',type:'"+match.typeValue+"',link:this});");
}
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment