How to track social media buttons with Google Analytics

Tracking social buttons in Google Analytics

Social Media is a vital part to a successful online presence.

As companies continue to realise its importance, they are adding more and more social buttons to their websites.

Those buttons might consist of typical share buttons that you see at the end of posts, or they might consist of Twitter’s ‘follow’ button, or LinkedIn’s follow button.

No matter what buttons you include on your website, you should be tracking how many people click on them, and which pages the buttons have most success on.

This article will take you through the steps to set up the Google Analytics Social Plugin report within your WordPress website.

A few requirements before I continue:

  • This is for a WordPress website, but you can use the code elsewhere
  • You must be able to access the files of your website – not just the admin screen
  • You must have Google Analytics set up to access the reports

Now we have the requirements out of the way, we can get into the good stuff!

Setting up the script

The following instructions will allow you to track the social interactions on buttons from Facebook, Twitter, LinkedIn and Google+.

Downloading the main script

First things first, you need to download this JavaScript file.

Upload the social-tracking.js file to your server – I put mine in a folder called ‘js’.

Editing your header

To get the script running, you need to edit your header.php file – This can be found in /wp-content/themes/your-theme/.

Open this file with a text editor – like Notepad or Notepad++. (You can also do this within the WordPress admin by going to ‘Appearance > Editor’ in the left menu.)

You need to enter the following code just before the </head> tag:

Please click here to access the code.

You need to update the UA-XXXXX-X with your Google Analytics number – This can be found by going into the analytics data for your website, and clicking ‘Admin’ in the top right.

You also need to double check the path to file you uploaded is correct.

Google Analytics account number

Editing your footer

Finally, we need to enter this code into our footer.php file just before the </body> tag:

Please click here to access the code.

That’s it! You should now be set up to track social media button clicks on your website!

Viewing the reports in Google Analytics

Now that we are passing the social media button clicks into Google Analytics, we need to know where to find it so that we can see the data.

  1. Login to Google Analytics
  2. Go into your websites analytics
  3. In the left menu, click Traffic Sources > Social > Plug-ins

This page will tell you the pages that create the most button clicks, you can go into each page individually to find out which buttons they are, or you could click ‘Social Source’ or ‘Social Source and Action’ to view an overview of all the button presses within your selected period.

Google Analtics social button tracking report

Now you can view which buttons your readers are clicking just by going to the Google Analytics Social Plug-ins page.

I hope you find this information useful, I would love to know how you got on with implementing this code, so drop me a comment below.

  • This works PERFECTLY Sam, thank you so much for helping me out with this!

    • That’s great news! I’m glad I could help and thank you for letting me know!

    • Joseph Cole

      Thanks for sharing this with me on Google+, Stephan! I’m going to follow it soon for my site.

      • Let me know if you get stuck, the Appearance > Editor section is where you’ll need to go.

      • Hey Joseph! Thanks for checking it out, let me know how you get on.

    • See my comments from this blog. The author did not correctly instruct anyone who uses wordpress themes. IMHO.

  • Paolo Laurenti

    Thanks for sharing this, it’s very useful. But at the moment I am not able to view social stats inside my Google Analytics.
    I have a question: where should I put the social tracking.js file? I don’t understand this:

    <!– If your using wordpress, add (without the backslash) just before ‘/js’ –>

    Thanks a lot!

    • Hey Paolo, you need to upload the file onto your web server, so you need to use an FTP system like FileZilla, or WinSCP and connect to your website. The upload should go into something like public_html/wp-content/themes/YOUR-THEME/js

      I hope that helps! – let me know if you have any more problems

      • Paolo Laurenti

        Thank you very much Sam, but i continue to see anything inside Social Interactions widject of my Google Analytics… I am not able to understand what I’m doing wrong…. how much time I have to wait before GA shows sharings that I’m doing for test purposes?

        • Hey Paolo, if you change the date to todays, then you should see the results pretty instantly.

          Just to confirm, you have uploaded the main social-tracking.js to your web server, then added in the additional scripts into both your header and footer respectively?

          Also, if you are using WordPress, make sure you read the comments in the code that goes within your header, you will need to add a little bit of PHP code (provided in the comments) to make sure your scripts connect properly.

          To check everything is loading, if you open your website in Chrome and hit f12 you should see the code for your web page. In the bottom right of that window, you may see a little red cross, click that to see if there is an error connecting the scripts together.

          Hope that helps – again, let me know if your still having trouble (and send me a link so I can take a look).

          • Paolo Laurenti

            Hi Sam,

            your help is very appreciated,you’re very kind, thank you very much.

            There was a problem with the scripts inside header.php: I was forgetting to add the php code you mentioned.

            Now I have less errors looking insider Chrome Inspection lines:

            duplicate in.js loaded, any parameters will be ignored in.js:5
            FB.getLoginStatus() called before calling FB.init(). all.js:52
            FB.init has already been called – this could indicate a problem all.js:52
            FB.getLoginStatus() called before calling FB.init(). all.js:52
            duplicate in.js loaded, any parameters will be ignored in.js:5
            Unable to post message to Recipient has origin
            Resource interpreted as Script but transferred with MIME type text/plain: “…one%2F&type=pidget&callback=PIDGET_1364908820701.f.devNull&t=1364908820701”. pidget.html:15

            I don’t understand if I am making others errors or not.
            I still don’t see anything inside GA Social Interactions. (The dates are correct)

            My goal is to understand how many times a post is shared on social networks…what am i doing wrong?

            Here it’s the link:

            Thanks a lot!!!!

          • Hey Paolo, There are a couple of errors on your page at the moment, the first is that you have two Google Analytics scripts running at once – there is the one being generated by the SEO plugin you are using, and another that you entered from the code I provided. You will need to stop the plugin from entering the GA code.

            With the code I provided, I have spotted an error (my fault, can’t apologise enough), there is a comment tag in the header asking you to change the UA-XXXX-XX, that needs to be moved just above the script tag (up two lines). – I will update my code as soon as possible

            In the footer, you have duplicates again, it looks like you have grabbed code from someone else’s site as well – it’s exactly the same as mine, so you can remove the code one of us has provided and that should continue to work correctly.

            That should clear up the Facebook and the duplicate file errors, the stumbleupon and pinterest errors are being caused by a separate plugin, so I’m afraid I can’t help with those ones.

            Hope that helps 🙂

          • Paolo Laurenti

            Hi Sam,

            thanks a lot again.

            I’ve made the updates that you wrote but unfortunately my GA Social interactions are still empty :-(.
            Can you check the inspection page on my blog again please?

            Now I see only the stumbleupon error and I am not able to understand where it comes from.

            Thanks a lot again!!! 🙂

          • No problem, your footer is now correct, however you still have duplicate GA tracking code in your header, I’d suggest disabling the Google Analytics by Yoast plugin – that will stop this conflict.

            In your header.php file, find the body tag, then add directly after it, that should help with the Facebook conflict.

            I’ve just noticed the share buttons your using – this code will only work with buttons that the social networks provide (like the ones you can see on my site) – I’d suggest switching your buttons for these unless you have a specific reason for not using them (I use the Digg Digg plugin to display mine).

            I hope that helps,

          • Paolo Laurenti

            I removed the duplicated GS tracking code inside header.php.

            Hi added after starting body tag inside header.php.

            Unfortunately I cannot use others icons for my share buttons and looking into Digg Digg is not possible to show share buttons like mines…

            So… I will not use GA Social Interactions and I will settle with Event Actions Tracking….even if it’s not the same thing.

            Thanks for your time, I appreciate it very much.

            I will advertise to all my social channels your beautiful site.

            Thank a lot!!

            p.s. If you have any suggestions, I am all ear!! 🙂

          • That’s no problem, I think event tracking is the best option for you – assuming you can add the tracking code to the buttons. Thank you for your comments, if I can help with anything else then do let me know.

  • Sam, great article. I have data coming into the Plugins reporting via a ShareThis connection, but can i use this data to fire Goals? the utmt param is being set as “Social” and can only find a way to set a goal when/if this param is an “event”.

    • Hi Scott,

      I’m afraid I’m not too familiar with ShareThis, however if your using it as a plugin, you may be able to access the code and find where the utm parameters are set. You can then change “social” to “event” – HOWEVER updating these files will be overwritten if they update their code, so not exactly an elegant solution.

      I’m sorry I can’t be more help with this issue.


  • Social Media Analytics

    Use this Social Media Analytics and make sure that your business site more valuable.Thanks.

  • michael

    How can I track the social media button clicks I have included in my email signature? Great article… extremely helpful.

  • CrazyAndroid Blog

    We use Yoast’s plugin and when we tried your methodology it just duplicated the Analytics code and we got a double spike in page views. Any idea how to implement this along side Yoast’s Google analytics plugin. We can’t stop using the plugin as it takes care of many GA stuffs automatically. Also just an FYI, we use Digg Digg social buttons on the blog.

    Your advice would be of great help.

  • Sean Deets

    Great post Sam! The instructions were very easy to follow and implement. The only issue is that the stats are not posting in Google Analytics. We currently use the Sharing plugin from JetPack and wanted to know if any modifications are necessary? Do you know any fixes that we would need to make? Otherwise, is there a Sharing plugin that you would recommend that works well with the code you provided?

  • Brian Huonker

    Thanks for the tips. I am curios if CrazyAndroid and Sean’s questions were answered. We use Yoast for our analytics plug in and Jetpack for the sharing buttons. Any thoughts on how to have our sites track the number of times the like/share buttons are clicked?

  • Hi Sam, thank you very much for your post.
    After some workarounds (I had to remove Yoast Analytics) I got everything to work fine on my blog, except for Linkedin buttons. That are not tracked at all and I can’t really tell why.
    Have you experienced such issue on other sites? I don’t know if I can post my blog url, so I won’t do it now. But, please, let me know if I can so that you or somebody else may be so kind to give a look at its code.
    Thank you very much and keep up the good work!

  • Laura

    Hi Sam,

    I have several sites where I want to use this for. So I tried it first with I see my Google + from today, but I don’t see the twenty and more likes on my latest blog ( Does FB need some more time to show it, or did I something wrong?

    Love to here from you,

  • aroojkh

    implemented the social tracking code but I don’t see all the networks or actions in GA? I would appreciate your response, thanks.

    • Hi Aroojkh. Sorry to hear you’re having problems. If you drop me a link to your site I’ll see if I can take a look.
      – Sam

      • aroojkh

        counts appear different on ga and floating bar.
        many thanks in advance.

        • The code looks set up correctly from what I can see. Your numbers will always be different to those on the actual buttons as the buttons account for shares from those social networks (so if I shared a link to your article on twitter, and someone retweeted it, the button would show 2 shares) – where as Google Analytics numbers will only say when the share button was clicked from your website (not including for the other retweet).

          Hope that helps clear things up.

          • aroojkh

            ok thanks for your response, so it means digg digg counts all the retweets per post.
            now Does it matter if the code that supposed to be in footer is before the dd_outer section before the close body tag? as i only see the “share this – tweets” or email and nothing else is registering… even though code looks good….

  • aroojkh

    hi Sam, could you please help me out with my issue of GA not tracking all social activity, after implementing what you have suggested. many thanks….

    i would appreciate your feedback, thanks.

  • Those script inserts do not conform to wordpress theming standards. The footer files should go into theme functions and wp_register and wp_enqueue.

  • Pingback: