如何在Facebook Ads API调用中避免“请求包含太多数据”

时间:2016-12-20 00:34:26

标签: facebook-graph-api

我使用Facebook Ads Api在一个广告帐户中调用adas统计信息:

GET graph.facebook.com/v2.7/act_xxxx/insights?level=ad

但它返回错误:

Please reduce the amount data you request.

我想知道如何解决它?感谢。

1 个答案:

答案 0 :(得分:1)

问题出现了,因为您在一个广告帐户下查询所有广告,但可能包含太多广告。

解决方案:首先尝试在广告帐户下查询广告系列,然后仅查询在特定时间范围内具有洞察力(统计信息)的广告系列。

Python API如下所示:(start_dt,end_dt,account_id是你应该输入的内容)

        total_insights = []

        # Get campaign first, then get ad insights
        account = AdAccount("act_" + str(fb_account_id))

        valid_campaigns = []
        campaigns_iterator = account.get_campaigns(
            fields=[
                Campaign.Field.id,
            ]
        )
        for campaigns in self.generate_batches(
            campaigns_iterator,
            BATCH_LIMIT,
        ):
            api_batch = self.api.new_batch()
            for campaign in campaigns:
                params = {
                    'time_range': { 'since': start_dt, 'until': end_dt },
                    'level': 'campaign',
                    'filtering': '[{field:"campaign.impressions",operator:"GREATER_THAN",value:0},]',
                    'fields': ['impressions']
                }
                campaign_insights = campaign.get_insights(params=params)
                if campaign_insights:
                    valid_campaigns.append(campaign)
            api_batch.execute()

        for campaign in valid_campaigns:
            params = {
                'time_range': { 'since': start_dt, 'until': end_dt },
                'level': 'ad',
                'filtering': '[{field:"ad.impressions",operator:"GREATER_THAN",value:0},]',
                'fields':['spend','unique_social_clicks', 'inline_link_clicks', 'impressions', 'actions', 'relevance_score', 'ad_id', 'adset_id', 'campaign_id'],
            }
            insights_iterator = campaign.get_insights(params=params)

            # query insights later
            for ad_insights in self.generate_batches(
                insights_iterator,
                BATCH_LIMIT,
            ):
                api_batch = self.api.new_batch()
                total_insights.extend(ad_insights)
                api_batch.execute()