skip to Main Content

I would like to get insights of all my active campaigns running on Facebook Ads. I manage to get all campaigns with FacebookAdsApi on my account but i am not able to use a filter so i only get campaigns with the “ACTIVE” status.

Here is my code so far:

from facebookads.api import FacebookAdsApi
from facebookads.objects import AdAccount, Ad, Insights, AdUser
import datetime

my_app_id = 'xxx'
my_app_secret = 'xxx'
my_access_token = 'xxx'
FacebookAdsApi.init(my_app_id, my_app_secret, my_access_token)

me = AdUser(fbid='me')
my_accounts = list(me.get_ad_accounts())
my_account = my_accounts[0]

fields = [
    Insights.Field.campaign_name,
    Insights.Field.spend
]

params = {
    'time_range': {'since': str(datetime.date(2015, 1, 1)), 'until': str(datetime.date.today())},
    'level': 'campaign',
    'limit': 1000
}

insights = my_account.get_insights(fields=fields, params=params)
print len(insights)
>>> 115

I tried to add the following line to params:

filtering': [{'field': 'campaign.effective_status','operator':'IN','value':['ACTIVE']}]

which results in this error-msg:

"error_user_msg": "The reporting data you are trying to fetch has too many rows. Please use asynchronous query or restrict amount of ad IDs or time ranges to fetch the data."

I can get all campaigns from my account (115) without any issue and there are only 10 active campaigns at the moment so i guess my filter is wrong?

2

Answers


  1. This is a common issue with insights queries. When working with lot of data (lot of campaigns, lot of days or both) you can easily run into described error.

    FB docs say:

    There is no explicit limit for when a query will fail. When it times out, try to break down the query into smaller queries by putting in filters like date range.

    In your query the issue is most likely caused by fetching data from beginning of 2015. For starters I suggest using for example date_preset=last_30_days (preset date intervals should be returned faster) and proceed from there, maybe by splitting your insights loading logic into more intervals.

    Another option is reducing page size (limit), which can also cause this issue.

    Or the ultimate solution – use async jobs for loading insights. This prevents FB from timing out, because the query runs asynchronously and you check for the job status and load data only when it’s done.

    Login or Signup to reply.
  2. Filtering is for values mostly. In your case you want to get the list of only active campaigns, that can be done very easily with the following modification to your code line:

    Change your params with this below:

    params = {
    'time_range': {'since': str(datetime.date(2015, 1, 1)), 'until': str(datetime.date.today())},
    'level': 'campaign',
    'limit': 1000,
    'effective_status': ['ACTIVE']  }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search