Pete Freitag Pete Freitag

Tracking JavaScript Events with Google Analytics

web

Did you know that you can track JavaScript events with Google Analytics? I just found this out... It's pretty cool, because allows you to track things like outbound link clicks, AJAX calls, static files, and more.

It's quite simple to do, just call the urchinTracker function (make sure that your urchin code defined before and above calling this function), and pass in a url. It will then track log the url as if it were a regular page visit. So for example lets say you wanted to log downloads of a zip file...

<a href="stuff.zip" onclick="urchinTracker('/downloads/stuff');"> download </a>

You can use the same method to log a outbound link click...

<a href="http://adobe.com/" onclick="urchinTracker('/outgoing/adobe_com');"> adobe </a>

You should see /downloads/stuff and /outgoing/adobe_com in your reports within 24-48 hours.


Like this? Follow me ↯

Tracking JavaScript Events with Google Analytics was first published on September 28, 2006.

If you like reading about google, analytics, javascript, ajax, urchintracker, or urchin then you might also like:

Comments

Hey Pete, great tip! Just a quick note to let you know that your iframe code to display your code snippets is collapsed in IE (which is the engine my blog reader uses). You can't see the code at all unless you open the link in Firefox.
by Dave Carabetta on 09/29/2006 at 9:00:52 AM UTC
Thank you for that suggestion. We operate a Google Analytics Consulting firm and I'll be sure to add this to our list of options with a link back to this thread. Our site is www.sonicko.com if you have any questions.
by Sonicko on 02/02/2007 at 2:51:50 PM UTC
Why do people always add "javascript:"? It's on a js onclick handler. Not needed.
by ziggy on 08/23/2007 at 11:46:37 AM UTC
I'm wondering if I can use this to track WITHIN a site - while I can use google navigation certainly, I'm thinking that this would be a more succinct manner to track say, a landing page to the product page.
by Andrea Canfield on 12/05/2007 at 11:21:51 AM UTC
I cannot get this to work on an iframe form on a .net site. Any idea why? This is the page with the form. http://www.beringer.net/IT-Services-Consulting.aspx http://www.beringer.net/MiniContactUs.aspx
by Dave on 02/15/2009 at 10:12:37 AM UTC
This is great, but doesn't work with the new pageTracker code. You can see how to implement the same thing here: http://code.google.com/apis/analytics/docs/eventTrackerGuide.html But I have a question, Pete. I've implemented the tracking code, but can't find the "Events Categories" in the reporting. Do you know where the events that are tracked will appear?
by Stuart Green on 03/02/2009 at 6:36:02 PM UTC
You can also use the events API to visualize your website's response time how real users see it, not the crawlers. You can achieve this by populating the fourth parameter (value) by the response time and then charting it using Analytics. For more details see my blog post: http://www.vineetmanohar.com/blog/2009/03/23/using-google-analytics-event-api-to-track-your-website-performance/
by Vineet Manohar on 03/24/2009 at 9:12:10 AM UTC
To use GoAn without using Java Script or programming, http://email-tracking-with-google-analytics.com/ lets you create a tracking pixel which you can implement in mobile pages, emails or anywhere else. No JS required.
by Austen on 08/07/2010 at 5:07:02 PM UTC