How to virtually change the pageview in google analytics? - javascript

I'm following this:
But in my real code, I've used google tag manager to manage google analytics. So I have just one script in the head of my website which belongs to gtm:
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-P8F58CT');
ga('create', 'UA-124402662-1', 'auto'); // Uncaught ReferenceError: ga is not defined
As you can see, I've added ga('create', 'UA-124402662-1', 'auto') right after that google tag manager (the same as the tutorial I've linked in the first line of my question). And I have a ga('send', 'pageview', newURL); code which will be executed when the page changes using ajax.
The problem is it throws: (as I've commented in code above)
Uncaught ReferenceError: ga is not defined
How can I fix it? Someone told me no need to add ga('create', 'UA-124402662-1', 'auto'); at all, and gtm will handle it all. But in my tests:
When I open my website (homepage), and run ga('send', 'pageview', "/adv"); in the console, nothing changes in google analytics and it still points to the homepage.
When I open my website (homepage), and run ga('create', 'UA-124402662-1', 'auto'); and then run ga('send', 'pageview', "/adv");, then the google analytics will be changed to the /adv (not homepage anymore). So I believe ga('create', 'UA-124402662-1', 'auto'); is needed. But why it is undefined?

You have Google Tag Manager snippet on your page. Not the Google Analytics’ analytics.js. You are confusing the two.
You shouldn't have this or anything related to ga() on the page.
ga('create', 'UA-124402662-1', 'auto'); // Uncaught ReferenceError: ga is not defined

Related

Google Analytics not working with Barbajs

Trying to use the latest google analytics tracking code in a site that uses Barba JS but it will not track past the first page, ie the path of the user, any ideas anybody?
The reason it is only working on the initial page is that the default GA snippet implemented using gtag.js only runs once after load. Since you're using it in a SPA setting, you need to manually re-send the page data when you route. Looking at the barbajs documentation, I think it'll be something like this:
// define a global hook, using afterEnter, but you can adjust to enter, beforeEnter, etc as you see fit.
barba.hooks.afterEnter((data) => {
// this hook will be called for each transitions
//data.current.url is the page path.
gtag('config', 'UA-#######-1', {'page_path': data.current.url});
});
use full code provide with google annalitics
https://developers.google.com/analytics/devguides/collection/analyticsjs/?hl=en
in you JS add
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');

How to create a pageview using ga() function?

I manage google analytics inside google tag manager and I don't have any separated script for google analytics in the head of my website.
I need to use ga() function like this:
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXX-Y', 'auto');
</script>
<!-- End Google Analytics -->
See? It has created ga right after google analytics script. But as I said, I don't have any script for google analytics. I have just a script (which is located in the head of my website) from google tag manager:
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-{$google_tag_manager_token}');
ga('create', 'UA-{$google_analytics_token}', 'auto');
</script>
The problem is, it throws:
Uncaught ReferenceError: ga is not defined
Any idea where should I place ga('create', 'UA-{$google_analytics_token}', 'auto'); exactly?
It should be noted, when I run ga('create', 'UA-{$google_analytics_token}', 'auto'); in the console of the browser, it will be created as well. Do I need a settimeout() ?
By default, there is no analytics tag set up into the Google Tag Manager script.
You must configure into GTM a new tag for which you set up the type of the tag with "Google Analytics - Universal Analytics" and set up an event to launch this tag. "ga" function will then be available when GTM actually runs the tag.
One more information here, GTM send a hit with a prefix (generated tracker name). Sample:
ga('gtm1.send', 'pageview', '/page-name');

Google Analytic's events sent not showing up in dashboard

I am using the Google Analytics snippet below
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXXXX-X', 'icantreveal.com');
</script>
<!-- End Google Analytics -->
I am sending my events on say a button click as follows
//google analytics click event hit record
if(typeof(ga) == "function")
ga('send', 'event','int_lp_btn','int_lp_findPeople_btn');
I am using Google Analytics debug Chrome addon for testing purpose. However when I test the same in my site I can see in console
Executing Google Analytics commands. Running command: ga("send", "event", "int_lp_btn", "int_lp_findPeople_btn") Sent beacon:some ga stuff
However the event report in dashborad does not have anything to show!. I have been unable to figure out what the problem is. Any help will be appreciated. thanks.
Replace your google tracking ID and set page view as ga('send', 'pageview');

Setup Google Analytics with Meteor App: Tracking not Installed Error

I am Trying to set up Google Analytics with Meteor and always get a Tracking not installed Error.
I have already tried to use the meteor Packets, I have tried to include the tracking code in the Head Section, in the Body section and as a rendered Callback on a template.
I always get the error from the Google Analytics Site "Tracking not Installed"
What is the best practice of GA tracking in Meteor Apps?
You don't need a package for GA, probably easier to do it yourself, like this:
First, put the GA tracker id in Meteor.settings.public. (more info about that here)
Then, create a file for the client, probably in the lib folder, for example /client/lib/google_analytics.js and add the regular GA tracker to it:
// CLIENT
/*****************************************************************************/
/* Google Analytics */
/*****************************************************************************/
if (Meteor.settings.public.GaTrackingId) {
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
}
Last, create another file to track the page load. You can do this in a Meteor.startup() method, but you don't have to as the tracker code is in /lib which is loaded before everything else.
// CLIENT
if (Meteor.settings.public.GaTrackingId) {
ga('create', Meteor.settings.public.GaTrackingId, 'auto');
ga('send', 'pageview');
}
That's it. Now you can also do GA event tracking, etc. from anywhere in your app if you'd like to.

Google Analytics Real-Time not showing results

The following script is not generating activity when cross-checked in Google Analytics : Real-Time section : Content section --
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-475021xx-x', 'auto');
ga('send', 'pageview');
</script>
where xx-x masks the real account code.
The script is placed just before </head>
We have upgraded our account to Universal Analytics. Google tells us that this is the script to use; directly copied and pasted here (with the masked account code).
On the Google Analytics page, the test page is loaded as an ACTIVE PAGE. I click links on the test page to trigger ga.
Is there an error in this script? For example, I see on other Stackoverflow posts that 'auto' is replaced with the real domain. But I don't see Google instructions to make that substitution.
Am I doing something else wrong?
I did the same thing as you back then.
Mine started showing data in about 12-24 hours after I put the Google Analytics code.
I suggest waiting for another day then checking if data comes up.
Given that this answer can only be verified until we wait for a certain amount of time, please hold off setting this as the correct answer until then.
For me, the issue was a dunce move. I have the Google Analytics Opt-out Add-on for Chrome enabled.
What I had to do was go to chrome://extensions/, scroll down to the Extension "Google Analytics Opt-out Add-on (by Google)" and uncheck the box that says, "Enable".
I hope this helps someone.
You can check the health of your tag using Google tag assistant, and also check with developer tools whether you can see data being sent to your GA account. Also look in your account under Property > Tracking Info > Tracking Code for the status of your tracking code. But if you are not seeing Real Time data then something could be wrong.
The third parameter in the 'create' method sets the cookie domain. It is optional and with 'auto', it sets the cookie domain to the highest domain level possible. Without it, the cookie domain will be set to the website domain without the www. prefix.
Here is what my issue was. I'm using google analytics with wordpress. I tried configuring google analytics for my website using plugin called Google Analytics Dashboard for WP (GADWP). This plugin generated a google analytics snippet that looks similarly to the one described in the question:
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-475021xx-x', 'auto');
ga('send', 'pageview');
</script>
However, in Google Analytics console, the suggested snippet looked like this:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-110984887-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-475021xx-x');
</script>
It looks like this gtag.js is a new format for the google analytics snippet. So I disabled that wordpress plugin and I manually added the script suggested by Google Analytics to the page.
I have recently fixed the same issue. Please login to your admin dashboard and check the filters.
What I did was to make sure my valid host name filter was the correct one.
If you have copied the custom dashboard from another project or if you had an error in the filter then this would be worth your time to check out.
I have placed the value "domain.com" in the included host names filter.
Let me know if this fixed the issue for you.

Categories