3

I am trying to use Facebook's ads-api to get data about advertising accounts/campaigns/etc within a specified time range.

Up until now I managed to get overall information (added below) using the official python sdk , but I can't figure out how to insert the time filter condition.

The answer is probably here under "Filtering results", but I don't understand how to translate what they are doing there to python... https://developers.facebook.com/docs/reference/ads-api/adstatistics/v2.2

I would really appreciate any help you can provide,

Thanks!


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 is:

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 is (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
PandaZ
  • 127
  • 3
  • 12

2 Answers2

3

The get_stats() method has an additional parameter named params where you can pass in 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 a number of different parameters documented here: https://developers.facebook.com/docs/reference/ads-api/adstatistics

More optional reading

The reason for both a params parameter and fields parameter requires a bit of explanation. Feel free to ignore this if you're not interested. :)

The implementation for the params parameter basically just constructs the query string for the API call:

params['start_time'] = 1415134405

creates:

?start_time=1415134405

The Ads API endpoints generally accept a fields parameter to define what data you want to return:

?fields=impressions,clicks&start_time=1415134405

which you have correctly defined, but because it's just fields in the query string, you could also technically do this:

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

The fields parameter in get_stats() (and other read methods) is simply an easy way to define this fields parameter. The implementation looks something like this:

def remote_read(self, params=[], fields=[]):
    ...
    params['fields'] = fields
    ...
Evan Chen
  • 106
  • 2
  • hi~ do you know what's meaning of action returned with start_time? does that mean action happened after start_time, thanks – linpingta Jan 19 '15 at 16:12
1

The "get_stats" method is deprecated in the V2.4 version of the API.

Instead, "get_insights" method should be used. The parameters for that method are liste on the page below: https://developers.facebook.com/docs/marketing-api/insights/v2.5

From the page above, the replacement for the "start_time" and "end_time" is the "time_ranges" attribute. Example:

"time_ranges": {'since': '2015-01-01', 'until': '2015-01-31'}
markonovak
  • 381
  • 4
  • 4