Connecting Facebook Business Manager with Python for Insights :

Most of the beginners would have already been in this confusion: Should I use Graph API or Marketing API .Facebook Documentation examples use both alternatively and which API should I use to connect Facebook and Python?

Vigneshwar Ilango
7 min readNov 7, 2019

I would right away give a very short description on the applications and use of the APIs that Facebook provide and primarily it would focus on connecting your Facebook Business Manager Account with Python to get data and Ad-Insights in Python.

Graph API:

As Facebook quotes, The Graph API is named after the idea of a “social graph” — a representation of the information on Facebook. It’s composed of:

  • nodes — basically individual objects, such as a User, a Photo, a Page, or a Comment
  • edges — connections between a collection of objects and a single object, such as Photos on a Page or Comments on a Photo
  • fields — data about an object, such as a User’s birthday, or a Page’s name. So you will be able to get information like considering a photo in your account and checking the people who have commented on it and their permitted information.

Example: Consider you are hosting an event and you want to know all the users attending the event , users who have declined the invitation , user comments , questions & posting back comments and such related fields through an API in Python.

Marketing API:

Marketing API is used to build marketing automation with Facebook’s advertising platform. There are three things you can do with Marketing API.

  1. Ad Insights : We would be able to get all the insights from the Ads/ Campaigns based on Ad level or Campaign level. Example : On Ads level you will be able to fetch information like how many unique clicks have been done, link-clicks, user’s engagement ,reach ,amount spent ,cost per click and other such insights.
  2. Audience Management: This is used to manage your first party data, find new customers, and re-engage existing customers through Custom Audience targeting on Facebook. Ex: Find how your ads are going, who have actively been viewing it ,change your ads target to get the new crowd who actually needs it based on insights.
  3. Ads Management: We provide a full set of APIs, from ad creation and management, reporting, and audience management.

Moving ahead, I will just write basic and simple procedure to get your Insights in Python. If you are familiar with these concepts and

if you are looking for the code, yes it’s here.

(Its based on OAuthV5.0.)

Lets get the connection done in 4 Steps !

To get Ad Insights from your Business Manager Account,
You would require the following things:

  1. Facebook Developer account and an app created using it.
  2. Basic settings configuration in the app.
  3. Requesting App permissions to fetch Insights.
  4. Creating Test users and user access tokens for them to access the information.

Step 1 :Creating Facebook Developer account and a trail App:

Go to https://developers.facebook.com/ and log in .Click on My Apps on the top left corner and click ‘Create App’ and give a name for your App and follow the procedures and finish it.

Facebook Developer Page
Creating First App

Step 2 :Configuring basic settings:

You need to fill these fields in basic settings in order to get permission ‘ads_management’ and ‘manage_pages’ which we require. Even if you want to create a sample app for just fetching data in python or to do some POC you would have to follow these steps. Go to Settings in the left pane and select Basic Settings.

Basic Setting Page

Fields to fill:

  1. Contact Email : Fill your contact email id and go to your Facebook account /mail to verify and connect to the app.
  2. Privacy Policy URL : Go to https://www.privacypolicies.com/ and get your policy link by following these steps.This field is a must-filled field as Facebook is holding tight to their security and they want to know what you want to do with it. Its is okay even if you are just testing it out. Just create one.
    There are quite a few steps and if you are planning to just get the data you can move with ‘choosing individual’ and selecting options which does not charge you. Don’t worry about the policy you create, just make sure you use this only for fetching some Insights and Analytics.
    In the end you will get a link , copy and paste the link in the ‘Privacy Policy URL’ tab.
  3. App Icon (1024 x 1024): Download any 1024 x 1024 or 512 x 512 image or your own logo in this format and upload it.
  4. Business Verification: Click on ‘Go to Verification’ and select your ‘Business Manager Account’. This is important and if you are a Developer/ Analyst who is trying to fetch data from your Business page, make sure you get ‘admin’ access from your Business Manager Admin or let the Admin follow these steps and get you user access token . Even if you are added in your Business Manager account and you are not an admin (I don’t know if it will be available for any other role but it worked only for ‘admin’ when I tried ), you wouldn’t be able to see your business account in the option.
    For others , if you are new to this, create your own new Business Manager Account and add it here.( You can add campaigns and ads in the UI itself or through API as well. )
  5. Platform: (optional) : If you are trying to fetch in Jupyter Notebook or any Python IDE ignore this. If you are planning to automate it as a part of your App / Website then you would need to add your website domain URL / App and get it verified.

