Search code examples
javascriptgoogle-analyticsanalytics.jsgtag.js

Migrate virtual pageviews to gtag.js


I'm having problems migrating from analytics.js to gtag.js. The issue is I can't make virtual pageviews work. How to replicate the following analytics.js code in gtag.js?

ga('set', 'page', path);
ga('send', 'pageview');

This code is in the a tag's onclick event.

This is a single page website which doesn't change the url when you click links.


Solution

  • Your example code:

    ga('set', 'page', path);
    ga('send', 'pageview');
    

    would become:

    gtag('config', 'UA-XXXXXXXXX-1', {'page_path': path});
    

    Where 'UA-XXXXXXXXX-1' would be your google analytics tracking id.

    Explained here.