Secure (SSL) Google Analytics

By  on  

Google Analytics is a tremendous tool, especially when your customer wants an eCommerce website. Analytics is even more powerful when your customer uses Adwords to drive in traffic.

Google provides the following code for you to add to your page footer so that Analytics can grab statistics:

The problem with this is that if you're pulling this code in on a secure page, a security warning will pop up mentioning that the page is loading both secure and non-secure items. How do you prevent this issue (as well as trust issues with the user)? Simply import the secure version of Google Analytics:

For websites that have some pages secure and others not HTTPS protected, I recommend always calling the secure version of Google Analytics' urchin.js.

Recent Features

  • By
    CSS @supports

    Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS.  What we end up doing is repeating the same properties multiple times with each browser prefix.  Yuck.  Another thing we...

  • By
    I’m an Impostor

    This is the hardest thing I've ever had to write, much less admit to myself.  I've written resignation letters from jobs I've loved, I've ended relationships, I've failed at a host of tasks, and let myself down in my life.  All of those feelings were very...

Incredible Demos

  • By
    dat.gui:  Exceptional JavaScript Interface Controller

    We all love trusted JavaScript frameworks like MooTools, jQuery, and Dojo, but there's a big push toward using focused micro-frameworks for smaller purposes. Of course, there are positives and negatives to using them.  Positives include smaller JS footprint (especially good for mobile) and less cruft, negatives...

  • By
    Detect Vendor Prefix with JavaScript

    Regardless of our position on vendor prefixes, we have to live with them and occasionally use them to make things work.  These prefixes can be used in two formats:  the CSS format (-moz-, as in -moz-element) and the JS format (navigator.mozApps).  The awesome X-Tag project has...


  1. Nice post! Google Analytics has a new tracking code option, ga.js instead of the urchin.js, that handles the ssl component. Here’s a look:

    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    document.write(unescape("%3Cscript src='" + gaJsHost + "' type='text/javascript'%3E%3C/script%3E"));
    var pageTracker = _gat._getTracker("UA-xxxxxxx-x");
  2. muddy

    The answer to my ssl headaches! Didn’t think to check if Google Analytics offered a secure tracking code. Will be so happy to say goodbye to that annoying “non-secure” pop-up.

    Thank ya thank ya thank ya.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!