How to do complex queries using Facebook's TargetingSearch API?

898 Views Asked by At

I found some nice documentation for doing targeting searches via Facebooks api here:

https://developers.facebook.com/docs/marketing-api/targeting-search/

The code that I have been using successfully, which was copied directly from the above documentation, is as follows:

(This is after I set up authentication with the Facebook API of course.)

params = {
    'q': 'baseball',
    'type': 'adinterest',
}

resp = TargetingSearch.search(params=params)
print(resp) 

This works great and returns a list of topics to do with baseball, along with information about these topics like the audience size.

Now what I really want to be able to do is perform a more complex query, something like so:

return to me info on the audience of people who are in Santa Monica and who have an interest in baseball.

I want to do this because I want to be able to see the audience size for this group, but can't seem to find documentation or examples on how to do this or if it's even possible.

Something like this is what I would imagine:

params = {
    'q': 'baseball, santa_monica',
    'type': 'adinterest, adlocation',
}

resp = TargetingSearch.search(params=params)
print(resp)

But the above does not work.

Any tips are much appreciated.

1

There are 1 best solutions below

2
On BEST ANSWER

The API you are looking for is called Reachestimate. You can call it with the targeting you have and will return the expected data.

As Example, using as targeting spec:

{
    "publisher_platforms": ["facebook"],
    "facebook_positions": ["feed", "right_hand_column"],
    "device_platforms": ["mobile", "desktop"],
    "geo_locations": {
        "countries": [],
        "custom_locations": [],
        "zips": [],
        "cities": [{
            "key": "2421905"
        }],
        "regions": [],
        "country_groups": [],
        "geo_markets": []
    },
    "excluded_geo_locations": {
        "countries": [],
        "custom_locations": [],
        "zips": [],
        "cities": [],
        "regions": [],
        "country_groups": [],
        "geo_markets": []
    },
    "genders": [1, 2],
    "age_min": 18,
    "age_max": 65,
    "relationship_statuses": [],
    "connections": [],
    "excluded_connections": [],
    "friends_of_connections": [],
    "interests": [{
        "id": "6003087413192",
        "name": "Baseball"
    }]
}

the following API Call:

curl -i -X GET \
 "https://graph.facebook.com/v3.2/act_XXX/reachestimate?targeting_spec=%7B%22publisher_platforms%22%3A%5B%22facebook%22%5D%2C%22facebook_positions%22%3A%5B%22feed%22%2C%22right_hand_column%22%5D%2C%22device_platforms%22%3A%5B%22mobile%22%2C%22desktop%22%5D%2C%22geo_locations%22%3A%7B%22countries%22%3A%5B%5D%2C%22custom_locations%22%3A%5B%5D%2C%22zips%22%3A%5B%5D%2C%22cities%22%3A%5B%7B%22key%22%3A%222421905%22%7D%5D%2C%22regions%22%3A%5B%5D%2C%22country_groups%22%3A%5B%5D%2C%22geo_markets%22%3A%5B%5D%7D%2C%22excluded_geo_locations%22%3A%7B%22countries%22%3A%5B%5D%2C%22custom_locations%22%3A%5B%5D%2C%22zips%22%3A%5B%5D%2C%22cities%22%3A%5B%5D%2C%22regions%22%3A%5B%5D%2C%22country_groups%22%3A%5B%5D%2C%22geo_markets%22%3A%5B%5D%7D%2C%22genders%22%3A%5B1%2C2%5D%2C%22age_min%22%3A18%2C%22age_max%22%3A65%2C%22relationship_statuses%22%3A%5B%5D%2C%22connections%22%3A%5B%5D%2C%22excluded_connections%22%3A%5B%5D%2C%22friends_of_connections%22%3A%5B%5D%2C%22interests%22%3A%5B%7B%22id%22%3A%226003087413192%22%2C%22name%22%3A%22Baseball%22%7D%5D%7D&access_token=<user_token>"

will return:

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

Hope this help