Part 1 - Detecting http and https Mode Using Javascript
A while back I came across a scenario where a website (typically an
ecommerce site) can serve part of their website in both http and https
mode. These sites typically use the same template or footer include
file for both browser modes. This causes a security alert popup in the
browser because the remote javascript file is called using a http
request. While this isn’t a security threat, it could cause some less
technically savvy users to be concerned about the site security and
perhaps not want to complete the transaction.
Google does offer the webmaster the ability to request the urchin.js file using a https call, which works well, except what we really need, is a way to detect which mode we’re in, then make the appropriate request on the javascript file.
With help from some members on SEORefugee we figured out how it can be done.
Part 2 - Only Obtaining External Referrers
Sunday night I was looking through my Top Content report and realized that after my hack to obtain the full referrer,
it’s fairly indiscriminate and will obtain all referrers, both internal
and external. While I already knew about this, I guess that night I was
tired and grumpy and it just bugged me enough to want to fix it.
The whole point of my hack was to obtain the external referrer, so I came up with some more javascript to detect whether the referrer is internal or external and write out the urchinTracker function accordingly, so it will only record the external referrers.
The Grand Finale
So putting all this together we get this:
<script type="text/javascript">
document.write( '<scr'+'ipt type="text/javascript" src="' );
if( document.location.toString().indexOf( 'https://' ) != -1 ) {
document.write( 'https://ssl' );
} else {
document.write( 'http://www' );
}
document.write( '.google-analytics.com/urchin.js"><\/scr'+'ipt>' );
</script>
<script type="text/javascript">
_uacct = "UA-XXXXXX-X";
urchinTracker();
</script>
<script type="text/javascript">
document.write( '<scr'+'ipt type="text/javascript">' );
if( document.referrer.toString().indexOf( 'www.mywebsite.com' ) ==-1 ) {
document.write( 'urchinTracker(document.referrer);' );
}
document.write( '<\/scr'+'ipt>' );
</script>
Just replace the XXX’s with your Analytics account number and “www.mywebsite.com” with your website.
April 13th, 2007 at 2:42 am
Hey Reuben, really useful posts.. I actually read through the series of Analytics post you have in this blog.. definitely nice and useful hacks. I found that one of the main stumbling blocks for beginners is to set up Analytics for Ecommerce and though Google provides enough info on that, the information is all scattered and might be a bit too much for a beginner. I just put them altogether in a easily comprehendable way ( both ecommerce tracking and adwords/ad campaign tracking ) with Google anlytics in my Guide to Google Analytics Setup and Ecommerce Tracking blog post.
May 15th, 2007 at 1:54 am
First, thanks for this. It drives me nuts that GA does not give full referrers.
Just curious, though, why write out the script tag within a script tag? Since you already have a script tag, won’t the conditional execute anyway, or is this just to delay things so that the conditional doesn’t execute until the JS interpreter gets up and going?
May 15th, 2007 at 2:10 am
Okay, forget the last question. If you omit that, it simply writes out “urchinTracker(document.referrer);” at the end of your document instead of evaluating it.
June 13th, 2007 at 6:50 am
Thanks for the code! I just added it to my sites and am waiting for new data to see how it works.
In the meantime, I made a small modification to automatically detect the current website to prevent full internal referrers. So now you don’t have to replace “www.mywebsite.com” in your original (final code). My issue was that sometimes visitors use “mywebsite.com” (no “www”) and the original code doesn’t account for that.
Here’s my modification to the last “script” block:
document.write( ” );
var wbx = window.location.toString().indexOf(”:”) + 3;
var wb = window.location.toString().substr(wbx,window.location.toString().indexOf(”/”,wbx)-wbx);
if( document.referrer.toString().indexOf(wb) == -1 )
document.write( ‘urchinTracker(document.referrer);’ );
document.write( ” );
—–
Hi Daniel
Thanks for the update.
Reuben