Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
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
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]
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_GET_ADGROUPS]
# _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 [ADCAMPAIGN_GET_ADGROUPS]
# _DOC open [ADCAMPAIGN_GET_ADGROUPS_WITH_STATUS_ARCHIVED]
# _DOC vars [campaign_group_id]
from facebookads.objects import AdGroup, AdCampaign
adcampaign = AdCampaign(campaign_group_id)
params = {
AdGroup.Field.status: [AdGroup.Status.archived],
}
adgroups = adcampaign.get_ad_groups(
)
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
AdSet.Field.bid_info: {