Image Click Tracking with Google Analytics Events

joe.price's picture

I recently wrote a short article about Tracking Form Submissions with Google Analytics Events that focused on one particular use case of event tracking. A client of mine wanted to begin tracking image clicks on their site and I thought I'd share what we did.

The question was, how many times is a particular linked image clicked? Google Analytics automatically tracks the page views on each end of the link, but it doesn't show if they got they there by clicking on a slideshow graphic or other promotional image you might be displaying.

(function ($) { $(document).ready(function() { // Attach onclick event to document only and catch clicks on all elements. $(document.body).click(function(event) { // Catch the closest surrounding link of a clicked element. $(event.target).closest("a,area").each(function() { _gaq.push(['_trackEvent', 'Image Click', $('img', this).attr('alt'), $(this).attr('href')]); }); }); }); })(jQuery);

This little bit of jQuery says: anytime a link is clicked that also has a child image (), track it as an Event with the category 'Image Click'. For action, use the alt tag on the image; for label, use the path of the link.

Tags: