Quantcast
Channel: Savio.no - Blog
Viewing all articles
Browse latest Browse all 31

Tracking Facebook Likes, Share, Send & Comments as Social Engagement in Google Analytics

$
0
0

There are already posted "tons of articles out there" about how to track Facebook Likes, Send & Comments as Social Engagement in Google Analytics, but since I also uses my own blog as a reminder of how to do things and a "script bank", I decided to write this blog post nevertheless.

Facebook Social Action Report in Google Analytics

To be able to track Facebook Likes, Share, Send & Comments on your web site, you have to use the XFBML version (Javascript SDK).

Below I have listed what that this article covers, and what you should do (or consider doing):

  1. Change your <html> opening tag
  2. Implement Facebook Meta Tags (Facebook AppID & AdminID)
  3. Register your web site as a Facebook App
  4. Implement Open Graph Protocol (Meta Tags)
  5. Use Facebook XFBML version (Javascript SDK) and load the script asynchronous
    1. Use Facebook channelURL to avoid problems with older versions of Internet Explorer
  6. Implement the Google Analytics Social Media Tracking Script
  7. Implement my script for tracking Traffic from Facebook Likes, Send & Comments in Google Analytics (and read that article as well)

Facebook and Open Graph Meta Tags

I recommend that you implement both Facebook and Open Graph Meta Tags. Open Graph Tags will give you better control of how your shared content will appear on Facebook and Google+ (Google+ is also using the Open Graph Meta Tags).

It's recommended that you change your <html> opening tag when implementing these meta-tags. Your new <html> tag should look something like the code below.

Remove formatting from Code

  1. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" xmlns:fb="http://www.facebook.com/2008/fbml" xmlns:og="http://ogp.me/ns#" >

Facebook AppID and AdminID Meta Tag

To administer your page, you need to associate it with either your Facebook account or your Facebook Platform application. It is valid to associate your page with both user accounts and a Facebook Platform Application.

To associate the page with your Facebook account, add the additional property fb:admins to your page with a comma-separated list of the user IDs or usernames of the Facebook accounts who own the page.

Register your web site as a Facebook App, and implement the meta-tags below.

Remove formatting from Code

  1. <meta property="fb:app_id" content="130796876963854" />
  2. <meta property="fb:admins" content="12345678" />

Replace values in red with your values.

Open Graph Protocol (Meta Tags)

The Open Graph Protocol gives you better control of how your content are presented on Facebook and Google+.

Remove formatting from Code

  1. <meta property="og:title" content="Your Awesome Title for Your Page"/>
  2. <meta property="og:type" content="article"/>
  3. <meta property="og:url" content="http://www.domain.com/canonical-url-to-article"/>
  4. <meta property="og:site_name" content="YourSiteName"/>
  5. <meta property="og:description" content="Your Awesome Description up to 300 characters"/>
  6. <meta property="og:image" content="http://www.domain.com/facebook-picture-150x150px.png"/>

Since Google+ also uses this data, I recommend that the size of your og:image (Object Type) has a width of minimum 150 pixels and not 50 pixels (as recommended by Facebook) .

What kind of og:type (Object Types) you should use can always be discussed. In the example code above I'm using "article" as Object Type, which is recommended for a blog post like this. However, if you use a different Object Type, Facebook Insight will show you deeper information about "Likes" for that particular page, and you can post follow up messages on Facebook to those who have liked the page.

See also Open Graph protocol information on Facebook.

Facebook XFBML version (Javascript SDK)

Remove formatting from Code

  1. <div id="fb-root"></div>
  2. <script type="text/javascript">
  3. window.fbAsyncInit = function() {
  4. FB.init({appId: 'YourAppID', 
  5. status: true,
  6. cookie: true,
  7. xfbml: true,
  8. channelUrl : 'http://www.yourdomain.com/facebook/include/channel.html' // add channelURL to avoid IE redirect problems
  9. }); };
  10. (function() {
  11. var e = document.createElement('script'); e.async = true;
  12. e.src = document.location.protocol +
  13. '//connect.facebook.net/en_US/all.js';
  14. document.getElementById('fb-root').appendChild(e);
  15. }()); 
  16. </script>

Replace values in red with your values. The best place to put this code is right before the closing </body> tag according to Facebook.

