Abstracting Analytics, Part 2
by Guillermo A. FisherSince the relaunch of the site, I’ve been working with certain elements of the JavaScript to improve performance and keep things clean. In the past, I talked about creating a brand new JavaScript library that I’d use here; and I started to do write it, looking to jQuery, Prototype, and DOMAssistant for inspiration. But, in the end, I decided that I didn’t want to add a bunch of bloat to the library (which is what ended up happening in my first attempt). So instead of worrying about complicated closures, unnecessary abstraction, and weird OOP implementations, I decided to only put in what was needed; and the result, thus far, is something lean and devoid of dollar sign functions, used primarily for adding analytics and ads to the UI.
Right now, I’m using the OOR (Out Of Repose) namespace. If you’re looking at the file at the time of this writing (check the date), you’ll only see 3 actual functions in the base object:
OOR.log(): logs messages to the Firebug console. I have plans to add code that will trigger alerts if a second parameter is passed.OOR.require(): loads a JavaScript file and executes a callback (if one is provided as the second parameter) once the file is loaded. There’s some logging in there, too. I intend to enable the dynamic loading of stylesheets in the near future.OOR.register(): registers a “widget”. A widget, as I’m defining it, is any UI element added to the page that has it’s own built-in HTML/CSS/JavaScript. If you take a look at the file, you’ll notice that I have 2 widgets registered:AdandAnalytics.
The first 2 functions are pretty self-explanatory, but my approach to widgets could use an explanation.
My Widgets
To register a widget in the OOR.widget namespace, I’m using the following convention: OOR.register('widgetName', vendors);, where vendors is a JSON object of functions that hold code for different widget “vendors”. A vendor — again, as I’m defining it — is just an implementation (or “version”, or “type”) of a widget. A widget ‘Foo’, for example, which displays an image on a site, could have different vendors: ‘Red’, ‘White’, ‘Blue’. The registration code for that widget would look something like this:
// simple example
OOR.register('Foo', {
Red: function(options){
document.write('<img src="red.gif" alt="' + options.description + ' and I hope you like it." />');
},
White: function(options){
document.write('<img src="white.gif" alt="' + options.description + ' and you are on my site." />');
},
Blue: function(options){
document.write('<img src="bluegif" alt="' + options.description + ' and you are looking at it." />');
}
});
This would create a new function — OOR.widget.Foo() — which, when called, would need 2 parameters: the desired vendor and it’s options (you’ll notice that each of the above vendors takes an “options” parameter, which is also a JSON object). So, if I wanted to display a red image on the site, I would make the following call, wrapped in a script tag, somewhere in the body of the page:
new OOR.widget.Foo('Red', { description: 'This is a red image' });
According to the registration code, the above call would display “red.gif”, and it’s alt tag would read: “This is a red image and I hope you like it.”
More to come
The next steps here are to modify the OOR.register() function to allow me to register singular widgets whose functionality is not vendor dependent, making the code look like this:
// register the widget and it's functionality...
OOR.register('Foo', function(options){
// some code
});
// the implementation code will read as follows...
new OOR.widget.Foo({foo:'sample'});
I’ll also need to make the options parameter optional, so that in cases where the vendor code doesn’t require any configurable options, none need to be passed. Oh, and I’ll need to write something to handle conflicting namespaces.
What this has to do with Analytics
Here’s what Google Analytics looks like on my pages:
new OOR.widget.Analytics('Google', 'UA-9999999');
Nice and neat, eh? Hope this was nerdy enough for ya.
Your Feedback Civil & constructive, please.
[…] javascript.Since implementing the changes to my Google Analytics installation as detailed in my last post, I’ve noticed a sharp decrease in page views: none recorded. I put the default code back on […]