Track page views in the IDRViewer with Google Analytics
To get Google Analytics to work with the IDRViewer, you can use the following code examples to add to the index.html file generated. All you need to do is add the example code into the index.html just above the closing </body>
tag and then replace the G-XXXXXXXX
sections with your Google Analytics Tracking code (check here if you need help getting this).
gtag.js:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXX"></script>
<script>
(function() {
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'G-XXXXXXXX');
function trackPageView(page) {
gtag('config', 'G-XXXXXXXX', {
'page_path': location.pathname + '?page=' + page
});
}
var THRESHOLD = 1000; // Length of time (in millis) required on page to be considered a pageview
var lastPage;
IDRViewer.on('pagechange', function (data) {
lastPage = data.page;
setTimeout(function () { // Wait until the page stops changing until sending page view
if (lastPage === data.page) {
trackPageView(data.page);
}
}, THRESHOLD);
});
})();
</script>
<!-- Global site tag ends -->