def create_adcampaign(params={}): campaign = AdCampaign(parent_id=test_config.account_id) campaign[AdCampaign.Field.name] = unique_name('Test Campaign') campaign[AdCampaign.Field.buying_type] = AdCampaign.BuyingType.auction campaign[AdCampaign.Field.objective] = AdCampaign.Objective.none campaign[AdCampaign.Field.status] = AdCampaign.Status.paused campaign.update(params) campaign.remote_create() atexit.register(remote_delete, campaign) return campaign
) api = FacebookAdsApi(session) if __name__ == '__main__': FacebookAdsApi.set_default_api(api) print('\n\n\n********** Ad Creation example. **********\n') ### Setup user and read the object from the server me = AdUser(fbid='me') ### Get first account connected to the user my_account = me.get_ad_account() ### Create a Campaign campaign = AdCampaign(parent_id=my_account.get_id_assured()) campaign.update({ AdCampaign.Field.name: 'Seattle Ad Campaign', AdCampaign.Field.objective: AdCampaign.Objective.website_clicks, AdCampaign.Field.status: AdCampaign.Status.paused, }) campaign.remote_create() print("**** DONE: Campaign created:") pp.pprint(campaign) ### Create an Ad Set ad_set = AdSet(parent_id=my_account.get_id_assured()) ad_set.update({ AdSet.Field.name: 'Puget Sound AdSet', AdSet.Field.status: AdSet.Status.paused, AdSet.Field.bid_type: AdSet.BidType.cpm, # Bidding for impressions
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. from facebookads import test_config as config ad_account_id = config.account_id access_token = config.access_token app_id = config.app_id app_secret = config.app_secret # _DOC open [ADCAMPAIGN_CREATE_WEBSITE_CONVERSIONS] # _DOC vars [ad_account_id:s] from facebookads.objects import AdCampaign campaign = AdCampaign(parent_id=ad_account_id) campaign[AdCampaign.Field.name] = 'My First Campaign' campaign[AdCampaign.Field.status] = AdCampaign.Status.paused campaign[AdCampaign.Field.objective] = AdCampaign.Objective.website_conversions campaign.remote_create() print(campaign) # _DOC close [ADCAMPAIGN_CREATE_WEBSITE_CONVERSIONS] campaign.remote_delete() # _DOC open [ADCAMPAIGN_CREATE_HOMEPAGE] # _DOC vars [ad_account_id:s] from facebookads.objects import AdCampaign campaign = AdCampaign(parent_id=ad_account_id) campaign.update({
def create_multiple_website_clicks_ads( account, name, country, titles, bodies, urls, image_paths, bid_type, bid_info, daily_budget=None, lifetime_budget=None, start_time=None, end_time=None, age_min=None, age_max=None, genders=None, campaign=None, paused=False, ): # Check for bad specs if daily_budget is None: if lifetime_budget is None: raise TypeError( 'One of daily_budget or lifetime_budget must be defined.') elif end_time is None: raise TypeError( 'If lifetime_budget is defined, end_time must be defined.') # Create campaign if not campaign: campaign = AdCampaign(parent_id=account.get_id_assured()) campaign[AdCampaign.Field.name] = name + ' Campaign' campaign[AdCampaign.Field.objective] = \ AdCampaign.Objective.website_clicks campaign[AdCampaign.Field.status] = \ AdCampaign.Status.active if not paused \ else AdCampaign.Status.paused campaign.remote_create() # Create ad set ad_set = AdSet(parent_id=account.get_id_assured()) ad_set[AdSet.Field.campaign_group_id] = campaign.get_id_assured() ad_set[AdSet.Field.name] = name + ' AdSet' ad_set[AdSet.Field.bid_type] = bid_type ad_set[AdSet.Field.bid_info] = bid_info if daily_budget: ad_set[AdSet.Field.daily_budget] = daily_budget else: ad_set[AdSet.Field.lifetime_budget] = lifetime_budget if end_time: ad_set[AdSet.Field.end_time] = end_time if start_time: ad_set[AdSet.Field.start_time] = start_time targeting = {} targeting[TargetingSpecsField.geo_locations] = {'countries': [country]} if age_max: targeting[TargetingSpecsField.age_max] = age_max if age_min: targeting[TargetingSpecsField.age_min] = age_min if genders: targeting[TargetingSpecsField.genders] = genders ad_set[AdSet.Field.targeting] = targeting ad_set.remote_create() # Upload the images first one by one image_hashes = [] for image_path in image_paths: img = AdImage(parent_id=account.get_id_assured()) img[AdImage.Field.filename] = image_path img.remote_create() image_hashes.append(img.get_hash()) ADGROUP_BATCH_CREATE_LIMIT = 10 ad_groups_created = [] def callback_failure(response): raise response.error() # For each creative permutation for creative_info_batch in generate_batches( itertools.product(titles, bodies, urls, image_hashes), ADGROUP_BATCH_CREATE_LIMIT): api_batch = account.get_api_assured().new_batch() for title, body, url, image_hash in creative_info_batch: # Create the ad ad = AdGroup(parent_id=account.get_id_assured()) ad[AdGroup.Field.name] = name + ' Ad' ad[AdGroup.Field.campaign_id] = ad_set.get_id_assured() ad[AdGroup.Field.creative] = { AdCreative.Field.title: title, AdCreative.Field.body: body, AdCreative.Field.object_url: url, AdCreative.Field.image_hash: image_hash, } ad.remote_create(batch=api_batch, failure=callback_failure) ad_groups_created.append(ad) api_batch.execute() return ad_groups_created
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. ''' This is a template for DocSmith samples in Python. Copy it and create yours Code should follow guidelines at https://our.intern.facebook.com/intern/wiki/Solutions_Engineering/DocSmith Each example should be run using facebookads/docs_runner/doc_runner.py Comments on style: - IDs should be defined outside of _DOC blocks so they don't appear into the docs - Dependencies, like campaigns, should be generated in the fixtures module ''' from examples.docs import fixtures campaign_group_id = fixtures.create_adcampaign().get_id_assured() #! _DOC open [TEMPLATE] #! _DOC vars [campaign_group_id] from facebookads.objects import AdCampaign, AdGroup ad_campaign = AdCampaign(campaign_group_id) ad_groups = ad_campaign.get_ad_groups(fields=[AdGroup.Field.name]) for ad_group in ad_groups: print(ad_group[AdGroup.Field.name]) #! _DOC close [TEMPLATE]