Perhaps you saw the articles a couple of weeks ago, indicating that Google had officially announced that its Google Analytics product was now able to track social media interaction. As the linked article mentioned, this is a welcome addition to the built-in capability of the Google +1 button to track clicks. However, this bit of news was soon overshadowed by all the buzz surrounding the (as yet, despite being enormous, and frequently discussed) beta of Google+ – the new Google social platform.

Social activity on Google AnalyticsWe had seen the articles and sort of filed them away for future reference. A friend got in touch last week, however, asking how to implement this, and indicating that the information wasn’t very clear. After reading the official documentation, we certainly had to agree. However, after a bit of testing, we were able to get it to work, and provide the results here, in case you’d like to use Google Analytics to track various social interaction on your website, blog, or whatever.

What we’re trying to accomplish

To be clear, the objective is to add some JavaScript to our website, blog, or whatever, that will allow us to use Google Analytics to track when someone clicks on social media buttons on the page – such as a Facebook Like button, or a Facebook share link, or a Twitter follow or Tweet button, or any other similar item. For those who enjoy figuring this out for themselves, the documentation can be found here.

For the rest of us, there are three pieces of code to insert (We haven’t attempted to use anything but Facebook and Twitter – though the same principle would work with any; also Google +1 forwards data to GA automatically, so there is no need to do anything special for it), and of course a couple of prerequisites.

Prerequisites:

  1. You will need to be able to add files to your web server, and edit the page(s) you want the tracking to work on.
  2. You must have Google Analytics tracking code on the site (I’m not sure if you have to use async code, but I suspect you do)
  3. You must be using the “New” GA – so it will show Social under the Visitors area.
  4. Grab the ga_social_tracking.js file from Google and put it in the same location as the page to be tracked (or change the path to the js file in the first script tag under Code step 1).

Code needed to make this work:

  1. In the section of the page (I would suggest after the GA code, but don’t know if that is mandatory), add:
    <script src="ga_social_tracking.js" type="text/javascript"></script>
    <script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>
    <script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script>
  2. Where you want the buttons to appear, put

    a. for Twitter
    <a href="http://www.twitter.com/share" data-url="http://www.YOURSITE.com" data-via="YOURTWITTERID" class="twitter-share-button">Tweet</a>

    You can leave off the data-via line if you don’t want tweets to show up in your twitter feed. Obviously change YOURSITE and YOURTWITTERID to the appropriate values. The Twitter documentation allows for other ways to create a Twitter button. This is just one example. Feel free to play with others, but you may have to make other changes if it doesn’t work the same way as this example.

    b. for Facebook

    <fb:like></fb:like>

    This is the simple version for creating a “simple” Facebook “Like” button. For other types, you can see the Facebook developer documentation. Here is one other example:

    Put this in place of the <fb:like></fb:like> section above:


    <div id="fb-root"></div>
    <fb:like href="http://www.YOURDOMAIN.com/PAGETOTRACK" send="true" show_faces="false" layout="button_count"></fb:like>


    Then change YOURDOMAIN.com to your domain, and PAGETOTRACK to the path and page you want to have the button track. Do not omit the div with the fb-root id.

  3. Just before the closing body tag on the page, put

    <script>
    _ga.trackSocial();
    </script>

Done and done.

Social activities seem to show up in the new Google Analytics within an hour or so.

×