Garrett Murphey

Ruby & JavaScript Development

jQuery Plugin: Outbound Analytics

July 22, 2012

A jQuery plugin aimed at making it easier to track outbound (external) links with Google Analytics.

Getting Started

Download the production version or the development version.

99% of the time you’ll probably just want to track clicks on all outbound links on a page. This is easy. In your web page:

<script src="jquery.js"></script>
<script src="dist/jquery.outbound-analytics.min.js"></script>
<script>
jQuery(function($) {
  // automatically tracks all outbound links (a[href]) in the DOM
  $.outboundAnalytics();
});
</script>

Sometimes you may want to track only certain links, or track links in your header or footer different. This is easy, too. In your web page:

<script src="jquery.js"></script>
<script src="dist/jquery.outbound-analytics.min.js"></script>
<script>
jQuery(function($) {
  // automatically tracks all outbound links (a[href]) in <header>
  $('header').outboundAnalytics();

  // track <aside> links as 'Reference Links'
  $('aside').outboundAnalytics({ 'category': 'Reference Links' });
});
</script>

Fully Customizable

The plugin comes with smart defaults and everything you pass to Google Analytics is totally customizable (options can even be passed as functions). Here’s what you have access to:

Name Type Default Description
category String Outbound Links The category used in Google Analytics
action String Click The action used in Google Analytics
label String The link’s href attribute The label used in Google Analytics
value Float null The value assigned to the event in Google Analytics
nonInteraction Boolean false Whether or not the event calculates into the bounce rate in Google Analytics

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using grunt.

Also, please don’t edit files in the “dist” subdirectory as they are generated via grunt. You’ll find source code in the “src” subdirectory!

Release History

  • 0.1.1: Improving external link detection.
  • 0.1.0: Intial release.

Other Recent Posts