Google Analytics: How to track pages in a single page application?

前端 未结 7 2267
鱼传尺愫
鱼传尺愫 2021-01-31 08:44

Currently in my website, I used HTML5\'s pushState() and popState in links to increase the speed. However, this doesn\'t really change the real URL and it

7条回答
  •  广开言路
    2021-01-31 09:22

    February 2018 Update - Global Site Tag (gtag.js)

    Google Analytics has a new tracking code snippet, so the other answers might not work for gtag.

    This is the default tracking code. It only runs once even though we try to run it each URL changes.

    gtag('config', 'GA_TRACKING_ID');
    

    But with a page_path parameter we can make GA run manually.

    gtag('config', 'GA_TRACKING_ID', {'page_path': '/new-page.html'});
    

    And we can make something like this.

    var origin = window.location.protocol + '//' + window.location.host;
    var pathname = window.location.href.substr(origin.length);
    gtag('config', 'GA_TRACKING_ID', {'page_path': pathname});
    

    Single page application tracking with gtag.js (Google documentation)

提交回复
热议问题