Last active
May 28, 2019 10:35
-
-
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.
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
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fire as Custom HTML Tag within Google Tag Manager on DOM Ready.