Mootools Hash Cookie Compatibility Class

Mootools is a fantastic Javascript framework library that gives you clean, well-tested, cross-browser mechanisms for browser capability detection, manipulating the DOM, injecting SWFs, making AJAX requests, and building animation into your client-side applications.  I’m won’t try to sell it over other libraries like scriptaculous, dojo or jquery.  But if you’ve selected mootools as your javascript framework, you might find this article useful.

Mootools has a nifty concept of a hash cookie, where you serialize a hashed array and stuff it into a cookie.  I like this, because as an application developer, you don’t have access to an unlimited number of cookies.  With a hash cookie, you can store a number of values in a single cookie.  

If you build any Web applications using javascript and PHP, you may find occasion to share cookies between client-side javascript and server-side PHP.   In a more sophisticated application, both the client and the server code may need to read and write to the cookie.  Enter my library for manipulating hash cookies in PHP.  With this library, you can move the hash seamlessly from your PHP code to your Javascript.

Let me give you an example of how you could use such a tool in practice.

On our site, we’re using an ad serving engine that has the capability to perform frequency capping, so that visitors won’t see the same ad too many times.  Many of our clients want to run “floaters”, or animated flash-based ads that float in a div above the page content for a few seconds when the page is loaded.  These are high-revenue ads, but they are quite intrusive to the visitor.  We don’t want to run too many of these ads.  Howevever, standard frequency capping doesn’t help us if we end up with a lot of advertisers running these floaters.  Even if the frequency cap is set to 1 impression per day, with 10 advertisers, the user might see 10 floaters in a day!  Given that the average user might look at 7 pages, every single page view would be obscured by a floater.  

No good.  We want to limit the number of floaters seen per user per day to 1 or 2.

Since the standard mechanisms in the ad server can’t control the delivery of floaters, we have to take matters into our own hands.

If we knew how many floaters a user had seen, our PHP code could generate ad tags that would direct the ad server to deliver anything but a floater.

But our server side PHP code has no knowledge of what the ad server selects and delivers.  The PHP just generates javascript ad tags, and the ad server injects the selected ads into the DOM.

So we turn to the javascript itself to help with the tracking.  A floater ad involves a lot of javascript along with the SWF that is displayed to the user.  At the end of the javascript, we load a mootools hash cookie and increment a “floater” value in the hash cookie by calling the ads_freq_record_imp() function:

Note that we have to pass in a key (which in this case might be “floater”).  This function is general-purpose in that it lets us perform frequency capping across a variety of different “classes” of ads.  We could use it to control the behavior of pushdown ads or expandables just by changing the key we use when we call this function.  No matter how many classes we use, we are only using one cookie, which keeps our “cookie footprint” to a minimum.

On the server, we can access this ads_freq hash cookie and compare the number of delivered impressions of each ad class against pre-configured maximum values.  If we’ve met our maximum for the day, we’ll spit out code for the ad server so that it won’t deliver ads of that class (I leave that as an exercise for the reader).

You can download the source to the library, along with a test script and an example PHP script here.  Note that this library emulates the mootools 1.2 API for Hash.Cookie.

Join the Conversation

1 Comment

Leave a comment

Your email address will not be published. Required fields are marked *