Package: pyfbook
A python package to easily collect data from Facebook Marketing APIInstallationOpen a terminal and install pyfbook package.
Your SSH key should be authorised on pyfook git repositoryFor github repository:pipinstallgit+ssh://git@github.com/[[repository_address]]/pyfbook.gitConfigurationDatabaseYou need to configure a database to use pyfbook.
Pyfbook required adbstreamobject 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 variablesFor Redshift:exportRED_[YOUR_PROJECT_NAME]_DATABASE="database_name"exportRED_[YOUR_PROJECT_NAME]_USERNAME="database_username"exportRED_[YOUR_PROJECT_NAME]_PASSWORD="database_password"exportRED_[YOUR_PROJECT_NAME]_HOST="database_host"exportRED_[YOUR_PROJECT_NAME]_PORT="database_port"For Azure:exportAZURE_[YOUR_PROJECT_NAME]_DATABASE="database_name"exportAZURE_[YOUR_PROJECT_NAME]_USERNAME="database_username"exportAZURE_[YOUR_PROJECT_NAME]_PASSWORD="database_password"exportAZURE_[YOUR_PROJECT_NAME]_HOST="database_host"exportAZURE_[YOUR_PROJECT_NAME]_PORT="database_port"Then you can init a dbstream object in your python code.For Redshift:# For RedshiftfrompyredimportRedDBStreamasDBStream# For AzurefrompyzureimportAzureDBStreamasDBStreamNAME="[YOUR_PROJECT_NAME]"CLIENT_ID=1datamart=DBStream(NAME,client_id=CLIENT_ID)Facebook ConnectorCredentialsAdd in your environment the following variablesexport[CHOOSEAFACEBOOKAPP_IDNAMEREFERENCE]=“YOUR_FACEBOOK_APP_ID”export[CHOOSEAFACEBOOKAPP_SECRETNAMEREFERENCE]=“YOUR_FACEBOOK_APP_SECRET”export[CHOOSEAFACEBOOKACCESSTOKENNAMEREFERENCE]=“YOUR_FACEBOOK_ACCESSTOKEN”Config FileYou need a .yaml config file to use pyfbook.You can create a default one in your working directory with the following python code :frompyfbook.FacebookReportimportFacebookFacebook()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 databaseYou need to execute the following python codefacebook=Facebook(config_path="[PATH_TO_YOUR_CONFIG_FILE]",dbstream=[YOURDBSTREAMOBJECT])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 useBy default, pyfbook use Graph API v8.0.
You can change that by adding a DEFAULT_GRAPH_API_VERSION environment variable.Get Facebook DataGet All Ads Accountsfacebook.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 ProcessPyfbook 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 FieldYour config field should look like the following:schema_name:pyfbook_raw_datareports:-name:accountlevel:accountfields:-impressions-spend-clicks-purchase-unique_clicks-reach-date_start-date_stop-account_idtime_increments:-day-lifetimebreakdowns:-countryYou can list as many report as you want.Results will be saved in [schema_name].[report_name]_[time_increment].Config DateYou can specify start and end dates in your python ‘get’ function : ‘YYYY-MM-DD’.NoteIf nothing is specified :If this reports was launched at least once before, start date will be :Time increment = day: The max end date saved for this report minus 1 dayOther Time increment: The max end date saved for this report minus 28 daysFirst time reportYou can specify a DEFAULT_START_DATE environment variableIf not start date will be current date minus 1 yearEnd date is current dateThis process is done at account level.