|
|
|
define([ |
|
'jquery', |
|
'jquery.cookie' |
|
], function($) { |
|
'use strict'; |
|
var $body = $('body'), |
|
sendEvent = function(category, action, label) { |
|
ga('send', 'event', { |
|
'eventCategory': category, |
|
'eventAction': action, |
|
'eventLabel': label |
|
}); |
|
}, |
|
sendSocial = function(shareNetwork, action, url) { |
|
ga('send', 'social', shareNetwork, action, url); |
|
}, |
|
stallClickThrough = function($link, e) { |
|
setTimeout(function () { |
|
window.location.href = $link.attr('href'); |
|
}, 100); |
|
e.preventDefault(); |
|
}, |
|
trackDownload = function(link, e) { |
|
var filetypes = /(zip|ZIP|pdf|PDF|csv|CSV|doc*|DOC*|xls*|XLS*)+$/, |
|
filename = /(\w|[\-.])+$/.exec(link.href), |
|
ext = /[^\.]+$/.exec(filename)[0], |
|
$link = $(link); |
|
|
|
if (!filetypes.test(ext)){ |
|
return false; |
|
} |
|
sendEvent('Download', '.' + ext + ' download', filename ); |
|
stallClickThrough($link, e); |
|
return true; |
|
}, |
|
trackCustomEvent = function($link) { |
|
var category = $link.data('category') || '', |
|
label = $link.data('label') || '', |
|
action = $link.data('action') || 'click'; |
|
sendEvent(category, action, label); |
|
}, |
|
socialTracking = function() { |
|
var poll, |
|
fbPoller = setInterval(function(){ |
|
//facebook |
|
if (typeof FB !== 'undefined' && FB.Event && FB.Event.subscribe) { |
|
FB.Event.subscribe('edge.create', function(targetUrl) { |
|
ga('send', 'social', 'facebook', 'like', targetUrl); |
|
}); |
|
FB.Event.subscribe('edge.remove', function(targetUrl) { |
|
ga('send', 'social', 'facebook', 'unlike', targetUrl); |
|
}); |
|
clearInterval( fbPoller ); |
|
} else if ( poll > 100 ) { |
|
clearInterval( fbPoller ); |
|
} else { |
|
poll += 1; |
|
} |
|
}, 100); |
|
|
|
}, |
|
attachHandlers = function() { |
|
$body.on('click', 'a', function(e) { |
|
var $link = $(this); |
|
|
|
//is downloadable type? |
|
//this will return false if not a downloadable .ext |
|
var isDownload = trackDownload(this, e); |
|
|
|
//is custom in page event? No forwarding link? |
|
if (!isDownload && $link.hasClass('js-trackevent')) { |
|
trackCustomEvent($link); |
|
e.preventDefault(); |
|
} |
|
//is custom event? But still needs to link through |
|
if (!isDownload && $link.hasClass('js-trackevent-linkthrough')) { |
|
trackCustomEvent($link); |
|
stallClickThrough($link, e); |
|
} |
|
}); |
|
var $eventoSendOnLoad = $('.js-sendGaEvent'); |
|
if ($eventoSendOnLoad.length) { |
|
$eventoSendOnLoad.each(function() { |
|
trackCustomEvent($(this)); |
|
}); |
|
} |
|
socialTracking(); |
|
|
|
}; |
|
|
|
var GaTracking = function() {}; |
|
|
|
GaTracking.prototype.activate = function() { |
|
attachHandlers(); |
|
}; |
|
GaTracking.prototype.sendShare = function(shareNetwork, action, url) { |
|
sendSocial(shareNetwork.toLowerCase(), action, url); |
|
}; |
|
GaTracking.prototype.sendEvent = function(category, action, label) { |
|
sendEvent(category, action, label); |
|
}; |
|
|
|
|
|
|
|
return new GaTracking(); |
|
}); |