Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
ad_account_id = test_config.account_id
page_id = fixtures.get_page_with_locations_id_assured()
campaign_id = fixtures.create_campaign({
Campaign.Field.objective: Campaign.Objective.local_awareness,
Campaign.Field.promoted_object: {
'page_id': page_id,
},
}).get_id()
ad_place_page_set_id = fixtures.create_ad_place_page_set().get_id()
# _DOC oncall [pruno]
# _DOC open [ADSET_CREATE_DLA]
# _DOC vars [campaign_id, ad_account_id:s, ad_place_page_set_id]
from facebookads.objects import AdSet
adset = AdSet(parent_id=ad_account_id)
adset.update({
AdSet.Field.name: 'Local Awareness Ad Set',
AdSet.Field.promoted_object: {
'place_page_set_id': ad_place_page_set_id,
},
AdSet.Field.optimization_goal: AdSet.OptimizationGoal.reach,
AdSet.Field.billing_event: AdSet.BillingEvent.impressions,
AdSet.Field.bid_amount: 2,
AdSet.Field.daily_budget: 1000,
AdSet.Field.campaign_id: campaign_id,
AdSet.Field.targeting: {
'geo_locations': {
'custom_locations': [
{
'latitude': 37.48327,
'longitude': -122.15033,
campaign_id = fixtures.create_campaign().get_id()
product_set_id = fixtures.create_product_set().get_id()
dynamic_audience_id = fixtures.create_product_audience(product_set_id).get_id()
# _DOC open [ADSET_CREATE_PRODUCT_CATALOG_SALES]
# _DOC vars [ad_account_id:s, dynamic_audience_id, campaign_id, product_set_id]
from facebookads.objects import AdSet, TargetingSpecsField
adset = AdSet(parent_id=ad_account_id)
adset[AdSet.Field.name] = 'Product Catalog Sales Adset'
adset[AdSet.Field.bid_amount] = 3000
adset[AdSet.Field.billing_event] = AdSet.BillingEvent.impressions
adset[AdSet.Field.optimization_goal] = \
AdSet.OptimizationGoal.offsite_conversions
adset[AdSet.Field.daily_budget] = 15000
adset[AdSet.Field.campaign_id] = campaign_id
adset[AdSet.Field.targeting] = {
TargetingSpecsField.geo_locations: {
TargetingSpecsField.countries: ['US'],
},
TargetingSpecsField.dynamic_audience_ids: [
dynamic_audience_id,
],
}
adset[AdSet.Field.promoted_object] = {
'product_set_id': product_set_id,
}
adset.remote_create()
# _DOC close [ADSET_CREATE_PRODUCT_CATALOG_SALES]
adset.remote_delete()
Args:
accountid: The ad account id (should be of the form act_)
for which you are running this exercise.
inlcudepaused: Boolen parameter to make your method consider ad
sets with paused states (PAUSED & CAMPAIGN_PAUSED). Checks
only ACTIVE by default.
Returns:
List of ad set objects (if found satisfying the conditions) or
an empty list.
"""
# use accountid to retrieve all active adsets
account = AdAccount(accountid)
adsetfields = [
AdSet.Field.id,
AdSet.Field.name,
AdSet.Field.campaign_id,
AdSet.Field.targeting,
AdSet.Field.effective_status,
]
adsets = list(account.get_ad_sets(fields=adsetfields))
# Filter ad sets received by desired status and placement types.
# Further filter by campaign objectives listed in the criteria below.
#
# Ref: https://developers.facebook.com/docs/
# marketing-api/audience-network/v2.5
desired_campaign_status = set(['ACTIVE'])
# mostly useful in testing when you don't have active campaigns
# DEALINGS IN THE SOFTWARE.
from examples.docs import fixtures
from facebookads import test_config
ad_account_id = test_config.account_id
campaign_id = fixtures.create_campaign().get_id()
product_set_id = fixtures.create_product_set().get_id()
dynamic_audience_id = fixtures.create_product_audience(product_set_id).get_id()
# _DOC open [ADSET_CREATE_PRODUCT_CATALOG_SALES]
# _DOC vars [ad_account_id:s, dynamic_audience_id, campaign_id, product_set_id]
from facebookads.objects import AdSet, TargetingSpecsField
adset = AdSet(parent_id=ad_account_id)
adset[AdSet.Field.name] = 'Product Catalog Sales Adset'
adset[AdSet.Field.bid_amount] = 3000
adset[AdSet.Field.billing_event] = AdSet.BillingEvent.impressions
adset[AdSet.Field.optimization_goal] = \
AdSet.OptimizationGoal.offsite_conversions
adset[AdSet.Field.daily_budget] = 15000
adset[AdSet.Field.campaign_id] = campaign_id
adset[AdSet.Field.targeting] = {
TargetingSpecsField.geo_locations: {
TargetingSpecsField.countries: ['US'],
},
TargetingSpecsField.dynamic_audience_ids: [
dynamic_audience_id,
],
}
adset[AdSet.Field.promoted_object] = {
},
AdSet.Field.end_time: end_date,
})
ad_set.remote_create()
print(ad_set)
# _DOC close [ADSET_CREATE_HOMEPAGE]
ad_set_id = fixtures.create_adset().get_id_assured()
# _DOC open [ADSET_GET_ADGROUPS]
# _DOC vars [ad_set_id]
from facebookads.objects import AdSet, AdGroup
ad_set = AdSet(ad_set_id)
ad_group_iter = ad_set.get_ad_groups(fields=[AdGroup.Field.name])
for ad_group in ad_group_iter:
print(ad_group[AdGroup.Field.name])
# _DOC close [ADSET_GET_ADGROUPS]
ad_set_id = fixtures.create_adset().get_id_assured()
# _DOC open [ADSET_READ_ADCREATIVE]
# _DOC vars [ad_set_id]
from facebookads.objects import AdSet, AdCreative
adset = AdSet(fbid=ad_set_id)
adset.get_ad_creatives([AdCreative.Field.object_story_id])
# _DOC close [ADSET_READ_ADCREATIVE]
### 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: {
AdSet.Field.BidInfo.impressions: 500, # $5 per 1000 impression
},
AdSet.Field.daily_budget: 3600, # $36.00
AdSet.Field.start_time: int(time.time()) + 15, # 15 seconds from now
AdSet.Field.campaign_group_id: campaign.get_id_assured(),
AdSet.Field.targeting: {
TargetingSpecsField.geo_locations: {
'countries': [
'US',
],
},
* `include_archived` specifies whether archived ad sets should be
analyzed.
* `limit` is how many ad sets to analyze. This script will analyze the
first `limit` ad sets as in the response, not including those which
use Instagram placement already. The more this limit is, the longer
it takes to run. If you run the script directly and are willing
to wait for a while, you can drop the lines of code around it.
For more information see the [Instagram Ads document](
https://developers.facebook.com/docs/marketing-api/guides/instagramads/)
"""
locale.setlocale(locale.LC_ALL, '')
if include_archived:
params = {
'limit': limit,
AdSet.Field.configured_status: [
'PENDING', 'ACTIVE', 'PAUSED', 'PENDING_REVIEW',
'DISAPPROVED', 'PREAPPROVED', 'PENDING_BILLING_INFO',
'CAMPAIGN_PAUSED', 'CAMPAIGN_GROUP_PAUSED', 'ARCHIVED'
],
}
else:
params = {'limit': limit}
account = AdAccount(account_id)
ad_sets = account.get_ad_sets(
fields=[
AdSet.Field.id,
AdSet.Field.campaign_id,
AdSet.Field.name,
AdSet.Field.configured_status,
AdSet.Field.targeting,
],
stats = adset.get_insights(params=params)
print(stats)
# _DOC close [ADSET_GET_INSIGHTS_LEVEL_ADGROUP]
params = {
AdCampaign.Field.objective: AdCampaign.Objective.product_catalog_sales,
AdCampaign.Field.promoted_object: {'product_catalog_id': product_catalog_id}
}
campaign_group_id = fixtures.create_adcampaign(params).get_id_assured()
# _DOC open [ADSET_CREATE_DYNAMIC_PROSPECTIING]
# _DOC vars [ad_account_id:s, product_set_id, campaign_group_id]
from facebookads.objects import AdSet, TargetingSpecsField
adset = AdSet(parent_id=ad_account_id)
adset[AdSet.Field.name] = 'Case 1 Adset'
adset[AdSet.Field.bid_amount] = 3000
adset[AdSet.Field.billing_event] = AdSet.BillingEvent.link_clicks
adset[AdSet.Field.optimization_goal] = AdSet.OptimizationGoal.link_clicks
adset[AdSet.Field.status] = AdSet.Status.active
adset[AdSet.Field.daily_budget] = 15000
adset[AdSet.Field.campaign_group_id] = campaign_group_id
adset[AdSet.Field.targeting] = {
TargetingSpecsField.geo_locations: {
TargetingSpecsField.countries: ['US'],
},
TargetingSpecsField.interests: [{
'id': 6003397425735,
'name': 'Tennis',
}],
}
{'': {'status': 1}},
{'': {'status': 0, 'message': ''}},
...
]
"""
results = []
# check if adsets is a list
if type(adsetids) is not list:
return results
# go over the list of adset ids
for adsetid in adsetids:
try:
# read ad set info
adset = AdSet(fbid=adsetid)
adsetobj = adset.remote_read(fields=[AdSet.Field.targeting])
# edit targeting spec info for placements
targetinginfo = copy.deepcopy(adsetobj[
AdSet.Field.targeting
])
targetinginfo[TargetingSpecsField.publisher_platforms]. \
append('audience_network')
# update ad set info
adset.update({
AdSet.Field.targeting: targetinginfo
})
adset.remote_update()
# update result list along with status