Google Analytics Tracking code - google-analytics

What is the differnce between the below two code in tracking the webpage.
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
_gaq.push(['_setDomainName', 'right.com']);
_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>
And
<script language="javascript" src="http://www.google-analytics.com/ga.js" type="text/javascript"></script>
<script type="text/javascript">var pageTracker = _gat._getTracker("UA-XXXXXXXX-X"); pageTracker._initData(); pageTracker._trackPageview();</script>

1) The first was was deprecated a few month ago, the second one has been deprecated many years ago (you should use neither of them)
2) The second version is very old, synchronously executed code. The downside was that loading the GA code synchronously meant that page load might under some circumstances be blocked until the ga.js file finished loading (which in turn meant that users sometimes saw an empty screen until the file had finished loading). Older browsers where not got with loading multiple files simultaneously, especially if the files content might affect the way the page was rendered.
3) The first example (asychronous code) stores all instructions for the analytics code in an javascript array. Since this uses native javascript code (the push method) this will work before the ga file has been loaded. Once the code is in place (it is injected into the pageheader via the short bootstrap script below the _gaq.push calls) it can read the array and process the instructions contained therein.
Again I'd like to point out that this information is of purely historical value since both versions of the tracking code are deprecated. You should use the new Universal Analytics code only.

Related

Custom events does not appear even if the are sent

On pageload does I load google analytics through he following code snippet:
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try{
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
pageTracker._trackPageview();
} catch(err) {}
</script>
And I use the following code to fire an event (after page load):
_gaq.push(['_trackEvent', 'test category', 'test action']);
Standard information get tracked, but NO CUSTOM EVENTS. I know all events get sent to Google Analytics because each time an event if fired, does an HTTP GET request get sent to the following url:
http://www.google-analytics.com/__utm.gif?.....more data here
What does I need to do to get custom events in google analytics to work?
Thanks in advance.
It seems you are running an old, "traditional", implementation of your Google Analytics.
I would strongly suggest that you upgrade to atleast the asynchronous snippet, where the syntax for _gaq.push is supported. As it stands now, you are trying to push an event using the new syntax into an old implementation which uses another syntax.
The differences between the two are detailed here: https://developers.google.com/analytics/devguides/collection/gajs/asyncMigrationExamples
In short, you are using a syntax for gat/pageTracker, where the newer asynchronous one uses gaq.
Please review this code that I have copied from above link:
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_trackPageview']);
...
<a onclick="_gaq.push(['_trackEvent', 'category', 'action', 'opt_label', opt_value]);">click me</a>
To what you are using now, the older syntax:
var pageTracker = _gat._getTracker('UA-XXXXX-X');
pageTracker._trackPageview();
...
<a onclick="pageTracker._trackEvent('category', 'action', 'opt_label', opt_value);">click me</a>
As you can see, you are trying to send an event to the old ga.js using new ga.js syntax. I would highly recommend using the newer ga.js which loads asynchronously. By asynchronously, it means that the javascript loads while the webpage is loading, which the old ga.js does not.
This is an example implementation of the newer ga.js, and it should be implemented right before closing </head>:
<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>
If you implement the above suggestion, then you are using the newer ga.js and the _gaq.push-syntax should work. Try it out!
And if you do not know what the asynchronous part means, a brief explanation:
The latest version of the Analytics tracking code offers an improved way to track website visitors with Google Analytics. It takes advantage of browser support for asynchronous JavaScript to enhance the speed in which the tracking code is loaded. With the latest version of the tracking code, you can also place the Analytics snippet higher in the page without delaying subsequent content from rendering.
It continues to load other elements of your webpage aside from eachother, instead of waiting for 1 to finish before loading 2, then 3, etc.

Google Analytics - Custom iFrame Tracking

