コード例 #1
0
 def test_total_is_defined(self):
     ei = api.Cursor(
         adaccount.AdAccount(fbid='123'),
         ad.Ad,
     )
     ei._total_count = 32
     self.assertEqual(ei.total(), 32)
コード例 #2
0
 def test_total_is_none(self):
     ei = api.Cursor(
         adaccount.AdAccount(fbid='123'),
         ad.Ad,
     )
     self.assertRaises(exceptions.FacebookUnavailablePropertyException,
                       ei.total)
コード例 #3
0
def get_ads_report(
    account_ids,
    since=(datetime.datetime.now() - timedelta(1)).strftime('%Y-%m-%d'),
    until=(datetime.datetime.now() - timedelta(1)).strftime('%Y-%m-%d')):
    """Function querying Facebook Insights API in order to get costs data at ad level, per region,
    for all account IDs in the list variable account_ids and appending data rows as lists into the costs_data list. 
    Default start date is yesterday, default end date is yesterday."""

    costs_data = []
    for ids in account_ids:
        accounts = adaccount.AdAccount(ids)
        report_fields = [
            adsinsights.AdsInsights.Field.account_name,
            adsinsights.AdsInsights.Field.account_id,
            adsinsights.AdsInsights.Field.campaign_name,
            adsinsights.AdsInsights.Field.adset_name,
            adsinsights.AdsInsights.Field.ad_name,
            adsinsights.AdsInsights.Field.impressions,
            adsinsights.AdsInsights.Field.clicks,
            adsinsights.AdsInsights.Field.spend
        ]

        params = {
            'time_range': {
                'since': since,
                'until': until
            },
            'level': 'ad',
            'breakdowns': ['region'],
            'time_increment': 1
        }

        insights = accounts.get_insights(fields=report_fields, params=params)
        # Querying the API on the defined fields and parameters

        for dataDict in insights:  # For all data dictionaries in the api response (= insights)
            report_data.append([
                dataDict['date_start'].encode('utf-8'),
                dataDict['date_stop'].encode('utf-8'),
                dataDict['region'].encode('utf-8'),
                dataDict['account_name'].encode('utf-8'),
                dataDict['account_id'].encode('utf-8'),
                dataDict['campaign_name'].encode('utf-8'),
                dataDict['adset_name'].encode('utf-8'),
                dataDict['ad_name'].encode('utf-8'),
                dataDict['impressions'].encode('utf-8'),
                dataDict['clicks'].encode('utf-8'),
                dataDict['spend'].encode('utf-8')
            ])

    print 'Facebook Ads Report successfully downloaded.'
    return report_data
コード例 #4
0
 def test_builds_from_array(self):
     """
 Sometimes the response returns an array inside the data
 key. This asserts that we successfully build objects using
 the objects in that array.
 """
     response = {"data": [{"id": "6019579"}, {"id": "6018402"}]}
     ei = api.Cursor(
         adaccount.AdAccount(fbid='123'),
         ad.Ad,
     )
     objs = ei.build_objects_from_response(response)
     assert len(objs) == 2
コード例 #5
0
 def test_builds_from_object(self):
     """
 Sometimes the response returns a single JSON object. This asserts
 that we're not looking for the data key and that we correctly build
 the object without relying on the data key.
 """
     response = {
         "id":
         "601957/targetingsentencelines",
         "targetingsentencelines": [{
             "content": "Location - Living In:",
             "children": ["United States"]
         }, {
             "content": "Age:",
             "children": ["18 - 65+"]
         }]
     }
     ei = api.Cursor(
         adaccount.AdAccount(fbid='123'),
         ad.Ad,
     )
     obj = ei.build_objects_from_response(response)
     assert len(
         obj) == 1 and obj[0]['id'] == "601957/targetingsentencelines"
コード例 #6
0
 def test_delitem_changes_history(self):
     account = adaccount.AdAccount()
     account['name'] = 'foo'
     assert len(account._changes) > 0
     del account['name']
     assert len(account._changes) == 0
コード例 #7
0
 def test_inherits_account_id(self):
     parent_id = 'act_19tg0j239g023jg9230j932'
     api.FacebookAdsApi.set_default_account_id(parent_id)
     ac = adaccount.AdAccount()
     assert ac.get_parent_id() == parent_id
     api.FacebookAdsApi._default_account_id = None