Tracking mit der Special Events API
by shapeshifta
HTML
<a class="track" href="#moep" title="Hallo, ich bin's!" data-category="Suchparameter Startseite" data-action="Laufzeit" data-label="test">click me</a>
<div id="count">0</div>
JavaScript
window.log = function(){
console.log( arguments );
};
(function($) {
$.event.special.track = {
setup: function( data, namespaces ) {
//wenn Event-Handler hinzugefĂĽgt wird
$( this ).on( "click mouseover", $.event.special.track.handler );
},
teardown: function( namespaces ) {
//wenn Event-Handler entfernt wird
$( this ).off( "click mouseover", $.event.special.track.handler );
},
handler: function( event ) {
//event type
event.type = "track";
//trigger track handler
jQuery.event.dispatch.apply( this, arguments );
var $this = $(this);
log(event.type, $this.data('category'), $this.data('action') || this.href, $this.data('label'));
}
};
})(jQuery);
var count = 0;
//track event muss am Anfang stehen!
$('a.track').on('track click mouseover', function(e){
if(e.type === 'click'){
$('#count').text(++count);
}
if(e.type === 'mouseover'){
$('#count').text(--count);
}
e.preventDefault();
});