Skip to content

Instantly share code, notes, and snippets.

@mousetree
Created March 7, 2018 10:14
Show Gist options
  • Save mousetree/d2052f11a1680c96f425b7bb5f3f4af7 to your computer and use it in GitHub Desktop.
Save mousetree/d2052f11a1680c96f425b7bb5f3f4af7 to your computer and use it in GitHub Desktop.
Tracking page views in an SPA with Adobe Analytics
/**
* Tracks a page view with Adobe Analytics
* @param {string} pageName The name of the page
*/
export function trackPageView(pageName) {
window.digitalData = {
event: {
eventInfo: [],
},
button: [],
page: {
pageInfo: {
pageName,
},
},
};
attemptTrack(); // inner function for trackPageView
/**
* Tries to run the Adobe Analytics tracking function. If AA is not yet loaded
* it will try again every 1 second for 10 seconds.
* @param interval
*/
function attemptTrack(interval = 0) {
if (interval > 10000) return;
// eslint-disable-next-line no-underscore-dangle
if (window._satellite) {
// eslint-disable-next-line no-underscore-dangle
window._satellite.track('all_pages');
} else {
setTimeout(() => attemptTrack(interval + 1000), interval);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment