Search code examples
facebook-marketing-apifacebook-java-api

Batch Requests for Insights on Facebook Marketing API (JAVA)


This should be really easy looking at FB documentation for the marketing api but the page is completely empty: https://developers.facebook.com/docs/marketing-api/asyncrequests/v2.10

Does anyone has a simple example on how to make an easy batch request for AdsInsights for Ads, Adsets or Campaigns?

Googling around I find only examples in js or python, so I can't seem to find the right class name in java to do this.


Solution

  • Ok, got it working. Code is in scala but should be the same in java.

    implicit val batch = new BatchRequest(FacebookApi.context)
    apiNodeList.asScala.map(getInsights)
    
    def getInsights(node: Ad)(implicit data: InsightData, batch: BatchRequest) = {
        node.getInsights
          .setBreakdowns(data.breakdowns)
          .setDatePreset(data.datePreset.toString)
          .setFields(data.fields)
          .setActionAttributionWindows(data.attributionWindow)
          .setTimeIncrement(data.timeIncrement)
          .addToBatch(batch)
    }
    
    val result = batch.execute()