Last active
June 11, 2016 15:48
-
-
Save khoatran/5671ea0ac620590768d521c01deda818 to your computer and use it in GitHub Desktop.
Google analytic tracking
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
var Analytic = (function(){ | |
var module = {}; | |
var trackEventOfElement = function(el) { | |
var category = el.attr('data-category'); | |
var action = el.attr('data-action'); | |
var label = el.attr('data-label'); | |
var value = el.attr('data-value'); | |
module.trackEvent(category, action, label, value); | |
}; | |
var trackSocialOfElement = function(el) { | |
var network = el.attr('data-network'); | |
var action = el.attr('data-action'); | |
var target = el.attr('data-target'); | |
var pagePath = el.attr('attr-page-path'); | |
module.trackSocial(network, action, target, pagePath); | |
}; | |
var trackEventsRenderedFromBackEnd = function() { | |
var hiddenDataBlock = $('#hidden-ga-data'); | |
if(hiddenDataBlock.length > 0) { | |
$('.ga-event-data', hiddenDataBlock).each(function () { | |
trackEventOfElement($(this)); | |
}); | |
} | |
}; | |
module.trackEvent = function(category, action, label, value){ | |
if(typeof(ga) == 'undefined') { | |
return; | |
} | |
ga('send', 'event', category, action, label, value); | |
}; | |
module.trackSocial = function(network, socialAction, opt_target, opt_pagePath){ | |
if(typeof(ga) == 'undefined') { | |
return; | |
} | |
if(typeof(opt_pagePath) == "undefined" ) { | |
ga('send', 'social', network, socialAction, opt_target); | |
} else { | |
ga('send', 'social', network, socialAction, opt_target, opt_pagePath); | |
} | |
}; | |
module.submit = function(form) { | |
e.preventDefault(); | |
setTimeout(function() { | |
form.submit(); | |
}, 200); | |
}; | |
module.linkClick = function(link) { | |
e.preventDefault(); | |
var linkURL = $.trim(link.href); | |
if(linkURL != "" && linkURL.indexOf("/") > -1) { | |
setTimeout('document.location = "' + link.href + '"', 200); | |
} | |
}; | |
module.init = function() { | |
trackEventsRenderedFromBackEnd(); | |
$('form.ga-form-track').attr("onsubmit","Analytic.submit(this);return false;"); | |
$('a.ga-event-data').attr("onclick","Analytic.linkClick(this);return false;"); | |
$("body").on("click", ".ga-event-data", function() { | |
trackEventOfElement($(this)); | |
}); | |
$("body").on("click", ".ga-social-data", function() { | |
trackSocialOfElement($(this)); | |
}); | |
}; | |
return module; | |
}()); |
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() { | |
Analytic.init(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment