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.

Send to Kindle

Never miss an article again!

Get all future articles delivered straight to your email inbox!

  • https://plus.google.com/105076725141939280120/about Stephan Hovnanian

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

    • http://www.stuffedweb.com/ Sam Scholfield

      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.

      • https://plus.google.com/105076725141939280120/about Stephan Hovnanian

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

      • http://www.stuffedweb.com/ Sam Scholfield

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

  • 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!

    • http://www.stuffedweb.com/ Sam Scholfield

      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
      Sam

      • 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?

        • http://www.stuffedweb.com/ Sam Scholfield

          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).
          Sam

          • 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 http://www.stumbleupon.com. Recipient has origin http://blog.elleffegiconsulting.com.
            widgets.js:27
            Resource interpreted as Script but transferred with MIME type text/plain: “http://widgets.pinterest.com/v3/pidgets/log/?via=http%3A%2F%2Fblog.elleffeg…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: blog.elleffegiconsulting.com

            Thanks a lot!!!!

          • http://www.stuffedweb.com/ Sam Scholfield

            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 :)
            Sam

          • 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!!! :-)

          • http://www.stuffedweb.com/ Sam Scholfield

            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,
            Sam

          • 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!! :-)

          • http://www.stuffedweb.com/ Sam Scholfield

            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

  • http://twitter.com/swcalise Scott Calise

    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”.

    • http://www.stuffedweb.com/ Sam Scholfield

      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.

      Sam

  • 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.