Do Facebook Graph API calls using field expansion count differently against the rate limits than batch calls - facebook

I am looking to optimize my Facebook app.
Today I make a batch call with four graph API calls:
/me
/me/friends
/me/likes
/me/feed
If I change this to a single graph API call using field expansion like this:
/me?fields=id,name,username,friends,likes,feed
Will that now count as one hit against the API instead of four for rate limiting purposes?

Unfortunately, each call in the batch is counted as an api call, it's just faster to call them within a batch since it will be 1 request. See here documentation on Facebook API:
Limits
We currently limit the number of requests which can be in a batch to 50, but each call within the batch is counted separately for the purposes of calculating API call limits and resource limits. For example, a batch of 10 API calls will count as 10 calls and each call within the batch contributes to CPU resource limits in the same manner.
Source:
https://developers.facebook.com/docs/reference/api/batch/

Based on real-world testing, I've found that field expansion can count for multiple uses under the rate limit. For example, starting from a quiet state, a sequence of 63 field-expanded calls to a single api (graph.facebook.com/IDENTITY/posts) brought us to the 600 call rate limit.

According to the Facebook Docs,
The Field Expansion feature of the Graph API, allows you to effectively "join" multiple graph queries into a single call.
So your queries above would represent four calls in the Batch form, and one call in the Field Expanded form.
As I noted in a comment above: A batch sends multiple-but-not-necessarily-related queries to Facebook in a single request. Field expansion is like doing joins in SQL through a single query.

Related

Possible to specify date_preset with insights edge in Facebook Ads API?

For the Marketing API, I know that I'm able to make one call to retrieve all of the adsets from a certain account along with their insights, but am I able to specify the date_preset for the insights edge in that same call?
For example, the following gives me lifetime insights stats:
/v2.4/{accountID}/adcampaigns?fields=insights
To be clear - I know this is possible to retrieve by making separate calls for each adset id (where I know I can specify the date_preset); instead, I'd like to do this via the call where I get a long list of the ad sets plus their insights details in one go.
Yes this is possible using query expansion, however you probably should not do it in this anyway.
Using query expansion results in multiple requests being executed in one HTTP call, in this case one to get all the adcampaigns, and then N requests where N is the number of adcampaigns returned. This will in turn affect your rate limiting.
The most efficient way to request all insights for all adcampaigns (ad sets) is instead to request them at the account level, specifying aggregation level:
/v2.4/act_{ADACCOUNT_ID}/insights?date_preset=last_7_days&level=campaign
This requires just 1 request, or the number of requests to retrieve the total number of pages.
If you really want to achieve this with query expansion, you can do the following for example:
/v2.4/act_{ADACCOUNT_ID}/adcampaigns?fields=insights.date_preset(last_30_days).time_increment(all_days)
You can see the parameters to insights that would normally be query parameters of the form param_name=param_value are now in the form of param_name(param_value).
To specify the date_preset , here is the correct format . Its important to use insights as edge to get the date_preset filtering .
/v2.10/act_{ADACCOUNT_ID}/insights?fields=impressions,clicks,ctr,unique_clicks,unique_ctr,spend,cpc&date_preset=last_3d
The above one is tested with latest Graph Api version(2.10) as of now . FOr more info related to the date_preset values refer to there api docs .
https://developers.facebook.com/docs/marketing-api/insights/parameters

Facebook Request(s): what counts as 1 request?

I am currently creating an application that polls facebook for data. First request a page in this fashion...
pageID/posts?fields=id,message,created_time,type&limit=250
This returns the top 250 posts from a page. I then check if there page next is set and if it is make another request for the next 250 posts. I continue this recursively until there are no more posts.
With each post that is returned I go out and fetch the post details from the graph api as well.
My question is if I had 500 posts on a page. Would that equate to 502 requests? (500 requests for each post + 2 for parsing through page data to get posts) or am I incorrect in my understanding of a "request". I know when batching calls each query included in the batch actually counts as 1 request. The goal is to avoid the 600 calls / 600 second rate limiting. Thanks!
Every API call is...well, 1 request. So every time you use the /posts endpoint with whatever limit, it will be 1 request. For example, if you do that call you posted, it will be one request that returns 250 elements.
Batch requests are just faster, but each call in the batch counts as a request. So if you combine 10 calls in a batch, it will be 10 requests. The benefit of batch calls is really just that they are a lot faster: as fast as the slowest call in the batch.
If you want to get 500 posts with that example of yours, you would only need 2 calls. First one with 250 returned elements, second one by using the API call defined in the "next" value to get another 250. Just keep in mind that the default is usually 25 elements, and you can´t use any limit you want. There is a max limit for calls and it gets changed from time to time afaik so don´t count on getting the same result every time.
Btw, don't be to fixated on that 600calls/600seconds limit, it's just a general limit. The real limit is dynamic and depends on many factors. It's not public, of course. But if you really hit the limit, you are doing something wrong anyway.

Batch Graph - count number of likes for numerous pages

I am currently getting the number of likes on individual pages by calling the below code, however I would like to get the likes for multiple pages at the same time. Can't seem to find any documentation on if BATCH will allow this.
$like_result = file_get_contents('http://graph.facebook.com/?id=http://www.myadd.com/page1.php);
$like_array = json_decode($like_result, true);
$like_no = $like_array['shares'];
i.e. need the number of likes for:
ahttp://graph.facebook.com/?id=http://www.myadd.com/page1.php
ahttp://graph.facebook.com/?id=http://www.myadd.com/page2.php
ahttp://graph.facebook.com/?id=http://www.myadd.com/page3.php
The Graph API documentation explains how to use ids to select multiple objects simultaneously:
/<API VERSION>/?ids=http://www.myadd.com/page1.php,http://www.myadd.com/page2.php,http://www.myadd.com/page3.php
You could also use the Batch Requests API (which has usage examples in the documentation) or a combination of the two (making batch calls of requests which ask for multiple objects)

How many Facebook API calls am I making with this line of code?

https://graph.facebook.com/me?fields=friends.limit(100).fields(name,books)&access_token=##############
I believe it is 1. But in the facebook documentation it says, batch requests api calls are calculated as if they were many individual calls. Since field expansion is also a "batch request", I am confused how they calculate.
I believe the number of calls will either be one of the following :
1. 1 - line of code
2. 100 - number of friends
3. 100 * (1 + 1) = 200 - 1 each for name and books
Can you please tell me how to calculate?
That's one - it's not a batch request unless you're actually creating a batch of requests per the batch request API documentation (which you're not here; here you're just making one 'call' asking for 100 friends' data)

Query multiple insights metrics in one API call

Is it possible to query more than one insights metric in one API call?
For example to get the daily added likes for a page I executed the following call:
https://graph.facebook.com/_object_id_/insights/page_fan_adds_unique/day/
But I would like to have another metric, e.g., page_fan_removes_unique, included as well.
Query the insights object entirely is a possibility, but gives me too much data I don't need, thus decreases performance.
Since your question shows that you're using the Graph API, you may want to use the batch requests method: http://developers.facebook.com/docs/reference/api/batch/
You can group multiple Graph API queries together so you can just query the Insights data you need in one request.
Yes, you can run a multiple FQL query in one API call and then parse the various results.
multi query: http://developers.facebook.com/docs/reference/rest/fql.multiquery/
insights table: http://developers.facebook.com/docs/reference/fql/insights/