Google Analytics Setting Up A PageView On Hash Change

梦想的初衷 提交于 2019-12-04 08:13:26

To change the page path that gets sent to GA, you would to just make a slight modification to your code:

ga('send', 'pageview', {'page': location.pathname+location.search+location.hash});

More information can be found here: https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced?hl=en#fieldObject

Sending page in sending pageview call is not recommended by Google:

While technically the send command for pageview hits accepts an optional page field as the third parameter, passing the page field that way is not recommend when tracking single page applications. This is because fields passed via the send command are not set on the tracker—they apply to the current hit only. Not updating the tracker will cause problems if your application sends any non-pageview hits (e.g. events or social interactions), as those hits will be associated with whatever page value the tracker had when it was created.

Use:

ga('set', 'page', location.pathname+location.search+location.hash);
ga('send', 'pageview');

Google Analytics guide on tracking Single Page Applications.

ykay says Reinstate Monica

Here is a full code sample to track hash pageviews and changes in google analtyics:

(function (i, s, o, g, r, a, m) {
        i['GoogleAnalyticsObject'] = r;
        i[r] = i[r] || function () {
            (i[r].q = i[r].q || []).push(arguments)
        }, i[r].l = 1 * new Date();
        a = s.createElement(o),
            m = s.getElementsByTagName(o)[0];
        a.async = 1;
        a.src = g;
        m.parentNode.insertBefore(a, m)
    })(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga');

    ga('create', 'UA-XXXXXXXX-X', 'auto');
    ga('set', 'page', location.pathname+location.search+location.hash);
    ga('send', 'pageview');
    window.addEventListener("hashchange", function (event) {
        ga('set', 'page', location.pathname + location.search + location.hash);
        ga('send', 'pageview');
    })

Currently Google Analytics configuration loads Google Tag Manager script and is using gtag function and not ga so for me, previous solutions throws errors because 'ga is not defined'. What i do is a modification of inital Google Analytics configuration script with:

<script async src="https://www.googletagmanager.com/gtag/js?id=YOUR-GA-ID"></script>
<script>
     window.dataLayer = window.dataLayer || [];
     function gtag() {
         dataLayer.push(arguments);
     }
     gtag('js', new Date());
     gtag('config', 'YOUR-GA-ID', {
         'page_path': location.pathname + location.hash
     });
 </script>

To send content changes when page does not reload or URL (and/or content) change without reloading javascript, you must include somewhere in your scripts this code:

window.addEventListener("hashchange", function (event) {
       gtag('event', 'pageview', {
         'page_path': location.pathname+location.search+location.hash
       });
});

You can take a look at https://developers.google.com/analytics/devguides/collection/gtagjs/sending-data

And at https://developers.google.com/gtagjs/reference/event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!