2

I often get this error (FacebookAds\Http\Exception\AuthorizationException, Code: 17: (#17) User request limit reached) when I call this function in my Laravel app to get a breakdown report of my Facebook Ads' performance.

/**
 * @param string $levelType
 * @param string $id
 * @param string $start (YYYY-MM-DD date)
 * @param string $end (YYYY-MM-DD date)
 * @param int $minImp
 * @return array
 */
public function getFbBreakdown($levelType, $id, $start, $end, $minImp = 100) {
    $cacheKey = 'fbBreakdown' . $levelType . $id . $start . $end . $minImp;
    return Cache::remember($cacheKey, 60, function () use ($levelType, $id, $start, $end, $minImp) {
                $fields = [
                    strtolower(self::SPEND),
                    AdsInsightsFields::IMPRESSIONS
                ];
                $params = [
                    'breakdowns' => self::AD_ACCT_HOURLY_STATS,
                ];
                $dateRanges = $this->fbTools->getDateRangeBrokenDownToDays($start, $end);
                foreach ($dateRanges as $range) {
                    $params[AdReportRunFields::TIME_RANGE] = $range;
                    $endpoint = '/' . $id . '/insights?' . 'fields=' . implode(',', $fields);
                    $fbr = new FacebookRequest($this->fbApp, $this->fbConfig[self::ACCESS_TOKEN], \FacebookAds\Http\RequestInterface::METHOD_GET, $endpoint, $params, null, self::FB_GRAPH_VERSION);
                    $requests[] = $fbr;
                }
                $batches = array_chunk($requests, 50); //Facebook limits batch requests to 50 at a time
                $chunkedFbResponses = [];
                foreach ($batches as $batchOfRequests) {
                    $response = $this->fb->sendBatchRequest($batchOfRequests, $this->fbConfig[self::ACCESS_TOKEN]);
                    $chunkedFbResponses[] = $response->getResponses();
                }
                $responses = [];
                foreach ($chunkedFbResponses as $chunkedFbResponse) {
                    $responses = array_merge($responses, $chunkedFbResponse);
                }
                return $responses;
            });
}

In my Facebook developer dashboard at https://developers.facebook.com/apps/99999/dashboard/?business_id=99999, I see "Ad Account Rate Limiting" as being reached, but I see nothing about "User request limit". Is it the same thing?

I know that the limit seems to reset every 5 minutes.

How many queries are allowed each 5 minutes?

Is there something else I could do to batch the queries better so that the function uses up fewer API requests?

1
  • 1
    Batching doesn't help. It still the same amount of work that needs to be done. You just have to do less. Commented Feb 5, 2019 at 17:18

1 Answer 1

3

Ad account level and user level both throw error code 17. The amount of queries allowed every 5 minutes is based on the ad account, Facebook doesn't actually explain how it's determined, but I do remember reading somewhere that it may be associated to ad spend on the account. It's actually against their terms of service to programmatically figure out how their rate limiting works.

Batching won't help at all with rate limiting. The best thing you can do is pull in the header x-ad-account-usage and put in a sleep once you hit a high percentage.

Check out the best practices page: https://developers.facebook.com/docs/marketing-api/best-practices

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.