Now click on Save changes.

Step 3: Requesting App permissions:

  1. Go to ‘App Review’ in the left pane and click on ‘Permissions and Features’. You will find a list of permissions on the main pane. Now we would require ‘ads_management’,’ads_read’(Optional) and ‘manage_pages’.
Page showing different permissions and you would find ‘Request’ to its right.

2. Go to each of the permissions, click on ‘Request’ and it will change to ‘Continue’. Click on it.

3. Scroll down and you will find Now you will find ‘Tell us how you will use ads_management’. Click on ‘Complete form’ or ‘->’ icon to its right.

4. In this first box describe what is the purpose (Example: I would require to POC by fetching the data in Business Manager account to Python).

App Verification Details Page
Drop your video and description here.

And it is mandatory to upload the ‘VIDEO’. Just describe what you are going to do by getting the permissions. Even if you are just doing some POC or want to check if any logic works. Just tell the same in the video. If you are using Linux I would suggest ‘Simple Screen Recorder’ which you can find in your Ubuntu software.In Windows, you know it. There are tons of apps which are proper for recording a good video.

5 . Upload the video and click on Submit.

6. Do the same procedure and upload the same video for manage_ads as well. Requesting Ads_management will automatically require manage_ads to be completed. After you get all blue ticks/completed stage , go for ‘Submit for Review’ and it will be approved within 24 hrs maximum. (This would be probably within an hour and don’t worry about this )

Then you can see that you will get live status near all this permissions.

All your permissions are now live and ready to use

Step 4: Creating test users and user access tokens:

Go to ‘Roles’ in the left pane and ‘Test Users’. Create ‘Test User’ and ‘Authorise the test user’. Now in Login Permissions, add ‘ads_management’ and ‘manage_pages’ permissions to the user and a new test user will be added. Click on ‘Edit’ and you will find ‘get an access token for this test user ’ and this is the user token you will be using in your python code.

Creating Test User
Getting User Access Token

Poof! Done! Now all you have is to code up your logic in Python. You will find your app id and app secret key in your Settings-> Basic.

Python Code:

You would require ‘facebookads’ and ‘facebook_business’ libraries. Follow the documentation or Google to install them.
You can get ‘Facebookads by cloning this and installing it:

  1. git clone https://github.com/facebook/facebook-python-ads-sdk.git
  2. pip install facebookads
  3. pip install facebook_business

You can get the code here and it is very self explanatory. The code uses OAuthV5.0 and Python 3.6.7. Please excuse me for not writing a proper code. I just wanted to help people jump the hurdle till the connection is done in Python.

The only problem you might face is in ‘get_insights_async’ function . Since it’s an async function it will take time to load the data. The code here uses a function for waiting so it wouldn’t go through the problem. But in case if you are trying it in some other scenario make sure you wait till the whole data is pulled and synced.

“Patience is bitter, but its fruit is sweet.”
Aristotle

Please feel free to connect on any further discussions:
LinkedIn : https://www.linkedin.com/in/vigneshwarilango/
Gmail: mr.vigneshwarilango@gmail.com

Regards,
Vigneshwar Ilango

--

--

Vigneshwar Ilango

I love data and can transform your data into profitable strategies. Open for opportunities. LinkedIn: https://www.linkedin.com/in/vigneshwarilango/