Created
March 7, 2018 10:14
-
-
Save mousetree/d2052f11a1680c96f425b7bb5f3f4af7 to your computer and use it in GitHub Desktop.
Tracking page views in an SPA with Adobe Analytics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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