def copy(self):
        store_url = config['link_url'][self.param['account']][self.param['os']]
        link_url = store_url.replace('https://', 'http://')
        campaign = AdCampaign(self.param['campaign_id'])
        campaign.remote_read(
            fields=[
                AdCampaign.Field.name
            ]
        )

        adset = AdSet(self.param['adset_id'])
        adset.remote_read(
            fields=[
                AdSet.Field.name,
                AdSet.Field.daily_budget,
                AdSet.Field.pacing_type,
                AdSet.Field.promoted_object,
                AdSet.Field.targeting,
                AdSet.Field.is_autobid,
                AdSet.Field.billing_event,
                AdSet.Field.optimization_goal,
                AdSet.Field.bid_amount,
                AdSet.Field.rtb_flag
            ]
        )
        # pp.pprint(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']
            }
        )
        # pp.pprint(adgroups)

        object_store_url = adset['promoted_object']['object_store_url']
        replace_url = object_store_url.replace('https://', 'http://')
        if link_url != replace_url:
            return self.different_os_copy(campaign, adset, adgroups)
        else:
            return self.same_os_copy(campaign, adset, adgroups)
Exemplo n.º 2
0
def create_adcampaign(params={}):
    campaign = AdCampaign(parent_id=test_config.account_id)
    campaign[AdCampaign.Field.name] = unique_name('Test Campaign')
    campaign[AdCampaign.Field.buying_type] = AdCampaign.BuyingType.auction
    campaign[AdCampaign.Field.objective] = AdCampaign.Objective.none
    campaign[AdCampaign.Field.status] = AdCampaign.Status.paused

    campaign.update(params)
    campaign.remote_create()

    atexit.register(remote_delete, campaign)

    return campaign
Exemplo n.º 3
0
)
api = FacebookAdsApi(session)

