def get_ad_set_data(ad_account: AdAccount) -> {}: """Retrieves the ad set data of the ad account as a dictionary Args: ad_account: An ad account for which to retrieve the ad set data Returns: A dictionary with {ad_set_id: {'name': 1, 'campaign_id': 2, 'attributes': {}}} format """ logging.info('get ad set data for account {}'.format( ad_account['account_id'])) ad_sets = ad_account.get_ad_sets( fields=['id', 'name', 'campaign_id', 'adlabels'], params={ 'limit': 1000, 'status': ['ACTIVE', 'PAUSED', 'ARCHIVED'] }) result = {} for ad_set in ad_sets: result[ad_set['id']] = { 'name': ad_set['name'], 'campaign_id': ad_set['campaign_id'], 'attributes': parse_labels(ad_set.get('adlabels', [])) } return result
def get_adsets_from_adaccount(account_id): account = AdAccount(account_id) fields = [ AdSet.Field.name, ] adsets = account.get_ad_sets(fields=fields) return adsets
def get_ad_set(self): account = AdAccount(self.act_id) adsets = account.get_ad_sets( fields=[ AdSet.Field.name, AdSet.Field.status, AdSet.Field.campaign_group_id ], params={ 'campaign_status': ['ACTIVE'], 'limit': 100 } ) campaigns = account.get_ad_campaigns( fields=[ AdCampaign.Field.name, AdCampaign.Field.status ], params={ 'campaign_group_status': ['ACTIVE'] } ) ads = [] for adset in adsets: for i in range(0, len(campaigns)): campaign = campaigns[i] if adset['campaign_group_id'] == campaign['id']: ads.append({ 'id': adset['id'], 'name': adset['name'], 'campaign_name': campaign['name'] }) break return sorted(ads, key=lambda ad: ad['campaign_name'] + ad['name'])
from facebookads.objects import AdAccount account = AdAccount(account_id) account[AdAccount.Field.spend_cap] = 10000 account.remote_update() # _DOC close [ADACCOUNT_UPDATE_SPEND_CAP] account[AdAccount.Field.spend_cap] = old_spend_cap account.remote_update() # _DOC open [ADACCOUNT_GET_ADSETS] from facebookads.objects import AdAccount, AdSet account = AdAccount(account_id) adsets = account.get_ad_sets(fields=[AdSet.Field.name]) for adset in adsets: print(adset[AdSet.Field.name]) # _DOC close [ADACCOUNT_GET_ADSETS] # _DOC open [ADACCOUNT_GET_CONNECTION_OBJECTS] from facebookads.objects import AdAccount account = AdAccount(account_id) objects = account.get_connection_objects() for obj in objects: print(obj[AdAccount.Field.name]) # _DOC close [ADACCOUNT_GET_CONNECTION_OBJECTS]
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))
account = AdAccount(account_id) account[AdAccount.Field.spend_cap] = 10000 account.remote_update() # _DOC close [ADACCOUNT_UPDATE_SPEND_CAP] account[AdAccount.Field.spend_cap] = old_spend_cap account.remote_update() # _DOC open [ADACCOUNT_GET_ADSETS] # _DOC vars [account_id:s] from facebookads.objects import AdAccount, AdSet account = AdAccount(account_id) adsets = account.get_ad_sets(fields=[AdSet.Field.name]) for adset in adsets: print(adset[AdSet.Field.name]) # _DOC close [ADACCOUNT_GET_ADSETS] # _DOC open [ADACCOUNT_GET_CONNECTION_OBJECTS] # _DOC vars [account_id:s] from facebookads.objects import AdAccount account = AdAccount(account_id) objects = account.get_connection_objects() for obj in objects: print(obj[AdAccount.Field.name]) # _DOC close [ADACCOUNT_GET_CONNECTION_OBJECTS]
def retrieve_eligible_adsets_for_an( self, accountid, includepaused=False, ): """ This method returns all eligible ad sets that can have audience networked turned on for a given ad account id. Args: accountid: The ad account id (should be of the form act_<act_id>) for which you are running this exercise. inlcudepaused: Boolen parameter to make your method consider ad sets with paused states (PAUSED & CAMPAIGN_PAUSED). Checks only ACTIVE by default. Returns: List of ad set objects (if found satisfying the conditions) or an empty list. """ # use accountid to retrieve all active adsets account = AdAccount(accountid) adsetfields = [ AdSet.Field.id, AdSet.Field.name, AdSet.Field.campaign_id, AdSet.Field.targeting, AdSet.Field.effective_status, ] adsets = list(account.get_ad_sets(fields=adsetfields)) # Filter ad sets received by desired status and placement types. # Further filter by campaign objectives listed in the criteria below. # # Ref: https://developers.facebook.com/docs/ # marketing-api/audience-network/v2.5 desired_campaign_status = set(['ACTIVE']) # mostly useful in testing when you don't have active campaigns if includepaused is True: desired_campaign_status.update({'PAUSED', 'CAMPAIGN_PAUSED'}) desired_campaign_objectives = set([ 'MOBILE_APP_INSTALLS', 'MOBILE_APP_ENGAGEMENT', 'LINK_CLICKS', 'CONVERSIONS', 'PRODUCT_CATALOG_SALES', ]) # Hold the result set eligible_adsets = [] for adset in adsets: if adset[AdSet.Field.effective_status] in desired_campaign_status: """ 'devide_platforms', 'publisher_platforms' and 'facebook_positions' could be absent for the default of 'ALL' """ device_platforms = None if TargetingSpecsField.device_platforms in \ adset[AdSet.Field.targeting]: device_platforms = set(adset[AdSet.Field.targeting][ TargetingSpecsField.device_platforms]) publisher_platforms = None if TargetingSpecsField.publisher_platforms in \ adset[AdSet.Field.targeting]: publisher_platforms = set(adset[AdSet.Field.targeting][ TargetingSpecsField.publisher_platforms]) facebook_positions = None if TargetingSpecsField.facebook_positions in \ adset[AdSet.Field.targeting]: facebook_positions = set(adset[AdSet.Field.targeting][ TargetingSpecsField.facebook_positions]) if ((facebook_positions is None or 'feed' in facebook_positions) and (device_platforms is None or 'mobile' in device_platforms)): if (publisher_platforms is None or 'audience_network' in publisher_platforms): # audience network already enabled, so just pass continue else: campaign = Campaign(adset[AdSet.Field.campaign_id]) campaignfields = [ Campaign.Field.id, Campaign.Field.name, Campaign.Field.effective_status, Campaign.Field.objective, ] campaign = campaign.remote_read(fields=campaignfields) if (campaign[Campaign.Field.objective] in desired_campaign_objectives): eligible_adsets.append(adset) return eligible_adsets