我正在使用 Insights API 查询多个营销活动数据,并且我还想检索 daily_budget 和 Lifetime_budget 信息,但 Insights API 不提供这些字段。
我知道我可以向
那么,有没有最好的方法来检索多个广告系列的预算信息?
使用批量请求,这可以帮助您避免速率限制。 下面是我从基于类的函数之一分享的 python 示例。下面我将向您展示如何在初始化 Facebook 广告 API 后获取我帐户的所有广告活动。请注意,self.my_account 是来自 facebook_business.adobjects.adaccount 的 AdAccount 类的实例
campaigns_to_return = []
next_campaign_page = True
def handle_campaign_response(response):
data = json.loads(response._body)
paging = data.get('paging', False)
nonlocal next_campaign_page
campaigns_data.extend(data['data'])
if paging and 'next' in paging:
next_campaign_page = paging['next']
else:
next_campaign_page = False
campaign_batch = FacebookAdsApiBatch(api=FacebookAdsApi.get_default_api())
campaign_fields = ["daily_budget", "lifetime_budget", "budget_remaining", ] ## add any other fields you want here
campaign_batch.add_request(
self.my_account.get_campaigns(
params={"limit":2500,'fields': campaign_fields},
batch=campaign_batch,
success=handle_campaign_response,
failure=lambda response: print(f'Failed to fetch campaigns: {response.error()}')
)
)
campaign_batch.execute()
while next_campaign_page:
try:
camp_response = requests.get(next_campaign_page)
res_json = camp_response.json()
if 'data' in res_json:
campaigns_data.extend(res_json['data'])
if 'paging' in res_json and 'next' in res_json['paging']:
next_campaign_page = res_json['paging']['next']
else:
next_campaign_page = False
except Exception as e:
raise e