def create_adgroup(ad_set=create_adset(), creative=create_creative()):
    adgroup = AdGroup(parent_id=test_config.account_id)
    adgroup[AdGroup.Field.name] = unique_name('My AdGroup')
    adgroup[AdGroup.Field.campaign_id] = ad_set.get_id_assured()
    adgroup[AdGroup.Field.status] = AdGroup.Status.paused
    adgroup[AdGroup.Field.creative] = {
        'creative_id': creative.get_id_assured(),
    }
    adgroup.remote_create()

    atexit.register(remote_delete, adgroup)

    return adgroup
    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(),
        AdGroup.Field.creative: {
            AdGroup.Field.Creative.creative_id: creative.get_id_assured(),
        },
    })
    ad.remote_create()
    print("**** DONE: Ad created:")
    pp.pprint(ad)
def create_multiple_website_clicks_ads(
    account,
    name,
    country,
    titles,
    bodies,
    urls,
    image_paths,
    bid_type,
    bid_info,
    daily_budget=None,
    lifetime_budget=None,
    start_time=None,
    end_time=None,
    age_min=None,
    age_max=None,
    genders=None,
    campaign=None,
    paused=False,
):
    # 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 not campaign:
        campaign = AdCampaign(parent_id=account.get_id_assured())
        campaign[AdCampaign.Field.name] = name + ' Campaign'
        campaign[AdCampaign.Field.objective] = \
            AdCampaign.Objective.website_clicks
        campaign[AdCampaign.Field.status] = \
            AdCampaign.Status.active if not paused \
            else AdCampaign.Status.paused
        campaign.remote_create()

    # Create ad set
    ad_set = AdSet(parent_id=account.get_id_assured())
    ad_set[AdSet.Field.campaign_group_id] = campaign.get_id_assured()
    ad_set[AdSet.Field.name] = name + ' AdSet'
    ad_set[AdSet.Field.bid_type] = bid_type
    ad_set[AdSet.Field.bid_info] = bid_info
    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[TargetingSpecsField.geo_locations] = {'countries': [country]}
    if age_max:
        targeting[TargetingSpecsField.age_max] = age_max
    if age_min:
        targeting[TargetingSpecsField.age_min] = age_min
    if genders:
        targeting[TargetingSpecsField.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.get_id_assured())
        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 = account.get_api_assured().new_batch()

        for title, body, url, image_hash in creative_info_batch:
            # Create the ad
            ad = AdGroup(parent_id=account.get_id_assured())
            ad[AdGroup.Field.name] = name + ' Ad'
            ad[AdGroup.Field.campaign_id] = ad_set.get_id_assured()
            ad[AdGroup.Field.creative] = {
                AdCreative.Field.title: title,
                AdCreative.Field.body: body,
                AdCreative.Field.object_url: url,
                AdCreative.Field.image_hash: image_hash,
            }

            ad.remote_create(batch=api_batch, failure=callback_failure)
            ad_groups_created.append(ad)

        api_batch.execute()

    return ad_groups_created
Example #4
0
    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
        '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(),
        AdGroup.Field.creative: {
            AdGroup.Field.Creative.creative_id: creative.get_id_assured(),
        },
    })
    ad.remote_create()
    print("**** DONE: Ad created:")
    pp.pprint(ad)
Example #6
0
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()
# _DOC close [ADGROUP_CREATE]

adgroup.remote_delete()
creative.remote_delete()

# _DOC open [ADGROUP_CREATE_REDOWNLOAD]
# _DOC vars [ad_set_id, creative_id, ad_account_id:s]
from facebookads.objects import AdGroup

adgroup = AdGroup(parent_id=ad_account_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=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()
# _DOC close [ADGROUP_CREATE]

adgroup.remote_delete()
creative.remote_delete()

# _DOC open [ADGROUP_CREATE_REDOWNLOAD]
# _DOC vars [ad_set_id, creative_id, ad_account_id:s]
from facebookads.objects import AdGroup
Example #8
0
            specs.AttachmentData.Field.name: name3,
            specs.AttachmentData.Field.description: description3,
            specs.AttachmentData.Field.image_hash: img3,
            specs.AttachmentData.Field.link: url3,
        })

        link[link.Field.child_attachments] = [product1, product2, product3]
        story[story.Field.link_data] = link
        creative[creative.Field.object_story_spec] = story

        creative.remote_create()
        print("**** DONE: Creative created:")
        pp.pprint(creative.get_id_assured())

        ### Get excited, we are finally creating an ad!!!
        ad = AdGroup(parent_id='act_{}'.format(act_id))
        ad.update({
            AdGroup.Field.name: ad_name,
            # AdGroup.Field.objective : 'WEBSITE_CONVERSIONS',
            # AdGroup.Field.conversion_specs:{
            #     'action.type':'offsite_conversion',
            #     'offsite_pixel':pixel_id
            # },
            AdGroup.Field.campaign_id: adset_id,
            AdGroup.Field.status: 'PAUSED',
            AdGroup.Field.creative: {
                AdGroup.Field.Creative.creative_id: creative.get_id_assured(),
            },
        })
        ad.remote_create()
        print("**** DONE: Ad created:")
import string

