Google Analytics Async Event Tracking - javascript

Wonder if somebody is able to clarify the following;
Using the aysnc google analytics code placed in the head of the document as follows
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-123456-1']);
_gaq.push(['_setDomainName', '.somedomain.co.uk']);
_gaq.push(['_trackPageview', pageUrl]);
(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);
})();
On some the pages I am tracking I also use custom events to track video plays and which forms are used. These events use a further _gaq.push() call at various points on the page.
My question is should the final section of the initial analytics code (the section that calls ga.js be split and placed at the end of the page code or will any calls made once the script has loaded still be passed to Analytics regardless of the position on the page.
Thanks in advance.

No, the final section of the initial analytics code need not be placed lower down. You can call as many _gaq.push() calls as you like throughout the page, they'll still successfully pass to analytics.
The _gaq.push() event calls will send new requests to Google Analytics in addition to the first one you make (_trackPageview). You can check those event calls my inspecting how many requests your page makes for _utm.gif in a tool like Firebug.

Related

detect and load google analytics if not already loaded, skip if its already loaded

I have a javascript widget that contains a google analytics loading code. There is already google analytics loading code on the html page where the javascript is going to live. The ga.js files are loading twice and the bounce rate has gone to less than 1% due to the multiple loading of ga.js.
Is there a javascript solution where I could check if the page already has ga.js loaded then skip the loading within the javascript widget file and load ga.js if it is already not loaded?
This is the code i'm using but it seems to load the ga.js again even if the page already has ga.js.
Note: The javascript widget that contains the below code is loaded within a document.ready call and the google analytics loading code is present within the head tag of the html page.
function loadtracking() {
window._gaq.push(['_setAccount', 'UA-XXX']);
window._gaq.push(['_setDomainName', window.location.hostname]);
window._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);
})();
}
if(window._gat && window._gat._getTracker && typeof _gat != 'undefined'){//if site already has google analytics loaded
var _gaq = _gaq || [];
window._gaq.push(['_setAccount', 'UA-43609253-2']);
window._gaq.push(['_setDomainName', window.location.hostname]);
}else{ //if site DOES NOT have google analytics loaded
var _gaq = _gaq || [];
loadtracking();
}

Google Analytics report to multiple profiles at the same time on different domains

I have two different GA profiles that I want to report to all the time. This HTML that will have the tracking code will also be run from different top-level domains.
Reading these links:
https://developers.google.com/analytics/devguides/collection/gajs/#MultipleCommands
https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingSite#multipleDomains
I created this sample code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
var parts = location.hostname.split('.');
var subdomain = parts.shift();
var topleveldomain = parts.join('.');
var _gaq = _gaq || [];
_gaq.push(
['_setAccount', 'UA-12345-5'],
['_setDomainName', topleveldomain],
['_setAllowLinker', true]
['_trackPageview'],
['b._setAccount', 'UA-12345-2'],
['b._setDomainName', topleveldomain],
['b._setAllowLinker', true]
['b._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>
</head>
<body>
Play
</body>
I used JavaScript to determine the top-level domain of the site hosting the tracking code, so it sets the command of _setDomainName to the value of the current domain.
I also added a link with a onclick event to send _trackPageview to GA for testing purposes.
The problem I'm having is that when the page loads, it doesn't sends the request to GA (should send page views request to a different profile each). This is not happening.
It just sends the request to one profile (the first in the array) when I do the onclick event, the normal page view is not firing up.
However, it does sends the page view requests if I remove the: ['_setAllowLinker', true] from both items of the array. But I need that if I'm going to be hosting the tracking code under different top-level domains, right?
Any ideas?
Thanks!
You're missing a couple of commas in your _gaq.push() parameters:
_gaq.push(
['_setAccount', 'UA-12345-5'],
['_setDomainName', topleveldomain],
['_setAllowLinker', true], <===
['_trackPageview'],
['b._setAccount', 'UA-12345-2'],
['b._setDomainName', topleveldomain],
['b._setAllowLinker', true], <===
['b._trackPageview']
);

Track Subdirectory Redirect link To PDF

I have a DotNetNuke website (hosted on IIS server), and most of my urls are very long. We have PDFs posted of our products, and we set up some shortcut urls for convenience that redirect using a meta refresh redirect. I recently read that these links will not be added to Google's index and can be considered "spammy". Also, these redirects are preventing Google Analytics from tracking these page views.
An example shortcut url we have set up is: mysite.com/awesomesauce
This would redirect to the PDF brochure of that product: mysite.com/products/sauces/awesome_sauce.pdf
I would like to add Google Analytics tracking to track when people access or click on the shortcut url link (mysite.com/awesomesauce). Is there a way to track access to this shortcut link that also avoids the "spammy" meta refresh method?
I considered putting some JavaScript at the bottom of the page:
<script>
// Google Analytics tracking code, etc.
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);
})();
// Redirect code (after tracking events fire)
window.location = "http://mysite.com/products/sauces/awesome_sauce.pdf";
</script>

Why does Google's Analytics tracking code execute first?

I was playing around with custom variables in Google Analytics and wasn't sure why the following actually works. Why does Google's tracking code get executed first, especially since it is at the bottom of the page?
Both scripts are in self-executing functions, so how does javascript determine which one to execute first?
// top of the page
<script type="text/javascript">
$(function ()
{
_gaq.push(['_setCustomVar', 1, 'Account', 'PartTime', 1]);
});
</script>
// bottom of the page
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxxxxx']);
_gaq.push(['_setDomainName', '.xxxxxxxx.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>
The first one does execute first, and isn't in a self executing function.
It consists of a call to the $ function and has one argument: an anonymous function.
$ is a very badly named function. The name itself is meaningless and it has been adopted by half a dozen different libraries to do half a dozen different things.
In jQuery, if you pass a function to $ it runs that function when the ready event is triggered (but the end of the HTML document being parsed). This is probably what is happening here.

Google Analytics _setCustomVar - strange data in my dashboard

I have set up the _setCustomVar for my website, like this:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX']);
_gaq.push(['_setDomainName', '.blog4ever.com']);
_gaq.push(['_trackPageview']);
_gaq.push(['_setCustomVar', 1, 'B4E_Type_Pub', 'Silver_ou_Gold', 3]);
_gaq.push(['_setCustomVar', 2, 'B4E_Etat_Blog', 'normal', 3]);
(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>
In my dashboard, when I go in: Visitors -> Custom variables, I see my 2 variables but the numbers are really strange (like 40 pages views although I have many thousands pages views).
I have it installed for 2 days now.
Did somebody face the same problem?
You're lucky even 40 pageviews recorded these variables.
You need to call _setCustomVar before _trackPageview. Otherwise, the _trackPageview call sends the data to Google Analytics, and only after that are the custom variables set. If you set the _setCustomVar after the _trackPageview, the custom variable data doesn't attach, and if no other __utm.gif hits are set during that pageview, the data is basically gone forever.
(Those 40 pageviews that recorded probably were tracked via some other on-page GA calls that are sending data after the pageview loads).

Categories

Resources