Skip to content

Instantly share code, notes, and snippets.

@cgrymala
Created July 31, 2013 16:34
Embed
What would you like to do?
Ensure proper cross-domain Analytics tracking
// Add 'external' CSS class to all external links
jQuery('a').filter( function() {
// Only take action when a link leads to a different domain than the one we're currently on,
// and that domain is in the list of internal sites
return this.hostname != location.hostname && this.hostname in umw_internal_sites;
} ).attr( 'analytics-cross-domain', 'yes' ).click( function() {
// If the link is supposed to open in a new window, let it do so
if ( jQuery( this ).attr( 'target' ) ) {
window.open( jQuery(this).attr('href'), jQuery( this ).attr( 'target' ) );
// Otherwise, tell Analytics that this is a link to somewhere else on our site
} else {
_gaq.push(['_link', jQuery(this).attr('href'), true]);
}
// Stop the link from actually doing its job, since the script above will handle that
return false;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment