alphagov/static — pull request #47 by gtrogers (2012-09-19 12:55:45+00:00)
Title: New event tracking code + previous functional tests for tracking.

This pull request makes a number of changes to the existing google analytics tracking code.

* Tracking no longer prevents default behaviour for opening links (opening in new tab while ctrl is pressed for instance)
* Tracking no longer prevents subsequent jQuery .click events from firing.

This is done by 'freezing' the click event as the tracking code receives it and then unfreezing it after a small delay to give the google analytics code enough time to fire. The unfrozen event will then continue as normal getting picked up by any other handlers and finally the browser. At a lower level we actually copy the event, prevent the original from propagating and then fire a new identical event when it is unfrozen. This is doen slightly differently pre/post ie9

* Added jasmine tests for the above.
* Added jasmine tests around tracking for guide and transaction formats.

Code for review
```javascript
// adding this to the jQuery name space
var registerEventIceBucket = function () {
$.extend({
freezeEvent:function (event) {
var newEventTrigger = {};

var cloneBrowserEvent = function (originalEvent) {
var newEvent = document.createEvent('MouseEvents');
newEvent.initMouseEvent(
originalEvent.type,
originalEvent.bubbles,
originalEvent.cancelable,
originalEvent.view,
originalEvent.detail,
originalEvent.screenX,
originalEvent.screenY,
originalEvent.clientX,
originalEvent.clientY,
originalEvent.ctrlKey,
originalEvent.altKey,
originalEvent.shiftKey,
originalEvent.metaKey,
originalEvent.button,
originalEvent.relatedTarget);
return newEvent;
};

if ((event.originalEvent !== undefined) && event.originalEvent.initMouseEvent) {
var newEvent = cloneBrowserEvent(event.originalEvent);
newEventTrigger = function () {
event.target.dispatchEvent(newEvent);
};
} else {
var shouldOpenInNewWindow = event.metaKey || event.ctrlKey;
newEventTrigger = function () {
if (shouldOpenInNewWindow) {
$(event.target).attr('target', '_blank');
}
event.target.click();
};
}
return { unfreeze:newEventTrigger };
}
});
};

$(registerEventIceBucket);
```
see: https://github.com/alphagov/static/blob/google_analytics_functional_tests/app/assets/javascripts/event-ice-bucket.js