Tony Pitale

Javascript Lessons Learned from Google Analytics

Over the years I’ve occasionally gone spelunking in the Google Analytics tracking javascript. Historically this code has been heavily obfuscated along with the usual minimizing. While it is difficult to tease out, I have learned some interesting things through my efforts.

undefined

In javascript, undefined is a variable. If someone less-sane than ourselves was around, they could reassign undefined to an entirely different value. o_0

To be certain that they’re really working with undefined, the GA tracking javascript uses void 0 or void(0) to ensure it works with an explicitly undefined undefined.

void(0) == undefined //=> true

arguments

I was only vaguely aware of arguments before my exploration, but it’s actually quite powerful. It’s a local variable that’s set for each function when it is called. It contains an array of any arguments passed to the function when it was called, regardless of the arguments that function would normally accept.

var show_args = function(a,b) {
  console.log(a);
  console.log(b);
  console.log(arguments);
}

show_args(1,2,3,4,5);

The output from the above should be:

1
2
[1,2,3,4,5]

As always, the Mozilla Developer Network docs have a ton of fantastic information.

Access functions in objects with array syntax

The usual pattern for calling a function is something like this

var blah = function() {alert('Blah.');}
blah();

Or if it’s inside an object:

var obj = {
  blah: function() {alert('Blah.');}
}

obj.blah();

But, because objects have the ability to access their contents like a hash in Ruby, or an array, we can get a reference to our function using [] syntax.

obj["blah"]();

These may not be new to all of you, but I find them extremely useful in working with my own javascript code. I’m sure I would have found them out eventually, but it really speaks to the value of reading code other than our own. There’s always something to learn. Though, I would not recommend you go diving through highly obfuscated javascript. At least not on a regular basis.

Read More Recent Posts