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
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:
…and for Facebook:
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_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.
Prior to GitHub, Ben was a member of the inaugural class of Presidential Innovation Fellows where he served as entrepreneur in residence reimagining the role of technology in brokering the relationship between citizens and government. Ben has also served as a Fellow in the Office of the US Chief Information Officer within the Executive Office of the President where he was instrumental in drafting the President’s Digital Strategy and Open Data Policy, on the SoftWare Automation and Technology (SWAT) Team, the White House’s first and only agile development team, and as a New Media Fellow, in the Federal Communications Commission’s Office of the Managing Director. His paper, Towards a More Agile Government was published in the Public Contract Law Journal, arguing that Federal IT Procurement should be more amenable to modern, agile development methods. More about the author →