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 key_raw(self): self.put = [] for ad_id in self.ad_ids: ad = Ad(ad_id) key = ad.get_keyword_stats(fields=[ 'id', 'name', 'clicks', 'impressions', 'reach', 'actions' ]) self.put.append(key)
def delete_ad(request, adid): FacebookAdsApi.init(access_token=access_token, app_id=app_id, app_secret=app_secret) ad = Ad(adid) ad.remote_delete() return redirect('view_ad')
def get_adcreative_by_adid(self, ad_id): ad = Ad('%s' % (ad_id)) creatives = ad.get_ad_creatives(fields=[ AdCreative.Field.id, AdCreative.Field.name, AdCreative.Field.image_hash, AdCreative.Field.image_url, AdCreative.Field.object_type, AdCreative.Field.object_story_id, AdCreative.Field.object_story_spec, AdCreative.Field.status ]) if creatives: return creatives return {}
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 schedule_ads(self, act_id, ad_name, ad_set_id, creative_id): ad = Ad(parent_id='act_{}'.format(act_id)) ad[Ad.Field.name] = ad_name ad[Ad.Field.adset_id] = ad_set_id ad[Ad.Field.creative] = { 'creative_id': creative_id, } ad.remote_create(params={ 'status': Ad.Status.paused, }) return ad
def create_ad(account_id, adset_id, creative_id, name): from facebookads.adobjects.ad import Ad ad = Ad(parent_id=account_id) ad[Ad.Field.name] = name ad[Ad.Field.adset_id] = adset_id ad[Ad.Field.creative] = { 'creative_id': creative_id, } ad.remote_create(params={ 'status': Ad.Status.paused, }) return ad
def setCreativeDict(self, ad_id): ad = Ad(ad_id) creative = ad.get_ad_creatives(fields=[ AdCreative.Field.title, AdCreative.Field.body, ]) creative = creative[0] title = creative.get('title', 'Unspecified') ad_copy = creative.get('body', 'Unspecified') self.creative_dict[ad_id] = \ { 'title': title, 'ad_copy': ad_copy }
def create_ad(request, adsetid): FacebookAdsApi.init(access_token=access_token, app_id=app_id, app_secret=app_secret) my_account = AdAccount('act_' + user_id) ad = Ad(parent_id=my_account.get_id_assured()) ad[Ad.Field.name] = 'My Ad' ad[Ad.Field.adset_id] = adsetid ad[Ad.Field.creative] = { 'creative_id': "creative", } ad.remote_create(params={ 'status': Ad.Status.paused, }) return redirect('view-ad')
def get_ad_insights(self, ad_id, breakdown_attribute=None, start_dt=None, stop_dt=None): ad = Ad('%s' % (ad_id)) params = {} if breakdown_attribute: params['breakdowns'] = breakdown_attribute if start_dt and stop_dt: params['time_range'] = {'since': start_dt, 'until': stop_dt} params['fields'] = [ 'account_id', 'account_name', 'campaign_id', 'campaign_name', 'adset_id', 'adset_name', 'ad_id', 'ad_name', 'buying_type', 'spend', 'clicks', 'impressions', 'cpc', 'cpm', 'cpp', 'ctr', 'actions', 'frequency' ] params['level'] = 'ad' insights = ad.get_insights(params=params) if insights: return insights return {}
creative_id = creative.get_id() print('creative_id:', creative_id, '\n') fields = [] params = { 'status': 'PAUSED', 'adset_id': ad_set_id, 'name': 'My Ad', 'creative': { 'creative_id': creative_id }, } ad = AdAccount(ad_account_id).create_ad( fields=fields, params=params, ) print('ad', ad) ad_id = ad.get_id() print('ad_id:', ad_id, '\n') fields = [] params = { 'ad_format': 'DESKTOP_FEED_STANDARD', } print(Ad(ad_id).get_previews( fields=fields, params=params, ))
# 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.ad import Ad 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 = { 'adgroup_status': 'PAUSED', } print Ad(id).update( fields=fields, params=params, )
def get_ad_insights(id, fields, params): ad = Ad(id) return list(ad.get_insights(params=params, fields=fields))
from facebookads.adobjects.ad import Ad ad = Ad('238430773153005') leads = ad.get_leads()
app_token = '216523165752115|PwXuxY-VMILR0UMgqTXEIt0S6_8' user_token='EAADE7TphWzMBAEwjGLP1ZBiqycUqaA7V9httYnu7lkLvVHINPZA0IGcDDUE0pEhnCsGfTTLISkH1u3tYOhqpVS7pX' access_token = 'EAADE7TphWzMBAKZBZBNWyKPDiWxYgb7HmbeLqaR0WxjzqcZBIlE29CJUNTCUGZAeGmFvnPDqGUOnJvk4DogM2Hw3FQhGeMmIRgDNUC8iiLzsEIZCa0RJgVLIGS9ilefgc6iPZB7razH3lORkdAGLvE4jh8ZAExYYYOKyeU2ZAxixdHxn9Fw7YhU2ntqQxImvPmUZD' """ app_id = "146054626071110" app_secret = "fb66e45bd80f57c619a209e77b09a905" user_token='EAAF7pPE6azkBABq1SjxBDZALNCyua8KncmEMDeZBHshZA2ySIZCKtN6jfBunBkGbYPS1LwrDxC5ETccT6Tcs' \ 'FDYX7ZA1m9QkSUgH3tEJpJPT6Vj9I5rBqe7TlPT3qCffGcScJm3nI6ZAzNyM9nZB61vCAmOV8IZBZAxm71DwnnVibDgZAdfne3wRpgFuHMcwx5eKoZD' access_token = "EAACE1f4a0kYBAPlUxz5AbMmwzQapkJ0DkkGLPEjDBB2ZCwRxZCAZAN84MGwEOTCgzJBgAEMEMKHFQzuBe5SZAbXjHA39I9ztYMDSlks9PxQ2mYKh23IiZBm2fBEoPnJ4YgnDCCuRltrqA2edePRYvKukhoFJUzio9wa8k2BXYqADmcKg0jI25ZADv2ZA9m5OZB4ZD" app_token = "417423328701241|hyOJvm8h5JpFXMyOiMQQZhOBwFs" FacebookAdsApi.init(app_id, app_secret, access_token) account = AdAccount('act_693598040844198') ad = Ad(fbid='act_693598040844198') creatives = ad.get_ad_creatives(fields=[ AdCreative.Field.id, AdCreative.Field.object_story_spec, ]) #print(account.get_activities()) #"event_time": "2018-03-10T01:05:58+0000", #"event_type": "update_ad_run_status" header = "access_token=" + access_token #url = "https://graph.facebook.com/v2.10/act_693598040844198/campaigns?fields=name,status,insights{reach,impressions,clicks}" #res = r.get(url,headers=header) #i = AdAccount(aid).get_insights(fields=fields, async=True)
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
from facebookads.adobjects.ad import Ad import initialization AD_ACCOUNT_ID = initialization.account_id AD_SET_ID = <YOUR_AD_SET_ID> ad = Ad(parent_id=AD_ACCOUNT_ID) ad[Ad.Field.name] = 'My Ad' ad[Ad.Field.adset_id] = AD_SET_ID ad[Ad.Field.creative] = { 'creative_id': <YOUR_CREATIVE_ID>, } ad.remote_create(params={ 'status': Ad.Status.paused, })
def create_multiple_website_clicks_ads( account, name, country, titles, bodies, urls, image_paths, optimization_goal, pixel_id, billing_event, bid_amount, daily_budget=None, lifetime_budget=None, start_time=None, end_time=None, age_min=None, age_max=None, genders=None, campaign=None, status=AdSet.Status.paused, ): # Check for bad specs if daily_budget is None: if lifetime_budget is None: raise TypeError( 'One of daily_budget or lifetime_budget must be defined.' ) elif end_time is None: raise TypeError( 'If lifetime_budget is defined, end_time must be defined.' ) # Create campaign if campaign: campaign = Campaign(fbid=campaign)[Campaign.Field.id] else: campaign = Campaign(parent_id=account) campaign[Campaign.Field.name] = name + ' Campaign' campaign[Campaign.Field.objective] = \ Campaign.Objective.conversions campaign[Campaign.Field.status] = \ Campaign.Status.active if not status \ else Campaign.Status.paused campaign.remote_create() campaign = campaign[AdSet.Field.id] # Create ad set ad_set = AdSet(parent_id=account) ad_set[AdSet.Field.campaign_id] = campaign ad_set[AdSet.Field.name] = name + ' AdSet' ad_set[AdSet.Field.optimization_goal] = optimization_goal ad_set[AdSet.Field.promoted_object] = { 'pixel_id': pixel_id, 'custom_event_type': 'COMPLETE_REGISTRATION' } ad_set[AdSet.Field.billing_event] = billing_event ad_set[AdSet.Field.bid_amount] = bid_amount if daily_budget: ad_set[AdSet.Field.daily_budget] = daily_budget else: ad_set[AdSet.Field.lifetime_budget] = lifetime_budget if end_time: ad_set[AdSet.Field.end_time] = end_time if start_time: ad_set[AdSet.Field.start_time] = start_time targeting = {} targeting[Targeting.Field.geo_locations] = { 'countries': [country] } if age_max: targeting[Targeting.Field.age_max] = age_max if age_min: targeting[Targeting.Field.age_min] = age_min if genders: targeting[Targeting.Field.genders] = genders ad_set[AdSet.Field.targeting] = targeting ad_set.remote_create() # Upload the images first one by one image_hashes = [] for image_path in image_paths: img = AdImage(parent_id=account) img[AdImage.Field.filename] = image_path img.remote_create() image_hashes.append(img.get_hash()) ADGROUP_BATCH_CREATE_LIMIT = 10 ad_groups_created = [] def callback_failure(response): raise response.error() # For each creative permutation for creative_info_batch in generate_batches( itertools.product(titles, bodies, urls, image_hashes), ADGROUP_BATCH_CREATE_LIMIT ): api_batch = api.new_batch() for title, body, url, image_hash in creative_info_batch: # Create the ad ad = Ad(parent_id=account) ad[Ad.Field.name] = name + ' Ad' ad[Ad.Field.adset_id] = ad_set[AdSet.Field.id] ad[Ad.Field.creative] = { AdCreative.Field.title: title, AdCreative.Field.body: body, AdCreative.Field.object_url: url, AdCreative.Field.image_hash: image_hash, } ad[Ad.Field.status] = status ad.remote_create(batch=api_batch, failure=callback_failure) ad_groups_created.append(ad) api_batch.execute() return ad_groups_created
creative_id = creative.get_id() print 'creative_id:', creative_id, '\n' fields = [ ] params = { 'name': 'My Ad', 'adset_id': ad_set_id, 'creative': {'creative_id': creative_id}, 'status': 'PAUSED', } ad = AdAccount(ad_account_id).create_ad( fields=fields, params=params, ) print 'ad', ad ad_id = ad.get_id() print 'ad_id:', ad_id, '\n' fields = [ ] params = { 'ad_format': 'DESKTOP_FEED_STANDARD', } print Ad(ad_id).get_previews( fields=fields, params=params, )
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 ad[Ad.Field.creative] = { 'creative_id': creative_id, } ad.remote_create(params={ 'status': ad_status, }) ########### List Campaigns ############# print '\nCurrent Campaigns:' my_campaigns = list(my_account.get_campaigns())
from facebookads.adobjects.ad import Ad import header import link_ad as created_ad ad = Ad(parent_id=header.my_account['id']) ad[Ad.Field.name] = 'My Ad' ad[Ad.Field.adset_id] = created_ad.create_adset.adset['id'] ad[Ad.Field.creative] = { 'creative_id': link_ad.creative['id'], } ad.remote_create(params={ 'status': Ad.Status.paused, })
'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={ 'status': Ad.Status.active, }) print('Campaign successfully created, congrats!')