Google Analytics Tracking Code – HTTPS and Full External Referrer Only

April 11, 2007 by reuben | Filed under Google Analytics, Tracking, Work.

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 “www.mywebsite.com” with your website.


11 Responses to “Google Analytics Tracking Code – HTTPS and Full External Referrer Only”

  1. Saptarshi says:

    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. Tom says:

    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. Tom says:

    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 “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

  5. [...] is if you use? http internally and SSL (https) externally.? If so, check out Reuben Yau’s post on how to handle both cases with a little extra javascript.? If you want to use it with SharePoint [...]

  6. Darryl says:

    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?

    Webpage: http://www.interface21.com
    Error: Invalid Character
    Code: 0

    document.write( ” );

    _uacct = “UA-2728886-1″;
    urchinTracker();

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

  7. Darryl says:

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

  8. reuben says:

    Hi Daryl

    You should probably start by replacing the fancy quotes with regular apostrophes.

  9. Hassan says:

    Hi Darryl,

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

    Many thanks,
    Hassan

  10. Chris Bae says:

    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.

  11. JK says:

    Is this still good and useful in 2011?