Filtering statistics from apache facebook using python sdk

I am trying to use Facebook ads-api to get data about advertising accounts / campaigns / etc. over a period of time.

So far I have managed to get general information (added below) using the official python sdk, but I cannot figure out how to insert the time filter condition.

The answer is probably in the Filtering Results section, but I don't understand how to translate what they do there to python ... https://developers.facebook.com/docs/reference/ads-api/adstatistics /v2.2

I would really appreciate any help you can provide,

Thank!


This is the relevant module (I think) from the official python sdk project: https://github.com/facebook/facebook-python-ads-sdk/blob/master/facebookads/objects.py

My current code:

from facebookads.session import FacebookSession
from facebookads.api import FacebookAdsApi
from facebookads import objects
from facebookads.objects import (
AdUser,
AdCampaign,
)

my_app_id = 'APP_ID'
my_app_secret = 'AP_SECRET'
my_access_token = 'ACCESS_TOKEN'
my_session = FacebookSession(my_app_id, my_app_secret, my_access_token)
my_api = FacebookAdsApi(my_session)
FacebookAdsApi.set_default_api(my_api)

me = objects.AdUser(fbid='me')
my_accounts = list(me.get_ad_accounts())


my_account=my_accounts[1]

print(">>> Campaign Stats")
for campaign in my_account.get_ad_campaigns(fields=[AdCampaign.Field.name]):
    for stat in campaign.get_stats(fields=[
        'impressions',
        'clicks',
        'spent',
        'unique_clicks',
        'actions',
    ]):
        print(campaign[campaign.Field.name])
    for statfield in stat:
        print("\t%s:\t\t%s" % (statfield, stat[statfield]))    

      

and the output I get (all caps and xxxx are mine):

Campaign Stats
CAMPAIGN_NAME1
    impressions:        xxxx
    unique_clicks:      xxxx
    clicks:     xxxx
    actions:        {u'mobile_app_install': xxxx, u'app_custom_event': xxxx,   u'app_custom_event.fb_mobile_activate_app': xxx}
    spent:      xxxx
CAMPAIGN_NAME2
    impressions:        xxxx
    unique_clicks:      xxxx
    clicks:     xxxx
    actions:        {XXXX}
    spent:      xxxx

      

+3


source to share


2 answers


The method get_stats()

has an additional parameter named params

where you can pass start_time

and / or end_time

.

params_data = {
    'start_time': 1415134405,
}

stats = campaign.get_stats(
    params=params_data,
    fields=[
        'impressions',
        'clicks',
        ...
    ]
)

for stat in stats:
    ...

      

The API accepts several different parameters here: https://developers.facebook.com/docs/reference/ads-api/adstatistics

More detailed reading

The reason for both the parameter params

and the parameter fields

needs a little explanation. Feel free to ignore this if you are not interested. :)

The parameter implementation params

basically just builds the query string for the API call:

params['start_time'] = 1415134405

      

creates:



?start_time=1415134405

      

The declaration API endpoints usually take a parameter fields

to determine what data you want to return:

?fields=impressions,clicks&start_time=1415134405

      

which you identified correctly, but since it's just fields

in the query string, you can technically do this as well:

params['fields'] = 'impressions,clicks'

      

The fields parameter in get_stats()

(and other read methods) is just an easy way to define that parameter. The implementation looks something like this:

def remote_read(self, params=[], fields=[]):
    ...
    params['fields'] = fields
    ...

      

+3


source


The "get_stats" method has been deprecated in API version 2.0.

The get_insights method should be used instead. The parameters for this method are listed on the page below: https://developers.facebook.com/docs/marketing-api/insights/v2.5



On the previous page, the replacement for "start_time" and "end_time" is the attribute "time_ranges". Example:

"time_ranges": {'since': '2015-01-01', 'until': '2015-01-31'}

      

0


source







All Articles