Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save benbalter/902105 to your computer and use it in GitHub Desktop.
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.
//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