shorten Google Analytics code - google-analytics

Can the following line in Google Analytics code:
ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js';
be shortened to
ga.src = '//stats.g.doubleclick.net/dc.js';
?
The full script looks like this:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
_gaq.push(['_setDomainName', 'example.com']);
_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>

It's called a Protocol Relative URL and using one to shorten the Google Analytics code can cause some problems with IE6.
From Paul Irish # Google on using Protocol-Relative URLs:
There is an edgecase bug in IE6 that causes a dialog to blow up… under some security settings from the non-‘ssl’ subdomain. So feel free to take 40 bytes off your GA snippet if you don’t care about IE6.. otherwise you’re gonna need that ternary operator. :)

If you do so it won't work if you load this file using the file: protocol to test your server for example. Besides that small problem it should work just fine.

Related

Why isn´t my Google analytics code being detected?

I paste the code just before in every webpage and it doesn´t work.
Can somebody check why?
My website is http://www.agriculturacanaria.com
It has html, asp and css files. I didn´t touch the css but the code is already in all the head section of both the two others.
I don't know if that's valid GOogle Analytics cdode. It may be an old way or something, but the modern code looks something like this:
<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>
You can probably use that code block and put in your own UA code and get it to work.
Microsoft Word makes for a really bad HTML editor, so you may want to find an alternative that allows for greater flexibility over the code and less bloat of the HTML. (You're probably being penalized for a tactic called "keyword stuffing" alone.)
Hope this helps!

Link multiple Google webmaster tools accounts to single Analytics property

I tried to look for a good solution but couldn't find any.
I try to link multiple Google webmaster tools accounts to a single Analytics property.
I tried creating multiple views, but it seems you can't link it to a view.
I found a solution to add multiple domains to a Google Analytics property, but this seems outdated since Universal tracking
Note: probably some people might say I should 301 redirect the domains for duplicate content. But they are regionaly implemented using the hreflang alternate method which should be just fine.
You can link multiple Google Analytics accounts.
<script type="text/javascript">
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
_gaq.push(['_trackPageview']);
// Second tracker
_gaq.push(['secondTracker._setAccount','UA-YYYYYYYY-Y']);
_gaq.push(['secondTracker._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>
See: http://seo-website-designer.com/Google-Analytics-Tracking-Multiple-Accounts

google analytics setup, not tracking

I'm trying to implement google analytics on a page but it refuses to track anything.
the public domain is on www.publicdomain.com, when entering the page its redirected to: www.publicdomain.com/sub1/sub2/index.php
this website is hosted on another domain:
www.privatedomain.com/publicdomain/sub1/sub2/index.php
This is the GA code I'm using inside the body tag of the index.php file:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-9999999-9']);
_gaq.push(['_setDomainName', 'publicdomain.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>
Any ideas of what could be wrong?
Here are some things to check:
Are you using the correct UA number? (i.e. not 999999-99)
Are the GA cookies being created? (__utm[abcz])
Is the __utm.gif request being sent back to Google? If not, is your browser blocking it? (i.e. is Do Not Track enabled?)

Google Analytics Custom Variables not working

I am trying to log the username of users who are using my system. Unfortunately, I couldn't track by using custom variables. I could track the page visit, but custom variable tracking is not working. Could you please help me? Thanks.
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxx-1']);
_gaq.push(['_setDomainName', 'none']);
_gaq.push(['_setCustomVar',1,'UserName','JohnPaul',2]);
_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 code is correct and it does track the users now. I just used the similar codes as above and it works now. But I needed to wait 2 days to track the custom variables and it appears in my reports now.
For more than one user, you'll have to use PHP to output user names.
_gaq.push(['_setCustomVar', 1, 'UserName', '<?php get_usernames(); ?>',2]);
***Substitute "get_usernames" with the function for your site that retrieves the usernames.

How do I track 2 domains which point to the same host. 2nd domain points to a sub directory of the 1st site

This is my exact setup. I have a single web host. I have assigned a primary dns to that host. for instance www.primary.com points to the host. THe host allows 'addon domains'. An addon domain is in effect a subdirectory of the host, is accessible via www.secondary.com OR secondary.primary.com. I can ALSO access the content at www.secondary.com by typing in www.primary.com/secondary.com
I want to track each domain as a totally separate website, with no overlap in the visitors. (the sites for all intents and purpose are two completely separate site with nothing really linking the two)
I am absolutely confused as to what code to use where and how to customize it. So any and all help would be appreciated.
Thanks in advance
Either you use two different profiles with unique profile ID to track them (e.g. UA-123456789-1 for the www.primary.com and UA-123456789-2 for the www.secondary.com) and make sure each site only has one of the codes, or you can use filters based on domain/sub-domain/directory structure to do this (or to support it).
I would go with separate tracking codes, as it is by far the easier choice.
Example:
Site 1:
<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>
Site 2:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-Y']);
_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>
Be sure to create the profile for both.

Resources