Skip to content

Instantly share code, notes, and snippets.

@cgrymala
Created July 31, 2013 16:34
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 cgrymala/6dac4610fb80dc77eabb to your computer and use it in GitHub Desktop.
Save cgrymala/6dac4610fb80dc77eabb to your computer and use it in GitHub Desktop.
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