if __name__ == '__main__':
    FacebookAdsApi.set_default_api(api)

    print('\n\n\n********** Ad Creation example. **********\n')

    ### Setup user and read the object from the server
    me = AdUser(fbid='me')

    ### Get first account connected to the user
    my_account = me.get_ad_account()

    ### Create a Campaign
    campaign = AdCampaign(parent_id=my_account.get_id_assured())
    campaign.update({
        AdCampaign.Field.name: 'Seattle Ad Campaign',
        AdCampaign.Field.objective: AdCampaign.Objective.website_clicks,
        AdCampaign.Field.status: AdCampaign.Status.paused,
    })
    campaign.remote_create()
    print("**** DONE: Campaign created:")
    pp.pprint(campaign)

    ### Create an Ad Set
    ad_set = AdSet(parent_id=my_account.get_id_assured())
    ad_set.update({
        AdSet.Field.name: 'Puget Sound AdSet',
        AdSet.Field.status: AdSet.Status.paused,
        AdSet.Field.bid_type: AdSet.BidType.cpm,  # Bidding for impressions
# 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 import test_config as config

ad_account_id = config.account_id
access_token = config.access_token
app_id = config.app_id
app_secret = config.app_secret

# _DOC open [ADCAMPAIGN_CREATE_WEBSITE_CONVERSIONS]
# _DOC vars [ad_account_id:s]
from facebookads.objects import AdCampaign

campaign = AdCampaign(parent_id=ad_account_id)
campaign[AdCampaign.Field.name] = 'My First Campaign'
campaign[AdCampaign.Field.status] = AdCampaign.Status.paused
campaign[AdCampaign.Field.objective] = AdCampaign.Objective.website_conversions

campaign.remote_create()
print(campaign)
# _DOC close [ADCAMPAIGN_CREATE_WEBSITE_CONVERSIONS]
campaign.remote_delete()

# _DOC open [ADCAMPAIGN_CREATE_HOMEPAGE]
# _DOC vars [ad_account_id:s]
from facebookads.objects import AdCampaign

campaign = AdCampaign(parent_id=ad_account_id)
campaign.update({
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
Exemplo n.º 6
0
)
api = FacebookAdsApi(session)

if __name__ == '__main__':
    FacebookAdsApi.set_default_api(api)

    print('\n\n\n********** Ad Creation example. **********\n')

    ### Setup user and read the object from the server
    me = AdUser(fbid='me')

    ### Get first account connected to the user
    my_account = me.get_ad_account()

    ### Create a Campaign
    campaign = AdCampaign(parent_id=my_account.get_id_assured())
    campaign.update({
        AdCampaign.Field.name: 'Seattle Ad Campaign',
        AdCampaign.Field.objective: AdCampaign.Objective.website_clicks,
        AdCampaign.Field.status: AdCampaign.Status.paused,
    })
    campaign.remote_create()
    print("**** DONE: Campaign created:")
    pp.pprint(campaign)

    ### Create an Ad Set
    ad_set = AdSet(parent_id=my_account.get_id_assured())
    ad_set.update({
        AdSet.Field.name: 'Puget Sound AdSet',
        AdSet.Field.status: AdSet.Status.paused,
        AdSet.Field.bid_type: AdSet.BidType.cpm,  # Bidding for impressions
Exemplo n.º 7
0
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
'''
    This is a template for DocSmith samples in Python. Copy it and create yours

    Code should follow guidelines at
    https://our.intern.facebook.com/intern/wiki/Solutions_Engineering/DocSmith

    Each example should be run using facebookads/docs_runner/doc_runner.py

    Comments on style:
    - IDs should be defined outside of _DOC blocks so they don't appear into the
    docs
    - Dependencies, like campaigns, should be generated in the fixtures module
'''

from examples.docs import fixtures

campaign_group_id = fixtures.create_adcampaign().get_id_assured()

#! _DOC open [TEMPLATE]
#! _DOC vars [campaign_group_id]
from facebookads.objects import AdCampaign, AdGroup

ad_campaign = AdCampaign(campaign_group_id)
ad_groups = ad_campaign.get_ad_groups(fields=[AdGroup.Field.name])

for ad_group in ad_groups:
    print(ad_group[AdGroup.Field.name])
#! _DOC close [TEMPLATE]
config_file = open(os.path.join(this_dir, 'config.json'))
config = json.load(config_file)
config_file.close()

ad_account_id = config['account_id']
access_token = config['access_token']
app_id = config['app_id']
app_secret = config['app_secret']

FacebookAdsApi.init(app_id, app_secret, access_token)

# _DOC open [ADCAMPAIGN_CREATE_WEBSITE_CONVERSIONS]
# _DOC vars [ad_account_id:s]
from facebookads.objects import AdCampaign

campaign = AdCampaign(parent_id=ad_account_id)
campaign[AdCampaign.Field.name] = 'My First Campaign'
campaign[AdCampaign.Field.status] = AdCampaign.Status.paused
campaign[AdCampaign.Field.objective] = AdCampaign.Objective.website_conversions

campaign.remote_create()
print(campaign)
# _DOC close [ADCAMPAIGN_CREATE_WEBSITE_CONVERSIONS]
campaign.remote_delete()

# _DOC open [ADCAMPAIGN_CREATE_HOMEPAGE]
# _DOC vars [ad_account_id:s]
from facebookads.objects import AdCampaign

campaign = AdCampaign(parent_id=ad_account_id)
campaign.update({
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
Exemplo n.º 10
0
"""
    This is a template for DocSmith samples in Python. Copy it and create yours

    Code should follow guidelines at
    https://our.intern.facebook.com/intern/wiki/Solutions_Engineering/DocSmith

    Each example should be run using facebookads/docs_runner/doc_runner.py

    Comments on style:
    - IDs should be defined outside of _DOC blocks so they don't appear into the
    docs
    - Dependencies, like campaigns, should be generated in the fixtures module
"""

from examples.docs import fixtures

campaign_group_id = fixtures.create_adcampaign().get_id_assured()


#! _DOC open [TEMPLATE]
#! _DOC vars [campaign_group_id]
from facebookads.objects import AdCampaign, AdGroup

ad_campaign = AdCampaign(campaign_group_id)
ad_groups = ad_campaign.get_ad_groups(fields=[AdGroup.Field.name])

for ad_group in ad_groups:
    print(ad_group[AdGroup.Field.name])
#! _DOC close [TEMPLATE]
Exemplo n.º 11
0
# DEALINGS IN THE SOFTWARE.

from facebookads import test_config
from examples.docs import fixtures
import time

ad_account_id = test_config.account_id
campaign_group_id = fixtures.create_adcampaign().get_id()
product_catalog_id = test_config.product_catalog_id
product_set_id = test_config.product_set_id

# _DOC open [ADCAMPAIGN_CREATE_HOMEPAGE]
# _DOC vars [ad_account_id:s]
from facebookads.objects import AdCampaign

campaign = AdCampaign(parent_id=ad_account_id)
campaign.update({
    AdCampaign.Field.name: 'Homepage Campaign',
    AdCampaign.Field.buying_type: AdCampaign.BuyingType.fixed_cpm,
    AdCampaign.Field.objective: AdCampaign.Objective.none,
    AdCampaign.Field.status: AdCampaign.Status.paused,
})

campaign.remote_create()
print(campaign)
# _DOC close [ADCAMPAIGN_CREATE_HOMEPAGE]
campaign.remote_delete()


# _DOC open [ADCAMPAIGN_CREATE_VIDEO_VIEWS]
# _DOC vars [ad_account_id:s]