Skip to content

Instantly share code, notes, and snippets.

@johandouma
Last active October 8, 2018 16:35
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 johandouma/ac8b4ef97a22af0c1982c9dc4c45eca1 to your computer and use it in GitHub Desktop.
Save johandouma/ac8b4ef97a22af0c1982c9dc4c45eca1 to your computer and use it in GitHub Desktop.
// External selector for jquery
$.expr[":"].external = function(obj, index, meta, stack) {
var a = $(obj).prop("href") || "";
var server = window.location.href.split('/'); server = server[0] + "//" + server[2];
if(a == "#" || a == "" || a.indexOf(server)>-1 || a.indexOf("javascript:")>-1 ) {
return false;
}
return true;
}
// download selector for jquery
$.expr[":"].download = function(obj, index, meta, stack) {
url = $(obj).prop("href") || "";
extension = (url = url.substr(1 + url.lastIndexOf("/")).split('?')[0]).substr(url.lastIndexOf("."));
if(['.pdf', '.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx', '.rtf', '.txt', '.csv', '.pages', '.mp3', '.mov', '.avi', '.jpg', '.jpeg', '.gif', '.png', '.zip', '.gzip'].indexOf(extension) > -1) {
return true;
}
return false;
}
// Has to be executed after ga is initialized (gets around adblockers, DNT, or ga loading issues)
ga(function(){
// External Link Tracking
$('a:external').click(function(e) {
var url = $(this).prop("href");
var event = {
hitType: 'event',
eventCategory: 'Outbound Link',
eventAction: 'click',
eventLabel: url,
hitCallback: function() { document.location = url; },
nonInteraction: true // do not count for bounce rate
};
if(url.substring(0, 7) == "mailto:") {
event.eventCategory = 'Email Link';
event.nonInteraction = false; // count for bounce rate (to decrease it)
}
if(url.substring(0, 4) == "tel:" || url.substring(0, 7) == "callto:") {
event.eventCategory = 'Tel Link';
event.nonInteraction = false;
}
if(url.substring(0, 4) == "sms:") {
event.eventCategory = 'SMS Link';
event.nonInteraction = false;
}
ga('send', event);
e.preventDefault();
});
// Track downloads as page views
$('a:download:not(:external)').click(function(e) {
var url = $(this).prop("href");
var $defaultPrevented = event.defaultPrevented;
ga('send', {
hitType: 'pageview',
page: url,
hitCallback: function() {
if(!$defaultPrevented) {
document.location = url;
}
}
});
e.preventDefault();
});
// Exception tracking with GA
window.onerror = function(msg, url, line, col, error) {
var extra = !col ? '' : '\ncolumn: ' + col;
extra += !error ? '' : '\nerror: ' + error;
ga('send', 'exception', {
'exDescription': "JS Error: " + msg + "\nurl: " + url + "\nline: " + line + extra,
'exFatal': false
});
return false;
};
/* Custom Event Tracking
Link Format:
<a href="some url"
data-ga-event
data-ga-event-category=""
data-ga-event-action=""
data-ga-event-label=""
data-ga-event-value="">
More Info
</a>
*/
$('a, button[type=submit], input[type=submit]').filter("[data-ga-event]").click(function(e) {
var $this = $(this);
if($this.data("ga-event-sent")) {
return true;
}
var eventInfo = {
hitType: 'event',
hitCallback: function(){ $this.trigger("click"); },
nonInteraction: true
};
if($this.data("ga-event-category")) {
eventInfo.eventCategory = $this.data("ga-event-category");
} else return true; // this is required by GA
if($this.data("ga-event-action")) {
eventInfo.eventCategory = $this.data("ga-event-action");
} else return true; // this is required by GA
if($this.data("ga-event-label")) {
eventInfo.eventLabel = $this.data("ga-event-label");
}
if($this.data("ga-event-value")) {
eventInfo.eventValue = $this.data("ga-event-value");
}
ga('send', eventInfo);
e.preventDefault();
$this.data("ga-event-sent", true);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment