Python - How to filter on Facebook Marketing API AdAccount.get_lead_gen_forms?

1.8k views Asked by At

How can i define a filter for Facebook Marketing API for the method get_lead_gen_forms? I have tried passing some params, but nothing works.

I need to filter by date because the number of data is exceeding my limit. Here's my code (* the code already works, need help with the filter):

from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.adaccount import AdAccount

FacebookAdsApi.init(my_app_id, my_app_secret, my_access_token)
account = AdAccount('act_accountid')
leadgenforms = account.get_lead_gen_forms()

And here is the params that i have tried:

# attempt 1
params = {
        'time_range': {
            'since': date_start, 
            'until': date_end
            }
         }
leadgenforms = account.get_lead_gen_forms(params=params)

# attempt 2
 params = {
         'filtering': [{ 
             'field': 'created_time', 
             'operator': 'GREATER_THAN', 
             'value': date_start }]
         }
leadgenforms = account.get_lead_gen_forms(params=params)
1

There are 1 answers

3
Matteo On

You should filter for the field time_created instead of created_time.

From the section Filtering Leads of the doc here:

This example filters leads based on timestamps. Timestamps should be Unix timestamp.

curl -G \
  --data-urlencode 'filtering=[ 
    { 
      "field": "time_created", 
      "operator": "GREATER_THAN", 
      "value": 1516682744 
    } 
  ]' \
  -d 'access_token=<ACCESS_TOKEN>' \
  https://graph.facebook.com/v2.11/<AD_ID>/leads

So, in the second attempt example:

# attempt 2
 params = {
         'filtering': [{ 
             'field': 'time_created', 
             'operator': 'GREATER_THAN', 
             'value': date_start }]
         }
leadgenforms = account.get_lead_gen_forms(params=params)

you should use, ad date_start, a date in format of the unix timestamp:

number of seconds since the Unix Epoch

Hope this help