Uncaught ReferenceError: _gaq is not defined (Google Analytics) - google-analytics

The following message appears when viewing a site page in the chrome debug log.
Uncaught ReferenceError: _gaq is not defined
The page itself is supposed to track an object using the onload event handler and fire a _trackEvent for Google Analytics.
My best guess is that perhaps the ga.js file doesn't load in time and therefore the onload _trackEvent triggered is not caught. the async snippet is being used before the </body> close and the object is positioned in the middle <body>.
(some other posts have referenced jQuery position too but, this might be a red herring)
any assistance greatly appreciated.

From https://developers.google.com/analytics/devguides/collection/gajs/ this code replaces your existing "traditional snippet" with the "latest, asynchronous version, you should remove the existing tracking snippet first."
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
To see your events come in (to know if this is working) look for "Events" under the "Real-Time" menu option on the left side of the "Reporting" page.

Regarding the position of the async snippet, the GA help page says -
Paste this snippet into your website template page so that it appears
before the closing </head> tag.
My first thought was that JS should be loaded at the bottom of the page to improve page speed. However, the GA async tracking snippet should be loaded in the head, as it will not load the ga.js immediately, and it won't block page execution.(It does this by dynamically adding the script tag to the DOM, which puts it at the back of the queue.)
If, for some reason, you can't move the async snippet to the head, you can define _gaq yourself, like this-
<button onclick="var _gaq = _gaq || []; _gaq.push(['_trackEvent', 'button3', 'clicked'])"/><button>

Had the same problem. You have to define the _gaq array. Just add this after your Google Analytics script in the header:
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXX-X']);
_gaq.push(['_trackPageview']);

You can use the last version of analytics.js instead of ga.js
ga.js is a legacy library. If you are starting a new implementation we
recommend you use the latest version of this library, analytics.js.
For exisiting implementations, learn how to migrate from ga.js to
analytics.js.
Here is an example:
ga('send', {
hitType: 'event',
eventCategory: 'Video',
eventAction: 'play',
eventLabel: 'cats.mp4'
});

Change the Tracking Code to:
<script type="text/javascript">
var gaq;
var _gaq = gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_setDomainName', 'yourdomain.com']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www')
+ '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>

Related

Google Analytics - add third party shopping cart to main domain reports

We would like to track if users sent from our main site to a third party site have bounced or clicked on "Register"
They are willing to put our Analytics code in their pages - both product and registration.
I looked at what is suggested by Google but not clear about a few things.
Our current code on our site:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-nnnnn-1']);
_gaq.push(['_setDomainName', 'none']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
Code suggested by Google to be on the third party site:
<script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-12345-1']);
_gaq.push(['_setDomainName', 'my-example-blogsite.com']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);
</script>
...
<a href="http://dogs.example-petstore.com/intro.html"
onclick="_gaq.push(['_link', 'http://dogs.example-petstore.com/intro.html']); return false;">
See my pet store</a>
Our code has 'none' in this line: _gaq.push(['_setDomainName', 'none']);
Google's sample code has the URL
_gaq.push(['_setDomainName', 'my-example-blogsite.com']);
Does it matter? Do I need to enter the domain as suggested?
Also Google's code has a link back to our site as follows
onclick="_gaq.push(['_link', 'http://dogs.example-petstore.com/intro.html']); return false;"
Do we need to have a link back? Can we remove the onclick code or will it affect the reporting?
Thanks!
Myalo
You need the onclick event. What is does is to "decorate" the link, i.e. it adds the url parameters that need to be carried over to the other domain (where they are picked up by the Google code and used to continue the session).
Please be aware that the code you are using is horribly outdated. If at all possible switch to the current version (Universal Analytics). While cross domain tracking on Universal Analytics works by the same principle it'S somewhat more elegant (uses a single parameter as opposed to the unwieldy query string for classic GA) and offers a number of helper functions to make cross domain tracking easier.

Using Google Analytics without a server or localhost?

I have spent quite a lot of time researching ways to use GA on a locally run file, but not using http://localhost:(some_port).
Every method I use never returns any regular data. This is the closest I have got to receiving anything:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-47519364-1']);
_gaq.push(['_setDomainName', 'none']);
_gaq.push(['_setAllowLinker', 'true']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/u/ga_debug.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
Anyone got this working in this fashion before?
Thanks for enlightening my eyes with the option of the '_debug.js' script.
I am using the analytics module and had the same problem.
So I used the debug script and got the following error when trying to send an event:
Unallowed document protocol. Aborting hit.
I googled it and found this thread exlaining how to bypass it:
https://productforums.google.com/forum/#!topic/analytics/KNz8TimivXo
This was the answer:
ga('create', 'UA-**********-6', {'cookieDomain': 'none'});
ga('set', 'checkProtocolTask', function(){ /* nothing */ });
ga('send', 'pageview');

how to check which version of Google Analytic code is working?

I am working on a website, and i want to do record event event on the website. for that i have searched on Google and found Event Tracking. but i how to check whether i am using ga.js or analytic.js and which is better to use and why.
My google Analytic code is:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '[TRACKING_ID]']);
_gaq.push(['_setDomainName',document.domain]);
_gaq.push(['_setAllowLinker',true]);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
please help me.
Universal Analytics does not use the .push syntax. Instead, it has a function ga().
You apear to not be using universal analtyics you should check this link on how to work with Event Tracking: Event Tracking - Web Tracking (ga.js)
This link should help you understand the diffrence: About Universal Analtyics

