Get AdSet data using the Facebook Marketing API

In the last post we extracted got Campaign data and the next layer of the onion is the AdSet, here’s the structure of Facebook Ads – and in the next post we’ll go and get the Ads… oh and the one below this is Ad Creative, we’ll end things at the Ad Creative layer.

We have the CampaignData from the previous post, we now just need to iterate through each of the campaigns to get the AdAccounts.

# Import the Campaign Class from the Facebook python SDK
from facebook_business.adobjects.campaign import Campaign

# Empty list to hold the AdSet data
adSetData = list()

# Set the fields to be extracted
fields = ['account_id', 'campaign_id', 'name', 'optimization_goal', 'status', ]

for campaign in campaigns:
    adSets = Campaign(campaign['id']).get_ad_sets(fields=fields)
    for adset in adSets:
        adsetData.append({'adset_id': adset['id'],
                          'account_id': adset['account_id'],
                          'campaign_id': adset['campaign_id'],
                          'name': adset['name'],
                          'optimization_goal': adset['optimization_goal'],
                          'status': adset['status'],
        })

Same again, let’s tidy this up a little.

# Import the Campaign Class from the Facebook python SDK
from facebook_business.adobjects.campaign import Campaign

# Empty list to hold the AdSet data
adSetData = list()

# Set the fields to be extracted
fields = ['account_id', 'campaign_id', 'name', 'optimization_goal', 'status', ]

# Iterate through the campaigns
for campaign in campaigns:
    # Append the AdSet data to the list 
    [adsetData.append({'adset_id': adset['id'],
                          'account_id': adset['account_id'],
                          'campaign_id': adset['campaign_id'],
                          'name': adset['name'],
                          'optimization_goal': adset['optimization_goal'],
                          'status': adset['status'],
        }) for adset in Campaign(campaign['id']).get_ad_sets(fields=fields)]

That’s it for getting the AdSet data out, you just need to decide what you want to do with the data, if you want to throw it into Pandas it’s pretty simple:

import pandas as pd

# Create new Data Frame from the AdSet data
dfAdSets pd.DataFrame(adsetData)
print(dfAdSets.head())

To get the AdSet data straight from the AdAccount you can hit the AdSet Edge.

from facebook_business.adobjects.adaccount import AdAccount

# out holding list again
allAdSets = list()

# The fields we're extracting
adset_fields = ['account_id', 'campaign_id', 'name', 'optimization_goal', 'status', ]

# Loop through our AdAccount list
for adAcc in allAdAccounts:

    for adAcc in allAdAccounts[10:15]:

    [allAdSets.append({'adset_id':          adset['id'],
                           'account_id':        adset['account_id'],
                           'campaign_id':       adset['campaign_id'],
                           'name':              adset['name'],
                           'optimization_goal': adset['optimization_goal'],
                           'status':            adset['status'],
                           }) for adset in AdAccount(adAcc['id']).get_ad_sets(fields=adset_fields)]

# Let's push them into a Pandas DataFrame
dfAdSets = pd.DataFrame(allAdSets)

That’s about it, we’ll do some more interesting stuff later down the line and on the next post we’ll look at extracting the AdCreatives from the AdSets – I know right, exciting stuff 🙂