O'Reilly logo

Facebook Cookbook by Jay Goldman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Analyzing Traffic with Google Analytics

Problem

I want to know more about what users are doing within my application, and I really love using Google Analytics on my off-Facebook site, but dropping its code into my Canvas page didn’t work. How can I use it?

Solution

Use the fb:google-analytics tag to insert your tracking code for you. The simplest form is:

<fb:google-analytics uacct="UA-9999999-99" />

where uacct is the _uacct variable from your Google-provided code block. The FBML parser will render that into:

<script src="https://ssl.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
    _uacct = "UA-9999999-99";
    urchinTracker();
</script>

Discussion

The first thing to note is that this tag still produces the old-style Analytics code that calls the urchinTracker(), rather than the new-style code:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl."
 : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/
ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-9999999-99");
pageTracker._initData();
pageTracker._trackPageview();
</script>

One of the features of the new code is the automatic detection of HTTP versus HTTPS, which would be wasted inside of Facebook because there are no secure connections to Facebook apps at this time. It also offers you the ability to track specific events ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required