var trackableExtensions =
"zip,exe,pdf,doc,docx,rtf,xls,xlsx,ppt,pptx,mp3,jpeg,jpg";
var baseUrl = "target site url";
var relativeBasePath = "/";
$(document).ready(function () {
$("a").on("click",
function () {
if
(isTrackableExtension($(this).attr('href'))) {
var filePath = $(this).attr('href');
if (filePath.toLowerCase().indexOf(baseUrl) > -1)
filePath = filePath.toLowerCase().replace(baseUrl, "");
else
filePath = filePath.toLowerCase().replace(relativeBasePath, "");
sendEventToAnalytics(filePath);
}
});
});
function getExtensions() {
return trackableExtensions.split(",");
}
function isTrackableExtension(linkUrl) {
var trackable = false;
var extenstions = getExtensions();
for (var i = 0; i <
extenstions.length; i++) {
if
(linkUrl.indexOf(extenstions[i]) > -1) {
trackable = true;
break;
}
}
return trackable;
}
function getFileExtension(filename) {
var extension = "";
var extenstions = getExtensions();
for (var i = 0; i <
extenstions.length; i++) {
if
(filename.indexOf(extenstions[i]) > -1) {
extension = extenstions[i].replace(".", "");
break;
}
}
return extension;
}
function getFileName(linkUrl) {
var fileName =
linkUrl.substring(linkUrl.lastIndexOf("/") + 1);
return fileName;
}
function sendEventToAnalytics(filename) {
var eventAction = 'Download-' +
getFileExtension(filename).toUpperCase();
_gaq.push(['_trackEvent', 'Document',
eventAction, filename]);
}
No comments:
Post a Comment