Reuben Yau

Online Marketing Consultant
Posted Wednesday, April 11th, 2007.

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( '"><\/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( '' ) ==-1 ) { document.write( 'urchinTracker(document.referrer);' ); } document.write( '<\/scr'+'ipt>' ); </script>

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

Send to digg,, reddit, etc
No comments? Rate this post instead
1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5 out of 5)
Loading ... Loading ...

Related Posts

If you found this page useful, consider linking to it.
Simply copy and paste the code below into your web site (Ctrl+C to copy)
It will look like this: Google Analytics Tracking Code - HTTPS and Full External Referrer Only



Post A Comment