Skip to content

Instantly share code, notes, and snippets.

@sampel
Created December 11, 2012 09:09
Show Gist options
  • Save sampel/4257219 to your computer and use it in GitHub Desktop.
Save sampel/4257219 to your computer and use it in GitHub Desktop.
Adding Google Analytics event tracking to outbound links
$(function() {
unction recordOutboundLink(link, category, action) {
try {
_gaq.push(['_trackEvent', category , action, 'visiting things']);
setTimeout('window.open("' + link.href + '")', 100);
} catch(err) {
console.error('Error recording outbound link.');
}
}
$('.link-box').on("click", function() {
recordOutboundLink(this, 'CF Systems', this.href);
});
});
@sampel
Copy link
Author

sampel commented Dec 11, 2012

Mistake in line 3: unction

My doubt is with the line 7, in this line i'm adding 1 second of delay to the window.open call because is a recomendation by Google to ensure that the request for GoogleAnalytics is sended before the new window is open. But the html link has the href attribute and when click opens the web in a new tab and in a new window :(

@sampel
Copy link
Author

sampel commented Dec 11, 2012

here is the html:

<a href="http://tmview.europa.eu/tmview/welcome.html" class="link-box" target="_blank">

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment