def create_creative(self, account_id, name, image_hash, message, page_id,
                        app_name, app_store_link, deferred_app_link):
        """
        Step 4: create ad creative with call to action type to be
        'INSTALL_MOBILE_APP'. See [Ad Creative][1] for further details on the
        API used here.
        [1]: https://developers.facebook.com/docs/marketing-api/adcreative
        """
        link_data = LinkData()
        link_data[LinkData.Field.link] = app_store_link
        link_data[LinkData.Field.message] = message
        link_data[LinkData.Field.image_hash] = image_hash
        call_to_action = {'type': 'INSTALL_MOBILE_APP'}
        call_to_action['value'] = {
            'link': app_store_link,
            'link_title': app_name
        }
        if deferred_app_link:
            call_to_action['value']['app_link'] = deferred_app_link

        link_data[LinkData.Field.call_to_action] = call_to_action

        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=account_id)
        creative[AdCreative.Field.name] = name
        creative[AdCreative.Field.object_story_spec] = object_story_spec
        creative.remote_create()

        return creative[AdCreative.Field.id]
Example #2
0
    def create_creative(self, accountid, name, imagehash, message, appinfo):
        """
        Step 4: create ad creative with call to action type to be
        'USE_MOBILE_APP'. See
        [Ad Creative](https://developers.facebook.com/docs/marketing-api/adcreative)
        for further details on the API used here.
        """
        link_data = LinkData()
        link_data[LinkData.Field.link] = appinfo['appstore_link']
        link_data[LinkData.Field.message] = message
        link_data[LinkData.Field.image_hash] = imagehash
        call_to_action = {'type': 'USE_MOBILE_APP'}
        call_to_action['value'] = {
            'link': appinfo['appstore_link'],
            'app_link': appinfo['app_deep_link'],
            'application': appinfo['fbapplication_id'],
            'link_title': appinfo['app_name']
        }
        link_data[LinkData.Field.call_to_action] = call_to_action

        object_story_spec = ObjectStorySpec()
        object_story_spec[ObjectStorySpec.Field.page_id] = appinfo['fbpage_id']
        object_story_spec[ObjectStorySpec.Field.link_data] = link_data

        creative = AdCreative(parent_id=accountid)
        creative[AdCreative.Field.name] = name
        creative[AdCreative.Field.object_story_spec] = object_story_spec
        creative.remote_create()

        return creative[AdCreative.Field.id]
Example #3
0
    def s4_create_creative(
        self,
        accountid,
        name,
        imagehashes,
        linktitles,
        deeplinks,
        message,
        appinfo
    ):
        """
        Step 4: create ad creative with call to action type to be
        'INSTALL_MOBILE_APP'. See
        [Ad Creative](
        https://developers.facebook.com/docs/marketing-api/adcreative)
        for further details on the API used here.
        """
        attachments = []
        for index, imagehash in enumerate(imagehashes):
            attachment = AttachmentData()
            attachment[AttachmentData.Field.link] = appinfo['appstore_link']
            attachment[AttachmentData.Field.image_hash] = imagehash
            call_to_action = {
                'type': 'INSTALL_MOBILE_APP',
                'value': {
                    'link_title': linktitles[index],
                },
            }
            if deeplinks and index in deeplinks:
                call_to_action['value']['app_link'] = deeplinks[index]

            attachment[AttachmentData.Field.call_to_action] = call_to_action
            attachments.append(attachment)

        link_data = LinkData()
        link_data[LinkData.Field.link] = appinfo['appstore_link']
        link_data[LinkData.Field.message] = message
        link_data[LinkData.Field.child_attachments] = attachments

        object_story_spec = ObjectStorySpec()
        object_story_spec[ObjectStorySpec.Field.page_id] = appinfo['fbpage_id']
        object_story_spec[ObjectStorySpec.Field.link_data] = link_data

        creative = AdCreative(parent_id=accountid)
        creative[AdCreative.Field.name] = name
        creative[AdCreative.Field.object_story_spec] = object_story_spec
        creative.remote_create()

        return creative[AdCreative.Field.id]
