I have code pulling Facebook Campaigns, Ads, and Insights from Business Account in Facebook. I sometimes have an issue where I get an AD_ID from my Insights return that does not show up in my Ads return and I was going to try to transfer all my calls to batch requests to see if I was getting limited somehow in regular calls.
I am having a hard time understanding the docs from Facebook to know how to translate my regular calls into a batch request and see the results.
Current working code:
FacebookAdsApi.init(<app_id>,<app_secret>,<access_token>)
account = AdAccount(f"act_{<account_id>}")
campaigns = account.get_campaigns(fields=[Campaign.Field.id,Campaign.Field.name],params={})
campaigns = pd.DataFrame([x for x in campaigns])
# Do similar for account.get_ads() and account.get_insights()
Batch attempt:
session = FacebookSession(<app_id>,<app_secret>,<access_token>)
api = FacebookAdsApi(session)
FacebookAdsApi.set_default_api(api)
account = AdAccount(f"act_{<account_id>}")
api_batch = api.new_batch()
api_batch.add_request(
account.get_campaigns(
fields=[Campaign.Field.id,Campaign.Field.name],
params={},
batch=api_batch
),
account.get_ads(),
account.get_insights()
)
api_batch.execute()
First, is the above correct? And if not, what am I missing?
Second, how do I get the data out? Should I be adding success/failure parameters in batch request and adding my dataframe transform as a success function?