Retrieving reach estimate using restfb

43 views
Skip to first unread message

Bilge

unread,
Sep 9, 2017, 10:25:23 AM9/9/17
to RestFB
Hello, i am trying to get reachestimate using restfb library but was unable to send targeting_spec parameter to facebook graph api. In php sdk the way to accomplish is like that:
$request = new FacebookRequest(
  $session,
  'GET',
  '/{ad-account-id}/reachestimate',
  array(
    'targeting_spec' => '{"behaviors": [{"id":6015559470583}],"geo_locations": {"countries":["US"],"location_types": ["home"]},"age_min": 20,"age_max": 40 }'
  )
);

$response = $request->execute();
$graphObject = $response->getGraphObject();
/* handle the result */
I tried using fetchConnection and send parameters using a map but no success.

Any help would be appreciated.

Thanks

Norbert Bartels

unread,
Sep 10, 2017, 7:05:12 AM9/10/17
to RestFB

You can use the Targeting object.

        List<IDName> behaviors = new ArrayList<IDName>();
        IDName behavior = new IDName();
        behavior.setId("6015559470583");
        behaviors.add(behavior);

        TargetingGeoLocation geoLocation = new TargetingGeoLocation();
        geoLocation.setCountries(Collections.singletonList("US"));
        geoLocation.setLocationTypes(Collections.singletonList("home"));

        Targeting targetingSpec = new Targeting();
        targetingSpec.setAgeMin(20L);
        targetingSpec.setAgeMax(40L);
        targetingSpec.setGeoLocations(geoLocation);
        targetingSpec.setBehaviors(behaviors);

In fetchConnection you simple add Parameter.with("targeting_spec", targetingSpec).
Everything else should work as you tried.

Hope this helps.
Norbert

Bilge

unread,
Sep 11, 2017, 4:30:05 AM9/11/17
to RestFB
Thanks for your response. I tried following

List<IDName> behaviors = new ArrayList<IDName>();
            IDName behavior = new IDName();
            behavior.setId("6015559470583");
            behaviors.add(behavior);
            TargetingGeoLocation geoLocation = new TargetingGeoLocation();
            geoLocation.setCountries(Collections.singletonList("US"));
            geoLocation.setLocationTypes(Collections.singletonList("home"));
            Targeting targetingSpec = new Targeting();
            targetingSpec.setAgeMin(20L);
            targetingSpec.setAgeMax(40L);
            targetingSpec.setGeoLocations(geoLocation);
            targetingSpec.setBehaviors(behaviors);
            JsonObject fetchObjectsResults=facebookClient.fetchConnection("{ad-account-id}/reachestimate", JsonObject.class, Parameter.with("targeting_spec", targetingSpec));

with JsonObject.class and String.class but got JsonObject[“data”] is not a JsonArray error when call fetchConnection.

Best Regards

Norbert Bartels

unread,
Sep 11, 2017, 7:25:17 AM9/11/17
to RestFB

Try fetchObject with String as response object and provide the returning String. Perhaps we get some more information. ATM it looks like the returned JSON does not contain a data field.

Best Regards

Bilge

unread,
Sep 11, 2017, 10:39:58 AM9/11/17
to RestFB
Thanks, i found the problem. Querying reach estimate endpoint returns an individual Json object but JsonObject.java class expects a JsonArray. Search queries return array of json objects but querying ad endpoints mostly return individual json objects only. Returned object is like that:

{
   "data": {
      "users": 21000000,
      "estimate_ready": true
   }
}

Attaching a screenshot also.

Best regards
Screen Shot 2017-09-11 at 17.30.01.png

Norbert Bartels

unread,
Sep 11, 2017, 11:43:20 AM9/11/17
to RestFB
Ah, and there's the problem. The connection expects the JSON to contain a field `data` with a `JsonArray` inside, but here it is a `JsonObject`. So fetchConnection cannot work. I think we have to check this further and find a solution for this problem.

Thanks for the json,

Best Regards

Bilge

unread,
Sep 15, 2017, 4:12:18 AM9/15/17
to RestFB
Yes that is the problem. For our project we applied a quick and dirty fix in JsonObject.java

public JsonArray getJsonArray(int index) {
    Object o = get(index);
    if (o instanceof JsonArray) {
      return (JsonArray) o;
    }else if(o instanceof JsonObject){
      JsonArray myJsonArr=new JsonArray();
      myJsonArr.put(o);
      return (JsonArray) myJsonArr;
    }
    throw new JsonException("JsonArray[" + index + "] is not a JsonArray.");
  }

If it helps, i can create an issue on github also.

Best Regards,

Norbert Bartels

unread,
Sep 19, 2017, 6:11:09 PM9/19/17
to RestFB
Yes, it would be really great if you create an issue at github with this information. So you can track the progress easily ...

Thanks,

Norbert
Reply all
Reply to author
Forward
0 new messages