Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# _DOC vars [ad_account_id:s, page_id, link:s, product_set_id_1]
from facebookads.objects import AdCreative
from facebookads.specs import ObjectStorySpec
story = ObjectStorySpec()
story[story.Field.page_id] = page_id
story[story.Field.template_data] = {
'message': 'Test {{product.name | titleize}}',
'link': link,
'name': 'Headline {{product.price}}',
'description': 'Description {{product.description}}',
'max_product_count': 3,
}
creative = AdCreative(parent_id=ad_account_id)
creative[AdCreative.Field.name] = 'Dynamic Ad Template Creative Sample'
creative[AdCreative.Field.object_story_spec] = story
creative[AdCreative.Field.product_set_id] = product_set_id_1
creative.remote_create()
# _DOC close [ADCREATIVE_CREATE_DPA_RETARGETING]
creative.remote_delete()
# FIXME excluded from runtime
exit(0)
retailer_id = ''
catalog_id = product_catalog_id
# _DOC open [ADCREATIVE_READ_DPA_PREVIEW]
# _DOC vars [creative_id, catalog_id, retailer_id:s]
import base64
from facebookads.objects import AdCreative, AdCreativePreview
item_id = retailer_id
result['objective_supported'] = 0
else:
result['objective_supported'] = 2
self.add_check_result(result, check)
# Get creative and check the media
if campaign[Campaign.Field.objective] == 'PRODUCT_CATALOG_SALES':
result['preview_url'] = \
'Images from product catalog are not supported.'
results.append(result)
result['creative_ready'] = False
continue
creatives = ad_set.get_ad_creatives([
AdCreative.Field.object_story_id,
])
result['creative_ready'] = False
if not creatives:
comment = 'No creative found in this ad set.'
self.add_check_result(
result,
{
"eligibility": 3,
}
)
result['preview_url'] = comment
results.append(result)
continue
creative = creatives[0]
story_id = creative.get(AdCreative.Field.object_story_id, 0)
if story_id == 0:
img = AdImage(parent_id=my_account.get_id_assured())
img[AdImage.Field.filename] = os.path.join(
os.path.dirname(__file__),
os.pardir,
'facebookads/test/misc/image.png'
)
img.remote_create()
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 = AdGroup(parent_id=my_account.get_id_assured())
ad.update({
AdGroup.Field.name: 'Puget Sound impression ad',
AdGroup.Field.campaign_id: ad_set.get_id_assured(),
AdGroup.Field.creative: {
AdGroup.Field.Creative.creative_id: creative.get_id_assured(),
},
})
ad.remote_create()
### Upload an image to an account.
img = AdImage(parent_id=my_account.get_id_assured())
img[AdImage.Field.filename] = os.path.join(
os.path.dirname(__file__),
os.pardir,
'facebookads/test/misc/image.png'
)
img.remote_create()
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 = AdGroup(parent_id=my_account.get_id_assured())
ad.update({
AdGroup.Field.name: 'Puget Sound impression ad',
AdGroup.Field.campaign_id: ad_set.get_id_assured(),
AdGroup.Field.creative: {
AdGroup.Field.Creative.creative_id: creative.get_id_assured(),
},
def create_creative(image=create_image()):
image_hash = image.get_hash()
link_data = LinkData()
link_data[LinkData.Field.message] = 'try it out'
link_data[LinkData.Field.link] = 'http://example.com'
link_data[LinkData.Field.caption] = 'www.example.com'
link_data[LinkData.Field.image_hash] = image_hash
object_story_spec = ObjectStorySpec()
object_story_spec[ObjectStorySpec.Field.page_id] = test_config.page_id
object_story_spec[ObjectStorySpec.Field.link_data] = link_data
creative = AdCreative(parent_id=test_config.account_id)
creative[AdCreative.Field.name] = unique_name('Test Creative')
creative[AdCreative.Field.object_story_spec] = object_story_spec
creative.remote_create()
atexit.register(remote_delete, creative)
return creative
AdCreative.Field.object_story_id,
])
result['creative_ready'] = False
if not creatives:
comment = 'No creative found in this ad set.'
self.add_check_result(
result,
{
"eligibility": 3,
}
)
result['preview_url'] = comment
results.append(result)
continue
creative = creatives[0]
story_id = creative.get(AdCreative.Field.object_story_id, 0)
if story_id == 0:
comment = 'No post fround in the first creative of this ad set.'
self.add_check_result(
result,
{
"eligibility": 3,
}
)
result['preview_url'] = comment
results.append(result)
continue
# Check whether the creative's post is IG ready
try:
# This Graph API call is not a part of Ads API thus no SDK
post = FacebookAdsApi.get_default_api().call(
adgroup.remote_create()
# _DOC close [ADGROUP_CREATE_TRACKING_MUSIC_LISTENS]
adgroup.remote_delete()
# _DOC open [ADGROUP_CREATE_INLINE_CREATIVE]
# _DOC vars [ad_account_id:s, image_hash:s, ad_set_id]
from facebookads.objects import AdCreative, AdGroup
# First, upload the ad image that you will use in your ad creative
# Please refer to Ad Image Create for details.
# Then, use the image hash returned from above
creative = AdCreative(parent_id=ad_account_id)
creative[AdCreative.Field.title] = 'My Test Creative'
creative[AdCreative.Field.body] = 'My Test Ad Creative Body'
creative[AdCreative.Field.object_url] = 'https://www.facebook.com/facebook'
creative[AdCreative.Field.image_hash] = image_hash
# Finally, create your ad along with ad creative.
# Please note that the ad creative is not created independently, rather its
# data structure is appended to the ad group
adgroup = AdGroup(parent_id=ad_account_id)
adgroup[AdGroup.Field.name] = 'My Ad'
adgroup[AdGroup.Field.campaign_id] = ad_set_id
adgroup[AdGroup.Field.creative] = creative
adgroup[AdGroup.Field.status] = AdGroup.Status.paused
adgroup.remote_create()
# _DOC close [ADGROUP_CREATE_INLINE_CREATIVE]
adgroup.remote_delete()
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]
campaign_group_id = fixtures.create_adcampaign().get_id_assured()
# _DOC open [ADSET_CREATE_LOCAL_AWARENESS]
# _DOC vars [ad_account_id:s, campaign_group_id:s]
from facebookads.objects import AdSet
adset = AdSet(parent_id=ad_account_id)
adset.update({
AdSet.Field.name: 'Local awareness adset',
AdSet.Field.daily_budget: 10000,
AdSet.Field.status: AdSet.Status.paused,
AdSet.Field.campaign_group_id: campaign_group_id,
AdSet.Field.optimization_goal: AdSet.OptimizationGoal.reach,
def create_creative(image=create_image()):
image_hash = image.get_hash()
link_data = LinkData()
link_data[LinkData.Field.message] = 'try it out'
link_data[LinkData.Field.link] = 'http://example.com'
link_data[LinkData.Field.caption] = 'www.example.com'
link_data[LinkData.Field.image_hash] = image_hash
object_story_spec = ObjectStorySpec()
object_story_spec[ObjectStorySpec.Field.page_id] = test_config.page_id
object_story_spec[ObjectStorySpec.Field.link_data] = link_data
creative = AdCreative(parent_id=test_config.account_id)
creative[AdCreative.Field.name] = unique_name('Test Creative')
creative[AdCreative.Field.object_story_spec] = object_story_spec
creative.remote_create()
atexit.register(remote_delete, creative)
return creative
# _DOC vars [ad_account_id:s, image_hash:s, page_id, link:s]
from facebookads.objects import AdCreative
from facebookads.specs import ObjectStorySpec, LinkData
link_data = LinkData()
link_data[LinkData.Field.message] = 'try it out'
link_data[LinkData.Field.link] = link
link_data[LinkData.Field.caption] = 'My caption'
link_data[LinkData.Field.image_hash] = image_hash
object_story_spec = ObjectStorySpec()
object_story_spec[ObjectStorySpec.Field.page_id] = page_id
object_story_spec[ObjectStorySpec.Field.link_data] = link_data
creative = AdCreative(parent_id=ad_account_id)
creative[AdCreative.Field.name] = 'AdCreative for Link Ad'
creative[AdCreative.Field.object_story_spec] = object_story_spec
creative.remote_create()
print(creative)
# _DOC close [ADCREATIVE_CREATE_LINK_AD]
creative.remote_delete()
url = 'http://www.domain.com'
# _DOC open [ADCREATIVE_CREATE_LINK_AD_CALL_TO_ACTION]
# _DOC vars [url:s, page_id, ad_account_id:s]
from facebookads.objects import AdCreative
from facebookads.specs import ObjectStorySpec, LinkData
link_data = LinkData()