Beispiel #1
0
    def ad_set_creation(self,
                        act_id,
                        ad_set_name,
                        campaign_id,
                        targeting,
                        optimisation_goal=AdSet.OptimizationGoal.link_clicks,
                        daily_budget=1000,
                        bid=None):
        """
        Creates an adset associated with a particular campaign with targeting.

        Returns
        <AdSet> {
         "id": "6090168487996"
        }

        """
        today = datetime.date.today()
        start_time = str(today + datetime.timedelta(weeks=1))
        end_time = str(today + datetime.timedelta(weeks=2))

        ad_account = AdAccount(fbid='act_{}'.format(act_id))

        params = {
            AdSet.Field.name: ad_set_name,
            AdSet.Field.campaign_id: campaign_id,
            AdSet.Field.daily_budget: 1000,
            AdSet.Field.billing_event: AdSet.BillingEvent.impressions,
            AdSet.Field.optimization_goal: optimisation_goal,
            AdSet.Field.targeting: targeting,
            AdSet.Field.start_time: start_time,
            AdSet.Field.end_time: end_time,
            AdSet.Field.status: AdSet.Status.active,
        }

        if bid == None:
            params[AdSet.Field.is_autobid] = True
        else:
            params[AdSet.Fields.bid_amount] = bid

        adset = ad_account.create_ad_set(params=params)

        return adset
Beispiel #2
0
def create_new_ad_set():
    """
    Create a new adset
    """
    ad_account = AdAccount(fbid=my_ad_account_id)
    
    """
    Create and update your ad set here
    """
#https://developers.facebook.com/docs/marketing-api/reference/ad-campaign
    params = {
        AdSet.Field.name: 'joon_SDK_competition',
        AdSet.Field.promoted_object: {
            'page_id': my_page_id,
        },
        AdSet.Field.campaign_id: my_campaign_id,
        AdSet.Field.is_autobid: True,
        AdSet.Field.start_time: 1520560800, 
        AdSet.Field.end_time: 1520701200,
        AdSet.Field.daily_budget: 200,
        AdSet.Field.billing_event: AdSet.BillingEvent.impressions,
        AdSet.Field.optimization_goal: AdSet.OptimizationGoal.page_likes,
# https://developers.facebook.com/docs/marketing-api/targeting-specs
        AdSet.Field.targeting: {
            'geo_locations': {
                'countries': ['IN', 'ID'],
            },
            'age_min': 13,
            'age_max': 35,
            'flexible_spec': [
                {
            
                    'interests': [
                        {
                            'id': 6003332344237 ,
                            'name': 'Dogs',
                        },
                        {
                            'id': 6004037726009,
                            'name': 'Pets',
                        },
                        {
                            'id': 6003266061909,
                            'name': 'Food',
                        },
                        {
                            'id': 6003156370433,
                            'name': 'Cute Animals',
                        },
                        {
                            'id': 625163160959478,
                            'name': 'Adorable Animals',
                        },

                    ],
                },
            ]
        },
        AdSet.Field.status: AdSet.Status.active,
    }
    adset = ad_account.create_ad_set(params=params)



    image = AdImage(parent_id=my_ad_account_id)
    image[AdImage.Field.filename] = '/Users/deriknguyen/Desktop/CS144/clickmaniac/dog_ads/3.jpg'
    image.remote_create()

    # Output image Hash
    image_hash = image[AdImage.Field.hash]

    # First, upload the ad image that you will use in your ad creative
    # Please refer to Ad Image Create for details.

    link_data = AdCreativeLinkData()
    link_data[AdCreativeLinkData.Field.message] = '"Like" to find ways to help man\'s best friend.'
    link_data[AdCreativeLinkData.Field.name] = 'Puppy Love'
    link_data[AdCreativeLinkData.Field.link] = 'http://www.facebook.com/caltech.clickmaniac'
    link_data[AdCreativeLinkData.Field.image_hash] = image_hash

    # Then, use the image hash returned from above
    object_story_spec = AdCreativeObjectStorySpec()
    object_story_spec[AdCreativeObjectStorySpec.Field.page_id] = my_page_id
    object_story_spec[AdCreativeObjectStorySpec.Field.link_data] = link_data
    

    creative = AdCreative(parent_id='my_ad_account_id')
    # creative[AdCreative.Field.title] = 'Puppy Love'
    # creative[AdCreative.Field.body] = 'Cats are one of the deadliest local predators of birds. "Like" this post if you agree they\'re overated!'
    # creative[AdCreative.Field.link_url] = 'http://www.facebook.com/caltech.clickmaniac'
    # creative[AdCreative.Field.image_hash] = image_hash

    creative[AdCreative.Field.object_story_spec] = object_story_spec
    

    # Finally, create your ad along with ad creative.
    # Please note that the ad creative is not created independently, rather its
    # data structure is appended to the ad group
    ad = Ad(parent_id=my_ad_account_id)
    ad[Ad.Field.name] = 'joon_dog_pos_competition'
    ad[Ad.Field.adset_id] = adset[AdSet.Field.id]
    ad[Ad.Field.creative] = creative
    ad.remote_create(params={
        'status': Ad.Status.active,
    })
    return adset