Get all active campaigns from facebook ads api - how to set the filter
<p>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. </p>

<p>Here is my code so far:</p>

<pre><code>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)
&gt;&gt;&gt; 115
</code></pre>

<p>I tried to add the following line to <code>params</code>:</p>

<pre><code>filtering': [{'field': 'campaign.effective_status','operator':'IN','value':['ACTIVE']}]
</code></pre>

<p>which results in this error-msg:</p>

<pre><code>"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."
</code></pre>

<p>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?</p>

<p>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.</p>

<p><a href="https://developers.facebook.com/docs/marketing-api/insights/overview/v2.7" rel="nofollow">FB docs</a> say:</p>

<blockquote>
  <p>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.</p>
</blockquote>

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

<p>Another option is reducing page size (<code>limit</code>), which can also cause this issue.</p>

<p>Or the ultimate solution - use <a href="https://developers.facebook.com/docs/marketing-api/insights/async/v2.7" rel="nofollow">async jobs</a> 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.</p>

python facebook-graph-api facebook-ads-api