Track JetPack Shares in Google Analytics

The JetPack plugin makes it easy to add share buttons to posts in WordPress. With a little custom code it’s also possible to track how often the share buttons are clicked and which URLs are being shared.

social

Here’s an example of the javascript needed to track shares for Twitter and Facebook:

/**
* Javacript for loading custom Google Analytics events
*
* @since 1.0.0
*/
(function($) {
// GA Docs for Social Interactions:
// https://developers.google.com/analytics/devguides/collection/analyticsjs/social-interactions
if ( typeof(ga) == "function" ) {
// Twitter
$('a.share-twitter').on( 'click', function() {
ga( 'send', {
hitType: 'social',
socialNetwork: 'Twitter',
socialAction: 'share',
socialTarget: $(this).attr('href').substr( 0, $(this).attr('href').indexOf('?') )
});
});
// Facebook
$('a.share-facebook').on( 'click', function() {
ga( 'send', {
hitType: 'social',
socialNetwork: 'Facebook',
socialAction: 'share',
socialTarget: $(this).attr('href').substr( 0, $(this).attr('href').indexOf('?') )
});
});
}
})(jQuery);
view raw tracking.js hosted with ❤ by GitHub

This code assumes jQuery is available. The code can be added to an existing javascript file or enqueued separately.

To view the data in Google Analytics, go to “Acquisition > Social > Plugins”.

You can also read more about social interaction tracking in the Google Analytics documentation.

About Devin

I'm a WordPress developer based in Austin, Texas. I run a little theme shop called DevPress and work for a startup called Nano. Find me on twitter @devinsays.

1 Response

Leave a Reply