Automatically Add Google Analytics Event Tracking With jQuery

Automatically add basic Google Analytics event tracking using jQuery. I wrote it for use on this site, but the script can be used as is or modified to suite your needs.

You can either:

  1. Add the code to an existing theme JavaScript file that is called on every page (or the pages you want to track events)
  2. Add the code to the footer of the every page before the closing body tag () (or the pages you want to track events)
  3. Download the script and include it on every page (..and again – or the pages you want to track events)

That’s it!

Then clicks will be automatically trigger a Google Analytics event on:

Other Outbound Links – Any other link that leaves your domain, but is not caught by the social media or affiliate match.

Outbound Affiliate Links – set as any link within your domain that includes “/go/”. You can alter to your liking by adjusting the match in the affilateLink variable. [code type=”js”]var affiliateLink = new RegExp(‘/go/+’);[/code]

Outbound Social Media Links – set as any outbound link that includes “Facebook, Twitter, Google+, Linkedin and Twitter”. You can change or adding or removing the names in the socialMedia variable. Make sure you seperate the values you want to match with a pipe “|” sign, and add them before the plus “+”. Case matters. [code type=”js”]var socialMedia = new RegExp(‘plus|linkedin|pinterest|facebook|twitter+’);[/code]

PDF Download Links – Any link that end in .pdf.
ZIP Download Links – Any link that ends in .zip
Anchor Text Links A link within your domain that includes a “#” sign, or a relative anchor link.

Any thoughts, feedback, or fixes would be appreciated.

Disclaimer: No guarantee! but it does work, I actively use both variations on sites

[code type=”js”]
jQuery(window).on(‘load’, function(){

var myWebsite = new RegExp(location.host);
var myWebsiteAnchor = new RegExp(‘#+’);
var badlinkJs = new RegExp(‘^javascript:+’);
var socialMedia = new RegExp(‘plus|linkedin|pinterest|facebook|twitter+’);
var affiliateLink = new RegExp(‘/go/+’);
var isDownloadPdf = new RegExp(‘.pdf$’);
var isDownloadzip = new RegExp(‘.zip$’);

jQuery(‘a’).each(function(){

var url = jQuery(this).attr(“href”);

if (badlinkJs.test(url)){
}
else if (affiliateLink.test(url)){
jQuery(this).attr(“onclick”,”_gaq.push([‘_trackEvent’, ‘Outbound’, ‘Affiliate’, ‘”+ url +”‘]);”);
}
else if (socialMedia.test(url)){
jQuery(this).attr(“onclick”,”_gaq.push([‘_trackEvent’, ‘Outbound’, ‘Social Media’, ‘”+ url +”‘]);”);
}
else if (myWebsiteAnchor.test(url)){
jQuery(this).attr(“onclick”,”_gaq.push([‘_trackEvent’, ‘Anchor’, ‘Click’, ‘”+ url +”‘]);”);
}
else if (! myWebsite.test(url)){
jQuery(this).attr(“onclick”,”_gaq.push([‘_trackEvent’, ‘Outbound’, ‘Other’, ‘”+ url +”‘]);”);
}
else if (isDownloadzip.test(url)){
jQuery(this).attr(“onclick”,”_gaq.push([‘_trackEvent’, ‘Download’, ‘Zip’, ‘”+ url +”‘]);”);
}
else if (isDownloadPdf.test(url)){
jQuery(this).attr(“onclick”,”_gaq.push([‘_trackEvent’, ‘Download’, ‘Pdf’, ‘”+ url +”‘]);”);
}
else { }

}); //End each function

}); //End window load function
[/code]

If you are using Google Analytics Universal, the code will be slightly different.

View below for UA event tracking
[code type=”js”]
jQuery(window).on(‘load’, function(){

var myWebsite = new RegExp(location.host);
var myWebsiteAnchor = new RegExp(‘#+’);
var badlinkJs = new RegExp(‘^javascript:+’);
var socialMedia = new RegExp(‘plus|linkedin|pinterest|facebook|twitter+’);
var affiliateLink = new RegExp(‘/go/+’);
var isDownloadPdf = new RegExp(‘.pdf$’);
var isDownloadzip = new RegExp(‘.zip$’);

jQuery(‘a’).each(function(){

var url = jQuery(this).attr(“href”);

if (badlinkJs.test(url)){
}
else if (affiliateLink.test(url)){
jQuery(this).attr(“onclick”,”ga(‘send’, ‘event’, ‘outbound’, ‘affiliate’, ‘”+ url +”‘]);”);
}
else if (socialMedia.test(url)){
jQuery(this).attr(“onclick”,”ga(‘send’, ‘event’, ‘outbound’, ‘social media’, ‘”+ url +”‘]);”);
}
else if (myWebsiteAnchor.test(url)){
jQuery(this).attr(“onclick”,”ga(‘send’, ‘event’, ‘anchor’, ‘click’, ‘”+ url +”‘]);”);
}
else if (! myWebsite.test(url)){
jQuery(this).attr(“onclick”,”ga(‘send’, ‘event’, ‘outbound’, ‘other’, ‘”+ url +”‘]);”);
}
else if (isDownloadzip.test(url)){
jQuery(this).attr(“onclick”,”ga(‘send’, ‘event’, ‘download’, ‘zip’, ‘”+url +”‘]);”);
}
else if (isDownloadPdf.test(url)){
jQuery(this).attr(“onclick”,”ga(‘send’, ‘event’, ‘download’, ‘pdf’, ‘”+ url +”‘]);”);
}
else { }

}); //End each function

}); //End window load function
[/code]

Or the compressed version:
[code type=”js”]
jQuery(window).on(“load”,function(){var d=new RegExp(location.host);var b=new RegExp(“#+”);var a=new RegExp(“^javascript:+”);var g=new RegExp(“plus|linkedin|pinterest|facebook|twitter+”);var e=new RegExp(“/go/+”);var c=new RegExp(“.pdf$”);var f=new RegExp(“.zip$”);jQuery(“a”).each(function(){var h=jQuery(this).attr(“href”);if(a.test(h)){}else{if(e.test(h)){jQuery(this).attr(“onclick”,”_gaq.push([‘_trackEvent’, ‘Outbound’, ‘Affiliate’, ‘”+h+”‘]);”)}else{if(g.test(h)){jQuery(this).attr(“onclick”,”_gaq.push([‘_trackEvent’, ‘Outbound’, ‘Social Media’, ‘”+h+”‘]);”)}else{if(b.test(h)){jQuery(this).attr(“onclick”,”_gaq.push([‘_trackEvent’, ‘Anchor’, ‘Click’, ‘”+h+”‘]);”)}else{if(!d.test(h)){jQuery(this).attr(“onclick”,”_gaq.push([‘_trackEvent’, ‘Outbound’, ‘Other’, ‘”+h+”‘]);”)}else{if(f.test(h)){jQuery(this).attr(“onclick”,”_gaq.push([‘_trackEvent’, ‘Download’, ‘Zip’, ‘”+h+”‘]);”)}else{if(c.test(h)){jQuery(this).attr(“onclick”,”_gaq.push([‘_trackEvent’, ‘Download’, ‘Pdf’, ‘”+h+”‘]);”)}else{}}}}}}}})});
[/code]

Any thoughts? Anything you would change or add? Please let me know!