OK, so you have implemented Facebook Like, Send or Comments on your website, and you see from your referral data in Google Analytics that you are getting traffic from Facebook. But are that traffic caused by Like Buttons or Comments? Referral data will not show you this.
Image by Ksayer1
In this blog post I explain how you can get better insight into your Facebook traffic by tracking traffic from Facebook Likes, Send or Comments as campaign traffic - and of course, how to do it.
The road to better insight
Unfortunately there isn't a easy road to what I consider to be the best solution for tracking Facebook Like, Send or Comments (I have tried several approaches). Here are what you will have to do:
- (Probably) change your Facebook Like & Send script
- Change your Google Analytics script
- Add filters to Google Analytics
- Exclude Facebook Parameters from your Google Analytics reports
- Beware of SEO implications that Facebook Parameters may cause
Facebook Like & Send
To the Facebook Like & Send script we will add something called the "ref attribute". The purpose of the ref attribute is originally to be able to help you test different placements and types of Like buttons. Append the ref="" attribute to the Like button, and make sure that the value you choose is less than 50 characters (a-z, A-Z, 0-9, + / = - . : _).
The ref attribute is going to be our Google Analytics Campaign Name. Since we also are going to use the ref attribute in our Google Analytics Filters, part of the ref attribute must contain something that makes us able to identify the value as "Facebook". Se example below:
- <div class="fb-like" data-href ="http://domain.com/canonical-url-to-article" data-send="true" data-layout="button_count" data-show_faces="false" data-font="arial" data-ref="facebook_your-campaign-name"></div>
When somebody "Like" or "Send" your article, the parameter fb_ref is automatically added to the URL. This makes us able to identify this as either Facebook Like or Send traffic as the example URL below shows.
- http://www.domain.com/url-to-article?fb_ref=facebook_your-campaign-name
Where on Facebook did the Link Get Clicked?
When you use the ref parameter, Facebook will also (but not always) add a fb_source parameter to the referrer URL, which includes the stream type ('home', 'profile', 'search', 'other') where the click occurred and the story type ('oneline', 'multiline') concatenated with an underscore.
The fb_source parameter will be tracked as Campaign Ad Content in Google Analytics.
Facebook Comments
You don't have to change anything in your Facebook Comments code. Example code below:
- <div class="fb-comments" data-width="500" data-num_posts="2" data-href="http://domain.com/canonical-url-to-article"></div>
When a user comments and posts to Facebook a fb_comment_id parameter will automatically be added to the URL like this:
- http://domain.com/url-to-article?fb_comment_id=commentidparameter123
The fb_comment_id parameter will be tracked as Campaign Ad Content in Google Analytics.
The Google Analytics script
To be able to track traffic from Facebook Like, Send or Comments as campaign traffic you will have to add Google Analytics Campaign Tracking Code to your Google Analytics script. In the example script below the Campaign Tracking Code is bolded.
Since the script below is handling both Like/Send and Comments, I have added a simple javascript checking for either the fb_ref or the fb_comment_id parameter in the URL. You can also do this server-side as I do on this site. Just change the script part in orange.
- <script type="text/javascript">
- // This script is provided AS IS without warranty of any kind.
- // See http://www.savio.no/blogg/a/107/ for more information.
- // Smacked together by Eivind Savio July 2011
- var _gaq = _gaq || [];
- _gaq.push(['_setAccount', 'UA-XXXXXX-X']);
- if (document.location.href.search("fb_ref=")!=-1){
- // Track traffic from Facebook Like & Send as Campaign traffic
- _gaq.push(
- ['_setCampMediumKey', 'fb_ref'],
- ['_setCampSourceKey', 'fb_ref'],
- ['_setCampNameKey', 'fb_ref'],
- ['_setCampContentKey', 'fb_source']
- );
- } else if (document.location.href.search("fb_comment_id=")!=-1){
- // Track traffic from Facebook Comments as Campaign traffic
- _gaq.push(
- ['_setCampMediumKey', 'fb_comment_id'],
- ['_setCampSourceKey', 'fb_comment_id'],
- ['_setCampNameKey', 'fb_comment_id'],
- ['_setCampContentKey', 'fb_comment_id']
- );
- }
- _gaq.push(['_trackPageview']);
- _gaq.push(['_trackPageLoadTime']);
- }
- (function() {
- var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
- ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
- var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
- })();
- </script>
Google Analytics Filters for Facebook Likes, Send & Comments
Without filtering the data in Google Analytics, our Campaign Name, Source and Medium reports will look like rubbish.
Facebook Like, Send & Comments Campaign Medium Filter
Field A -> Extract A | Campaign Medium | ^facebook_|^fbc_ |
Field B -> Extract B | - | |
Output To -> Constructor | Campaign Medium | social-media |
Field A Required | Yes | |
Field B Required | No | |
Override Output Field | Yes | |
Case Sensitive | No |
Facebook Like, Send & Comments Campaign Source Filter
Field A -> Extract A | Campaign Source | ^facebook_|^fbc_ |
Field B -> Extract B | - | |
Output To -> Constructor | Campaign Source | facebook.com |
Field A Required | Yes | |
Field B Required | No | |
Override Output Field | Yes | |
Case Sensitive | No |
Facebook Comments Campaign Name Filter
Field A -> Extract A | Campaign Name | ^fbc_ |
Field B -> Extract B | - | |
Output To -> Constructor | Campaign Name | facebook-comment |
Field A Required | Yes | |
Field B Required | No | |
Override Output Field | Yes | |
Case Sensitive | No |
Done! Now traffic from Facebook Like, Send or Comments will be tracked as campaigns in Google Analytics and will give you better understanding of your Facebook traffic.
Google Analytics Report Explanation
As mentioned in the beginning of this blog post the parameter fb_ref will automatically be added to the URL on Facebook.
Unfortunately not all traffic caused by a "Like" will have that parameter. The exception is when somebody "Like" an article, and then somebody on Facebook reshare that article. From my experience the posted URL on Facebook will then loose the fb_ref parameter, and traffic from the "Liked" URL will from now on be tracked as referral traffic in Google Analytics.
With other words, this solution isn't perfect, but I found it to be the best method.
Facebook Campaign Name Report in Google Analytics
Below you find an example of how the Campaign Name Report may look like.In the example above you will see that I have differentiated between "Likes" in left column or below article (I have 2 sets of Like buttons on my sites for the moment). Perhaps not the best campaign names, but I know this traffic is caused by either Facebook Likes, Send or Comments.
Facebook Campaign Ad Content Report in Google Analytics
Below you find an example of how the Campaign Ad Content Report may look like.In the beginning of this blog post I shortly explained that the fb_source parameter can be added to your URLs. If your traffic comes from a Facebook Like button, the fb_source parameter will be added.
If your traffic comes from a Facebook Send button, the fb_source parameter is message, but I have also experienced that it can be missing. The report above shows some traffic reported as (not set). That is in this case traffic from the Send button.
The Ad Content Report containing the fbc_xxxx value is traffic from Facebook Comments, and the value is the Comment ID.
Cleaning up your data
The parameters from Facebook will create duplicate content in your page content reports in Google Analytics. I recommend you exclude those parameters.
Some final recommendations
Implementing Facebook scripts on your website aren't necessary so easy as it may look like. Facebook scripts may slow down your site, cause problems for Internet Explorer, and finally, cause problems for search engines like Google (and with this, cause SEO problems).
These are my recommendations when you implement Facebook scripts on your website (the 5 first points are covered in my blog post Tracking Facebook Likes, Share, Send & Comments as Social Engagement in Google Analytics):
- Use the HTML5 solution.
- Load your Facebook XFBML scripts Asynchronously.
- Implement Open Graph protocol
- Use Facebook Channel URL to reduce problems with Internet Explorer.
- Implement Google Analytics Social Interaction Analytics
- Implement Canonical tag or exclude Facebook parameters in Webmaster Central to avoid SEO problems.
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 Traffic from Facebook Likes, Send or Comments.
Related articles
- How to Track Facebook iFrames with Google Analytics
- Tracking Facebook Likes, Share, Send & Comments as Social Engagement in Google Analytics
- Behavioral Targeting using Facebook Connect & Google Analytics Custom Variables