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
    Conquering Impostor Syndrome

    Two years ago I documented my struggles with Imposter Syndrome and the response was immense.  I received messages of support and commiseration from new web developers, veteran engineers, and even persons of all experience levels in other professions.  I've even caught myself reading the post...

  • By
    9 Mind-Blowing WebGL Demos

    As much as developers now loathe Flash, we're still playing a bit of catch up to natively duplicate the animation capabilities that Adobe's old technology provided us.  Of course we have canvas, an awesome technology, one which I highlighted 9 mind-blowing demos.  Another technology available...

Incredible Demos

  • By
    CSS Tooltips

    We all know that you can make shapes with CSS and a single HTML element, as I've covered in my CSS Triangles and CSS Circles posts.  Triangles and circles are fairly simply though, so as CSS advances, we need to stretch the boundaries...

  • By
    RealTime Stock Quotes with MooTools Request.Stocks and YQL

    It goes without saying but MooTools' inheritance pattern allows for creation of small, simple classes that possess immense power.  One example of that power is a class that inherits from Request, Request.JSON, and Request.JSONP:  Request.Stocks.  Created by Enrique Erne, this great MooTools class acts as...

Discussion

  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 + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
    
    
    var pageTracker = _gat._getTracker("UA-xxxxxxx-x");
    pageTracker._initData();
    pageTracker._trackPageview();
    
    
  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!