ad_account_id = test_config.account_id
app_id = test_config.app_id
page_id = test_config.page_id
ad_set_id = fixtures.create_adset().get_id_assured()
ad_group_id = fixtures.create_adgroup().get_id_assured()
creative_id = fixtures.create_creative().get_id_assured()
image_hash = fixtures.create_image().get_hash()


# _DOC open [ADGROUP_CREATE]
# _DOC vars [ad_account_id:s, ad_set_id, creative_id]
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()
# _DOC close [ADGROUP_CREATE]
adgroup.remote_delete()


# _DOC open [ADGROUP_CREATE_TRACKING_APP_INSTALLS]
# _DOC vars [ad_account_id:s, ad_set_id, creative_id, app_id]
from facebookads.objects import AdGroup
    def same_os_copy(self, campaign, adset, adgroups):
        parent_id = config['act_id'][self.param['account']]
        account = AdAccount(parent_id)
        custom_audiences = account.get_custom_audiences(
            fields=[
                CustomAudience.Field.name,
            ],
            params={'limit': 100}
        )

        copy_custom_audience = []
        copy_excluded_custom_audience = []
        excluded_ids = self.param.getlist('excluded_custom_audiences')
        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 excluded_ids:
                excluded_audience = {
                    'id': custom_audience['id'],
                    'name': custom_audience['name']
                }
                copy_excluded_custom_audience.append(excluded_audience)

        copy_targeting = copy.deepcopy(adset['targeting'])
        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_custom_audience

        # 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: adset['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)

        adgroups = adset.get_ad_groups(
            fields=[
                AdGroup.Field.name,
                AdGroup.Field.creative,
                AdGroup.Field.tracking_specs,
                AdGroup.Field.status
            ],
            params={
                AdGroup.Field.status: ['ACTIVE']
            }
        )

        for i in range(0, len(adgroups)):
            adgroup = adgroups[i]
            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: adgroup['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
    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
def create_multiple_website_clicks_ads(
    account,

    name,
    country,

    titles,
    bodies,
    urls,
    image_paths,

    bid_type,
    bid_info,
    daily_budget=None,
    lifetime_budget=None,
    start_time=None,
    end_time=None,

    age_min=None,
    age_max=None,
    genders=None,

    campaign=None,
    paused=False,
):
    # 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 not campaign:
        campaign = AdCampaign(parent_id=account.get_id_assured())
        campaign[AdCampaign.Field.name] = name + ' Campaign'
        campaign[AdCampaign.Field.objective] = \
            AdCampaign.Objective.website_clicks
        campaign[AdCampaign.Field.status] = \
            AdCampaign.Status.active if not paused \
            else AdCampaign.Status.paused
        campaign.remote_create()

    # Create ad set
    ad_set = AdSet(parent_id=account.get_id_assured())
    ad_set[AdSet.Field.campaign_group_id] = campaign.get_id_assured()
    ad_set[AdSet.Field.name] = name + ' AdSet'
    ad_set[AdSet.Field.bid_type] = bid_type
    ad_set[AdSet.Field.bid_info] = bid_info
    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[TargetingSpecsField.geo_locations] = {
        'countries': [country]
    }
    if age_max:
        targeting[TargetingSpecsField.age_max] = age_max
    if age_min:
        targeting[TargetingSpecsField.age_min] = age_min
    if genders:
        targeting[TargetingSpecsField.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.get_id_assured())
        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 = account.get_api_assured().new_batch()

        for title, body, url, image_hash in creative_info_batch:
            # Create the ad
            ad = AdGroup(parent_id=account.get_id_assured())
            ad[AdGroup.Field.name] = name + ' Ad'
            ad[AdGroup.Field.campaign_id] = ad_set.get_id_assured()
            ad[AdGroup.Field.creative] = {
                AdCreative.Field.title: title,
                AdCreative.Field.body: body,
                AdCreative.Field.object_url: url,
                AdCreative.Field.image_hash: image_hash,
            }

            ad.remote_create(batch=api_batch, failure=callback_failure)
            ad_groups_created.append(ad)

        api_batch.execute()

    return ad_groups_created
Example #13
0
from examples.docs import fixtures
import string

ad_account_id = test_config.account_id
app_id = test_config.app_id
page_id = test_config.page_id
ad_set_id = fixtures.create_adset().get_id_assured()
ad_group_id = fixtures.create_adgroup().get_id_assured()
creative_id = fixtures.create_creative().get_id_assured()
image_hash = fixtures.create_image().get_hash()

# _DOC open [ADGROUP_CREATE]
# _DOC vars [ad_account_id:s, ad_set_id, creative_id]
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()
# _DOC close [ADGROUP_CREATE]
adgroup.remote_delete()

# _DOC open [ADGROUP_CREATE_TRACKING_APP_INSTALLS]
# _DOC vars [ad_account_id:s, ad_set_id, creative_id, app_id]
from facebookads.objects import AdGroup

adgroup = AdGroup(parent_id=ad_account_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=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()
# _DOC close [ADGROUP_CREATE]

adgroup.remote_delete()
creative.remote_delete()
img.remote_delete(params={AdImage.Field.hash: image_hash})

# _DOC open [ADGROUP_CREATE_REDOWNLOAD]
# _DOC vars [ad_set_id, creative_id, ad_account_id:s]