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
    Responsive Images: The Ultimate Guide

    Chances are that any Web designers using our Ghostlab browser testing app, which allows seamless testing across all devices simultaneously, will have worked with responsive design in some shape or form. And as today's websites and devices become ever more varied, a plethora of responsive images...

  • By
    Write Better JavaScript with Promises

    You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...

Incredible Demos

  • By
    Sexy Opacity Animation with MooTools or jQuery

    A big part of the sexiness that is Apple software is Apple's use of opacity. Like seemingly every other Apple user interface technique, it needs to be ported to the web (</fanboy>). I've put together an example of a sexy opacity animation technique...

  • By
    HTML5&#8217;s placeholder Attribute

    HTML5 has introduced many features to the browser;  some HTML-based, some in the form of JavaScript APIs, but all of them useful.  One of my favorites if the introduction of the placeholder attribute to INPUT elements.  The placeholder attribute shows text in a field until the...

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!