Track LinkedIn Share Buttons in Google Analytics
I’ve been building up a technical post series on tracking social media hook-ups in Google Analytics. I wrote about tracking Google +1 Votes before Google automated it. After they unveiled new Social reports in GA, I followed up with an update on tracking Facebook Like buttons. Next on the docket: LinkedIn Sharing … because as analysts, to be ready to answer the tough questions, you really should be tracking everything.
The method of tracking for LinkedIn buttons differs a little bit from the others I’ve posted so far. Currently, LinkedIn does not provide a callback function that executes when an action is completed — there’s no flag in the code that says “visitor shared something, now what?”. Instead, we have to build our own JavaScript function that we can bind to an action. In order to do so, this example uses jQuery, a popular JavaScript library.
The first step is to get your button code snippet from LinkedIn. LinkedIn calls this service LinkedIn for Publishers. When you generate the code, it will look something like this:
<script type="text/javascript" src="http://platform.linkedin.com/in.js"></script><script type="in/share" data-url="http://example.com/article/" data-counter="top"></script>This script from LinkedIn above generates new HTML which is inserted into the page in order to create the ‘Share’ button and its functionality. The code generated includes a new <div> element, which has the class name IN-widget.
This gives us the chance to reference the generated button with a little code of our own. Using the jQuery .ready event handler — to ensure that the code does not execute until LinkedIn is finished building the button — we construct a new function that binds to the “click” event for all objects with the class name ‘IN-widget‘. In this function, we include the Google Analytics social tracking code, to create a new social event.
$(window).ready(function() {
$('.IN-widget').click(function() {
_gaq.push(['_trackSocial', 'LinkedIn', 'Share', window.location.href]);
});
});This new code can be added pretty much anywhere on your page — you may want to include it right after the code LinkedIn gives you to keep them together, or put it in your header if that’s how you prefer to organize your site.
Note that in this example, the last parameter, window.location.href, will send the current URL along as the resource shared. In different systems, you may prefer to output the URL directly, rather than through JavaScript. For instance, in WordPress, you may replace the last parameter with the permalink for a post:
_gaq.push(['_trackSocial', 'LinkedIn', 'Share', '<?php the_permalink(); ?>']);Now, there is one issue which must be kept in mind. In the case of the Facebook and Google +1 buttons I’ve previously shared, the social event code only fires after something has been shared. That’s the benefit of the callback function provided by those services. In the case of LinkedIn, however, what we’re really tracking are clicks to the share button — it’s possible for someone to cancel sharing after clicking the button, so you may get an inflated total.
With that caveat in mind, you’re all set! Once you’ve added this JavaScript, your LinkedIn shares will appear in the new social reports in Google Analytics. You can find these reports under “Visitors > Social” in the new version of GA. With this, your web analyst can help you determine what impact these buttons have on your site and your business. For instance, whether frequently-shared articles get much more converting traffic from LinkedIn, and whether the people who share things are also the people who convert themselves.
Until next time,
Colin


August 10th, 2011 at 5:43 am
Hi Colin,
I wrote a related post last week on tracking social media with web analytics – http://bit.ly/nGBEKt. I didn’t go into any of the detail you have here (which is incredibly useful) but one thing I recommended was to add GA campaign parameters to the URL generated by the sharing buttons. You can then see not only the usage of the sharing buttons but how much traffic was generated by the links shared.
Any chance you can extend the posts in this series to show how to include these campaign parameters?
Cheers
Peter