Social interaction of your customers are very important to analyze their
impact on your conversions.
Reading the official Google
Analytics developers guide, the solution seems to be the usage of the
following code:
FB.Event.subscribe('edge.create', function(targetUrl)
{
_gaq.push(['_trackSocial', 'facebook', 'like', targetUrl]);
});
Unfortunately this code doesn't work if you are correctly
loading the Facebook SDK asynchronously, that's because may be
the the SDK is still loading when you subscribe to the event.
<div id="fb-root"></div>
<script>
window.fbAsyncInit =
function()
{
// init the FB JS SDK
FB.init(
{
appId : 'YOUR_APP_ID',
channelUrl : '//WWW.YOUR_DOMAIN.COM',
status : true,
cookie : true,
xfbml : true
});
FB.Event.subscribe('edge.create',
function (targetUrl)
{
_gaq.push(['_trackSocial','facebook',
'like', targetUrl]);
});
};
// Load the SDK's source Asynchronously
(function(d)
{
var js, id = 'facebook-jssdk', ref =
d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id
= id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
</script>
If you want to add the event handler programmatically, the correct solution is to use the window.fbAsyncInit
event to
append the like event subscription to the Facebook SDK initialization.
In the following code I used jQuery to append the event
subscription after DOM initialization.
$
(function ()
{
var exsistingFbAsyncInit = window.fbAsyncInit;
if (exsistingFbAsyncInit == null)
window.fbAsyncInit = function ()
{
FB.Event.subscribe('edge.create',
function (targetUrl)
{
_gaq.push(['_trackSocial','facebook',
'like', targetUrl]);
});
};
else
window.fbAsyncInit = function ()
{
exsistingFbAsyncInit();
FB.Event.subscribe('edge.create',
function (targetUrl)
{
_gaq.push(['_trackSocial','facebook',
'like', targetUrl]);
});
};
})
;
By the way if you are using Infrared CMS, that's a problem you have not to worry about.