Link
Skip to main content

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 -->

Get started with BuildVu in 3 steps

  1. Fill in the form to download the trial jar →
  2. Copy the code snippets as instructed on the next page
  3. Build your solution using our docs

Learn more about BuildVu

Start Your Free Trial

Trial License Type