Google Analytics Tracking Code – HTTPS and Full External Referrer Only

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:

Just replace the XXX’s with your Analytics account number and “” with your website.

About reuben 109 Articles
SEO consultant from Columbus OH.


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

  2. 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?

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

  4. 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 “” in your original (final code). My issue was that sometimes visitors use “” (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.


  5. Reuben,

    I’m getting an error in IE 7 (Firefox seems to be OK) when I add the following analytics code that includes Daniel Strickland’s modified last block. Do you know why this might be causing an error in IE?

    Error: Invalid Character
    Code: 0

    document.write( ” );

    _uacct = “UA-2728886-1”;

    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( ” );

  6. Correction: The analytics code pasted into my last post didn’t come out correctly. Please goto and view source code. The analytics code is located just before the closing tag.

  7. Hi Darryl,

    Since the new GA script (ga.js) is out, how do you think we can get the full referrer?

    Many thanks,

  8. Hi Darryl,
    I am very happy to find your blog that I can ask questions.
    I tried to insert tracking code in my blog.
    Where can I find html page?
    I know I can put the tracking code before “body” in html page.
    But I cannot find where is the html page.
    Could anyone help me as soon as possible.

    Thank you very much in advance.

1 Trackback / Pingback

  1. Kale Davis » Google Analytics

Comments are closed.