1

I want to show the Facebook ads that have been active on the last 7 days. The following code shows me a certain campaign but I need all the campaigns.

today = datetime.date.today()
start_time = str(today - datetime.timedelta(days=7))
end_time = str(today)

campaign = Campaign(campaign_id)
params = {
    'time_range': {
        'since': start_time,
        'until': end_time,
    },
    'fields': [
        AdsInsights.Field.campaign_id,
        AdsInsights.Field.campaign_name,
        AdsInsights.Field.impressions,
        AdsInsights.Field.unique_clicks,
        AdsInsights.Field.reach    
    ],
}
insights = campaign.get_insights(params=params)        

print(insights)

How can I show all Facebook campaigns that have been active on the last 7 days?

Tonechas
  • 13,398
  • 16
  • 46
  • 80

1 Answers1

0

On account level there is a method called get_campaigns. What you have to do is get the campaigns and fetch the insights for each of them.

campaigns = account.get_campaigns()
insights = {camp['id']:camp.get_insights(params=params)[0] for camp in campaigns}

Then you may want to sort those out those did not contain any information

insights = {key:insights for key, insight in insights if insight}