def create_ads(self, fb_account_id, fb_adset_id, fb_creative_id, logger): """ create multi ads in one adset with one creative""" error_reasons = [] ad_infos = [] ad_error_infos = [] try: api_batch = self.api.new_batch() def callback_success(response, ad_info = []): ''' nothing to do for success''' pass def callback_failure(response, ad_info = [], error_reasons = [], ad_error_infos = []): ''' add ad_id to fail_list''' error_info_dict = {} response_error = response.error() error_info_dict['body']= response_error.body() try: error_user_msg = error_info_dict['body']['error']['error_user_msg'] except Exception as e: error_reasons.append(error_info_dict) else: error_reasons.append(error_user_msg) ad_error_infos.append(ad_info) count = 0 MAX_ADSET_NUM_PER_BATCH = 25 for i in range(100): callback_failure = partial( callback_failure, ad_info = ad_info, error_reasons = error_reasons, ad_error_infos = ad_error_infos, ) callback_success = partial( callback_success, ad_info = ad_info, ) ad = Ad(parent_id='act_' + str(fb_account_id)) ad.update({ Ad.Field.name: 'test_ad', Ad.Field.status: Ad.Status.active, Ad.Field.ad_id: fb_ad_id, Ad.Field.creative: { Ad.Field.Creative.creative_id: fb_creative_id, }, }) ad.remote_create(batch=api_batch, success=callback_success, failure=callback_failure,) count = count + 1 if count > MAX_ADSET_NUM_PER_BATCH: api_batch.execute() api_batch = self.api.new_batch() count = 0 api_batch.execute() except Exception as e: logger.exception(e)
def create_ad(self, fb_account_id, fb_adset_id, fb_creative_id, logger): """ create ad""" try: ad = Ad(parent_id='act_' + str(fb_account_id)) ad.update({ Ad.Field.name: 'test_ad', Ad.Field.status: Ad.Status.active, Ad.Field.adset_id: fb_adset_id, Ad.Field.creative: { Ad.Field.Creative.creative_id: fb_creative_id, }, }) ad.remote_create() except Exception as e: logger.exception(e)
from examples.docs import fixtures from facebookads import test_config ad_account_id = test_config.account_id ad_set_id = fixtures.create_adset().get_id() creative_id = fixtures.create_creative().get_id() pixel_id = fixtures.create_ad_conversion_pixel().get_id() # !_DOC [pruno] # _DOC open [ADGROUP_CREATE_TRACKING_ADCONVERSIONPIXELS] # _DOC vars [ad_account_id:s, ad_set_id, creative_id, pixel_id] from facebookads.objects import Ad ad = Ad(parent_id=ad_account_id) ad.update({ Ad.Field.name: 'test', Ad.Field.adset_id: ad_set_id, Ad.Field.creative: { 'creative_id': creative_id, }, Ad.Field.tracking_specs: { 'action.type': 'offsite_conversion', 'offsite_pixel': pixel_id, }, }) ad.remote_create() # _DOC close [ADGROUP_CREATE_TRACKING_ADCONVERSIONPIXELS] ad.remote_delete()
print("**** DONE: Image uploaded:") pp.pprint(img) # The image hash can be found using img[AdImage.Field.hash] ### Create a creative. creative = AdCreative(parent_id=my_account.get_id_assured()) creative.update({ AdCreative.Field.title: 'Visit Seattle', AdCreative.Field.body: 'Beautiful Puget Sound!', AdCreative.Field.object_url: 'http://www.seattle.gov/visiting/', AdCreative.Field.image_hash: img.get_hash(), }) creative.remote_create() print("**** DONE: Creative created:") pp.pprint(creative) ### Get excited, we are finally creating an ad!!! ad = Ad(parent_id=my_account.get_id_assured()) ad.update({ Ad.Field.name: 'Puget Sound impression ad', Ad.Field.adset_id: ad_set.get_id_assured(), Ad.Field.creative: { Ad.Field.Creative.creative_id: creative.get_id_assured(), }, }) ad.remote_create(params={ 'status': AdSet.Status.paused, }) print("**** DONE: Ad created:") pp.pprint(ad)