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

前端 未结 7 2266
鱼传尺愫
鱼传尺愫 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:32

    I know it's old question but since this question is first result in Google about tracking pushState() in Google Analytics and all answers are wrong I decided to answer it.

    In other answers they mentioned to use directly ga('send' ..... ) but this is wrong way to do it.

    First you have to 'set' parameters and then use 'send' to track it.

    If you want to update only url, use following code

    // set new url 
    ga('set', 'page', '/new-page');
    
    // send it for tracking
    ga('send', 'pageview');

    If you want to update url and title, add title parameter to it

    // set new url and title
    ga('set', {
      page: '/new-page',
      title: 'New Page'
    });
    
    // send it for tracking
    ga('send', 'pageview');

    Source Single Page Application Tracking - Web Tracking (analytics.js)

提交回复
热议问题