How to find out if your users are on Facebook

Before committing to a social strategy it can be useful to do some analysis first. For example, it might be useful to find out if your users are on a specific platform at all. At Searchlove London last week, Mat Clayton of Mixcloud gave an awesome tip on how to find out how many of your users are logged in to Facebook.

I modified his script a bit to make use of a new Google Analytics feature that makes sure the tracking does not affect bounce rates, as it by default does. Andre Scholten wrote a post on the change and how it works.

1. Create a Facebook App

The first step is to create an empty Facebook App that we’re only going to use for this purpose, so do not bother with all the settings. The only thing you have to do is to fill in the ‘App Domain’ and the ‘Site URL’ , as marked by the arrows in the screenshot.

We will use this app to check if the user authorized it to use his Facebook data. The Facebook API returns a status code for the currently logged in user, even though they will never see any prompts or popups from Facebook itself. This gives us the possibility to silently track the number of people logged in to Facebook!

2. Add Javascript to your website

The next step is to insert the following script (with your App ID on line #4) at the bottom of your site:

<div id="fb-root"></div>
<script type="text/javascript">
window.fbAsyncInit = function(){
    FB.init({ appId:'INSERT_YOUR_APP_ID_HERE', status:true, cookie:true, xfbml:true }); 
    FB.getLoginStatus(function(response) {
        _gaq.push(['_trackEvent', 'FB', 'status', response.status, 1, true]);
    });
};
 
(function(d){
   var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
   js = d.createElement('script'); js.id = id; js.async = true;
   js.src = "//connect.facebook.net/en_US/all.js";
   d.getElementsByTagName('head')[0].appendChild(js);
 }(document));
</script>

This script loads the Facebook API asynchronically (so it doesn’t slow down your site) and checks if the user is authorized with your app. Since we only created a shell app that no one ever sees, the status returned will be either one of the following options: not_authorized or unknown.

not_authorized means the user is not connected with your app (which is obvious, as we never asked them to authorize), but the real value is in combination the other status: unknown. This means that the user is not logged in to Facebook at all. As a result you can measure the number of visitors that are logged in to Facebook, which I think is a pretty good indicator if they are on Facebook at all. Of course some people are logged out of Facebook, for example at work or on their phone, but overall this gives pretty solid data.

Lastly, the script calls a Google Analytics TrackEvent with the specific status code as the value. By using the updated API call it will not affect bounce rates, so you can use your normal Analytics profile.

3. Watch the data flow in

In Analytics the data becomes available under Content > Event Tracking. In this example the category is called ‘Facebook’ and the event is called ‘Status’. You can change both in the script if you like.

Finally, you should look at ‘Unique Events’ as this number only counts visitors once. After a short while we found out that 32,8% of our Dutch visitors are on Facebook, and this number seems pretty stable the more data points we gather.

I’ve tried to do something similar with both Twitter and LinkedIn, but unfortunately their APIs only return if the user is authorized or not, so no tricks there. If anyone knows how to set this up for them, please let me know!

Update 19-01-2012: Facebook changed ‘NotConnected’ to ‘not_authorized’. 

Tagged

10 Responses to How to find out if your users are on Facebook

  1. Arend says:

    Dennis, geweldige feature die ik meteen heb geprobeerd; Zie echter niks in Google Analytics binnenkomen; moet ik nog een koppeling tussen dat script en Analytics maken? Of moet ik meer geduld hebben voordat de data tonen (nu 1 uur geleden boel geinstalleerd)? Thx. En doe de groeten aan Ruben

  2. Dennis Paagman says:

    Arend, je moet inderdaad wat langer wachten. GA analyseert data niet zo snel, ik denk dat het gemiddeld zo rond de 4 uur duurt voordat de data beschikbaar is, maar dat kan ook langer duren. Als het goed is hoef je verder niks te doen en moet je dus alleen wat geduld hebben :)

  3. Tim Wilson says:

    Dennis — I finally got around to giving this a try. Great, simple instructions (with the added bonus of getting me to set up a Facebook app, which I’d never done before and needed to play with for some Facebook Insights for Web Sites reasons).

    I’m getting two event values: “not_authorized” and “unknown.” Does that make sense? Has Facebook changed something that would make “notConnected” become “not_authorized” instead?

  4. Dennis Paagman says:

    Hi Tim, I haven’t used the script in a while but is sounds like that’s the case. I’ll update the post later on to reflect the changes :) Thanks, hopefully the data will be insightful to you :)

  5. Tim Wilson says:

    Definitely! And, if I can suggest a “two birds with one stone” — once you’ve got an app created, you might as well drop that in a meta tag on the site and make that control who gets access to Facebook Insights for Web Sites data. No?

  6. Dennis Paagman says:

    Yeah you definitely should!

  7. Tim Wilson says:

    Hey, Dennis. I’m back…! :-)

    So, I’ve had this running for almost a full week now. Looking at the date range of “full days” where this was implemented, I’ve got 5 full days of data. I set up advanced segments — one for “unknown” (not logged into Facebook) and one for “not_authorized” (logged into Facebook). I would have expected the sum of the visits from each segment to be pretty close to the total visits to the site (possibly a little hire, in the event that someone wound up logging into Facebook mid-visit).

    That’s not what I saw, though. Simplifying it a bit, the number of visits in this period for a segment that had a Facebook/status/ event fire at all was 1,113, while the overall visits to the site was 1,489. Coming at it another way, there were 1,113 Unique Events for the “Facebook” event…against an overall 1,489 visits.

    I’ve checked the different types of pages on my site, and they all appear to have the code deployed (it’s in the global footer, so that’s to be expected).

    Have you looked at the unique events for Facebook / status and compared that to visits to the site overall? Are you seeing a closer match? I’m perplexed. The code seems to be pretty straightforward (not that I could write it!), so I’m scratching my head as to what I’m missing.

  8. Dennis Paagman says:

    Hi Tim, Sorry for the late response, didn’t notice your comment earlier. I haven’t checked that, to be honest. Can you find any evidence that point towards the script not working in a particular browser or something? I Might rerun my experiment and try to find out why the numbers are lower. Thanks for pointing this out :)

  9. Tom Anthony has managed to achieve this for Twitter also: http://www.seomoz.org/blog/visitor-social-network-login-status-google-analytics

    although Tom uses _setCustomVar whereas you use _trackEvent.

    Can you comment on that choice Dennis?

  10. MartijnSch says:

    Hi Paul,

    Thanks for your comment, I really liked Toms post and also thought it’s quite interesting why he chose for using the Custom Variables over the Event Tracking. In our case we chose Event Tracking over Custom Variables because of the limit(5). W’re currently tracking the template per traffic via our Custom Variables and didn’t want to screw up that data.

    You also found out about our brand new WP Plugin based on Toms post ;-) http://www.springest.co.uk/social-analytics.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">