Tuesday, March 15, 2011

Facebook iframe like box tracking with Clicky (or Google Analytics)

This is how to do GetClicky tracking for facebooks fbxml iframe like box and the fbxml like button.

Without going into details about fb app id and such, this is a working method to implement the GetClicky tracking for facebook like boxes and buttons.

Standard fb script:
<div id="fb-root"></div>
<script type="text/javascript">
//<![CDATA[
window.fbAsyncInit = function() {
 FB.init({appId: '157019890979609', status: true, cookie: true,
 xfbml: true});
});
(function() {
var e = document.createElement('script'); 
e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/sv_SE/all.js';
document.getElementById('fb-root').appendChild(e);
}());
//]]>
</script>

The facebook "like" event hook:
FB.Event.subscribe('edge.create', function(href, widget) {
// do something here
});

The clicky call:
if(typeof(clicky) != "undefined"){
clicky.log('page url','facebook like clicked','click');
}


Combined:
<div id="fb-root"></div>
<script type="text/javascript">
//<![CDATA[
window.fbAsyncInit = function() {
FB.init({appId: '157019890979609', status: true, cookie: true,
xfbml: true});
FB.Event.subscribe('edge.create', function(href, widget) {
if(typeof(clicky) != "undefined"){
clicky.log('page url','facebook like clicked','click');
}
});
};
(function() {
var e = document.createElement('script'); 
e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/sv_SE/all.js';
document.getElementById('fb-root').appendChild(e);
}());
//]]>
</script>

Of course the same works for Google Analytics, just swap clicky.log for _gaq.push

No comments:

Post a Comment