Skip to content

Instantly share code, notes, and snippets.

@jsn789
Last active May 28, 2019 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsn789/85841c1308158c4f40d6491480fdbf1e to your computer and use it in GitHub Desktop.
Save jsn789/85841c1308158c4f40d6491480fdbf1e to your computer and use it in GitHub Desktop.
jQuery Click Listener for Google Tag Manager replicating the "Click Element" and "Link Click" functionality within Google Tag Manager.
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
/* jQuery is required for this script.
*/
/// load jQuery in no-conflict mode
jQuery.noConflict();
(function($) {
/// dom ready
$(function() {
$('body').on('click', function(e){
dataLayer.push({
"gtm.element": e.target,
"gtm.elementClasses": $(e.target).attr('class'),
"gtm.elementId": $(e.target).attr('id'),
"gtm.elementTarget": e.target,
"gtm.elementUrl": $(e.target).attr('href'),
"event": "gtm.click"
});
});
/// add gtm.linkClick handlers
$(document).on('click', 'a', function(e){
dataLayer.push({
"gtm.element": e.currentTarget,
"gtm.elementClasses": $(e.currentTarget).attr('class'),
"gtm.elementId": $(e.currentTarget).attr('id'),
"gtm.elementTarget": e.target,
"gtm.elementUrl": $(e.currentTarget).attr('href'),
"event": "gtm.linkClick"
});
});
});
})(jQuery);
</script>
@jsn789
Copy link
Author

jsn789 commented May 28, 2019

Fire as Custom HTML Tag within Google Tag Manager on DOM Ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment