How to use facebookads - 10 common examples

To help you get started, we’ve selected a few facebookads examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github facebook / facebook-python-business-sdk / examples / docs / adcreative.py View on Github external
# _DOC open [ADCREATIVE_CREATE_DPA_RETARGETING]
# _DOC vars [ad_account_id:s, page_id, link:s, product_set_id_1]
from facebookads.objects import AdCreative
from facebookads.specs import ObjectStorySpec
story = ObjectStorySpec()
story[story.Field.page_id] = page_id
story[story.Field.template_data] = {
    'message': 'Test {{product.name | titleize}}',
    'link': link,
    'name': 'Headline {{product.price}}',
    'description': 'Description {{product.description}}',
    'max_product_count': 3,
}

creative = AdCreative(parent_id=ad_account_id)
creative[AdCreative.Field.name] = 'Dynamic Ad Template Creative Sample'
creative[AdCreative.Field.object_story_spec] = story
creative[AdCreative.Field.product_set_id] = product_set_id_1
creative.remote_create()
# _DOC close [ADCREATIVE_CREATE_DPA_RETARGETING]
creative.remote_delete()

# FIXME excluded from runtime
exit(0)

retailer_id = ''
catalog_id = product_catalog_id
# _DOC open [ADCREATIVE_READ_DPA_PREVIEW]
# _DOC vars [creative_id, catalog_id, retailer_id:s]
import base64
from facebookads.objects import AdCreative, AdCreativePreview
github facebook / facebook-python-business-sdk / examples / docs / customaudience.py View on Github external
from facebookads.objects import CustomAudience

audience = CustomAudience(custom_audience_id)
users = [user_id_1, user_id_2]
apps = [app_id]
audience.add_users(CustomAudience.Schema.uid, users, apps)
# _DOC close [CUSTOM_AUDIENCE_USERS_ADD_ID]


custom_audience_id = fixtures.create_custom_audience().get_id_assured()

# _DOC open [CUSTOM_AUDIENCE_USERS_REMOVE_EMAILS]
# _DOC vars [custom_audience_id:s]
from facebookads.objects import CustomAudience

audience = CustomAudience(custom_audience_id)
users = ['test1@example.com', 'test2@example.com', 'test3@example.com']

audience.remove_users(CustomAudience.Schema.email_hash, users)
# _DOC close [CUSTOM_AUDIENCE_USERS_REMOVE_EMAILS]


exit(0)

##
# (#2654) Source Audience is Too Small: There aren't enough people in your
# source in the country you chose. Please choose a country that includes at
# least 100 people in your source.

custom_audience_id = fixtures.create_custom_audience().get_id_assured()
video_id = fixtures.upload_video(video_path)['id']
github facebook / facebook-python-business-sdk / examples / docs / customaudience.py View on Github external
exit(0)

##
# (#2654) Source Audience is Too Small: There aren't enough people in your
# source in the country you chose. Please choose a country that includes at
# least 100 people in your source.

custom_audience_id = fixtures.create_custom_audience().get_id_assured()
video_id = fixtures.upload_video(video_path)['id']

# _DOC open [CUSTOM_AUDIENCE_CREATE_VIDEO_VIEWS_RETARGET]
# _DOC vars [ad_account_id:s, video_id]
from facebookads.objects import CustomAudience

lookalike = CustomAudience(parent_id=ad_account_id)
lookalike.update({
    CustomAudience.Field.subtype: CustomAudience.Subtype.lookalike,
    CustomAudience.Field.lookalike_spec: {
        'ratio': 0.01,
        'country': 'US',
        'engagement_specs': [
            {
                'action.type': 'video_view',
                'post': video_id,
            },
        ],
        'conversion_type': 'dynamic_rule',
    },
})

lookalike.remote_create()
github facebook / facebook-python-business-sdk / examples / docs / adimage.py View on Github external
],
}
images = account.get_ad_images(params=params)
# _DOC close [ADIMAGE_READ_MULTI_WITH_HASH]


# Failed to delete account image: Image with hash
# 7aa4a47d513acd589f968c833f2757b1 is still being used!
exit(0)

# _DOC open [ADIMAGE_DELETE]
# _DOC vars [image_id, ad_account_id:s, image_hash:s]
from facebookads.objects import AdImage

image = AdImage(image_id, ad_account_id)
image.remote_delete(params={AdImage.Field.hash: image_hash})
# _DOC close [ADIMAGE_DELETE]
github facebook / facebook-python-business-sdk / examples / docs / adgroup.py View on Github external
# _DOC close [ADGROUP_CREATE_TRACKING_APP_INSTALLS]
adgroup.remote_delete()

object_story_id = fixtures.create_post(
    message=fixtures.unique_name('Test Post '))['id']

post_id = string.split(object_story_id, '_')[1]

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

adgroup = AdGroup(parent_id=ad_account_id)
adgroup[AdGroup.Field.name] = 'test'
adgroup[AdGroup.Field.campaign_id] = ad_set_id
adgroup[AdGroup.Field.creative] = {
    'object_story_id': object_story_id
}
adgroup[AdGroup.Field.tracking_specs] = {
    'action.type': 'like',
    'post': post_id,
    'post.wall': page_id
}
adgroup.remote_create()
# _DOC close [ADGROUP_CREATE_TRACKING_POST_LIKES]
adgroup.remote_delete()


# _DOC open [ADGROUP_CREATE_TRACKING_LIKE_MENTIONS]
# _DOC vars [ad_account_id:s, ad_set_id, creative_id, post_id, page_id]
from facebookads.objects import AdGroup
github xiaowan / pyadmin / library / RPCClient.py View on Github external
def call(self, access_token='', method='', path=(), params={}):
        caller = FacebookAdsApi.init(self.app_id, self.app_secret, access_token)
        try:
            return caller.call(method, path, params).json()
        except FacebookRequestError as fberror:
            return fberror.body()
        except Exception as es:
            raise es
github facebook / facebook-python-business-sdk / examples / docs / adcreative.py View on Github external
# _DOC vars [ad_account_id:s, page_id, link:s, product_set_id_1]
from facebookads.objects import AdCreative
from facebookads.specs import ObjectStorySpec
story = ObjectStorySpec()
story[story.Field.page_id] = page_id
story[story.Field.template_data] = {
    'message': 'Test {{product.name | titleize}}',
    'link': link,
    'name': 'Headline {{product.price}}',
    'description': 'Description {{product.description}}',
    'max_product_count': 3,
}

creative = AdCreative(parent_id=ad_account_id)
creative[AdCreative.Field.name] = 'Dynamic Ad Template Creative Sample'
creative[AdCreative.Field.object_story_spec] = story
creative[AdCreative.Field.product_set_id] = product_set_id_1
creative.remote_create()
# _DOC close [ADCREATIVE_CREATE_DPA_RETARGETING]
creative.remote_delete()

# FIXME excluded from runtime
exit(0)

retailer_id = ''
catalog_id = product_catalog_id
# _DOC open [ADCREATIVE_READ_DPA_PREVIEW]
# _DOC vars [creative_id, catalog_id, retailer_id:s]
import base64
from facebookads.objects import AdCreative, AdCreativePreview

item_id = retailer_id
github facebook / facebook-python-business-sdk / facebookads / adobjects / business.py View on Github external
from facebookads.adobjects.adspixel import AdsPixel
        param_types = {
            'id_filter': 'string',
            'name_filter': 'string',
        }
        enums = {
        }
        request = FacebookRequest(
            node_id=self['id'],
            method='GET',
            endpoint='/adspixels',
            api=self._api,
            param_checker=TypeChecker(param_types, enums),
            target_class=AdsPixel,
            api_type='EDGE',
            response_parser=ObjectParser(target_class=AdsPixel, api=self._api),
        )
        request.add_params(params)
        request.add_fields(fields)

        if batch is not None:
            request.add_to_batch(batch)
            return request
        elif pending:
            return request
        else:
            self.assure_call()
            return request.execute()
github facebook / facebook-python-business-sdk / facebookads / adobjects / business.py View on Github external
def get_owned_pages(self, fields=None, params=None, batch=None, pending=False):
        param_types = {
        }
        enums = {
        }
        request = FacebookRequest(
            node_id=self['id'],
            method='GET',
            endpoint='/owned_pages',
            api=self._api,
            param_checker=TypeChecker(param_types, enums),
            target_class=AbstractCrudObject,
            api_type='EDGE',
            response_parser=ObjectParser(target_class=AbstractCrudObject, api=self._api),
        )
        request.add_params(params)
        request.add_fields(fields)

        if batch is not None:
            request.add_to_batch(batch)
            return request
        elif pending:
            return request
        else:
            self.assure_call()
            return request.execute()
github facebook / facebook-python-business-sdk / facebookads / adobjects / business.py View on Github external
def get_client_ad_accounts(self, fields=None, params=None, batch=None, pending=False):
        from facebookads.adobjects.adaccount import AdAccount
        param_types = {
        }
        enums = {
        }
        request = FacebookRequest(
            node_id=self['id'],
            method='GET',
            endpoint='/client_ad_accounts',
            api=self._api,
            param_checker=TypeChecker(param_types, enums),
            target_class=AdAccount,
            api_type='EDGE',
            response_parser=ObjectParser(target_class=AdAccount, api=self._api),
        )
        request.add_params(params)
        request.add_fields(fields)

        if batch is not None:
            request.add_to_batch(batch)
            return request
        elif pending:
            return request
        else:
            self.assure_call()
            return request.execute()