Skip to main content

Google Analytics Tracking of Jetpack (Sharedaddy) Social Engagement

2 min read

Summary: 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:

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

Keep reading

Ben Balter

I'm Ben Balter, and I write here about developer platforms, product and engineering leadership, open source, and showing your work. I spent a decade building developer platforms at GitHub: five years as a product manager for trust and safety, shipping 500+ features and the API endpoints behind them to 50M+ developers, plus a bottom-up rewrite of GitHub Pages and the open source library behind GitHub's license detection. My open source projects have hundreds of millions of downloads. I wrote Open and Async, the playbook for remote and distributed teams, and shipped an MCP server alongside it so agents can query it directly. At GitHub I was also Director of Hubber (Employee) Enablement, owning the internal platform 4,000+ GitHubbers use to get their work done, Chief of Staff for Security, and the company'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

Originally published June 30, 2011 View revision history

This page is open source Help improve this article on GitHub

Keyboard shortcuts

Search

  • / or ⌘ K Open search
  • ↓ or j ↑ or k Move through results
  • Enter Open the selected result
  • Esc Close search

Go to

  • g then h Home
  • g then p All posts

This page

  • y Copy link to this page
  • ? Show this list