Search code examples
facebookfacebook-graph-apifacebook-ads-apifacebook-insightsfacebook-marketing-api

Unexpected/unpredictable results with batch requests to Facebook Marketing Insights API


I have a Google Apps script that's sending batch requests to the Facebook Marketing API (Insights). I'm fetching mobile installs, ad spend per day over several campaigns. However, the results are unexpected for large amounts of data.

Each relative URL in the batch is requesting a day-wise breakdown for a single Facebook campaign as follows:

{"method":"GET",   
 "relative_url":"<CAMPAIGN_ID>/insights?fields=actions,spend&time_range={'since':'yyyy-mm-dd','until':'yyyy-mm-dd'}&time_increment=1"}

For a given date range, I'm creating a batch request URL for n such campaigns as follows:

  var fbCampaigns = [ C1, C2, C3 ... ]; 
  var batchRequests = []; 

  for(var i=0; i<fbCampaigns.length; i++) {
    // URL encoded version of the relative URL above
    batchRequests.push("%7B%22method%22%3A%22GET%22%2C%22relative_url%22%3A%22"
      + fbCampaigns[i]+"%2Finsights%3Ffields%3Dactions%2Ccampaign_id%2Cspend%26"
      + "time_range%3D%7B%27since%27%3A%27"+start+"%27%2C%27until%27%3A%27"
      + end+"%27%7D%26time_increment%3D1%22%7D");
  }

  var url =  "https://graph.facebook.com/v2.11/?batch=["
             + batchRequests.join(",")
             + "]&access_token="+fbToken;

Since the URL was getting too long, I split the campaign array into parts of 5 and ran the above for each part separately.

This works great for a single date or a short date range. However, for much larger date ranges (100+), it would begin by fetching correct data, and then suddenly start to retrieve data for either all, only some or none of the campaigns, quite unpredictably.

I didn't get any error codes or warnings about throttling. My question is, is there a limit somewhere that I'm missing in either the number of allowed dates or the number of batch requests? It's rather odd, because I'm only placing three batch requests for my entire data.


Solution

  • Found the issue! After looking for patterns I realised that for each campaign, a maximum of 51 dates worth of data were being fetched.

    Absolutely couldn't find details about this hidden limit on the web - if anyone has any further information please add to this.

    Edit: Have since realised there's a pagination system that I had missed in the documentation.