Python Tutorial 45: Meta Ads Campaign Creation and Update Using Facebook Business and Graph API

Along with user data privacy being cared for and protected more than ever, Meta has pledged to build an advertising ecosystem that relies on fewer data. It looks like the social media giant looks to push forward with its privacy agenda. Recently Meta has announced more removal of Facebook Ads audience targeting options across four categories. Plus the stress and impact from industrial players like iOS upgrade, I believe platform players just would have more new changes that create more challenges for marketers to reach out to customers accurately like before. 

However, the fact is platforms are shifting the data protection liability to the brands and marketers. For continuing monetizing the inventory without user privacy limitations, platforms are providing more mature marketing APIs for brands to adopt and leverage to reach out to target audiences even much better than before. And the brands and marketers build the target feature themselves at this time. This article is about how to leverage Meta APIs to create and update your Facebook, Instagram, Messenger, Whatsapp ads.

meta ads

Along with user data privacy being cared for and protected more than ever, Meta has pledged to build an advertising ecosystem that relies on fewer data. It looks like the social media giant looks to push forward with its privacy agenda. Recently Meta has announced more removal of Facebook Ads audience targeting options across four categories. Plus the stress and impact from industrial players like iOS upgrade, I believe platform players just would have more new changes that create more challenges for marketers to reach out to customers accurately like before. 

However, the fact is platforms are shifting the data protection liability to the brands and marketers. For continuing monetizing the inventory without user privacy limitations, platforms are providing more mature marketing APIs for brands to adopt and leverage to reach out to target audiences even much better than before. And the brands and marketers build the target feature themselves at this time. This article is about how to leverage Meta APIs to create and update your Facebook, Instagram, Messenger, Whatsapp ads.

This is a new series of Python tutorials regarding marketing platform APIs to manage ads. I’ll release content from ads creation, ads automation, customization to ads optimization. You can learn using Facebook business modules, Graph API to create and update ads campaigns in this Meta ads ecosystem. They are Facebook, audience network, Instagram, Whatsapp, Messenger.

Python Modules and Tools: Facebook Business, Facebook Graph API

Type of social channel: Take Facebook for the example

Type of ads: Take video ads for the example

Meta Ads: Why Facebook Business Module and Graph API is the right way to go

meta ads

There are three main reasons why the future is the marketing API world, which includes Meta ads.

User Data Privacy Restrict

User privacy and data protection have been in the spotlight and it doesn’t return back to the life we had before. In a way, it makes sense to protect users’ data. So from a platform perspective, less accurate targeting options and less precise tracking have become a fact. It’s because its data protection liability directly reflects on the features and functions given to brands and marketers.

Thus, platforms are providing opener and more mature marketing APIs to brands, which shifts the liability to brands making decisions on how to use and collect user data. Meanwhile, brands and markets can have a way out to continue operating ads in an efficient approach.

Ads Automation

Tons of 3rd party advertising operation platforms out there provide digital ads automation. Basically, these platforms use marketing APIs to build easy-to-use SaaS platforms for marketers. The importance of ads automation is no doubt as more variables in the market impact the performance of the ads, so it needs more variant versions to engage with the audiences, by optimizing bidding, ad creatives. And people need automated reporting to support making proper decisions. 

In this whole process, it’s basically impossible to manually manage because of workload and accuracy considerations.

Ads Customization

Meta ads ecosystem continues to narrow down the targeting options on the platform and it will continue of course. It means brands have more challenges on the advertising ROI. But marketing API doesn’t have any limit on this aspect, it’s because brands lead and make decisions where to collect user data, what data to use. For example, you can integrate with the stock market pricing database to create dynamic customized ads based on a specific symbol pricing up or down. Or you can partner with clearbit to run lead generation campaigns in the Meta ads ecosystem like running ads in Linkedin.

Through marketing APIs, people can have a feeling of having more choices than just being sucked on the platform options.

Meta Ads – Facebook Business Module in Python

The Facebook business module provides plenty of parameters and methods to connect with Meta Graph API and interact with business managers and ads accounts. 

The module installation is straightforward and easy. In Macbook, we open the terminal and input the command pip3 install facebook-business with connecting the internet.

In the Python script, here is a list of modules we need to import:

from facebook_business.adobjects.adaccount import AdAccount

from facebook_business.adobjects.campaign import Campaign

from facebook_business.api import FacebookAdsApi

from facebook_business.adobjects.adset import AdSet

from facebook_business.adobjects.targetingsearch import TargetingSearch

from facebook_business.adobjects.targeting import Targeting

from facebook_business.adobjects.adimage import AdImage

from facebook_business.adobjects.adcreative import AdCreative

from facebook_business.adobjects.ad import Ad

Graph API credential: App ID, Page ID, Access_token, Ads account ID

First thing first, we need to create a Meta developer account and a new app in the Meta developer platform.

In the new app, we need to add the Facebook login and marketing API into the application. And please go to the basic setting on the left-hand side to check if you are using the latest API 13.0 version. Last but not least, please copy and paste the app ID, and put it in a place first. You will need it soon.

meta ads

Then, we need to go to the tools on the top menu and click into the Graph API explorer. In this section of the right-hand side, we select the Facebook app we just created above. And then it’s necessary to select which Facebook page you will use in the ads campaign. Here is a tip it’s best you use your ads account email address to sign up for the developer account. Thus, here it would give you the pages directly.

