How to create a pageview using ga() function? - javascript

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');

Related

Google Analytics simple client

I am trying to integrate GA into a QT5 / QML project. For this I created a dummy GA web page project to test connectivity. First step is to test the setup with a simple web page (code below).
I had to specify a URL for the GA project. The page, however, is on my desktop, not online. I see no traffic in the real time GA view. Not sure why. Is it that my page is not on www.my-domain.com but literally on my desktop?
SETUP
On the GA settings page, this is my setup
Basic Settings
Tracking Id
UA-1234567890-1
Property Name
Default URL
http://www.my-domain.com
LOCAL TEST PAGE CODE
<html>
<head>
<!-- Global site tag (gtag.js) - 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-1234567890-1', 'auto');
ga('send', 'TEST');
console.log(ga.q);
</script>
</head>
<body>
<button onclick="ga('send', 'BUTTON PUSH')">
PUSH ME
</button>
</body>
</html>
I have used GA before and do not remember the integration was this bumpy. Right now, I am also confused by the difference of GA view and Firebase view on the live report. However, I first need to establish proper tracking.

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 virtually change the pageview in google analytics?

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

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.

Categories