def create_creative(self, accountid, name, imagehash, message, appinfo): """ Step 4: create ad creative with call to action type to be 'USE_MOBILE_APP'. See [Ad Creative](https://developers.facebook.com/docs/marketing-api/adcreative) for further details on the API used here. """ link_data = LinkData() link_data[LinkData.Field.link] = appinfo['appstore_link'] link_data[LinkData.Field.message] = message link_data[LinkData.Field.image_hash] = imagehash call_to_action = {'type': 'USE_MOBILE_APP'} call_to_action['value'] = { 'link': appinfo['appstore_link'], 'app_link': appinfo['app_deep_link'], 'application': appinfo['fbapplication_id'], 'link_title': appinfo['app_name'] } link_data[LinkData.Field.call_to_action] = call_to_action object_story_spec = ObjectStorySpec() object_story_spec[ObjectStorySpec.Field.page_id] = appinfo['fbpage_id'] object_story_spec[ObjectStorySpec.Field.link_data] = link_data creative = AdCreative(parent_id=accountid) creative[AdCreative.Field.name] = name creative[AdCreative.Field.object_story_spec] = object_story_spec creative.remote_create() return creative[AdCreative.Field.id]
def create_creative(self, account_id, name, image_hash, message, page_id, app_name, app_store_link, deferred_app_link): """ Step 4: create ad creative with call to action type to be 'INSTALL_MOBILE_APP'. See [Ad Creative][1] for further details on the API used here. [1]: https://developers.facebook.com/docs/marketing-api/adcreative """ link_data = LinkData() link_data[LinkData.Field.link] = app_store_link link_data[LinkData.Field.message] = message link_data[LinkData.Field.image_hash] = image_hash call_to_action = {'type': 'INSTALL_MOBILE_APP'} call_to_action['value'] = { 'link': app_store_link, 'link_title': app_name } if deferred_app_link: call_to_action['value']['app_link'] = deferred_app_link link_data[LinkData.Field.call_to_action] = call_to_action 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=account_id) creative[AdCreative.Field.name] = name creative[AdCreative.Field.object_story_spec] = object_story_spec creative.remote_create() return creative[AdCreative.Field.id]
def build_video_creative(self, fb_account_id, promotion_url, video_id, video_image_url, title, body, fb_page_id, logger, instagram_actor_id=0): """ create facebook/instagram video creative""" try: video_data = VideoData() video_data[VideoData.Field.video_id] = video_id video_data[VideoData.Field.image_url] = video_image_url video_data[VideoData.Field.description] = body video_data[VideoData.Field.call_to_action] = { 'type': 'INSTALL_MOBILE_APP', 'value': { 'link_title': title, 'link': promotion_url, } } object_story_spec = ObjectStorySpec() object_story_spec[ObjectStorySpec.Field.page_id] = fb_page_id object_story_spec[ObjectStorySpec.Field.video_data] = video_data if instagram_actor_id: object_story_spec[ObjectStorySpec.Field.instagram_actor_id] = instagram_actor_id fb_creative = AdCreative(parent_id='act_' + str(fb_account_id)) fb_creative.update({ AdCreative.Field.object_story_spec : object_story_spec }) fb_creative.remote_create() return fb_creative[AdCreative.Field.id] except Exception as e: logger.exception(e)
def build_image_creative(self, fb_account_id, promotion_url, fb_image_id, title, body, fb_page_id, logger, instagram_actor_id=0): """ create facebook / instagram single-image creative""" try: object_story_spec = ObjectStorySpec() # build Link-Ad call_to_action = { 'type': 'INSTALL_MOBILE_APP', 'value': { 'link': promotion_url, } } link_data = LinkData() link_data[LinkData.Field.link] = promotion_url link_data[LinkData.Field.call_to_action] = call_to_action link_data[LinkData.Field.message] = body link_data[LinkData.Field.image_hash] = fb_image_id # add to object_story_spec object_story_spec[ObjectStorySpec.Field.link_data] = link_data object_story_spec[ObjectStorySpec.Field.page_id] = fb_page_id if instagram_actor_id: object_story_spec[ObjectStorySpec.Field.instagram_actor_id] = instagram_actor_id # creative creation fb_creative = AdCreative(parent_id='act_' + str(fb_account_id)) fb_creative.update({ AdCreative.Field.object_story_spec : object_story_spec }) fb_creative.remote_create() return fb_creative[AdCreative.Field.id] except Exception as e: logger.exception(e)
def createAdCreative(self, name, imageHash, message, headline, description, caption, url, pageId): linkData = AdCreativeLinkData() linkData[AdCreativeLinkData.Field.message] = message linkData[AdCreativeLinkData.Field.link] = url linkData[AdCreativeLinkData.Field.caption] = caption linkData[AdCreativeLinkData.Field.description] = description linkData[AdCreativeLinkData.Field.name] = headline linkData[AdCreativeLinkData.Field.image_hash] = imageHash objectStorySpec = AdCreativeObjectStorySpec() objectStorySpec[AdCreativeObjectStorySpec.Field.page_id] = pageId objectStorySpec[AdCreativeObjectStorySpec.Field.link_data] = linkData params = { AdCreative.Field.image_hash: imageHash, AdCreative.Field.body: description, AdCreative.Field.title: headline, AdCreative.Field.actor_id: pageId, AdCreative.Field.object_story_spec: objectStorySpec, AdCreative.Field.name: name, } adCrea = AdCreative() return adCrea.api_create(parent_id=self.get_id_assured(), params=params)
def thumbnail_url_dimensions(adcreative): creative = AdCreative(adcreative) fields = [AdCreative.Field.thumbnail_url, AdCreative.Field.body, AdCreative.Field.id, AdCreative.Field.name] params = { 'thumbnail_width': 150, 'thumbnail_height': 120, } creative.remote_read(params=params, fields=fields) return creative
def create_creative_from_object_story_id(object_story_id): creative = AdCreative(parent_id=test_config.account_id) creative[AdCreative.Field.object_story_id] = object_story_id creative[AdCreative.Field.name] = 'AdCreative from object story ID' creative.remote_create() atexit.register(remote_delete, creative) return creative
def s4_create_creative( self, accountid, name, imagehashes, linktitles, deeplinks, message, appinfo ): """ Step 4: create ad creative with call to action type to be 'INSTALL_MOBILE_APP'. See [Ad Creative]( https://developers.facebook.com/docs/marketing-api/adcreative) for further details on the API used here. """ attachments = [] for index, imagehash in enumerate(imagehashes): attachment = AttachmentData() attachment[AttachmentData.Field.link] = appinfo['appstore_link'] attachment[AttachmentData.Field.image_hash] = imagehash call_to_action = { 'type': 'INSTALL_MOBILE_APP', 'value': { 'link_title': linktitles[index], }, } if deeplinks and index in deeplinks: call_to_action['value']['app_link'] = deeplinks[index] attachment[AttachmentData.Field.call_to_action] = call_to_action attachments.append(attachment) link_data = LinkData() link_data[LinkData.Field.link] = appinfo['appstore_link'] link_data[LinkData.Field.message] = message link_data[LinkData.Field.child_attachments] = attachments object_story_spec = ObjectStorySpec() object_story_spec[ObjectStorySpec.Field.page_id] = appinfo['fbpage_id'] object_story_spec[ObjectStorySpec.Field.link_data] = link_data creative = AdCreative(parent_id=accountid) creative[AdCreative.Field.name] = name creative[AdCreative.Field.object_story_spec] = object_story_spec creative.remote_create() return creative[AdCreative.Field.id]
def get_ad_creative_data(adcreative): creative = AdCreative(adcreative) fields = [AdCreative.Field.name, AdCreative.Field.body, AdCreative.Field.image_url, AdCreative.Field.object_url, AdCreative.Field.adlabels, AdCreative.Field.call_to_action_type, AdCreative.Field.image_hash, AdCreative.Field.link_url, AdCreative.Field.title, AdCreative.Field.template_url, ] creative.remote_read(fields=fields) return creative
def build_carousel_creative(self, fb_account_id, promotion_url, title, sub_title, body, fb_image_ids, fb_page_id, logger, instagram_actor_id=0): """ create facebook/instagram carousel creative""" try: # build Link-Ad call_to_action = { 'type': 'INSTALL_MOBILE_APP', 'value': { 'link_title': sub_title, 'link': promotion_url, } } child_attachments = [] for i in range(len(fb_image_ids)): attachment_data = AttachmentData() attachment_data[AttachmentData.Field.call_to_action] = call_to_action attachment_data[AttachmentData.Field.name] = 'name %d' % i attachment_data[AttachmentData.Field.description] = 'description %d' % i attachment_data[AttachmentData.Field.image_hash] = fb_image_ids[i] attachment_data[AttachmentData.Field.link] = promotion_url child_attachments.append(attachment_data) link_data = LinkData() link_data[LinkData.Field.link] = promotion_url link_data[LinkData.Field.child_attachments] = child_attachments link_data[LinkData.Field.multi_share_optimized] = True link_data[LinkData.Field.call_to_action] = call_to_action link_data[LinkData.Field.message] = body object_story_spec = ObjectStorySpec() object_story_spec[ObjectStorySpec.Field.page_id] = fb_page_id object_story_spec[ObjectStorySpec.Field.link_data] = link_data fb_creative = AdCreative(parent_id='act_' + str(fb_account_id)) fb_creative.update({ AdCreative.Field.object_story_spec : object_story_spec }) fb_creative.remote_create() return fb_creative[AdCreative.Field.id] except Exception as e: logger.exception(e)
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
def create_creative(image=None): if image is None: image = create_image() image_hash = image.get_hash() link_data = LinkData() link_data[LinkData.Field.message] = 'try it out' link_data[LinkData.Field.link] = test_config.app_url link_data[LinkData.Field.caption] = 'Caption' 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
img = AdImage(parent_id=ad_account_id) img[AdImage.Field.filename] = image_path img.remote_create() image_hash = img.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] = 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() creative_id = creative.get_id_assured() # _DOC open [ADGROUP_CREATE] # _DOC vars [ad_account_id:s, ad_set_id, creative_id:s] from facebookads.objects import AdGroup adgroup = AdGroup(parent_id=ad_account_id) adgroup[AdGroup.Field.name] = 'My AdGroup' adgroup[AdGroup.Field.campaign_id] = ad_set_id adgroup[AdGroup.Field.status] = AdGroup.Status.paused adgroup[AdGroup.Field.creative] = { 'creative_id': creative_id,
from facebookads.specs import TemplateData, ObjectStorySpec template = TemplateData() template.update({ TemplateData.Field.name: '{{page.name}}', TemplateData.Field.message: 'Ad Message', TemplateData.Field.description: 'Ad Description', TemplateData.Field.link: url, TemplateData.Field.picture: image_url, TemplateData.Field.call_to_action: { 'type': 'CALL_NOW', }, }) story = ObjectStorySpec() story.update({ ObjectStorySpec.Field.page_id: page_id, ObjectStorySpec.Field.template_data: template, }) creative = AdCreative(parent_id=ad_account_id) creative.update({ AdCreative.Field.place_page_set_id: ad_place_page_set_id, AdCreative.Field.dynamic_ad_voice: 'DYNAMIC', AdCreative.Field.object_story_spec: story, }) creative.remote_create() # _DOC close [ADCREATIVE_CREATE_DLA_DYNAMIC_CALL_NOW] creative.remote_delete()
def create_carousel_ad( self, accountid, page_id, site_link, caption, message, optimization_goal, billing_event, bid_amount, name, targeting, products, call_to_action_type=None, ): """ There are 5 steps in this sample: 1. Create a campaign 2. Create an ad set 3. For each product: a. Upload the product's image and get an image hash b. Create a story attachment using the product's creative elements 4. Prepare the ad creative 5. Create the ad using the ad creative """ daily_budget = 10000 """ Step 1: Create new campaign with WEBSITE_CLICKS objective See [Campaign Group](https://developers.facebook.com/docs/marketing-api/reference/ad-campaign-group) for further details on the API used here. """ campaign = Campaign(parent_id=accountid) campaign[Campaign.Field.name] = name + ' Campaign' campaign[Campaign.Field.objective] = \ Campaign.Objective.link_clicks campaign.remote_create(params={'status': Campaign.Status.paused}) """ Step 2: Create AdSet using specified optimization goal, billing event and bid. See [AdSet](https://developers.facebook.com/docs/marketing-api/reference/ad-campaign) for further details on the API used here. """ ad_set = AdSet(parent_id=accountid) ad_set[AdSet.Field.campaign_id] = campaign.get_id_assured() ad_set[AdSet.Field.name] = name + ' AdSet' ad_set[AdSet.Field.optimization_goal] = optimization_goal ad_set[AdSet.Field.billing_event] = billing_event ad_set[AdSet.Field.bid_amount] = bid_amount ad_set[AdSet.Field.daily_budget] = daily_budget ad_set[AdSet.Field.targeting] = targeting ad_set.remote_create() story_attachments = [] """ Step 3: Upload images and get image hashes for use in ad creative. See [Ad Image](https://developers.facebook.com/docs/marketing-api/reference/ad-image#Creating) for further details on the API used here. Then create a new attachment with the product's creative """ call_to_action = { 'type': call_to_action_type, 'value': { 'link': site_link, 'link_caption': call_to_action_type, } } if call_to_action_type else None for product in products: img = AdImage(parent_id=accountid) img[AdImage.Field.filename] = product['image_path'] img.remote_create() image_hash = img.get_hash() attachment = AttachmentData() attachment[AttachmentData.Field.link] = product['link'] attachment[AttachmentData.Field.name] = product['name'] attachment[ AttachmentData.Field.description] = product['description'] attachment[AttachmentData.Field.image_hash] = image_hash if call_to_action: attachment[ AttachmentData.Field.call_to_action] = call_to_action story_attachments.append(attachment) """ Step 4: Prepare the ad creative including link information Note that here we specify multi_share_optimized = True this means you can add up to 10 products and Facebook will automatically select the best performing 5 to show in the ad. Facebook will also select the best ordering of those products. """ link = LinkData() link[link.Field.link] = site_link link[link.Field.caption] = caption link[link.Field.child_attachments] = story_attachments link[link.Field.multi_share_optimized] = True link[link.Field.call_to_action] = call_to_action story = ObjectStorySpec() story[story.Field.page_id] = page_id story[story.Field.link_data] = link creative = AdCreative() creative[AdCreative.Field.name] = name + ' Creative' creative[AdCreative.Field.object_story_spec] = story """ Step 5: Create the ad using the above creative """ ad = Ad(parent_id=accountid) ad[Ad.Field.name] = name + ' Ad' ad[Ad.Field.adset_id] = ad_set.get_id_assured() ad[Ad.Field.creative] = creative ad.remote_create(params={'status': Ad.Status.paused}) return (campaign, ad_set, ad)
# Policies [http://developers.facebook.com/policy/]. This copyright notice # shall be included in all copies or substantial portions of the software. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # 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 examples.docs import fixtures from facebookads import test_config ad_account_id = test_config.account_id url = test_config.app_url creative_id = fixtures.create_creative().get_id() # _DOC open [ADCREATIVE_GET_ADPREVIEWS] # _DOC vars [creative_id] from facebookads.objects import AdPreview, AdCreative creative = AdCreative(creative_id) params = { AdPreview.Field.ad_format: AdPreview.AdFormat.desktop_feed_standard, } ad_preview = creative.get_ad_preview(params=params) print(ad_preview) # _DOC close [ADCREATIVE_GET_ADPREVIEWS]
product_set_id = fixtures.create_product_set().get_id() link = test_config.app_url # _DOC open [ADCREATIVE_CREATE_DPA_CAROUSEL_TEMPLATE_URL] # _DOC vars [ad_account_id:s, page_id, link:s, product_set_id] 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 template_url = ('http://clicktrack.com/cm325?id={{product.retailer_id}}' '&redirect_url={{product.url|urlencode}}') creative[AdCreative.Field.template_url] = template_url creative[AdCreative.Field.product_set_id] = product_set_id creative.remote_create() # _DOC close [ADCREATIVE_CREATE_DPA_CAROUSEL_TEMPLATE_URL] creative_id = creative.get_id() creative.remote_delete()
product1[AttachmentData.Field.image_hash] = image_hash product1[AttachmentData.Field.video_id] = video_id product2 = AttachmentData() product2[AttachmentData.Field.link] = url + '/product2' product2[AttachmentData.Field.name] = 'Product 2' product2[AttachmentData.Field.description] = '$9.99' product2[AttachmentData.Field.image_hash] = image_hash product3 = AttachmentData() product3[AttachmentData.Field.link] = url + '/product3' product3[AttachmentData.Field.name] = 'Product 3' product3[AttachmentData.Field.description] = '$10.99' product3[AttachmentData.Field.image_hash] = image_hash link = LinkData() link[link.Field.link] = url link[link.Field.child_attachments] = [product1, product2, product3] link[link.Field.caption] = 'My caption' story = ObjectStorySpec() story[story.Field.page_id] = page_id story[story.Field.link_data] = link creative = AdCreative(parent_id=ad_account_id) creative[AdCreative.Field.name] = 'MPA Creative' creative[AdCreative.Field.object_story_spec] = story creative.remote_create() print(creative) # _DOC close [ADCREATIVE_CREATE_MULTI_PRODUCT_AD]
print("**** DONE: Ad Set created:") pp.pprint(ad_set) ### 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(),
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # 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 examples.docs import fixtures campaign_id = fixtures.create_campaign().get_id() creative_id = fixtures.create_creative().get_id() # _DOC open [ADCREATIVE_READ_THUMBNAIL_WITH_DIMENSIONS] # _DOC vars [creative_id] from facebookads.objects import AdCreative creative = AdCreative(creative_id) fields = [AdCreative.Field.thumbnail_url] params = { 'thumbnail_width': 150, 'thumbnail_height': 120, } creative.remote_read(fields=fields, params=params) print(creative[AdCreative.Field.thumbnail_url]) # _DOC close [ADCREATIVE_READ_THUMBNAIL_WITH_DIMENSIONS] creative.remote_delete()
# use, copy, modify, and distribute this software in source code or binary # form for use in connection with the web services and APIs provided by # Facebook. # As with any software that integrates with the Facebook platform, your use # of this software is subject to the Facebook Developer Principles and # Policies [http://developers.facebook.com/policy/]. This copyright notice # shall be included in all copies or substantial portions of the software. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # 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 examples.docs import fixtures creative_id = fixtures.create_creative().get_id() # _DOC open [ADCREATIVE_READ_DISPLAY_URL_OVERRIDE] # _DOC vars [creative_id] from facebookads.objects import AdCreative creative = AdCreative(creative_id) creative.remote_read( fields=['link_destination_display_url'], ) # _DOC close [ADCREATIVE_READ_DISPLAY_URL_OVERRIDE]
'action.type': 'music.listens', 'application': app_id } 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]
def create_ad_creative(self): parent_id = config['act_id'][self.param['account']] page_id = config['page_id'][self.param['account']] link_url = config['link_url'][self.param['account']][self.param['os']] # Upload an image to an account. img = AdImage(parent_id=parent_id) img[AdImage.Field.filename] = os.path.join( this_dir, '../../../upload/' + self.filename ) img.remote_create() print("**** DONE: Image uploaded:") pp.pprint(img) # The image hash can be found using img[AdImage.Field.hash] # Create link data link_data = LinkData() link_data[LinkData.Field.link] = link_url link_data[LinkData.Field.message] = self.param['message'] link_data[LinkData.Field.image_hash] = img.get_hash() call_to_action = {'type': self.param['call_to_action']} call_to_action['value'] = { 'link': link_url, 'link_title': self.param['title'], 'application': config['app_id'][self.param['account']], } if self.param['deeplink_text']: call_to_action['value']['app_link'] = self.param['deeplink_text'] link_data[LinkData.Field.call_to_action] = call_to_action # Create object story spec object_story_spec = ObjectStorySpec() object_story_spec[ObjectStorySpec.Field.page_id] = page_id object_story_spec[ObjectStorySpec.Field.link_data] = link_data # Create a creative creative = AdCreative(parent_id=parent_id) creative[AdCreative.Field.name] = self.param['creative_name'] creative[AdCreative.Field.object_story_spec] = object_story_spec creative.remote_create() print("**** DONE: Creative created:") pp.pprint(creative) # Get excited, we are finally creating an ad!!! adset_ids = self.param.getlist('adset_ids') ads = [] for adset_id in adset_ids: ad = AdGroup(parent_id=parent_id) ad.update({ AdGroup.Field.name: self.param['creative_name'], AdGroup.Field.campaign_id: adset_id, AdGroup.Field.status: self.param['status'], AdGroup.Field.creative: { AdGroup.Field.Creative.creative_id: creative['id'], }, AdGroup.Field.tracking_specs: [ { 'action.type': ['mobile_app_install'], 'application': config['app_id'][self.param['account']], }, ], }) ad.remote_create() ads.append(ad) print("**** DONE: Ad created:") pp.pprint(ads) return ads
def different_os_copy(self, campaign, adset, adgroups): parent_id = config['act_id'][self.param['account']] link_url = config['link_url'][self.param['account']][self.param['os']] account = AdAccount(parent_id) custom_audiences = account.get_custom_audiences( fields=[ CustomAudience.Field.name, ], params={'limit': 100} ) copy_custom_audience = [] copy_excluded_audience = [] for i in range(0, len(custom_audiences)): custom_audience = custom_audiences[i] if custom_audience['id'] in self.param.getlist('custom_audiences'): audience = { 'id': custom_audience['id'], 'name': custom_audience['name'] } copy_custom_audience.append(audience) if custom_audience['id'] in self.param.getlist('excluded_custom_audiences'): excluded_audience = { 'id': custom_audience['id'], 'name': custom_audience['name'] } copy_excluded_audience.append(excluded_audience) copy_promoted_object = copy.deepcopy(adset['promoted_object']) copy_promoted_object['object_store_url'] = link_url copy_targeting = copy.deepcopy(adset['targeting']) copy_targeting['user_os'] = [config['user_os'][self.param['os']]] copy_targeting['age_max'] = self.param['age_max'] copy_targeting['age_min'] = self.param['age_min'] copy_targeting['geo_locations']['countries'] = self.param.getlist('countries') copy_targeting['custom_audiences'] = copy_custom_audience copy_targeting['excluded_custom_audiences'] = copy_excluded_audience # Default to all. if 'user_device' in copy_targeting: del copy_targeting['user_device'] # Defaults to all. Do not specify 0. if(self.param['gender']): copy_targeting['genders'] = [self.param['gender']] else: copy_targeting['genders'] = [1, 2] copy_adset = AdSet(parent_id=parent_id) copy_adset.update({ AdSet.Field.name: self.param['adset_name'], AdSet.Field.daily_budget: adset['daily_budget'], AdSet.Field.promoted_object: copy_promoted_object, AdSet.Field.is_autobid: adset['is_autobid'], AdSet.Field.targeting: copy_targeting, AdSet.Field.status: self.param['status'], AdSet.Field.campaign_group_id: self.param['campaign_id'], AdSet.Field.billing_event: adset['billing_event'], AdSet.Field.optimization_goal: adset['optimization_goal'], AdSet.Field.rtb_flag: adset['rtb_flag'] }) if 'bid_amount' in adset: copy_adset.update({ AdSet.Field.bid_amount: adset['bid_amount'] }) copy_adset.remote_create() print("*** DONE: Copy AdSet ***") pp.pprint(copy_adset) creatives = adset.get_ad_creatives( fields=[ AdCreative.Field.name, AdCreative.Field.object_story_spec ], params={ 'limit': 50 } ) for i in range(0, len(adgroups)): adgroup = adgroups[i] creative = {} for j in range(0, len(creatives)): if (adgroup['creative']['id'] == creatives[j]['id']): creative = creatives[j] break object_story_spec = creative['object_story_spec'] if 'link_data' in object_story_spec: copy_object_story_spec = self.get_object_story_spec_for_image( object_story_spec, link_url ) elif 'video_data' in object_story_spec: copy_object_story_spec = self.get_object_story_spec_for_video( object_story_spec, link_url ) else: break copy_creative = AdCreative(parent_id=parent_id) copy_creative[AdCreative.Field.name] = creative['name'], copy_creative[AdCreative.Field.object_story_spec] = copy_object_story_spec copy_creative.remote_create() print("*** DONE: Copy Creative ***") pp.pprint(copy_creative) copy_adgroup = AdGroup(parent_id=parent_id) copy_adgroup.update({ AdGroup.Field.name: adgroup['name'], AdGroup.Field.campaign_id: copy_adset['id'], AdGroup.Field.status: adgroup['adgroup_status'], AdGroup.Field.creative: { AdGroup.Field.Creative.creative_id: copy_creative['id'] }, AdGroup.Field.tracking_specs: adgroup['tracking_specs'] }) copy_adgroup.remote_create() print("*** DONE: Copy AdGroup ***") pp.pprint(copy_adgroup) result = { 'adset': copy_adset, 'campaign': campaign, 'adgroups': adgroups } return result
print url3 print 'ad_name' print ad_name print 'message' print message print 'pixel_id' print pixel_id print 'bid' print bid print 'budget' print budget print 'adset_id' print adset_id creative = AdCreative(parent_id='act_{}'.format(act_id)) creative[objects.AdCreative.Field.name] = cluster story = specs.ObjectStorySpec() story[story.Field.page_id] = 480594848645810 link = specs.LinkData() link[link.Field.link] = parent_url link[link.Field.caption] = caption link[link.Field.message] = message product1 = specs.AttachmentData() product1.update({ specs.AttachmentData.Field.name: name1, specs.AttachmentData.Field.description: description1, specs.AttachmentData.Field.image_hash: img1,
# As with any software that integrates with the Facebook platform, your use # of this software is subject to the Facebook Developer Principles and # Policies [http://developers.facebook.com/policy/]. This copyright notice # shall be included in all copies or substantial portions of the software. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # 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 examples.docs import fixtures campaign_id = fixtures.create_campaign().get_id() creative_id = fixtures.create_creative().get_id() creative_name = fixtures.unique_name('Test Creative') # _DOC open [ADCREATIVE_UPDATE] # _DOC vars [creative_id, creative_name] from facebookads.objects import AdCreative creative = AdCreative(creative_id) creative[AdCreative.Field.name] = creative_name creative.remote_update() print(creative) # _DOC close [ADCREATIVE_UPDATE]
# _DOC open [ADCREATIVE_CREATE_LINK_AD] # _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() img.remote_delete(params={AdImage.Field.hash: image_hash}) # _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()
LinkData.Field.caption: "Caption", LinkData.Field.description: "My description", LinkData.Field.call_to_action: { "type": "USE_APP", "value": { "link": url, "link_caption": "CTA caption", }, }, }) story = ObjectStorySpec() story.update({ ObjectStorySpec.Field.link_data: link_data, ObjectStorySpec.Field.page_id: page_id, }) creative = AdCreative() creative.update({ AdCreative.Field.object_story_spec: story, }) account = AdAccount(ad_account_id) params = { AdPreview.Field.ad_format: AdPreview.AdFormat.desktop_feed_standard, AdPreview.Field.creative: creative.export_data(), } ad_preview = account.get_ad_preview(params=params) print(ad_preview) # _DOC close [ADACCOUNT_GET_PREVIEWS_MAIA_WITH_OBJECT_STORY_SPEC]
from facebookads.objects import AdCreative from facebookads.specs import ObjectStorySpec, LinkData link_data = LinkData() link_data[LinkData.Field.message] = 'My message' link_data[LinkData.Field.link] = url link_data[LinkData.Field.caption] = 'www.domain.com' call_to_action = { 'type': 'SIGN_UP', 'value': { 'link': url, 'link_caption': 'Sign up!', } } link_data[LinkData.Field.call_to_action] = call_to_action 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 with CTA' creative[AdCreative.Field.object_story_spec] = object_story_spec creative.remote_create() print(creative) # _DOC close [ADCREATIVE_CREATE_LINK_AD_CALL_TO_ACTION] creative.remote_delete()
}, }, }) ad_set.remote_create() print("**** DONE: Ad Set created:") pp.pprint(ad_set) ### Upload an image to an account. img = AdImage(parent_id=my_account.get_id_assured()) img[AdImage.Field.filename] = os.path.join(this_dir, 'test.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(),
# _DOC open [ADCREATIVE_CREATE_LINK_AD] # _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
def create_lead_ad( self, account_id, name, page_id, form_id, optimization_goal, billing_event, bid_amount, daily_budget, targeting, image_path, message, link, caption, description, cta_type='SIGN_UP', ): """ Create Campaign """ campaign = Campaign(parent_id=account_id) campaign[Campaign.Field.name] = name + ' Campaign' campaign[Campaign.Field.objective] = \ Campaign.Objective.lead_generation campaign[Campaign.Field.buying_type] = \ Campaign.BuyingType.auction campaign.remote_create(params={'status': Campaign.Status.paused}) """ Create AdSet """ adset = AdSet(parent_id=account_id) adset[AdSet.Field.campaign_id] = campaign.get_id_assured() adset[AdSet.Field.name] = name + ' AdSet' adset[AdSet.Field.promoted_object] = { 'page_id': page_id, } adset[AdSet.Field.optimization_goal] = optimization_goal adset[AdSet.Field.billing_event] = billing_event adset[AdSet.Field.bid_amount] = bid_amount adset[AdSet.Field.daily_budget] = daily_budget adset[AdSet.Field.targeting] = targeting adset.remote_create() """ Image """ image = AdImage(parent_id=account_id) image[AdImage.Field.filename] = image_path image.remote_create() image_hash = image[AdImage.Field.hash] """ Create Creative """ link_data = LinkData() link_data[LinkData.Field.message] = message link_data[LinkData.Field.link] = link link_data[LinkData.Field.image_hash] = image_hash link_data[LinkData.Field.caption] = caption link_data[LinkData.Field.description] = description link_data[LinkData.Field.call_to_action] = { 'type': cta_type, 'value': { 'lead_gen_form_id': form_id, }, } 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=account_id) creative[AdCreative.Field.name] = name + ' Creative' creative[AdCreative.Field.object_story_spec] = object_story_spec creative.remote_create() """ Create Ad """ ad = Ad(parent_id=account_id) ad[Ad.Field.name] = name ad[Ad.Field.adset_id] = adset.get_id_assured() ad[Ad.Field.creative] = {'creative_id': str(creative.get_id_assured())} ad.remote_create() return { 'image_hash': image_hash, 'campaign_id': campaign['id'], 'adset_id': adset['id'], 'creative_id': creative['id'], 'ad_id': ad['id'], }
img = AdImage(parent_id=ad_account_id) img[AdImage.Field.filename] = image_path img.remote_create() image_hash = img.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] = 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() creative_id = creative.get_id_assured() # _DOC open [ADGROUP_CREATE] # _DOC vars [ad_account_id:s, ad_set_id, creative_id:s] from facebookads.objects import AdGroup adgroup = AdGroup(parent_id=ad_account_id) adgroup[AdGroup.Field.name] = "My AdGroup" adgroup[AdGroup.Field.campaign_id] = ad_set_id adgroup[AdGroup.Field.status] = AdGroup.Status.paused adgroup[AdGroup.Field.creative] = {"creative_id": creative_id} adgroup.remote_create()