def create_creative(image=create_image()):
    image_hash = image.get_hash()

    link_data = LinkData()
    link_data[LinkData.Field.message] = 'try it out'
    link_data[LinkData.Field.link] = 'http://example.com'
    link_data[LinkData.Field.caption] = 'www.example.com'
    link_data[LinkData.Field.image_hash] = image_hash

    object_story_spec = ObjectStorySpec()
    object_story_spec[ObjectStorySpec.Field.page_id] = test_config.page_id
    object_story_spec[ObjectStorySpec.Field.link_data] = link_data

    creative = AdCreative(parent_id=test_config.account_id)
    creative[AdCreative.Field.name] = unique_name('Test Creative')
    creative[AdCreative.Field.object_story_spec] = object_story_spec
    creative.remote_create()

    atexit.register(remote_delete, creative)

    return creative
    def create_lead_ad(
        self,
        account_id,
        name,
        page_id,
        form_id,
        optimization_goal,
        billing_event,
        bid_amount,
        daily_budget,
        targeting,
        image_path,
        message,
        link,
        caption,
        description,
        cta_type='SIGN_UP',
    ):
        """
        Create Campaign
        """
        campaign = Campaign(parent_id=account_id)
        campaign[Campaign.Field.name] = name + ' Campaign'
        campaign[Campaign.Field.objective] = \
            Campaign.Objective.lead_generation
        campaign[Campaign.Field.buying_type] = \
            Campaign.BuyingType.auction

        campaign.remote_create(params={'status': Campaign.Status.paused})
        """
        Create AdSet
        """
        adset = AdSet(parent_id=account_id)
        adset[AdSet.Field.campaign_id] = campaign.get_id_assured()
        adset[AdSet.Field.name] = name + ' AdSet'
        adset[AdSet.Field.promoted_object] = {
            'page_id': page_id,
        }
        adset[AdSet.Field.optimization_goal] = optimization_goal
        adset[AdSet.Field.billing_event] = billing_event
        adset[AdSet.Field.bid_amount] = bid_amount
        adset[AdSet.Field.daily_budget] = daily_budget
        adset[AdSet.Field.targeting] = targeting
        adset.remote_create()
        """
        Image
        """
        image = AdImage(parent_id=account_id)
        image[AdImage.Field.filename] = image_path
        image.remote_create()
        image_hash = image[AdImage.Field.hash]
        """
        Create Creative
        """
        link_data = LinkData()
        link_data[LinkData.Field.message] = message
        link_data[LinkData.Field.link] = link
        link_data[LinkData.Field.image_hash] = image_hash
        link_data[LinkData.Field.caption] = caption
        link_data[LinkData.Field.description] = description
        link_data[LinkData.Field.call_to_action] = {
            'type': cta_type,
            'value': {
                'lead_gen_form_id': form_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=account_id)
        creative[AdCreative.Field.name] = name + ' Creative'
        creative[AdCreative.Field.object_story_spec] = object_story_spec
        creative.remote_create()
        """
        Create Ad
        """
        ad = Ad(parent_id=account_id)
        ad[Ad.Field.name] = name
        ad[Ad.Field.adset_id] = adset.get_id_assured()
        ad[Ad.Field.creative] = {'creative_id': str(creative.get_id_assured())}
        ad.remote_create()

        return {
            'image_hash': image_hash,
            'campaign_id': campaign['id'],
            'adset_id': adset['id'],
            'creative_id': creative['id'],
            'ad_id': ad['id'],
        }
link_data.update({
    LinkData.Field.link: url,
    LinkData.Field.message: "Message",
    LinkData.Field.name: "Name",
    LinkData.Field.caption: "Caption",
    LinkData.Field.description: "My description",
    LinkData.Field.call_to_action: {
        "type": "USE_APP",
        "value": {
            "link": url,
            "link_caption": "CTA caption",
        },
    },
})

story = ObjectStorySpec()
story.update({
    ObjectStorySpec.Field.link_data: link_data,
    ObjectStorySpec.Field.page_id: page_id,
})

creative = AdCreative()
creative.update({
    AdCreative.Field.object_story_spec: story,
})

account = AdAccount(ad_account_id)
params = {
    AdPreview.Field.ad_format: AdPreview.AdFormat.desktop_feed_standard,
    AdPreview.Field.creative: creative.export_data(),
}
    def create_carousel_ad(
        self,
        accountid,
        page_id,
        site_link,
        caption,
        message,
        optimization_goal,
        billing_event,
        bid_amount,
        name,
        targeting,
        products,
        call_to_action_type=None,
    ):
        """
        There are 5 steps in this sample:

        1. Create a campaign
        2. Create an ad set
        3. For each product:
          a. Upload the product's image and get an image hash
          b. Create a story attachment using the product's creative elements
        4. Prepare the ad creative
        5. Create the ad using the ad creative
        """

        daily_budget = 10000
        """
        Step 1: Create new campaign with WEBSITE_CLICKS objective
        See
        [Campaign Group](https://developers.facebook.com/docs/marketing-api/reference/ad-campaign-group)
        for further details on the API used here.
        """
        campaign = Campaign(parent_id=accountid)
        campaign[Campaign.Field.name] = name + ' Campaign'
        campaign[Campaign.Field.objective] = \
            Campaign.Objective.link_clicks

        campaign.remote_create(params={'status': Campaign.Status.paused})
        """
        Step 2: Create AdSet using specified optimization goal, billing event
        and bid.
        See
        [AdSet](https://developers.facebook.com/docs/marketing-api/reference/ad-campaign)
        for further details on the API used here.
        """
        ad_set = AdSet(parent_id=accountid)
        ad_set[AdSet.Field.campaign_id] = campaign.get_id_assured()
        ad_set[AdSet.Field.name] = name + ' AdSet'
        ad_set[AdSet.Field.optimization_goal] = optimization_goal
        ad_set[AdSet.Field.billing_event] = billing_event
        ad_set[AdSet.Field.bid_amount] = bid_amount
        ad_set[AdSet.Field.daily_budget] = daily_budget
        ad_set[AdSet.Field.targeting] = targeting
        ad_set.remote_create()

        story_attachments = []
        """
        Step 3: Upload images and get image hashes for use in ad creative.
        See
        [Ad Image](https://developers.facebook.com/docs/marketing-api/reference/ad-image#Creating)
        for further details on the API used here.
        Then create a new attachment with the product's creative
        """
        call_to_action = {
            'type': call_to_action_type,
            'value': {
                'link': site_link,
                'link_caption': call_to_action_type,
            }
        } if call_to_action_type else None

        for product in products:
            img = AdImage(parent_id=accountid)
            img[AdImage.Field.filename] = product['image_path']
            img.remote_create()
            image_hash = img.get_hash()
            attachment = AttachmentData()
            attachment[AttachmentData.Field.link] = product['link']
            attachment[AttachmentData.Field.name] = product['name']
            attachment[
                AttachmentData.Field.description] = product['description']
            attachment[AttachmentData.Field.image_hash] = image_hash
            if call_to_action:
                attachment[
                    AttachmentData.Field.call_to_action] = call_to_action
            story_attachments.append(attachment)
        """
        Step 4: Prepare the ad creative including link information
        Note that here we specify multi_share_optimized = True
        this means you can add up to 10 products and Facebook will
        automatically select the best performing 5 to show in the ad. Facebook
        will also select the best ordering of those products.
        """
        link = LinkData()
        link[link.Field.link] = site_link
        link[link.Field.caption] = caption
        link[link.Field.child_attachments] = story_attachments
        link[link.Field.multi_share_optimized] = True
        link[link.Field.call_to_action] = call_to_action

        story = ObjectStorySpec()
        story[story.Field.page_id] = page_id
        story[story.Field.link_data] = link

        creative = AdCreative()
        creative[AdCreative.Field.name] = name + ' Creative'
        creative[AdCreative.Field.object_story_spec] = story
        """
        Step 5: Create the ad using the above creative
        """
        ad = Ad(parent_id=accountid)
        ad[Ad.Field.name] = name + ' Ad'
        ad[Ad.Field.adset_id] = ad_set.get_id_assured()
        ad[Ad.Field.creative] = creative

        ad.remote_create(params={'status': Ad.Status.paused})
        return (campaign, ad_set, ad)
from facebookads.objects import AdCreative
from facebookads.specs import TemplateData, ObjectStorySpec

template = TemplateData()
template.update({
    TemplateData.Field.name: '{{page.name}}',
    TemplateData.Field.message: 'Ad Message',
    TemplateData.Field.description: 'Ad Description',
    TemplateData.Field.link: url,
    TemplateData.Field.picture: image_url,
    TemplateData.Field.call_to_action: {
        'type': 'CALL_NOW',
    },
})

story = ObjectStorySpec()
story.update({
    ObjectStorySpec.Field.page_id: page_id,
    ObjectStorySpec.Field.template_data: template,
})

creative = AdCreative(parent_id=ad_account_id)
creative.update({
    AdCreative.Field.place_page_set_id: ad_place_page_set_id,
    AdCreative.Field.dynamic_ad_voice: 'DYNAMIC',
    AdCreative.Field.object_story_spec: story,
})
creative.remote_create()
# _DOC close [ADCREATIVE_CREATE_DLA_DYNAMIC_CALL_NOW]

creative.remote_delete()
image_hash = fixtures.create_image().get_hash()
link = 'http://example.com'


# _DOC open [ADCREATIVE_CREATE_LINK_AD]
# _DOC vars [ad_account_id:s, image_hash:s, page_id, link:s]
from facebookads.objects import AdCreative
from facebookads.specs import ObjectStorySpec, LinkData

link_data = LinkData()
link_data[LinkData.Field.message] = 'try it out'
link_data[LinkData.Field.link] = link
link_data[LinkData.Field.caption] = 'My caption'
link_data[LinkData.Field.image_hash] = image_hash

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()

print(creative)
# _DOC close [ADCREATIVE_CREATE_LINK_AD]
creative.remote_delete()


url = 'http://www.domain.com'
# _DOC vars [video_id, thumbnail_url:s, app_store_url:s, page_id, ad_account_id:s]
from facebookads.objects import AdCreative
from facebookads.specs import ObjectStorySpec, VideoData

video_data = VideoData()
video_data.update({
    VideoData.Field.video_id: video_id,
    VideoData.Field.description: 'Creative description',
    VideoData.Field.image_url: thumbnail_url,
    VideoData.Field.call_to_action: {
        'type': 'INSTALL_MOBILE_APP',
        'value': {
            'link': app_store_url,
            'link_title': 'Link title',
        },
    },
})

story = ObjectStorySpec()
story.update({
    ObjectStorySpec.Field.page_id: page_id,
    ObjectStorySpec.Field.video_data: video_data,
})

creative = AdCreative(parent_id=ad_account_id)
creative[AdCreative.Field.object_story_spec] = story
creative.remote_create()
# _DOC close [ADCREATIVE_CREATE_MAIA_VIDEO]

creative.remote_delete()