3

I'm looking for a very simple "hello world" type of example that will help me understand how to use JavaScript to retrieve the Insight data for my domain.

I want to return the total number of Likes (domain_like_adds) for the day on my website, but I'm not sure how to go about it. The Facebook documentation has me running in circles.

Thanks for your help!

genesis
  • 50,477
  • 20
  • 96
  • 125
HWD
  • 1,547
  • 7
  • 36
  • 72

1 Answers1

0

See this page for more information on how to define your query:

http://developers.facebook.com/docs/reference/fql/insights/

Here's the basic query and facebook JS API query with callback function:

var insight_domain = "http://www.mydomain.com";

var insight_query = "SELECT metric, value FROM insights where metric="domain_like_adds" AND end_time=end_time_date('2011-04-06')  AND period=period('day') AND object_id in (select id from object_url where url ='{0}')";

var query = FB.Data.query(insight_query, insight_domain);
query.wait(function(data) {
  //process your insight data here
});
Jake
  • 2,471
  • 15
  • 24
  • Can't seem to get this working. I've plugged by domain into this code and tried to output the data, but the script and everything after it stops working during the FB.Data.query call... any thoughts? – HWD Apr 08 '11 at 19:57
  • You've authenticated with a FB.init and your app id before executing this? – Jake Apr 08 '11 at 20:11
  • Yeah, then I've put this right after. The way this line is formatted is throwing up some errors: `var insight_query = "SELECT metric, value FROM insights where metric="domain_like_adds" AND end_time=end_time_date('2011-04-06') AND period=period('day') AND object_id in (select id from object_url where url ="{0}")";` The double-quotes around domain_like_adds and the {0} are causing problems. – HWD Apr 08 '11 at 21:17
  • Is it possible that a user session is needed to get insight data for a domain? How would I do that? Even using the exact example from the documentation, I cannot get it to output any insight data. – HWD Apr 09 '11 at 04:50
  • The double quotes at the end around the template placeholder {0} is a typo, change those to single quotes (will update example). You can try logging into facebook in the same browser and then attempting the above query (assuming your facebook account is the admin for this app/url you are fetching stats for). If that works then you need to look more into authenticating and creating a user session. – Jake Apr 10 '11 at 22:56