On February 4, 2004, Mark Zuckerberg introduced Facebook as a network for students. Today it is the largest social network in the world. The list of products and services that Facebook offers to its customers is expanding from year to year.
One of the domains the company develops is marketing. Millions of users have the opportunity to place their ads both within the network and outside of it. Also, a user-friendly API allows developers to easily integrate this product into their applications.
Our company has been actively using various products created by Facebook for a long time. Herb & Sky, Neoavantgarde, Zantepay and many other apps we have created for our clients use Facebook Sign-In and Social Network Integration.
However, we’ve met the Advertising API recently, working on the Slash Digital project. The main idea of this portal is providing its users with a convenient tool for managing their own ads. All one need to do is enter his/her Facebook account ID and then receive a complete ads manager!
In addition, the product roadmap says about adding another interesting opportunity – statistics viewing, which will be advantageous to differentiate this manager among similar products on the market.
Now let’s get deeper into Facebook Advertising API.
Step 1: A bit of theory
Ads on Facebook are paid advertisements that advertisers create for specific (promotional) purposes. The selected goal helps to achieve the desired results:
- Increase Product Recognition
- Attract attention to a product or service
- Encourage people to buy or use the advertised product
- Campaign is the basis of Facebook advertising because here you can define the ads targeting. Any campaign can have only one goal and include one or more ad sets.
- Ad Set – sets the rules for displaying ads. This can be performed through targeting settings. The advertiser determines the audience – the gender, age, and location of the people who will be shown ads. In addition, the budget and schedule should be set and the placements selected. Each group contains one or more advertisements.
- An ad is what a specific audience will see. Ads can include text, images, videos, and action buttons.
Facebook provides several tools for managing ads:
- Ads Manager is a handy advertisement manager. It provides the ability to create, edit and manage ads, as well as receive a variety of statistics.
- Business Manager is a free Facebook platform designed to simplify a company’s resource management (such as company business pages or ads accounts).
- Advertising API is a set of tools that allow developers to work programmatically with Facebook ads.
Step 2: Preparing to launch
If you are new to Facebook and just start developing Facebook products, then your first step on this path should be signing up at the developer portal – https://developers.facebook.com. Here you may find:
- current news
- information about company products
- required documentation
- answers to frequently asked questions about the development process
- application manager
The interaction with the API provided by Facebook is set via applications. In apps, a developer can select and configure the sets of products that will be used.
Creating an app is very simple – in “My Apps” drop-down menu, select “Add New App.”
For each new application, you must first define a few general settings (Settings Basic):
- the name of the application
- its logotype
- the address of the page where the application will be used
- its category
And do not forget about some more (Settings Advanced)
- create a list of allowed domain names in the Domain Manager
- set up additional security settings if needed.
Copy and save the App ID and App Secret fields of your application in advance. You’ll need them a bit later when working with the API.
- App ID is a unique identifier for your app
- App Secret is a unique secret key which serves as an identifier of apps queries to Facebook servers. Keep this key strictly secret.
Now it’s a high time to add Facebook products we want to work with to our app. For working with ads we will need two products:
- Facebook Login to handle authorization via Facebook
- Marketing API for direct ads managing
For Facebook Login, enable the Web OAuth Login option and specify a script URL that will handle responses from the authorization server (we will create the script later).
But the marketing API setup is a bit more complicated. Since working with ads is paid, Facebook took care of the testing possibilities. This API has 3 access levels:
- Development – Try the API work in test mode. At this level, it is possible to work with only one ad account.
- Basic – testing the API work. The number of ad accounts increased to 25.
- Standard – fully-featured API access.
All ad accounts for our app can be edited in the “Settings” section.
and you can create a test ad account in the “Tools” section.
Now we completed the app creation. For more info, visit https://developers.facebook.com/docs/apps/register.
Step 3: Implementation
Preempting a bit, I will say that in the process of developing a web app we will solve a few basic tasks:
- Authorizing via Facebook and receiving user data, in particular, we need a user ID to work with ads.
- Integration Facebook Ads API SDK for PHP for simplifying API work
- Developing client-side and server-side of web application
In my opinion, the best choice to handle this task is Laravel. This is one of the best PHP frameworks for today. So, we create a new project:
composer create-project –prefer-dist laravel/laravel facebook-api
To organize the authorization via Facebook, we will use the popular Socialite library. Go to the folder with the created project and run the command in the console:
composer require laravel/socialite
Then we just have to create two routes:
- For sending an authorization request to the social network.
- For handling the answer. Do not forget to specify this URL in the settings of your Facebook application (Valid OAuth Redirect URIs in the Facebook Login section).
By the way, this library also can help you to organize authorization through other popular social networks.
Working with the API
To simplify the organization of requests to Facebook Graph API, we will connect Facebook Ads API SDK for PHP library:
1. Add the corresponding entry to the composer.json file
2. Setup the library by executing the command in the console
In addition, we will create a set of classes containing the basic methods for working with ads elements:
|class AdvertisingApi()||The basic class, which is imitated by all other classes.|
|class CampaignsAPI()||Class to work with companies (Campaign).|
|class SetsAPI()||Class for working with group ads (Set)|
|class CreativesAPI()||Class for working with media (Creative).|
|class AdsAPI()||Class to work with advertisements (Ad).|
Server’s and client’s sides
Usually, PHP frameworks provide developers with tools to develop only the server side of an application. But it’s not about Laravel. Using the mechanism of presets, everyone can easily connect a set of libraries to the project and use them further:
- php artisan preset none
- php artisan preset bootstrap
- php artisan preset react
Using the sets of components that Laravel offers, as well as the classes that we have prepared earlier, you easily implement the collection of data forms, their validation and transfer to the API.
Remember, that only work on Development level is free. When switching to Basic or Standard, advertising costs will be charged.
Of course, the steps described could not totally disclose the full range of tools provided to the developer. But I’m sure they will be useful to anyone who dares to go on a long and definitely interesting journey called ‘Facebook Advertising API’.
Author: Andrey Kambur, Full stack developer