I made a small little Tic Tac Toe game that plays perfectly at http://tic.cwoebker.com.
The actual tic tac toe game is loaded from (/tic) and embedded in an iframe.
Therefore others could easily embed the game on their own site if they wanted.
I am doing some event tracking inside of the iframe.
Right now I have it setup so it fires a page view on both the main page
and the actual game code inside the iframe.
I was wondering whether I could somehow only fire the page view for the iframe if its not
embedded on http://tic.cwoebker.com but on another site.
So that everything thats tracked under root (/) is traffic on my site and everything tracked in the i frame (/tic) traffic generated by embedding on another site.
Right now my analytics code in the iframe looks like this:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxxxx-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>
Thanks a lot in advance.
As you're in an iFrame on a separate domain, you can't access the parent window to get the location. Check out this demo and/or google it a bit: http://jsfiddle.net/TomFuertes/RRB52/2/
_gaq.push(['tictactoe._setAccount', 'UA-xxxxxxxxx-x']);
// Line below causes security issues, thus won't work
_gaq.push(['_setCustomVar', 1, 'Domain', window.parent.location.host, 3]);
_gaq.push(['tictactoe._trackPageview']);
You can pass the domain using a querystring on your iFrame page, you'd need to modify the include code to look like this: https://stackoverflow.com/a/5697801/94668
<script type="text/javascript">
document.write('<iframe src="//tic.cwoebker.com/?url=' + window.location + '"></iframe>');
</script>
Then you'd filter out your Google Analytics appropriately.
You can use Google Analytics campaigns to track the traffic. You will need to distribute the embed code that already has the campaign parameters as part of the embed iframes src.
Hope that helps.
Interested to hear from others.

Google Analytics code always reports page name as being "$A"

I have some code (below) that I'm using to track pageviews on Google Analytics. In Google Analytics, all pages end up having the name "$A".
I can't seem to find anyone who has had the same issue and I can't see what I'm doing wrong:
<script type="text/javascript">
(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);
})();
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
_gaq.push(['_trackPageview']);
</script>
<script src="/__utm.js" type="text/javascript"></script>
It sounds like, rather than there being a problem with the tracking code, that the problem is actually with a filter configured for your account.
Specifically, $A makes it sound like you have an improperly configured Reyes based filter that, rather than outputting the regex match, is instead outputting the string "$A", which is meaningless unless you're properly using RegEx.
Can you post detailed information about the profile? In any case, you shouldn.t be filtering on a main profile, since filters cannot be undone, so any configuration errors on your main profile will result in permanently lost data.
My recommendation is to totally remove the filters in pladce, duplicate the profile and apply your desired filters against the duplicate, to mitigate potential data loss problems like this.

Google Analytics, Page views are not tracking correctly

I have installed the below google tracking code in my site
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxxx-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);
})();
In my GA account under visitors -> page views are not showing correctly. After many visits to the site it is showing only page views as 2. The total number of page views doesn't seems correct.
Is there any problem in my tracking code. Please give me a solution on this.
The Code is placed just before the </body> closing tag as it was recommented as best practice of placing analytics code. We placed the code in all pages and the status in GA is "Receiving Data". It is a video sharing website where we also use Google Analytics Plugin of JW Player. Following is a url to a site page storybridge.tv/StoryBridge/freespirits/story/fightclub
I also checked and confirmed that there is no other script that use the variables _gat and _gaq.
Make sure that other scripts on your pages do not use the variables _gat and _gaq, since those are global variables used by the asynchronous tracking code. As long as those variables are not used by other scripts on your pages, there should be no other interference with the tracking code from your own scripts.

Google Analytics reports to be receiving data, but none is showing up

Hi recently redesigned my site (http://bit.ly/gwrYwb) which was formerly tracking pageviews properly via Google Analytics. After the redesign, I inserted the exact same code right before the </head> tag:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-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>
(The UA number is correct on the actual page.) However, GA is mysteriously reporting zero pageviews/visits, even though it claims to be receiving data. Has anyone experienced a problem like this before? The only other JS I'm using is including jQuery, as well as a widely-used jQuery plugin on one of my pages.
This is kind of lame, but have you checked your profile filters? You might still have old filters (for the former website) applied that filter all urls out.
We had this problem with another website and were debugging for weeks until we found this simple solution...

Resources