I am using the below code to pull data for insights of every active account. The problem I am seeing is that the api is not fetching insights for action_values, video_thruplay for certain accounts. For example if I have 270 active accounts out of which 25 have these insights, the api is showing me results for only 5. I tried everything like reducing date_range, fields but it's not working. I tried pulling the same using the sync method get_insights and that is able to pull these insights for all accounts. I even played around with the limit parameters to no avail.
def wait_for_async_job(job):
for _ in range(TIMEOUT):
time.sleep(1)
job = job.api_get()
status = job[AdReportRun.Field.async_status]
if status == "Job Completed":
return job.get_result(params={"limit": 1000})
me = User(fbid='me')
platform_insights_async=[]
ad_accounts = me.get_ad_accounts(fields=['id', 'account_status'])
count = 0
TIMEOUT=300
for ad_account in ad_accounts:
if ad_account['status'] == 1 :
fields = ['account_id','account_currency','account_name','campaign_id','campaign_name','adset_id','adset_name','ad_id','actions','action_values','ad_name', 'objective','clicks','spend','video_thruplay_watched_actions','impressions']
params = {
'date_preset': 'last_28d',
'time_increment': 1,
'level': 'ad',
'breakdowns': ['publisher_platform'],
'filtering':[{'field':'ad.impressions','operator': 'GREATER_THAN','value': '0'},{'field':'action_type','operator': 'IN','value': ['purchase','post_engagement','omni_app_install']}],
'use_unified_attribution_setting':True,
}
ad_insights_platform = ad_account.get_insights_async(fields=fields, params=params)
result_cursor = wait_for_async_job(ad_insights_platform)
results = [item for item in result_cursor]
for insight in results:
try:
insight_action=str(insight['actions'])
except Exception as ex:
print(f"actions not available. {ex}")
insight_action=''
try:
insight_action_value=str(insight['action_values'])
except Exception as ex:
print(f"action_values not available. {ex}")
insight_action_value=''
try:
insight_thruplay=str(insight['video_thruplay_watched_actions']['action_type'=='video_view']['value'])
except Exception as ex:
print(f"video_thruplay_watched_actions not available{ex}")
insight_thruplay='0'
print(insight)
ad_pf_insight_dict={
"account_id":insight['account_id'],
"account_currency":insight['account_currency'],
"account_name":insight['account_name'],
"actions":insight_action,
"ad_id":insight['ad_id'],
"adset_id":insight['adset_id'],
"adset_name":insight['adset_name'],
"campaign_id":insight['campaign_id'],
"campaign_name":insight['campaign_name'],
"clicks":insight['clicks'],
"date_start":insight['date_start'],
"date_stop":insight['date_stop'],
"objective":insight['objective'],
"publisher_platform":insight['publisher_platform'],
"spend":insight['spend'],
"action_values":insight_action_value,
"video_thruplay_watched_actions":insight_thruplay,
"impressions":insight['impressions']
}
print(ad_pf_insight_dict)
platform_insights_async.append(ad_pf_insight_dict)