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.
The biggest difference between these two snippets is that the analytics. js code includes a separate "send pageview" call, while the gtag. js code doesn't.
To send your own pageviews, add the following event: gtag('event', 'page_view', { page_title: 'My Profile', page_location: 'https://example.com/me', // Include the full URL send_to: '<MEASUREMENT_ID>' }); Google uses default values for any parameters you don't set.
“Google Tag Manager is an asynchronous tag, meaning that when it executes, it does not block other elements from rendering on the page.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With