I'm querying an Insights endpoint with the Facebook Python SDK and have a hard time making the response I get work with Python and, subsequently pandas. I do the following call:
account = AdAccount('act_id')
params = {
'fields': [Insights.Field.impressions,Insights.Field.clicks,Insights.Field.actions,Insights.Field.spend],
'breakdowns': [Insights.Breakdown.hourly_stats_aggregated_by_advertiser_time_zone],
'time_range': {
'since': 'start',
'until': 'end',
},
'action_attribution_windows': ['7d_click'],
}
result = account.get_insights(params=params)
print (result)
which returns data like so:
[<Insights> {
"actions": [
{
"7d_click": 600,
"action_type": "custom_event_xyz",
"value": 50
},
{
"7d_click": 600,
....
}
],
"clicks": 1500,
"date_start": "start",
"date_stop": "end",
"hourly_stats_aggregated_by_advertiser_time_zone": "00:00:00 - 00:59:59",
"impressions": "60000",
"spend": 60
}, <Insights> {
....
]
While putting the data excluding the actionsdata into a pandas DataFrame, I do not manage to properly flatten the actions data so that the aggregation level is consistent (i.e. the "actions" keys as column headers). Having checked online and also on Stackoverflow, loading the json with python and processing it accordingly then as well as reading it with pandas are both options that do not work.
Summing up I don't see how I can elegantly mine the deeper nested parts of the response and easily make the contents compatible with the rest.