Skip to content

Instantly share code, notes, and snippets.

Created January 15, 2015 16: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 anonymous/ed46d56840095d798ce2 to your computer and use it in GitHub Desktop.
Save anonymous/ed46d56840095d798ce2 to your computer and use it in GitHub Desktop.
Example of a generic client site GTM HTML Tag to collect custom events
// This is intended as an example, please don't assume it works or judge my hackery style
// This would be wrapped in a script tag in GTM and make sure jQuery is present, you could
// use vanilla Javascript if you please.
// Place our generic function
function simoPush(c,a,l,v) {
dataLayer.push({'event' : 'GAEvent','eventCategory':c,'eventAction':a,'eventLabel':l,'eventValue':v});
}
// Now add in your client side events to measure
// Form submits
$('form').on('submit',function(){
simoPush( 'Forms', 'Submit', $(this).attr('id'), window.location.pathname);
return true;
});
// External link clicks
$('a[rel="external"').on('click',function(){
simoPush( 'External Links', 'Click', $(this).attr('href'), window.location.pathname);
return true;
});
// Youtube plays with embeds set as enablejsapi=1
function simoTrackYT(e) {
if( e.data == YT.PlayerState.PLAYING ) {
var vid_url = e.target.getVideoUrl();
simoPush( 'Videos', 'Play', vid_url);
}
}
}
function onYouTubeIframeAPIReady() {
var player = new YT.Player('player', {
events: {'onStateChange':simoTrackYT}
});
}
$('form').on('submit',function(){
return true;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment