Created
March 14, 2015 10:54
-
-
Save sitefinitysteve/f7be00e033d24bb5e04a to your computer and use it in GitHub Desktop.
Sitefinity Google Analytics Document\Link Tracking Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function wireUpGADocumentTracking() { | |
if ($("body.sfPageEditor").length == 0) { | |
var filetypes = /\.(zip|exe|dmg|pdf|doc.*|xls.*|ppt.*|mp3|txt|rar|wma|mov|avi|wmv|flv|wav)$/i; | |
var baseHref = ''; | |
if (jQuery('base').attr('href') != undefined) baseHref = jQuery('base').attr('href'); | |
var hrefRedirect = ''; | |
jQuery('#content-wrapper').on('click', 'a', function (event) { | |
var el = jQuery(this); | |
var track = true; | |
var href = (typeof (el.attr('href')) != 'undefined') ? el.attr('href') : ''; | |
//Custom | |
//href = href.split('?')[0]; | |
var isThisDomain = href.match(document.domain.split('.').reverse()[1] + '.' + document.domain.split('.').reverse()[0]); | |
if (!href.match(/^javascript:/i)) { | |
var elEv = []; elEv.value = 0, elEv.non_i = false; | |
if (href.match(/^mailto\:/i)) { | |
elEv.category = 'email'; | |
elEv.action = 'click'; | |
elEv.label = href.replace(/^mailto\:/i, ''); | |
elEv.loc = href; | |
} | |
else if (href.split('?')[0].match(filetypes) || el.hasClass("sfdownloadTitle") || el.hasClass("track")) { | |
//var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined; | |
elEv.category = 'download'; | |
elEv.action = 'click'; | |
elEv.label = href.replace(/ /g, '-'); | |
elEv.loc = baseHref + href; | |
} | |
else if (href.match(/^https?\:/i) && !isThisDomain) { | |
elEv.category = 'external'; | |
elEv.action = 'click'; | |
elEv.label = href.replace(/^https?\:\/\//i, ''); | |
elEv.non_i = true; | |
elEv.loc = href; | |
} | |
else if (href.match(/^tel\:/i)) { | |
elEv.category = 'telephone'; | |
elEv.action = 'click'; | |
elEv.label = href.replace(/^tel\:/i, ''); | |
elEv.loc = href; | |
} | |
else track = false; | |
if (track) { | |
var ret = true; | |
if ((elEv.category == 'external' || elEv.category == 'download') && (el.attr('target') == undefined || el.attr('target').toLowerCase() != '_blank')) { | |
hrefRedirect = elEv.loc; | |
ga('send', 'event', elEv.category.toLowerCase(), elEv.action.toLowerCase(), elEv.label.toLowerCase(), elEv.value, { | |
'nonInteraction': elEv.non_i, | |
'hitCallback': gaHitCallbackHandler | |
}); | |
ret = false; | |
} | |
else { | |
ga('send', 'event', elEv.category.toLowerCase(), elEv.action.toLowerCase(), elEv.label.toLowerCase(), elEv.value, { | |
'nonInteraction': elEv.non_i | |
}); | |
} | |
return ret; | |
} | |
} | |
}); | |
gaHitCallbackHandler = function () { | |
window.location.href = hrefRedirect; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Steve,
I see that you started from the Blast code to create this, but (among other changes) omitted the setTimeout 400 milliseconds that (as I understand) ensures google analytics is called by not redirecting too quickly. Is there a reason you omitted it?