The following Python 3 code snippet demonstrates the usage of the Facebook Business API to retrieve the statistics for avertising camapaigns.

 

The sample code requires the Facebook Business library to be installed (pip install facebook_business).


#!/usr/bin/python3
# -*- coding: UTF-8 -*-

import os
from os.path import basename
import sys
from sys import platform
import json
import time
import requests
from datetime import timedelta, date
from datetime import datetime, timedelta
from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.adaccount import AdAccount
from facebook_business.adobjects.adset import AdSet
from facebook_business.adobjects.ad import Ad
from facebook_business.adobjects.adcreative import AdCreative

 

FACEBOOK_CLIENT_ID = '<your client id>'
FACEBOOK_CLIENT_SECRET = '<your client secret>'
FACEBOOK_ACCESS_TOKEN = '<your access token>'

 

def get_campaign_statistics_for_day(date, account_id):
  date_string = date.strftime("%Y-%m-%d")

  ad_account = AdAccount(account_id)

  fields = [
    'account_name',
    'campaign_name',
    'campaign_id',
    'adset_name',
    'adset_id',
    'ad_name',
    'ad_id',
    'impressions',
    'clicks',
    'spend',
    'actions',
    'reach'
  ]

  params = {
    'breakdowns':['device_platform'],
    'time_range': {
      'since': date_string,
      'until': date_string
    },
    'level': 'ad',
    'limit': 100000
  }

  ad_insights = ad_account.get_insights(fields, params)

  # do whatever you want with the statistics in JSON format
  print(ad_insights) 

  return ad_insights

 

if __name__ == '__main__':

  FacebookAdsApi.init(FACEBOOK_CLIENT_ID, FACEBOOK_CLIENT_SECRET, FACEBOOK_ACCESS_TOKEN)

  facebook_account_id = 'act_<your account id>'

  my_date = (datetime.now() - timedelta(1))

  get_campaign_statistics_for_day(my_date, facebook_account_id)