Skip to main content

Google Analytics Tracking of Jetpack (Sharedaddy) Social Engagement

3 min read
: Google recently added social engagement tracking to its analytics suite. With a little bit of leg work, Google Analytics can track not only +1s, but also Facebook and Twitter shares via a simple _gaq.push call.

Google recently added social engagement tracking to its analytics suite. With a little bit of leg work, Google Analytics can track not only +1s, but also Facebook and Twitter shares via a simple _gaq.push call.

If your site uses WordPress’s Jetpack plugin with Sharedaddy, and you already have Google Analytics up and running, you can use jQuery to attach the virtual event to the share button.

To add Google Analytics to Sharedaddy’s Twitter share button:

$('a.share-twitter').click( function() {
_gaq.push( ['_trackSocial', 'twitter', 'share',
$(this).attr('href').substr(0, $(this).attr('href').indexOf('?'))]);
});

…and for Facebook:

$('a.share-facebook').click( function() {
_gaq.push( ['_trackSocial', 'faceboook', 'share',
$(this).attr('href').substr(0, $(this).attr('href').indexOf('?'))]);
});

The above code simply listens for the share button to be clicked, and if so, passes the target URL back to Google, along with the service’s name. Putting it all together into a plugin with a hook to wp_head wp_footer you get the full plugin code available as a GitHub Gist.

The code should work out of the box with the standard share buttons (seen below), but can easily be adapted with a few minor modifications to apply to like and other iterations of the social media icons.

More details on the tracking code can be found over in the Google Analytics Social Engagement Documentation. Improve the code to work with your site? Feel free to fork the gist and contribute it back.

Originally published June 30, 2011 View revision history
Share

More to explore

Manage like an engineer

8 min read

If issues, pull requests, and project boards are the best way to develop software, should they not also be the best way to manage software development?

Analysis of Federal Executive .govs

3 min read

A scan of every federal executive .gov reveals that only 73% are live, Drupal dominates as a CMS, and 93% of live domains use no detectable CMS at all.

WordPress as a Collaboration Platform

1 min read

A presentation on how teams use WordPress beyond publishing, from collaborative document editing to progress tracking and workflow integration.

Leaders show their work

10 min read

Great leaders don't just communicate decisions—they explain how and why. Without that context, every decision sounds like "because I said so."

Ben Balter

I'm Ben Balter — I write here about engineering leadership, open source, and showing your work. I wrote Open & Async, the playbook for remote and distributed teams. My open source projects have hundreds of millions of downloads. I was the Director of Hubber Enablement at GitHub, where I helped thousands of GitHubbers do their best remote work. Before this role: Chief of Staff for Security, enterprise PM, and GitHub's first Government Evangelist. Before GitHub: attorney, Presidential Innovation Fellow, and member of the White House's first agile development team. More about the author →

Follow along: Bluesky LinkedIn

This page is open source Help improve this article on GitHub