Skip to main content

Pyfbook Documentation

A python package to easily collect data from Facebook Marketing API

Installation

Open a terminal and install pyfbook package. Your SSH key should be authorised on pyfook git repository

For github repository:

pip install git+ssh://git@github.com/[[repository_address]]/pyfbook.git

Configuration

Database

You need to configure a database to use pyfbook. Pyfbook required a dbstream object to work properly.

You can use pyred, pyzure or pybigquery depending on your database provider.

To define a dbstream object, you need to add in your environment the following variables

For Redshift:

export RED_[YOUR_PROJECT_NAME]_DATABASE="database_name"
export RED_[YOUR_PROJECT_NAME]_USERNAME="database_username"
export RED_[YOUR_PROJECT_NAME]_PASSWORD="database_password"
export RED_[YOUR_PROJECT_NAME]_HOST="database_host"
export RED_[YOUR_PROJECT_NAME]_PORT="database_port"

For Azure:

export AZURE_[YOUR_PROJECT_NAME]_DATABASE="database_name"
export AZURE_[YOUR_PROJECT_NAME]_USERNAME="database_username"
export AZURE_[YOUR_PROJECT_NAME]_PASSWORD="database_password"
export AZURE_[YOUR_PROJECT_NAME]_HOST="database_host"
export AZURE_[YOUR_PROJECT_NAME]_PORT="database_port"

Then you can init a dbstream object in your python code.

For Redshift:

# For Redshift
from pyred import RedDBStream as DBStream
# For Azure
from pyzure import AzureDBStream as DBStream


NAME = "[YOUR_PROJECT_NAME]"
CLIENT_ID = 1

datamart = DBStream(
    NAME,
    client_id=CLIENT_ID
)

Facebook Connector

Credentials

Add in your environment the following variables

export [CHOOSE A FACEBOOK APP_ID NAME REFERENCE] = “YOUR_FACEBOOK_APP_ID”
export [CHOOSE A FACEBOOK APP_SECRET NAME REFERENCE] = “YOUR_FACEBOOK_APP_SECRET”
export [CHOOSE A FACEBOOK ACCESSTOKEN NAME REFERENCE] = “YOUR_FACEBOOK_ACCESSTOKEN”

Config File

You need a .yaml config file to use pyfbook.

You can create a default one in your working directory with the following python code :

from pyfbook.FacebookReport import Facebook

Facebook()

Do not forget to update the schema_name. This is where everything will happen in you database!

Create a app_system_user object in your database

You need to execute the following python code

facebook = Facebook(config_path="[PATH_TO_YOUR_CONFIG_FILE]", dbstream=[YOUR DBSTREAM OBJECT])
facebook.create_app_system_user(
    app_id_name="APP_ID NAME REFERENCE",
    app_secret_name="APP_SECRET NAME REFERENCE",
    access_token_name="ACCESSTOKEN NAME REFERENCE"
 )

Specify Graph API version you want to use

By default, pyfbook use Graph API v8.0. You can change that by adding a DEFAULT_GRAPH_API_VERSION environment variable.

Get Facebook Data

Get All Ads Accounts

facebook.get_all_ad_accounts()

It will create a table with the list of ad accounts you have access to.

You can update the “active” field in this table to choose which account you want to recover the data.

Get Facebook Marketing Data - General Process

Pyfbook uses asynchronous reports to get data.

facebook.get(
    report_name=None,
    time_increment=None,
    start=None, end=None,
    list_account_ids=None
)

When you execute this python code, it will launch and fetch asynchronous reports.

Config Field

Your config field should look like the following:

schema_name: pyfbook_raw_data
reports:
  - name: account
    level: account
    fields:
      - impressions
      - spend
      - clicks
      - purchase
      - unique_clicks
      - reach
      - date_start
      - date_stop
      - account_id
    time_increments:
      - day
      - lifetime
    breakdowns:
      - country

You can list as many report as you want.

Results will be saved in [schema_name].[report_name]_[time_increment].

Config Date

You can specify start and end dates in your python ‘get’ function : ‘YYYY-MM-DD’.

This process is done at account level.

Metadata

Release files for pyfbook 0.3.20

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyfbook 0.3.20
File Size Uploaded Provenance Actions
pyfbook-0.3.20.tar.gz 12.8 kB Without provenance Details for pyfbook-0.3.20.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pyfbook 0.3.20
File Interpreter ABI Platform Actions
pyfbook-0.3.20-py3-none-any.whl Python 3 none any Details for pyfbook-0.3.20-py3-none-any.whl

Total release size: 28.7 kB

Release files / pyfbook-0.3.20.tar.gz

Download URL pyfbook-0.3.20.tar.gz
Size 12.8 kB
Tags Source
SHA-256 checksum
How to use checksums
ff945332390392ea9e521879524023d170b7f308b6c702afa79ce3699bcc6d13
BLAKE2b-256 checksum
How to use checksums
2f9c12b8f02ad9c4150b53bc227f65f3015101ebe5370c4d90da146d60a2bee1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.2

Release files / pyfbook-0.3.20-py3-none-any.whl

Download URL pyfbook-0.3.20-py3-none-any.whl
Size 15.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5d25a57ddef4e2fda1ce4fc0d600864e3b9eb19c70e7f4054526b7cb89b0adcc
BLAKE2b-256 checksum
How to use checksums
aa6bb98de87c8117d2958083391dda7fc36872969c368cfd4b13d249f084b8ba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.2

Security for pyfbook 0.3.20

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page