Google Analytics Custom Event Tracking – jQuery

With inline content being loaded via ajax, you are loosing a lot of interesting usage data. These can be tracked using Google Analytics Events or by sending a Pageview.

LETS SHOW IT ALL FIRST

  1. (function($) {
  2. track = {
  3.    config:{
  4.         pageName         : "Your Page",
  5.         eventSelector    : ".trackEvent a, .trackEvent",
  6.         pageviewSelector : ".trackPage a, .trackPage",
  7.    },
  8.  
  9.   init                  : function(){  
  10.          
  11.    $("body").delegate(this.config.eventSelector ,"click", function(){    
  12.    
  13.         // Navigation / Readmore / Apply / FAQ / Contact / Share
  14.     var cat             = $(this).attr("data-track-cat")    || "Navigation";
  15.     var action  = $(this).attr("data-track-action") || "Click";
  16.     var label   = $(this).attr("data-track-label")  || $(this).text() ;
  17.                
  18.     this.sendEvent(cat, action, label);  
  19.          
  20.   });
  21.  
  22.   $("body").delegate(this.config.pageviewSelector, "click", function(){
  23.          
  24.    var label =  $(this).text() +" - "+ this.config.pageName            
  25.    var page  =  "/"+label.split(' ').join('-').toLowerCase()+"/";
  26.          
  27.    this.sendPageview(page,label);      
  28.          
  29.   });
  30.  
  31.   },
  32.  
  33.   sendEvent: function(cat, action, label){
  34.            
  35.   ga('send', 'event', {
  36.         'eventCategory' : cat ,
  37.         'eventAction'   : action,
  38.         'eventLabel'    : label,
  39.         'eventValue'    : 1
  40.   });
  41.   },
  42.  
  43.  sendPageview : function(page,label){
  44.          
  45.   ga('send', {
  46.         'hitType': 'pageview',
  47.         'page'   : page ,
  48.         'title'  : label
  49.    });
  50.  }
  51. };
  52.  
  53. /* INIT */
  54. track.init();
  55.  
  56. })(jQuery);

The above allows to automate tracking by attaching simple classes and use HTML5 data attributes to assign category, action and label.  Direct tracking is also possible. Lets split it up :)

EVENT TRACKING INIT

  1. $("body").delegate(".trackEvent a, .trackEvent","click", function(){     
  2.     // Navigation / Readmore / Apply / FAQ / Contact / Share
  3.     var cat     = $(this).attr("data-track-cat")    || "Navigation";
  4.     var action  = $(this).attr("data-track-action") || "Click";
  5.     var label   = $(this).attr("data-track-label")  || $(this).text() ;
  6.                
  7.     this.sendEvent(cat, action, label);  
  8.          
  9.   });
  10.  

This monitors links with the class .trackEVENT attached and fills the event data using HTML5 data attributes. All attributes have default values assigned.

A possible link would look like this:

  1. <a href="#" class=".trackEvent" data-track-cat="Subscribe" data-track-action="View" data-track-label="All my news!">Subscribe to my news!</a>

The sendEvent function than sends this to Google Analytics.

PAGEVIEW TRACKING

  1. $("body").delegate(".trackPage a, .trackPage", "click", function(){
  2.          
  3.    var label =  $(this).text() +" - "+ this.pageName           
  4.    var page  =  "/"+label.split(' ').join('-').toLowerCase()+"/";
  5.          
  6.    this.sendPageview(page,label);      
  7.          
  8. });

Much simpler,  this just gets the element text and submits the click as a new Pageview. The label gets the pagename attached and the actual page url is constructed from the label. The sendPageview function than sends this to Google Analytics.

DIRECT USE

  1. track.sendEvent(cat, action, label);
  2.  
  3. track.sendPageview(page, label);

Really simple and effective way within a simple OnePager or a bigger web application. BTW I am using delegation to make sure that also links within AJAX content can be tracked.

Enjoy coding …

Alex

I am a full-stack developer. I love programming,  design and know my way around server architecture as well.  I would never feel complete, with one of these missing. I have a broad range of interests, that’s why I constantly dive into new technologies and expand my knowledge where ever required. Technologies are evolving fast and I enjoy using the latest. Apart from that, I am a peace loving guy who tries to have people around him that think the same.  I truly believe in the principle: “If you help someone, someone will help you, when you need it."

Recent Posts

Particle Network Animations in Javascript

What are particle animations? Particle network animations in JavaScript typically involve creating visual representations of… Read More

3 days ago

B&B / Hotel Booking Solutions for WordPress | 2024

BOOKING SOLUTIONS 202x This is my take on a subset of booking, appointment, PMS or… Read More

1 month ago

WordPress Cron + WP-CLI + Ntfy

THE GOAL Create a system cron for WordPress, that is accessible and can be easily… Read More

2 months ago

2024 is here and now :)

2024, what's cooking? Slowly getting into the 2024 spirit. 3 projects coming to a close… Read More

4 months ago

2023 ends and whats next !

Short look back at 2023 This has been a busy and interesting year. I am… Read More

4 months ago

cubicFUSION Grid Tweaker – Elementor Grid made easy.

Elementor Pro provides grid containers as an experimental feature. The options provided are limited, when… Read More

5 months ago