How to track Facebook ads using Google Analytics?

Tweet about this on TwitterShare on FacebookShare on Google+Share on LinkedIn

Last month, we kickstarted our Facebook ad campaign. But we have been struggling to get good analytics from that campaign because we just couldn’t get Google Analytics to recognize and categorize all the traffic coming from the Facebook ads. Yes, it is easy to measure the Impressions and Click Through Rates (CTR) within Facebook, but we wanted more than that. We wanted to measure pageviews, demo views, sign up rates etc once the traffic lands on our website.

So, why is this so hard? Because Google Analytics, for some reason, doesn’t recognize the “source” of the traffic coming from Facebook. Neither does it recognize the keywords that the users are clicking on. And therefore, it just categorizes all Facebook ad traffic as “direct” traffic – which anyone who is familiar with analytics knows is Google’s way of throwing its hands up and saying “I don’t know where this traffic came from”.

I thought that this has got to be a very common problem. And so after some quick search, I found this answer. The basic idea is to use the Google URL Builder tool to generate a special url with all those extra parameters. I did that and still Google Analytics wouldn’t detect the traffic source as Facebook. So, that answer does not completely solve the problem.

And finally, today I figured out the correct solution to this. Here it goes.

1. Construct a special URL using Google URL Builder (as explained in the article I have linked above). Our special URL , for example, looks like this

http://www.ordoro.com/?utm_source=facebook&utm_medium=cpc&utm_campaign=December_2010&utm_keyword=eCommerce_related_keyword

2. Now, tweak the Google Analytics code on your website to explicitly make it detect those special variables that you have added into that URL. Here is how the default Google Analytics code looks like (as generated by your Google Analytics admin screen. As explained here.) –

<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-123456789-1']);
_gaq.push(['_trackPageview']);
(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>

Once you have that code generated from the Google Analytics admin screen, now you must add more variables to that code (new variables are highlighted in red)

<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-123456789-1']);
_gaq.push(['_setCampSourceKey', 'utm_source']);
_gaq.push(['_setCampMediumKey', 'utm_medium']);
_gaq.push(['_setCampContentKey', 'utm_keyword']);
_gaq.push(['_setCampTermKey', 'utm_keyword']);
_gaq.push(['_setCampNameKey', 'utm_campaign']);
_gaq.push(['_trackPageview']);
(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>

That does the trick. Post this updated code snippet into your website, and Google Analytics will start tracking the source, campaign, and keywords for your Facebook Ads traffic. Obviously, this technique is not just limited to Facebook Ads. You can use it for all external ad campaigns that you are running.

If you want to know more details on what those variables mean, you can check out the Campaign Tracking section of the Google Analytics API documentation.