# _DOC open [PAGE_POST_CREATE_VIDEO_VIRTUAL_GOODS] # _DOC vars [page_id, thumbnail_url:s, app_store_url:s, video_path:s, product_link:s] from facebookads import FacebookAdsApi params = { 'name': 'My Video', 'massage': 'Buy coins now!', 'thumbnail': thumbnail_url, 'published': 0, 'call_to_action': { 'type': 'BUY_NOW', 'value': { 'link': app_store_url, 'product_link': product_link, }, }, } files = {'source': open(video_path, 'rb')} data = FacebookAdsApi.get_default_api().call( 'POST', (page_id, 'videos'), params=params, files=files ) # _DOC close [PAGE_POST_CREATE_VIDEO_VIRTUAL_GOODS] FacebookAdsApi.get_default_api().\ call('DELETE', (data.json()['id'],))
# _DOC vars [video_path:s, page_id] from facebookads import FacebookAdsApi link = 'fbgeo://37.48327, -122.15033, "1601 Willow Rd Menlo Park CA"' params = { 'message': 'Come check out our new store in Menlo Park!', 'published': 0, 'call_to_action': { 'type': 'GET_DIRECTIONS', 'value': { 'link': link, }, }, } url = 'https://graph-video.facebook.com/' + FacebookAdsApi.API_VERSION path = "/{}/videos".format(page_id) files = {'source': open(video_path, 'rb')} data = FacebookAdsApi.get_default_api().call( 'POST', url + path, params=params, files=files ) # _DOC close [PAGE_POST_CREATE_VIDEO_GET_DIRECTIONS] FacebookAdsApi.get_default_api().\ call('DELETE', (data.json()['id'],))
# You are hereby granted a non-exclusive, worldwide, royalty-free license to # 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 from facebookads import FacebookAdsApi FacebookAdsApi.set_default_api(fixtures.get_page_api()) # _DOC oncall [pruno] # _DOC open [PAGE_GET_SUBSCRIBEDAPPS] from facebookads import FacebookAdsApi accounts = FacebookAdsApi.get_default_api()\ .call('GET', ('/me/subscribed_apps',)) # _DOC close [PAGE_GET_SUBSCRIBEDAPPS]
def get_ad_sets(self, account_id, include_archived, limit): """ Retrieves and displays a list of ad sets of a given account, and analyze how likely a similar ad for Instagram can be created. Params: * `account_id` is your Facebook Ad Account id. * `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, ], params=params) cache = {} count = 0 results = [] for ad_set in ad_sets: if count >= limit: break count += 1 result = {} result['id'] = ad_set['id'] result['name'] = ad_set['name'] logger.error(ad_set) # Get targeting from ad set targeting = ad_set.get(AdSet.Field.targeting, None) logger.error(targeting) if targeting is not None: publisher_platforms = targeting.get('publisher_platforms', None) pp_str = '' if publisher_platforms is None: result['publisher_platforms'] = '<li>DEFAULT</li>' else: for pp in publisher_platforms: pp_str += ('<li>' + self.translate_placement_publisher( str(pp)) + '</li>') result['publisher_platforms'] = pp_str params = { 'currency': 'USD', 'targeting_spec': targeting, 'optimize_for': AdSet.OptimizationGoal.impressions, } if publisher_platforms is not None and "instagram" in \ publisher_platforms: count -= 1 continue reach_fb = account.get_reach_estimate(params=params)[0].get( 'users', 0) targeting['publisher_platforms'] = ["instagram"] targeting['facebook_positions'] = None params = { 'currency': 'USD', 'targeting_spec': targeting, 'optimize_for': AdSet.OptimizationGoal.impressions, } reach_ig = account.get_reach_estimate(params=params)[0].get( 'users', 0) self.add_check_result(result, self.check_audience(reach_fb, reach_ig)) result["audience"] = reach_ig * 100 / reach_fb result["ig_audience"] = locale.format("%d", reach_ig, grouping=True) # Get objective and status from Campaign campaign_id = ad_set[AdSet.Field.campaign_id] campaign = self.get_ad_campaign(cache, campaign_id) result["c_objective"] = \ campaign[Campaign.Field.objective].replace("_", " ") result["c_status"] = campaign[Campaign.Field.configured_status] check = self.check_objective(result["c_objective"]) if check['eligibility'] == 5: result['objective_supported'] = 1 elif check['eligibility'] == 1: 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: 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( 'GET', (story_id, ), params={ 'fields': 'is_instagram_eligible,child_attachments' }, ) post_ig_eligible = post.json()['is_instagram_eligible'] except FacebookRequestError: post_ig_eligible = False result['creative_ready'] = post_ig_eligible if post_ig_eligible: self.add_check_result(result, { "eligibility": 5, }) # Generate preview # As we do not know which IG account you will use, # just use a hardcoded one for preview. jasper_ig_account = "1023317097692584" ad_format = 'INSTAGRAM_STANDARD' creative_spec = { 'instagram_actor_id': jasper_ig_account, 'object_story_id': story_id, } params = { AdPreview.Field.creative: creative_spec, AdPreview.Field.ad_format: ad_format, } preview = account.get_generate_previews(params=params) result['preview_url'] = preview[0].get_html() \ .replace('width="320"', 'width="340"', 1) else: comment = 'The creative needs to be modified for Instagram.' self.add_check_result(result, { "eligibility": 3, }) result['preview_url'] = comment results.append(result) return list( sorted(results, key=lambda result: result['eligibility'], reverse=True))
# You are hereby granted a non-exclusive, worldwide, royalty-free license to # 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 from facebookads import FacebookAdsApi FacebookAdsApi.set_default_api(fixtures.get_page_api()) # _DOC oncall [pruno] # _DOC open [PAGE_GET_LEADGENQUALIFIERS] from facebookads import FacebookAdsApi accounts = FacebookAdsApi.get_default_api()\ .call('GET', ('/me/leadgen_qualifiers',)) # _DOC close [PAGE_GET_LEADGENQUALIFIERS]
# _DOC vars [page_id, thumbnail_url:s, app_url:s, video_path:s] from facebookads import FacebookAdsApi params = { 'name': 'My Video', 'massage': 'Check out this app!', 'thumbnail': thumbnail_url, 'published': 0, 'call_to_action': { 'type': 'PLAY_GAME', 'value': { 'link': app_url, }, }, } url = 'https://graph-video.facebook.com/' + FacebookAdsApi.API_VERSION path = "/{}/videos".format(page_id) files = {'source': open(video_path, 'rb')} data = FacebookAdsApi.get_default_api().call( 'POST', url + path, params=params, files=files ) # _DOC close [PAGE_POST_CREATE_VIDEO_DESKTOP] delete_path = '/' + data.json()['id'] response = FacebookAdsApi.get_default_api().call('DELETE', url + delete_path)
# DEALINGS IN THE SOFTWARE. from facebookads import test_config, FacebookAdsApi from examples.docs import fixtures from facebookads.objects import AdImage page_id = test_config.page_id app_id, app_store_url = fixtures.get_promotable_ios_app() app_deep_link = "facebook://" app_secret = test_config.app_secret image_url = fixtures.create_image()[AdImage.Field.url] FacebookAdsApi.set_default_api(fixtures.get_page_api()) # _DOC open [PAGE_POST_CREATE_IMAGE_WITH_DEEP_LINK] # _DOC vars [page_id, image_url:s, app_store_url:s, app_deep_link:s] from facebookads import FacebookAdsApi params = { "massage": "Download Facebook today. Available on iTunes.", "picture": image_url, "published": 1, "call_to_action": {"type": "LEARN_MORE", "value": {"link": app_store_url, "app_link": app_deep_link}}, } path = "/{}/feed".format(page_id) data = FacebookAdsApi.get_default_api().call("POST", (path,), params=params) # _DOC close [PAGE_POST_CREATE_IMAGE_WITH_DEEP_LINK] delete_path = "/" + data.json()["id"] response = FacebookAdsApi.get_default_api().call("DELETE", (delete_path,))
from facebookads.objects import AdImage page_id = test_config.page_id app_id, app_store_url = fixtures.get_promotable_ios_app() app_secret = test_config.app_secret image_url = fixtures.create_image()[AdImage.Field.url] FacebookAdsApi.set_default_api(fixtures.get_page_api()) # _DOC open [PAGE_POST_CREATE_IMAGE] # _DOC vars [page_id, image_url:s, app_store_url:s] from facebookads import FacebookAdsApi params = { 'massage': 'Download Facebook today. Available on iTunes.', 'picture': image_url, 'published': 1, 'call_to_action': { 'type': 'SIGN_UP', 'value': { 'link': app_store_url, }, }, } data = FacebookAdsApi.get_default_api().\ call('POST', (page_id, 'feed'), params=params) # _DOC close [PAGE_POST_CREATE_IMAGE] FacebookAdsApi.get_default_api().\ call('DELETE', (data.json()['id'],))
def api_post(path, params={}, api=None, url=default_api_url, **kwargs): if api is None: api = FacebookAdsApi.get_default_api() response = api.call('POST', url + path, params=params, **kwargs) return response.json()
def api_get(path, api=None, url=default_api_url): if api is None: api = FacebookAdsApi.get_default_api() response = api.call('GET', url + path) return response.json()
# Copyright 2014 Facebook, Inc. # You are hereby granted a non-exclusive, worldwide, royalty-free license to # 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. # _DOC oncall [pruno] # _DOC open [ME_GET_ACCOUNTS] from facebookads import FacebookAdsApi accounts = FacebookAdsApi.get_default_api().call('GET', ('/me/accounts',)) # _DOC close [ME_GET_ACCOUNTS]
AdConversionPixel, ConnectionObject, LeadgenForm, ProductAudience, ProductCatalog, ProductSet, Page, AdPlacePageSet) from facebookads.specs import LinkData, ObjectStorySpec import time import atexit default_url = 'https://graph.facebook.com/' + FacebookAdsApi.API_VERSION default_api = FacebookAdsApi.get_default_api() def api_get(path, params=None, api=default_api, url=default_url): if params is None: params = {} response = api.call('GET', url + path) return response.json() def api_delete(path, params=None, api=default_api, url=default_url): if params is None: params = {} response = api.call('DELETE', url + path, params=params) return response.json()