Tracking like button click events

October 14th, 2011 by Abby Sims | Comments Off | Filed in Facebook, programming

Sometimes, you want to track certain Facebook interactions within your internal analytic tools. Below is code to detect like, unlike, and send message clicks for the Facebook Like Button social plugin. This logs to Google Analytics. Just add the following event subscriptions between some Javascript tags after the Facebook JS SDK has been loaded. “response” is the URL that was liked or sent. Adjust what happens for each event, as desired.

       FB.Event.subscribe('edge.create', function (response) {
            _gaq.push(['_trackEvent', 'Something', 'Like', response]);
        });
        FB.Event.subscribe('edge.remove', function (response) {
            _gaq.push(['_trackEvent', 'Something', 'Un-Like', response]);
        });
        FB.Event.subscribe('message.send', function (response) {
            _gaq.push(['_trackEvent', 'Something', 'Comment', response]);
        });

Facebook iframe being cut off in Firefox 3.6.x on Mac when using setAutoResize()

October 21st, 2010 by Abby Sims | Comments Off | Filed in Facebook, programming

Another day, another Facebook development quirk.

If you’re having issues with your iframe not automatically resizing to full height in Firefox on Mac when using FB.Canvas.setAutoResize, try manually setting the height using FB.Canvas.setSize as in the code below. Obviously, this is only really an option if your know how tall your application will render and it’s the same from page to page or you manually set it on each page.

    <div id="fb-root"></div>    <script type="text/javascript"  src="http://connect.facebook.net/en_US/all.js"></script>    <script type="text/javascript">        FB.init({ appId: '<%= ConfigurationManager.AppSettings["FacebookAppID"] %>', status: true, cookie: true, xfbml: true });        FB.Event.subscribe('auth.sessionChange', function (response) {            if (response.session) {                // A user has logged in, and a new cookie has been saved                            } else {                // The user has logged out, and the cookie has been cleared            }        });

        $(document).ready(function () {            FB.Canvas.setSize({ height: 1100 });        });    </script>

Querying Facebook Comment Table with FQL.Query and C#

September 20th, 2010 by Abby Sims | Comments Off | Filed in C#, Facebook, programming

Here’s a code snippet for querying the Facebook Comment table using the Facebook C# SDK. Note that this query uses an app signed request, so you’ll get results whether your user is logged in to Facebook or not. This code queries for all comments xids used by your application. If you have more than one like I did, you can then filter through the results to get counts, latest post information, or whatever you need. For more info about the Comment table, read the official documentation.

public void GetFacebookComments()
{
    var appId = ConfigurationManager.AppSettings["AppID"];
    var appSecret = ConfigurationManager.AppSettings["Secret"];
    // use app signed api key
    FacebookAPI api = new FacebookAPI(appId + "|" + appSecret);

    string url = String.Format("https://api.facebook.com/method/fql.query?query=SELECT fromid, text, id, time, username, xid, object_id FROM comment WHERE xid IN (SELECT xid FROM comments_info WHERE app_id = {0}) ORDER BY time desc&access_token={1}&format=json", appId, api.AccessToken);

    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
    {
        StreamReader reader = new StreamReader(response.GetResponseStream());
        comments = reader.ReadToEnd();
    }
    JSONObject fbComments = JSONObject.CreateFromString(comments);
    if (fbComments.Array != null)
    {
        String[] xidArray = new String[] { "xid1", "xid2", "etc" };

        foreach (var xid in xidArray) {
            var xidComments = fbComments.Array.Where(m => m.Dictionary["xid"].String == xid);

            if (xidComments != null && xidComments.Count() > 0)
            {
                var lastDiscussBy = xidComments.First().Dictionary["fromid"].String;
                var dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
                var lastDiscussTime = dtDateTime.AddSeconds(Convert.ToDouble(chzComments.Last().Dictionary["time"].String)).ToLocalTime();
            }
        }
    }
}