从 facebook 广告中获取所有有效的活动 api - 如何设置过滤器

Get all active campaigns from facebook ads api - how to set the filter

我想深入了解我在 Facebook 广告上的所有活跃活动 运行。我设法在我的帐户上使用 FacebookAdsApi 获取所有广告系列,但我无法使用过滤器,因此我只能获取状态为 "ACTIVE" 的广告系列。

到目前为止,这是我的代码:

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

我尝试将以下行添加到 params:

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

导致此错误信息:

"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."

我可以毫无问题地从我的帐户 (115) 中获取所有广告系列,但目前只有 10 个活动广告系列,所以我猜我的过滤器有误?

这是见解查询的常见问题。当处理大量数据(大量活动、大量天数或两者)时,您很容易 运行 进入描述的错误。

FB docs 说:

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.

在您的查询中,问题很可能是由从 2015 年初开始获取数据引起的。对于初学者,我建议使用例如 date_preset=last_30_days(预设日期间隔应该更快地返回)并从那里继续,也许通过将您的见解加载逻辑拆分为更多间隔。

另一种选择是减小页面大小 (limit),这也可能导致此问题。

或最终解决方案 - 使用 async jobs 加载见解。这可以防止 FB 超时,因为查询 运行 是异步的,您检查作业状态并仅在完成时加载数据。