Created
December 11, 2012 09:09
-
-
Save sampel/4257219 to your computer and use it in GitHub Desktop.
Adding Google Analytics event tracking to outbound links
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
$(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); | |
}); | |
}); |
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
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 :(