def ad_creative(self): creative = None error = None try: link_data = AdCreativeLinkData() link_data[AdCreativeLinkData.Field.message] = self.post['title'] link_data[AdCreativeLinkData.Field.link] = self.post['link'] link_data[AdCreativeLinkData.Field.caption] = self.post['link'] object_story_spec = AdCreativeObjectStorySpec() object_story_spec[AdCreativeObjectStorySpec.Field. page_id] = os.getenv("FACEBOOK_PAGE_ID") object_story_spec[ AdCreativeObjectStorySpec.Field.link_data] = link_data creative = AdCreative(parent_id=self.ad_account_id) creative[ AdCreative.Field.name] = 'AdCreative for ' + self.post['name'] creative[AdCreative.Field.object_story_spec] = object_story_spec creative.remote_create() except Exception as err: error = err return creative, error
def get_adcreative_by_id(self, creative_id): creatives = AdCreative('%s' % (creative_id)) creatives.remote_read(fields=[ AdCreative.Field.id, AdCreative.Field.name, AdCreative.Field.image_hash, AdCreative.Field.image_url, AdCreative.Field.object_type, AdCreative.Field.object_story_spec, AdCreative.Field.status ]) if creatives: return creatives return {}
def create_new_ad(adset, image_hash): link_data = AdCreativeLinkData() link_data[AdCreativeLinkData.Field.message] = '“Like” to find ways to help man’s best friend.' link_data[AdCreativeLinkData.Field.link] = 'https://www.facebook.com/caltech.clickmaniac' link_data[AdCreativeLinkData.Field.image_hash] = image_hash object_story_spec = AdCreativeObjectStorySpec() object_story_spec[AdCreativeObjectStorySpec.Field.page_id] = my_page_id object_story_spec[AdCreativeObjectStorySpec.Field.link_data] = link_data creative = AdCreative(parent_id=my_ad_account_id) creative[AdCreative.Field.name] = 'SDK Creative 1' creative[AdCreative.Field.object_story_spec] = object_story_spec # 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 ad = Ad(parent_id=my_ad_account_id) ad[Ad.Field.name] = 'SDK Dog Ad 1' ad[Ad.Field.adset_id] = adset[AdSet.Field.id] ad[Ad.Field.creative] = creative ad[Ad.Field.status] = Ad.Status.active ad.remote_create() pass
def create_ad_creative(self, message, link, image_hash, page_id, act_id, adcreative_name): link_data = AdCreativeLinkData() link_data[AdCreativeLinkData.Field.message] = message link_data[AdCreativeLinkData.Field.link] = link link_data[AdCreativeLinkData.Field.image_hash] = image_hash object_story_spec = AdCreativeObjectStorySpec() object_story_spec[AdCreativeObjectStorySpec.Field.page_id] = page_id object_story_spec[ AdCreativeObjectStorySpec.Field.link_data] = link_data creative = AdCreative(parent_id='act_{}'.format(act_id)) creative[AdCreative.Field.name] = 'AdCreative for Link Ad' creative[AdCreative.Field.object_story_spec] = object_story_spec creative.remote_create() return (creative)
def create_new_ad_set(): """ Creating a new ad set for the ad account You can use the adset.update method specified here: https://developers.facebook.com/docs/marketing-api/reference/ad-campaign """ adset = AdSet(parent_id=my_ad_account_id) """ UPDATE/CREATE ADSET HERE """ adset[AdSet.Field.campaign_id] = my_campaign_id adset[AdSet.Field.name] = 'Toast_competition' adset[AdSet.Field.promoted_object] = { 'page_id': my_page_id, } adset[AdSet.Field.billing_event] = "IMPRESSIONS" adset[AdSet.Field.daily_budget] = 200 adset[AdSet.Field.is_autobid] = True adset[AdSet.Field.targeting] = { 'geo_locations': { 'countries': ['IN'] }, 'genders': [0], 'age_min': 18, 'age_max': 40, 'interests': [] } adset.remote_create() image = AdImage(parent_id=my_ad_account_id) image[AdImage.Field.filename] = "dog_ads/3.jpg" image.remote_create() image_hash = image[AdImage.Field.hash] creative = AdCreative(parent_id=my_ad_account_id) creative[AdCreative.Field.title] = 'Puppy Love' creative[AdCreative.Field. body] = '"Like" to find ways to help man\'s best friend.' creative[AdCreative.Field.object_id] = my_page_id # creative[AdCreative.Field.object_url] = 'https://www.facebook.com/caltech.clickmaniac/' creative[AdCreative.Field.image_hash] = image_hash ad = Ad(parent_id=my_ad_account_id) ad[Ad.Field.name] = 'Pos' ad[Ad.Field.adset_id] = adset[AdSet.Field.id] ad[Ad.Field.creative] = creative ad.remote_create(params={ 'status': Ad.Status.active, }) return adset
def create_creative(account_id, name, caption, message, link, imagehash, page_id): from facebookads.adobjects.adcreativelinkdata import AdCreativeLinkData from facebookads.adobjects.adcreativeobjectstoryspec \ import AdCreativeObjectStorySpec link_data = AdCreativeLinkData() link_data[AdCreativeLinkData.Field.message] = message link_data[AdCreativeLinkData.Field.link] = link link_data[AdCreativeLinkData.Field.caption] = caption link_data[AdCreativeLinkData.Field.image_hash] = imagehash object_story_spec = AdCreativeObjectStorySpec() object_story_spec[AdCreativeObjectStorySpec.Field.page_id] = page_id object_story_spec[AdCreativeObjectStorySpec.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() print(creative) return creative
def create_carousel_ad(caption, adset_id, ad_name, times, design_list, account_id, land_on_design, url, campaign_tag): logger = logging.getLogger('testlogger') logger.debug("In create carousel") conn = None simple_list = [] account_medium_list = { "act_940036526039709": "fb_ocpc", "act_938286879548007": "acpm", "act_1010404049002956": "acpm", "act_1385041538425866": "acpm", "act_1128744890502204": "jcpc", "act_10152414205523137": "int", "act_972844956092199": "exp" } utm_medium = account_medium_list[account_id] try: urlparse.uses_netloc.append("postgres") database_url = urlparse.urlparse(constants.database_url) conn = psycopg2.connect(database=database_url.path[1:], user=database_url.username, password=database_url.password, host=database_url.hostname, port=database_url.port) curr = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) for i in xrange(times): design_id = design_list[i] curr.execute( 'SELECT discount_percent,designer_id from designs where id=' + str(design_id)) row = curr.fetchone() curr.execute( 'SELECT id,photo_file_name FROM images where design_id = ' + str(design_id)) rows = curr.fetchone() curr.execute( 'SELECT name FROM "categories" INNER JOIN "categories_designs" ON "categories"."id" = "categories_designs"."category_id" WHERE design_id =' + str(design_id)) category_name = curr.fetchone() image_link = image_hash.get_image_link(rows['photo_file_name'], rows['id']) if row['discount_percent'] is None: row['discount_percent'] = 0 product1 = AdCreativeLinkDataChildAttachment() if land_on_design: product1[ AdCreativeLinkDataChildAttachment.Field. link] = 'www.mirraw.com/designers/' + str( row['designer_id'] ) + '/designs/' + str( design_id ) + '?utm_source=facebook-auto&utm_medium=' + utm_medium + '&utm_campaign=' + campaign_tag else: product1[ AdCreativeLinkDataChildAttachment.Field. link] = url + '?pid=' + str( design_id ) + '&utm_source=facebook&utm_medium=' + utm_medium + '&utm_campaign=' + campaign_tag product1[AdCreativeLinkDataChildAttachment.Field. name] = category_name['name'] action = AdCreativeLinkDataCallToAction() link_value = AdCreativeLinkDataCallToActionValue() link_value[AdCreativeLinkDataCallToActionValue.Field. link_title] = 'Discount ' + str( row['discount_percent']) + '%' action[AdCreativeLinkDataCallToAction.Field. type] = AdCreativeLinkDataCallToAction.Type.shop_now action[AdCreativeLinkDataCallToAction.Field.value] = link_value product1[AdCreativeLinkDataChildAttachment.Field. call_to_action] = action product1[AdCreativeLinkDataChildAttachment.Field. description] = 'Discount ' + str( row['discount_percent']) + '%' product1[AdCreativeLinkDataChildAttachment.Field. image_hash] = image_hash.get_image_hash( image_link, rows[1], account_id) sleep(0.5) simple_list.append(product1) link = AdCreativeLinkData() link[ link.Field. link] = url + '&utm_source=facebook&utm_medium=' + utm_medium + '&utm_campaign=' + campaign_tag link[link.Field.child_attachments] = simple_list link[ link.Field. caption] = url + '&utm_source=facebook&utm_medium=' + utm_medium + '&utm_campaign=' + campaign_tag call_to_action_button = AdCreativeLinkDataCallToAction() call_to_action_button[ AdCreativeLinkDataCallToAction.Field. type] = AdCreativeLinkDataCallToAction.Type.shop_now link[link.Field.call_to_action] = call_to_action_button link[link.Field.message] = caption logger.info(link) story = AdCreativeObjectStorySpec() story[story.Field.page_id] = constants.page_id story[story.Field.link_data] = link creative = AdCreative(parent_id=account_id) creative[AdCreative.Field.name] = 'MPA Creative' creative[AdCreative.Field.object_story_spec] = story creative.remote_create() creative = json.loads(str(creative).replace('<AdCreative> ', '')) logger.info(creative) ad = Ad(parent_id=account_id) ad[Ad.Field.name] = ad_name ad[Ad.Field.adset_id] = adset_id ad[Ad.Field.status] = Campaign.Status.paused ad[Ad.Field.creative] = {'creative_id': str(creative['id'])} logger.info('Creating Ad') ad.remote_create() logger.info(ad) except psycopg2.DatabaseError, e: logger.error('Error %s' % e) return False
# 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 facebookads.adobjects.adcreative import AdCreative from facebookads.api import FacebookAdsApi access_token = '<ACCESS_TOKEN>' app_secret = '<APP_SECRET>' app_id = '<APP_ID>' id = '<ID>' FacebookAdsApi.init(access_token=access_token) fields = [ 'name', 'object_story_id', ] params = {} print AdCreative(id).get( fields=fields, params=params, )
# 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 facebookads.adobjects.adcreative import AdCreative from facebookads.adobjects.adpreview import AdPreview from facebookads.api import FacebookAdsApi access_token = '<ACCESS_TOKEN>' app_secret = '<APP_SECRET>' app_id = '<APP_ID>' id = '<ID>' FacebookAdsApi.init(access_token=access_token) fields = [] params = { 'ad_format': 'DESKTOP_FEED_STANDARD', 'product_item_ids': ['<productItemID>'], } print AdCreative(id).get_previews( fields=fields, params=params, )
def create_carousel_ad(caption, adset_id, ad_name, design_list, land_on_design, url, campaign_tag, country_code): FORMAT = '%(name)s:%(levelname)s:%(asctime)-15s:%(message)s' logging.basicConfig(filename='%s-facebook-automated.log' % date.today(), format=FORMAT, level=logging.DEBUG) logging.getLogger('create_carousel') logging.info('In create carousel') connection = None simple_list = [] utm_medium = 'exp' try: connection = header.create_connection( os.environ['FB_APP_DATABASE_URL']) cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor) for design_id in design_list: cursor.execute( 'SELECT designer_id,discount_price from designs where id=' + str(design_id[0])) row = cursor.fetchone() cursor.execute( 'SELECT id,photo_file_name FROM images where design_id = ' + str(design_id[0])) rows = cursor.fetchone() cursor.execute( 'SELECT name FROM "categories" INNER JOIN "categories_designs" ON "categories"."id" = "categories_designs"."category_id" WHERE design_id =' + str(design_id[0])) category_name = cursor.fetchone() cursor.execute( "SELECT rate FROM currency_converts WHERE country_code = '" + country_code + "'") rate = cursor.fetchone() image_link = image_hash.get_image_link(rows['photo_file_name'], rows['id']) product1 = AdCreativeLinkDataChildAttachment() if land_on_design: product1[ AdCreativeLinkDataChildAttachment.Field. link] = 'www.mirraw.com/designers/' + str( row['designer_id'] ) + '/designs/' + str( design_id[0] ) + '?utm_source=facebook-auto&utm_medium=' + utm_medium + '&utm_campaign=' + campaign_tag else: product1[ AdCreativeLinkDataChildAttachment.Field. link] = url + '?pid=' + str( design_id ) + '&utm_source=facebook&utm_medium=' + utm_medium + '&utm_campaign=' + campaign_tag product1[AdCreativeLinkDataChildAttachment.Field. name] = category_name['name'] price = (row['discount_price'] * 1.0 / rate['rate']) product1[AdCreativeLinkDataChildAttachment.Field. description] = 'Price: $' + str(round(price, 2)) logging.info(image_link) logging.info(rows['photo_file_name']) product1[AdCreativeLinkDataChildAttachment.Field. image_hash] = image_hash.get_image_hash( image_link, rows['photo_file_name']) sleep(10) simple_list.append(product1) link = AdCreativeLinkData() link[link.Field.link] = 'www.mirraw.com' link[link.Field.child_attachments] = simple_list link[link.Field.caption] = caption story = AdCreativeObjectStorySpec() story[story.Field.page_id] = header.page_id story[story.Field.link_data] = link creative = AdCreative(parent_id=header.my_account['id']) creative[AdCreative.Field.name] = 'MPA Creative' creative[AdCreative.Field.object_story_spec] = story creative.remote_create() creative = json.loads(str(creative).replace('<AdCreative> ', '')) ad = Ad(parent_id=header.my_account['id']) ad[Ad.Field.name] = ad_name ad[Ad.Field.adset_id] = adset_id ad[Ad.Field.status] = Campaign.Status.paused ad[Ad.Field.creative] = {'creative_id': str(creative['id'])} logging.info('Creating Ad') ad.remote_create() logging.info(ad) except psycopg2.DatabaseError, e: logging.error('Error %s' % e) return False
product1[AdCreativeLinkDataChildAttachment.Field. image_hash] = image_hash.get_image_hash( image_link, rows[1]) simple_list.append(product1) except psycopg2.DatabaseError, e: print 'Error %s' % e sys.exit(1) finally: if con: con.close() caption = raw_input("Please enter a caption for the Ad.\n") link = AdCreativeLinkData() link[link.Field.link] = 'www.mirraw.com' link[link.Field.child_attachments] = simple_list link[link.Field.caption] = caption story = AdCreativeObjectStorySpec() story[story.Field.page_id] = header.page_id story[story.Field.link_data] = link creative = AdCreative(parent_id=header.my_account['id']) creative[AdCreative.Field.name] = 'MPA Creative' creative[AdCreative.Field.object_story_spec] = story creative.remote_create() creative = json.loads(str(creative).replace('<AdCreative> ', '')) print(creative) print creative['creative_id'] print creative['success']
#Image image = AdImage(parent_id=my_parent_id) image[AdImage.Field.filename] = img_filename image.remote_create() #Creative link_data = AdCreativeLinkData() link_data[AdCreativeLinkData.Field.message] = link_message link_data[AdCreativeLinkData.Field.link] = link_url link_data[AdCreativeLinkData.Field.image_hash] = image[AdImage.Field.hash] object_story_spec = AdCreativeObjectStorySpec() object_story_spec[AdCreativeObjectStorySpec.Field.page_id] = page_id object_story_spec[AdCreativeObjectStorySpec.Field.link_data] = link_data creative = AdCreative(parent_id=my_parent_id) creative[AdCreative.Field.name] = 'AdCreative for Link Ad' creative[AdCreative.Field.object_story_spec] = object_story_spec creative.remote_create() creative = AdCreative(parent_id=my_parent_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() # The Actual Ad ad = Ad(parent_id=my_parent_id) # alternate way to specify parameters, could have also used params dictionary like in the Campaign or AdSet creation ad[Ad.Field.name] = ad_name ad[Ad.Field.adset_id] = ad_set_id
from facebookads.adobjects.adcreative import AdCreative from facebookads.adobjects.adcreativelinkdata import AdCreativeLinkData from facebookads.adobjects.adcreativeobjectstoryspec \ import AdCreativeObjectStorySpec import initialization import create_adimage IMAGE_HASH = create_adimage.image_hash AD_ACCOUNT_ID = initialization.account_id PAGE_ID = <YOUR_PAGE_ID> link_data = AdCreativeLinkData() link_data[AdCreativeLinkData.Field.message] = 'try it out' link_data[AdCreativeLinkData.Field.link] = 'www.facebook.com' link_data[AdCreativeLinkData.Field.image_hash] = IMAGE_HASH object_story_spec = AdCreativeObjectStorySpec() object_story_spec[AdCreativeObjectStorySpec.Field.link_data] = link_data object_story_spec[AdCreativeObjectStorySpec.Field.page_id] = PAGE_ID 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)
def create_new_ad_set(): """ Create a new adset """ ad_account = AdAccount(fbid=my_ad_account_id) """ Create and update your ad set here """ #https://developers.facebook.com/docs/marketing-api/reference/ad-campaign params = { AdSet.Field.name: 'joon_SDK_competition', AdSet.Field.promoted_object: { 'page_id': my_page_id, }, AdSet.Field.campaign_id: my_campaign_id, AdSet.Field.is_autobid: True, AdSet.Field.start_time: 1520560800, AdSet.Field.end_time: 1520701200, AdSet.Field.daily_budget: 200, AdSet.Field.billing_event: AdSet.BillingEvent.impressions, AdSet.Field.optimization_goal: AdSet.OptimizationGoal.page_likes, # https://developers.facebook.com/docs/marketing-api/targeting-specs AdSet.Field.targeting: { 'geo_locations': { 'countries': ['IN', 'ID'], }, 'age_min': 13, 'age_max': 35, 'flexible_spec': [ { 'interests': [ { 'id': 6003332344237 , 'name': 'Dogs', }, { 'id': 6004037726009, 'name': 'Pets', }, { 'id': 6003266061909, 'name': 'Food', }, { 'id': 6003156370433, 'name': 'Cute Animals', }, { 'id': 625163160959478, 'name': 'Adorable Animals', }, ], }, ] }, AdSet.Field.status: AdSet.Status.active, } adset = ad_account.create_ad_set(params=params) image = AdImage(parent_id=my_ad_account_id) image[AdImage.Field.filename] = '/Users/deriknguyen/Desktop/CS144/clickmaniac/dog_ads/3.jpg' image.remote_create() # Output image Hash image_hash = image[AdImage.Field.hash] # First, upload the ad image that you will use in your ad creative # Please refer to Ad Image Create for details. link_data = AdCreativeLinkData() link_data[AdCreativeLinkData.Field.message] = '"Like" to find ways to help man\'s best friend.' link_data[AdCreativeLinkData.Field.name] = 'Puppy Love' link_data[AdCreativeLinkData.Field.link] = 'http://www.facebook.com/caltech.clickmaniac' link_data[AdCreativeLinkData.Field.image_hash] = image_hash # Then, use the image hash returned from above object_story_spec = AdCreativeObjectStorySpec() object_story_spec[AdCreativeObjectStorySpec.Field.page_id] = my_page_id object_story_spec[AdCreativeObjectStorySpec.Field.link_data] = link_data creative = AdCreative(parent_id='my_ad_account_id') # creative[AdCreative.Field.title] = 'Puppy Love' # creative[AdCreative.Field.body] = 'Cats are one of the deadliest local predators of birds. "Like" this post if you agree they\'re overated!' # creative[AdCreative.Field.link_url] = 'http://www.facebook.com/caltech.clickmaniac' # creative[AdCreative.Field.image_hash] = image_hash creative[AdCreative.Field.object_story_spec] = object_story_spec # 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 ad = Ad(parent_id=my_ad_account_id) ad[Ad.Field.name] = 'joon_dog_pos_competition' ad[Ad.Field.adset_id] = adset[AdSet.Field.id] ad[Ad.Field.creative] = creative ad.remote_create(params={ 'status': Ad.Status.active, }) return adset
link_data[AdCreativeLinkData.Field.name] = "Le titre de votre publicité" link_data[AdCreativeLinkData.Field.caption] = 'votredomaine.fr' call_to_action = { 'type': 'LEARN_MORE', 'value': { 'link': url, }, } link_data[AdCreativeLinkData.Field.call_to_action] = call_to_action object_story_spec = AdCreativeObjectStorySpec() object_story_spec[AdCreativeObjectStorySpec.Field.page_id] = page_id_facebook object_story_spec[AdCreativeObjectStorySpec.Field.link_data] = link_data creative = AdCreative(parent_id=account_publicitaire) creative[AdCreative.Field.url_tags] = "vos balises utm ici" creative[AdCreative.Field.object_story_spec] = object_story_spec creative.remote_create() ad = Ad(parent_id=account_publicitaire) ad[Ad.Field.name] = variable + ' Ad' ad[Ad.Field.adset_id] = ad_set[AdSet.Field.id] ad[Ad.Field.tracking_specs] = { 'action.type': 'offsite_conversion', 'fb_pixel': YOURFACEBOOKPIXEL } ad[Ad.Field.creative] = { 'creative_id': creative['id'], } ad.remote_create(params={