Ben Balter
  • Posts
  • About
  • Contact
Technology leadership, collaboration, and open source

Google Analytics Tracking of Jetpack (Sharedaddy) Social Engagement

TL;DR: 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.

❗ Heads up! This post is archived and here for historical purposes. It may no longer be accurate or reflect my views. Proceed at your own risk.

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:

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

…and for Facebook:

```javascript $('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 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.

Update: Dedicated reader All-around rabble-rouser Andrew Nacin points out that by default, jQuery is queued up into the footer. Updated the above code to hook into wp_footer with a priority of 20 (higher than jQuery’s 10 hook).

Update: Special thanks to @Ramoonus for didn’t queue up jQuery.

Originally published June 30, 2011 | View revision history

If you enjoyed this post, you might also enjoy:

  • How I re-over-engineered my home network for privacy and security
  • Twelve tips for growing communities around your open source project
  • How I over-engineered my home network for privacy and security
  • Why open source
  • 19 reasons why technologists don't want to work at your government agency
  • Four characteristics of modern collaboration tools
  • Why WordPress
  • The difference between 18F and USDS
  • Five best practices in open source: external engagement
  • Manage like an engineer
  • Why FedRAMP actually makes it harder for government agencies to move to the cloud
benbalter

Ben Balter is the Director of Engineering Operations and Culture at GitHub, the world’s largest software development platform. Previously, as Chief of Staff for Security, he managed the office of the Chief Security Officer, improving overall business effectiveness of the Security organization through portfolio management, strategy, planning, culture, and values. As a Staff Technical Program manager for Enterprise and Compliance, Ben managed GitHub’s on-premises and SaaS enterprise offerings, and as the Senior Product Manager overseeing the platform’s Trust and Safety efforts, Ben shipped more than 500 features in support of community management, privacy, compliance, content moderation, product security, platform health, and open source workflows to ensure the GitHub community and platform remained safe, secure, and welcoming for all software developers. Before joining GitHub’s Product team, Ben served as GitHub’s Government Evangelist, leading the efforts to encourage more than 2,000 government organizations across 75 countries to adopt open source philosophies for code, data, and policy development. More about the author →

This page is open source. Please help improve it.

Edit
  • Other recommended reading
  • Fine Print