Monitor Prototype Ajax Requests With Google Analytics
Tagged ajax, google-analytics, Prototype
Since Prototype 1.6 final (released this week) all Ajax transport objects are wrapped in an Ajax.Responders object. Using this object we can add generic behavior to Ajax Requests. Imagine you want to show a loading icon every time a request is ongoing. Or even better, monitoring request that are made using Google Analytics.
Observing Ajax requests
So with the Ajax.Responders object we can observe all kinds of events, fired by Ajax.Request, Ajax.Updater and Ajax.PeriodicalUpdater. These are the events that are observable by Ajax.Responders:
- onCreate
- onComplete
- onException
- onInteractive
- onLoaded
- onLoading
- onUninitialized
You can read more about these events on the Prototype API docs. With the Ajax.Responders object we can register callback functions on these events. In the following example I’ll show how to monitor Ajax requests using Google Analytics.
Ajax.Responders.register({
onComplete: function(request){
// Check if urchinTracker function is available.
if(typeof(urchinTracker) == 'function' && request && request.url){
// Notice Analytics about the completed request.
urchinTracker(request.url);
}
}
});
You can also unregister callback functions. Read more about it at the Prototype API docs.
PLEASE
MAKE
PLOTR
WORK
WITH
1.6
THANKS!