Why isnt my Google Analytics code tracking data across all domains? - javascript

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_setDomainName', 'example.com']);
_gaq.push(['_setLocalRemoteServerMode']);
_gaq.push(['_setLocalGifPath', 'http://www.example.com/__utm.gif']);
_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);
})();
I cannot display the actual websites and content so I changed the websites to random.com and the two websites that are not being tracked to example1 and example2.com Also the main website is example.com
This is a external js file that has GA tracking code. That is in the head tag of example.com It also has the cross tracking section with it. In var domains[ example1.com and example2.com] dont actually get tracked via google analytics.
I do not have access to external js file. So what I am wondering if there is a error in the way it was written.
I believe the problem could be one of three things or I am completely wrong ;
1.) example1 and example2 are actually within the website so subdomains not different domains.
2.) Or that it is not running through the whole array. Because all domains in var domains have push data into google analytics except example1.com and example2.com.
3.) Or gaq.push(['_setAllowLinker', true] needs to be gaq.push(['_setAllowLinker', [here the domains] true]
Edit: Removed irrelevant pieces of code

Found the solution. So i needed to ignore most of the code.
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_setDomainName', 'example.com']);
_gaq.push(['_setLocalRemoteServerMode']);
_gaq.push(['_setLocalGifPath', 'http://www.example.com/__utm.gif']);
_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);
})();
This is the only relevant part. I needed to delete
_gaq.push(['_setLocalRemoteServerMode']); //Pulls remote GA code for Urchin
_gaq.push(['_setLocalGifPath', 'http://www.example.com/__utm.gif']);
It seems this is outdated classic GA code after that the websites worked fine.

Related

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>

Google-analytics _trackEvent doesn't get called from linked JS file

I have the asynchronous JavaScript from Google added to my HTMl page which adds the tracker:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-1']);
_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);
})();
Then in an external JS file which is definitely loaded after ga.js I have the following:
function activateAnalyticsTracking(href) {
_gaq.push(['trackEvent', "test", "tester"]);
console.log("_gaq.push called");
}
Now my console outputs "_gaq.push called", and I don't have any other vars on the page called _gaq, so I don't understand why, when I use a HTTP logging tool like Fiddler2 I don't see a call made to Google-analytics.
I do see a call though when I simply run _gaq.push(['_trackEvent', "test", "tester"]) from the console, so the code for that call is definitely right.
I have this function used specifically because I run other events when I link is clicked, so I'd like to use it, can anyone help me understand why this wouldn't work?
TIA
Cheers,
Blaise.
Seems like you're missing one underscore in _trackEvent. It should look like this:
_gaq.push(['_trackEvent', "test", "tester"]);

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).

Google Analytics Async Event Tracking

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.

Categories

Resources