Grouping Blogger posts for blogs monitored via Google Analytics

I'm trying to exploit the content grouping feature provided by Google Analytics for a blog hosted by Google Blogger.
The blog has been correctly set to be monitored by using Google Analytics (GA). GA provides 3 ways of grouping but in my case only the grouping by tracking code option seems to be the correct one. As far as I can see inspecting a blog page, by putting into the template the GA include
<b:include data='blog' name='google-analytics'/>
I correctly get the following JavaScript ga.js snippet into the page
<script type='text/javascript'>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'XXXXXXXXXXX']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = (document.location.protocol == 'https:' ?
'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>
The problem is that I've no way to add the required call to set the group as required.
_gaq.push(['_setAccount', 'UA-40265412-2']);
_gaq.push(['_setPageGroup', 1, 'My Group Name']);
_gaq.push(['_trackPageview']);
Any idea in which way this can be implemented?
Thanks in advance

Event tracking not working

I am trying to integrate the Google's event tracking on my application using the below script. I can able to see the Events in "Realtime" tab. But I am not able to see the events in "Content" tab.
var _gaq = _gaq || [];
_gaq.push(['_setDomainName', 'none']);
_gaq.push(['_setAccount', 'XXXXX']);
_gaq.push(['_trackPageview']);
_gaq.push(['_trackEvent', 'request.fullpath', 'plan_desc', 'plan_code>']);
Are you grabbing the script that defines the gaq function?
var _gaq = _gaq || [];
_gaq.push(['_setDomainName', 'none']);
_gaq.push(['_setAccount', 'XXXXX']);
_gaq.push(['_trackPageview']);
_gaq.push(['_trackEvent', 'request.fullpath', 'plan_desc', 'plan_code>']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
Also, for your event push, are the values you are pushing in supposed to be JS objects? if so, you may want to get rid of the quotation marks.
GA takes a certain amount of time before it translates data that you see in real-time into it's other sections.
It might be that if you have only added the code within 48 hours that results are not ready to view yet.
I would suggest you trying Google Analytics Debugger extension for Chrome to see if tracking events reach google.
https://chrome.google.com/webstore/detail/google-analytics-debugger/jnkmfdileelhofjcijamephohjechhna?hl=en
If you are trying to use Rails variables in the JS, as request.fullpath looks like one you must make sure you are rendering it out in your templating engine. For example, if you are using ERB, you must call it like this...
_gaq.push(['_trackEvent', '<%= request.fullpath %>', 'plan_desc', 'plan_code>']);
Obviously repeat for the other variables, if they aren't Ruby and are plain JS objects, you need to omit the quote marks around them.

Resources