Created
April 4, 2011 18:13
-
-
Save benbalter/902105 to your computer and use it in GitHub Desktop.
Google Analytics virtual event tracking code for external links, downloads, and mailtos using jQuery.
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
//Mailto tracking code | |
$('a[href^="mailto\:"]').click(function() { | |
_gaq.push(['_trackEvent','Email', 'Click', $(this).attr('href') ]); | |
}); | |
//Download Tracking Code | |
$('a[href$="zip"],a[href$="pdf"],a[href$="doc"],a[href$="docx"],a[href$="xls"],a[href$="xlsx"],a[href$="ppt"],a[href$="pptx"],a[href$="txt"],a[href$="csv"]').click(function() { | |
var u = $(this).attr('href'); _gaq.push(['_trackEvent','Download', u.match(/[^.]+$/), u ]); | |
}); | |
//External link tracking code for old site | |
$('a[href^="http"]').click(function(){ | |
if (this.hostname != location.hostname ) | |
_gaq.push(['_trackEvent', 'External Link', 'Click', $(this).attr('href') ]); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment