Last active
April 6, 2020 16:18
-
-
Save Greg-Boggs/482c0e170ecc084dac34a4a8236a0e5f to your computer and use it in GitHub Desktop.
data layer tracking example
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
jQuery(document).ready(function ($) { | |
// Track all link clicks internal and external | |
$('a').click(function(e) { | |
var linkUrl = e.target.href; | |
var linkText = $(this).text(); | |
if (this.hostname && this.hostname !== location.hostname) { | |
dataLayer.push({ | |
'event' : 'gaEvent', | |
'eventCategory' : 'Outbound Link', | |
'eventAction' : linkText, | |
'eventLabel' : linkUrl, | |
'transport' : 'beacon' | |
}); | |
} else { | |
dataLayer.push({ | |
'event' : 'gaEvent', | |
'eventCategory' : 'Internal Link', | |
'eventAction' : linkText, | |
'eventLabel' : linkUrl | |
}); | |
} | |
}); | |
// Track email contact links | |
$('a[href^="mailto:"]').click(function(e) { | |
var linkUrl = e.target.href; | |
var linkText = $(this).text(); | |
dataLayer.push({ | |
'event' : 'gaEvent', | |
'eventCategory' : 'Mailto', | |
'eventAction' : linkText, | |
'eventLabel' : linkUrl | |
}); | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment