Skip to content

Instantly share code, notes, and snippets.

@blafrance
Forked from kressaty/ga-facebook.js
Created November 11, 2012 04:50
Show Gist options
  • Save blafrance/4053758 to your computer and use it in GitHub Desktop.
Save blafrance/4053758 to your computer and use it in GitHub Desktop.
Facebook Like Button Google Analytics
// This assumes Facebook buttons are using the Facebook Javascript SDK.
// Thanks to Google for most of this code, see the Social Interaction Analytics guide at
// https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingSocial
// Facebook provides a Javascript event framework that allows you to subscribe to events
// happening within the Facebook Javascript SDK and provides data accordingly.
// To track a like, subscribe to the edge.create event and then use the callback to
// execute the Analytics tracking code:
FB.Event.subscribe('edge.create', function(targetUrl) {
_gaq.push(['_trackSocial', 'facebook', 'like', targetUrl]);
});
// To track an unlike, subscribe to the edge.remove event and then use the callback to
// execute the Analytics tracking code:
FB.Event.subscribe('edge.remove', function(targetUrl) {
_gaq.push(['_trackSocial', 'facebook', 'unlike', targetUrl]);
});
// To track a share, subscribe to the message.send event and then use the callback to
// execute the Analytics tracking code:
FB.Event.subscribe('message.send', function(targetUrl) {
_gaq.push(['_trackSocial', 'facebook', 'send', targetUrl]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment