Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 364
DBalazs Avatar asked Jan 24 '18 21:01

DBalazs


People also ask

What is difference between GTAG js and Analytics js?

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.

How do you get page views in GA4?

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.

Is GTAG asynchronous?

“Google Tag Manager is an asynchronous tag, meaning that when it executes, it does not block other elements from rendering on the page.


1 Answers

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.

like image 56
joshweir Avatar answered Sep 21 '22 22:09

joshweir