See also Javascript SDK information on Facebook.

Facebook channelURL

The contents of the channel.html file should be this single line.

Remove formatting from Code

  1. <script src="http://connect.facebook.net/en_US/all.js" ></script>

See also FB.init information on Facebook.

Google Analytics Social Media Tracking Script for Facebook

Now that all the basic Facebook stuff is implemented, it is time to implement the script that will track Likes, Share, Send & Comments as Social Media in Google Analytics.

Replace values in red with your values. I also recommend that you don't implement this script as inline javascript, but instead save the script into a javascript file and point to that file to improve page speed.

Place this code below all your other Facebook scripts (before </body>).

Remove formatting from Code

  1. <script type="text/javascript">
  2. window.fbAsyncInit = function() {
  3. FB.init({appId: 'YourAppID', status: true, cookie: true, xfbml: true});
  4. try {
  5. if (FB && FB.Event && FB.Event.subscribe) {
  6. // Like and Share
  7. FB.Event.subscribe("edge.create",function(response) {
  8. if (response.indexOf("facebook.com") > 0) {
  9. // if the returned link contains 'facebook,com'. It's a 'Like' for your Facebook Page
  10. _gaq.push(['_trackSocial','Facebook','Like',response]);
  11. } else {
  12. // else, somebody is Sharing the current page
  13. _gaq.push(['_trackSocial','Facebook','Share',response]);
  14. }
  15. });
  16. // UnLike and UnShare
  17. FB.Event.subscribe('edge.remove',function(response) {
  18. if (response.indexOf("facebook.com") > 0) {
  19. // if the returned link contains 'facebook,com'. It's a 'UnLike' for your Facebook page
  20. _gaq.push(['_trackSocial','Facebook','UnLike',response]);
  21. } else {
  22. // else, somebody is UnSharing the current page
  23. _gaq.push(['_trackSocial','Facebook','UnShare',response]);
  24. }
  25. });
  26. // Facebook Send
  27. FB.Event.subscribe('message.send',function(response){
  28. _gaq.push(['_trackSocial','Facebook','Send',response]);
  29. });
  30. // Facebook Comments
  31. FB.Event.subscribe('comment.create',function(response) {
  32. _gaq.push(['_trackSocial','Facebook','Comment',response]);
  33. });
  34. FB.Event.subscribe('comment.remove',function(response) {
  35. _gaq.push(['_trackSocial','Facebook','UnComment',response]);
  36. });
  37. }
  38. } catch (e) {}
  39. };
  40. </script>

The script above is inspired from Google Analytics and from Yoast. In addition I have also added tracking of Facebook Comments.

Yoast's version tracks interaction with Facebook Like & Send buttons (this will be tracked as "Share" or "Send"), and with the Facebook Like Box (this will be tracked as "Like").

Now that all the basic code and tracking scripts are in place, it's time to implement buttons, boxes or comment fields on your site.

Facebook Share & Send Button

Remove formatting from Code

  1. <div class="fb-like" data-href="http://domain.com/canonical-url-to-article" data-send="true" data-layout="button_count" data-show_faces="false" font="arial" ref="facebook_your-campaign-name"></div>

See also Like Button info on Facebook and my article about How to Track Traffic from Facebook Likes, Send or Comments in Google Analytics.

Facebook Like Box

Remove formatting from Code

  1. <div class="fb-like-box" data-href="http://www.facebook.com/Savio.no" data-width="265" data-show_faces="true" data-stream="true" data-border_color="#ccc" data-header="false" data-ref="facebook_fb-page"></fb:like-box>

See also Like Box info on Facebook.

Facebook Comment Box

Remove formatting from Code

  1. <div class="fb-comments" data-width="500" data-num_posts="2" data-href="http://domain.com/canonical-url-to-article"></div>

Just so I have mentioned it, if you comment on your own site, your comments will not be tracked in Google Analytics.

See also information on Facebook about the Comment Box.

If you see anything that could be improved or errors (I'm not a programmer), or if you just found this article valuable, feel free to comment, Like, Tweet or G+.

Happy tracking Facebook Likes, Share, Send & Comments as Social Engagement in Google Analytics.

Related articles


Viewing all articles
Browse latest Browse all 31

Trending Articles