Detecting Google Universal Analytics

By  on  

Just about everyone uses Google Analytics and why shouldn't they?  The amount of useful data you can pull from Google Analytics, especially if you use custom dimensions, is amazing.  Whether you're A/B testing, trying to increase engagement, or improve sales, Google Analytics is just about perfect.

I recently experimented with detecting external link clicks with the current version of Google Analytics called Universal Analytics.  In doing so I realized that utilities like Ghostery blocked GA, which is fine, but they also shim the ga function that I was using to detect if the third party service was loaded.  Damn.  It turns out you need to do an additional check:

if(window.ga && ga.create) {
	// Do you ga stuff
	ga('send', 'pageview');
}

Checking for the Analytics's custom create method ensures that Google's utility is there and not simply shimmed by Ghostery or a likewise service!

Recent Features

  • By
    Camera and Video Control with HTML5

    Client-side APIs on mobile and desktop devices are quickly providing the same APIs.  Of course our mobile devices got access to some of these APIs first, but those APIs are slowly making their way to the desktop.  One of those APIs is the getUserMedia API...

  • By
    CSS Animations Between Media Queries

    CSS animations are right up there with sliced bread. CSS animations are efficient because they can be hardware accelerated, they require no JavaScript overhead, and they are composed of very little CSS code. Quite often we add CSS transforms to elements via CSS during...

Incredible Demos

  • By
    Flexbox Equal Height Columns

    Flexbox was supposed to be the pot of gold at the long, long rainbow of insufficient CSS layout techniques.  And the only disappointment I've experienced with flexbox is that browser vendors took so long to implement it.  I can't also claim to have pushed flexbox's limits, but...

  • By
    Translate Content with the Google Translate API and JavaScript

    Note:  For this tutorial, I'm using version1 of the Google Translate API.  A newer REST-based version is available. In an ideal world, all websites would have a feature that allowed the user to translate a website into their native language (or even more ideally, translation would be...

Discussion

  1. > Just about everyone uses Google Analytics and why shouldn’t they?

    Google Analytics is one of the key pieces of the Orwellian world which Google is building for us. Virtually every website puts this innocent looking piece of JavaScript on all their pages. It gathers people’s browsing habits and centralizes this information in Google’s data farms. That data end up being sold on the data market and refined by data brokers such as the Acxiom company or given away for free to intelligence agencies, as everyone is now aware of. I understand why you do it and I’m not blaming you, I used to do the same until I realized what I was contributing to by doing so. I hope you’ll agree that we should thrive to make the web a tool of freedom and not a weapon of control. It’s not that people at Google are bad either, it’s just that any company that grows so big fatally end up being dehumanized. Piwik works fine ;-)

  2. Great, thanks for posting – this works well. After years of checking for _gaq comes to and end…

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