Skip to content

Instantly share code, notes, and snippets.

@zolitch
Created October 29, 2014 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zolitch/bbb4cdb334e99b7008a6 to your computer and use it in GitHub Desktop.
Save zolitch/bbb4cdb334e99b7008a6 to your computer and use it in GitHub Desktop.
GA event tracking module with share tracking
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();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment