Check for Google Analytics Using MooTools 1.2

By  on  

When I launched my new design last week, I used PHP to comment out Google Analytics because I didn't want to skew the stats. Well, I did skew my stats that day because I forgot to uncomment the damn GA code. Rook move, I know. With that in mind, I've created a MooTools script that checks to make sure Google Analytics is loaded. If not, you get a friendly JavaScript alert() reminder.

The MooTools 1.2 Code

window.addEvent('load', function() {
	if(typeof(eval(window)['_ufsc']) == 'undefined') {
		alert('David, turn on GA!');
	}
});

Not having 10 hours worth of stats is a big deal. This code will (hopefully) prevent this from happening again in the future.

Recent Features

  • By
    Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event...

  • By
    6 Things You Didn’t Know About Firefox OS

    Firefox OS is all over the tech news and for good reason:  Mozilla's finally given web developers the platform that they need to create apps the way they've been creating them for years -- with CSS, HTML, and JavaScript.  Firefox OS has been rapidly improving...

Incredible Demos

  • By
    PHP Woot Checker – Tech, Wine, and Shirt Woot

    If you haven't heard of Woot.com, you've been living under a rock. For those who have been under the proverbial rock, here's the plot: Every day, Woot sells one product. Once the item is sold out, no more items are available for purchase. You don't know how many...

  • By
    5 More HTML5 APIs You Didn’t Know Existed

    The HTML5 revolution has provided us some awesome JavaScript and HTML APIs.  Some are APIs we knew we've needed for years, others are cutting edge mobile and desktop helpers.  Regardless of API strength or purpose, anything to help us better do our job is a...

Discussion

  1. Jeff Hartman

    Why aren’t you using the same if statement as you do for your config file as mentioned in a previous post?

    http://davidwalsh.name/knowing-website-state-php

    As a matter of fact, you commented on your own post saying you use it for Google Analytics!

  2. @Jeff: Because I wasn’t developing locally, per say. I was dragging each file to my desktop, and then reuploading as I changed it. Again, not the best way of doing things.

    I do, however, use my “website state” strategy at work.

  3. @Jeff: Simply put….I was lazy with my own site and paid the price for it.

  4. Works great unless you are the type to upload something and not confirm the changes (and then have all your visitors get the popup). Hey, if you can make one lazy mistake you can make another, haha.

  5. @Sameer: Very true!

  6. A much simpler and easier to manage solution is to set up a filter in google analytics itself.

    For example, “exclude all traffic from user agents marked ‘website-tester.'” Then simply use Firefox’s user-agent switcher extension to use ‘website-tester’ whilst testing.

    This also allows more flexibility than the “website state” strategy as PHP, Google Analytics etc… can be clever about your own use of the site for testing purposes even on a website that is live and working for everybody else.

    Obviously the website state idea still has validity when when you have two near identical sites, one for production and the other live. As an example my platform generates a robots.txt automatically and on the staging site adds the “Disallow: /” directive to prevent the staging site from being indexed and all those duplicate content issues.

  7. Hey David, this post is a few years old now and it still works! :D but have you come across any new ways of detecting Google Analytics?

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