meta ads

In the permission menu, we need to select all modules relevant to the ads and page management, which can facilitate us to build the application later on.

Now we can generate the access_token by clicking the blue button on the right-hand side. The token would be short-lived and would expire every 1 hour. So you can generate it when you like to activate your application.

Last but not least, we can generate the page id here as well inputting me?field=id in the bar on the top. Regarding the ads account ID, you can either output here or copy the one in your ads account interface. But please remember to add act_ before the ads account ID

All in all, now you can create some variables and input relevant values before creating a campaign. For the campaign id, ads set id, creative id, and ads id, you would grab those later on.

  • access_token = ‘ ’
  • app_id =  '
  • id = ''
  • campaignId = ' '
  • adsSET_id = ' '
  • adsCreativeId = ' '
  • adsId = ' '

Authenticate the access to token and state the API version

Before any coding, we need to authenticate our token to access the API and implement the activities we command. Here I will suggest adding the API version as well as the Facebook business module sometimes can’t detect which version you are using. This is a tiny step to avoid errors later on.

FacebookAdsApi.init(access_token=access_token, api_version='v13.0')

Campaign creation: objective

AI-driven is one of the most powerful ads features on Facebook. And selecting the campaign object in the first step is critical to start the ads in the right direction. For more details, please check out this article about sales vs click-driven campaigns.

Sales vs Clicks Driven Campaign In Facebook Advertising

In the API section, there are 13 objective options and we have to select one as well as create a campaign in the Facebook ads manager. The default of the campaign objective is none.

CANVAS_APP_ENGAGEMENT

CANVAS_APP_INSTALLS

EVENT_RESPONSES

LOCAL_AWARENESS

MOBILE_APP_ENGAGEMENT

MOBILE_APP_INSTALLS

NONE (default)

OFFER_CLAIMS

PAGE_LIKES

POST_ENGAGEMENT

VIDEO_VIEWS

Then, we need to add the parameters and set up the campaign as we like. For the campaign creation statement and methods, it’s AdAccount().create_campaing(). For more parameters, we can use, please refer to the meta developer API documentation.

fields = [

]

params = {

  'name': 'python testing',

  'objective': 'LINK_CLICKS',

  'status': 'PAUSED',

  'special_ad_categories': [],

}

print(AdAccount(id).create_campaign(

  fields=fields,

  params=params,

))

Once you command these codes, you can get the campaign id and it’s for you to update, read and delete in the future. It’s very critical to implement automation and ads customization.

Ads set creation: bidding, targeting, optimization goal, and billing event

The ad set is a vital step as it deals with bidding strategy, media buy approach, customer data, targeting, GEO, budget, etc.

In the parameters, we need to add campaign_id and access_token, as we need to tell the API which campaign this ad set would be added on. For the ad set statement and method, we can use AdAccount().create_ad_set()

For more parameter information, please refer to the meta developer API documentation. Once you command these codes, you can get the ad set id

Take video ads for the example in ads creative creation

In the ads creative section, you can code to create the type of ads, input the ads materials and select the ads placement based on the type of ads you are going to run.

Basically, there are 11 ads options you can select, which are single image ads, post ads, page like ads, story ads, slideshow ads, app ads, carousel ads, dynamic ads, Instagram ads, leads ads, and videos.

For Facebook dynamic ads strategy, please check out this article for more details.

Facebook Ads: How to Optimise Dynamic Product Ads for Sales & Revenue

Apart from the parameters of the ad to set up your ads and access token, you can need to add the ad set id and page id to tell. It tells which page you will use to run ads and which ad set the creative would be added on.

For the video ads element settings, we need to add the thumbnail image, description, videos, call-to-action, message, etc. For more parameter details, please refer to the meta developer API documentation.

Creating an ad creative is not done yet, because Facebook puts it as a mission complete on the creative message and placement confirmation. We need to create the ads using the other set of codings.

Update campaign, ad set, and ads

Creating is the earliest stage of using meta APIs, as the most beautiful thing of using API is to operate and optimize. I’ll release other articles to share different approaches. Here today is to share how to update a single campaign, ad set, and ads. Once you pick up the logic, it’s very easier to update batches you would learn from my articles in the future.

In the update section, the method is api_update(), which is different from the creation stage method.

Full Python Script of Using Facebook Business and Graph API to Create Ads Campaigns

If you are interested in the full Python script of Python Tutorial 45: Meta Ads Campaign Creation and Update Using Facebook Business and Graph API, please subscribe to our newsletter by adding the message “Python Tutorial 45”. We would send you the script immediately to your mailbox.

I hope you enjoy reading Python Tutorial 45: Meta Ads Campaign Creation and Update Using Facebook Business and Graph API. If you did, please support us by doing one of the things listed below, because it always helps out our channel.

  • Support my channel through PayPal (paypal.me/Easy2digital)
  • Subscribe to my channel and turn on the notification bell Easy2Digital Youtube channel.
  • Follow and like my page Easy2Digital Facebook page
  • Share the article to your social network with the hashtag #easy2digital
  • Buy products with Easy2Digital 10% OFF Discount code (Easy2DigitalNewBuyers2021)
  • You sign up for our weekly newsletter to receive Easy2Digital latest articles, videos, and discount code on Buyfromlo products and digital software
  • Subscribe to our monthly membership through Patreon to enjoy exclusive benefits (www.patreon.com/louisludigital)