Ads Buying

The following guide shows how to create ad campaigns for Click to Messenger Ads and Facebook Page promotions.

Click to Messenger Ads

Click-to-Messenger Ads allow people to directly start a thread with your Facebook Page when they click on your ad.

Requirements

  • An Ad Account ID

Sample Code

The following code example shows you how to create an ad that will run during a designated time period.

use DateTime;
use FacebookAds\Object\AdSet;
use FacebookAds\Object\Fields\AdSetFields;
use FacebookAds\Object\Values\AdSetBillingEventValues;
use FacebookAds\Object\Values\AdSetOptimizationGoalValues;

$start_time = (new \DateTime("+1 week"))->format(DateTime::ISO8601);
$end_time = (new \DateTime("+2 week"))->format(DateTime::ISO8601);

$adset = new AdSet(null, 'act_<AD_ACCOUNT_ID>');
$adset->setData(array(
  AdSetFields::NAME => 'My Ad Set',
  AdSetFields::OPTIMIZATION_GOAL => AdSetOptimizationGoalValues::REACH,
  AdSetFields::BILLING_EVENT => AdSetBillingEventValues::IMPRESSIONS,
  AdSetFields::BID_AMOUNT => 2,
  AdSetFields::DAILY_BUDGET => 1000,
  AdSetFields::CAMPAIGN_ID => <CAMPAIGN_ID>,
  AdSetFields::TARGETING => <TARGETING>,
  AdSetFields::START_TIME => $start_time,
  AdSetFields::END_TIME => $end_time,
));
$adset->create(array(
  AdSet::STATUS_PARAM_NAME => AdSet::STATUS_PAUSED,
));
import datetime
from facebookads.adobjects.adset import AdSet

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

adset = AdSet(parent_id='act_<AD_ACCOUNT_ID>')
adset.update({
    AdSet.Field.name: 'My Ad Set',
    AdSet.Field.campaign_id: <CAMPAIGN_ID>,
    AdSet.Field.daily_budget: 1000,
    AdSet.Field.billing_event: AdSet.BillingEvent.impressions,
    AdSet.Field.optimization_goal: AdSet.OptimizationGoal.reach,
    AdSet.Field.bid_amount: 2,
    AdSet.Field.targeting: <TARGETING>,
    AdSet.Field.start_time: start_time,
    AdSet.Field.end_time: end_time,
})

adset.remote_create(params={
    'status': AdSet.Status.paused,
})
AdSet adSet = new AdAccount(act_<AD_ACCOUNT_ID>, context).createAdSet()
  .setName("My Ad Set")
  .setOptimizationGoal(AdSet.EnumOptimizationGoal.VALUE_REACH)
  .setBillingEvent(AdSet.EnumBillingEvent.VALUE_IMPRESSIONS)
  .setBidAmount(2L)
  .setDailyBudget(1000L)
  .setCampaignId(<CAMPAIGN_ID>)
  .setTargeting(
    new Targeting()
      .setFieldGeoLocations(
        new TargetingGeoLocation()
          .setFieldCountries(Arrays.asList("US"))
      )
  )
  .setStartTime(start_time)
  .setEndTime(end_time)
  .setStatus(AdSet.EnumStatus.VALUE_PAUSED)
  .execute();
String ad_set_id = adSet.getId();
curl \
  -F 'name=My Ad Set' \
  -F 'optimization_goal=REACH' \
  -F 'billing_event=IMPRESSIONS' \
  -F 'bid_amount=2' \
  -F 'daily_budget=1000' \
  -F 'campaign_id=<CAMPAIGN_ID>' \
  -F 'targeting={"geo_locations":{"countries":["US"]}}' \
  -F 'start_time=2018-02-06T04:45:17+0000' \
  -F 'end_time=2018-02-13T04:45:17+0000' \
  -F 'status=PAUSED' \
  -F 'access_token=<ACCESS_TOKEN>' \
  https://graph.facebook.com/v2.11/act_<AD_ACCOUNT_ID>/adsets

Page Promotion

Create an ad to increase traffic to your Facebook Page.

Requirements

  • An Ad Account ID
  • A Facebook Page ID

Sample Code

The following code example shows you how to create an ad campaign to promote your Page to get more Page Likes. The ad will run in the US with a daily budget of $1000 USD where ad impressions is the goal of the campaign.

$fields = array(
);
$params = array(
  'objective' => 'PAGE_LIKES',
  'status' => 'PAUSED',
  'buying_type' => 'AUCTION',
  'name' => 'My Campaign',
);
$campaign = (new AdAccount(<ACCOUNT_ID>))->createCampaign(
  $fields,
  $params
);
$campaign_id = $campaign->id;

$fields = array(
);
$params = array(
  'status' => 'PAUSED',
  'targeting' => array('geo_locations' =>  array('countries' =>  array('US'))),
  'daily_budget' => '1000',
  'billing_event' => 'IMPRESSIONS',
  'bid_amount' => '20',
  'campaign_id' => $campaign_id,
  'optimization_goal' => 'PAGE_LIKES',
  'promoted_object' => array('page_id' =>  <PAGE_ID>),
  'name' => 'My AdSet',
);
$ad_set = (new AdAccount(<ACCOUNT_ID>))->createAdSet(
  $fields,
  $params
);
$ad_set_id = $ad_set->id;

$fields = array(
);
$params = array(
  'body' => 'Like My Page',
  'image_url' => 'https://static.xx.fbcdn.net/rsrc.php/v3/yu/r/66zXtGTxCWr.png',
  'name' => 'My Creative',
  'object_id' => <PAGE_ID>,
  'title' => 'My Page Like Ad',
);
$creative = (new AdAccount(<ACCOUNT_ID>))->createAdCreative(
  $fields,
  $params
);
$creative_id = $creative->id;

$fields = array(
);
$params = array(
  'status' => 'PAUSED',
  'adset_id' => $ad_set_id,
  'name' => 'My Ad',
  'creative' => array('creative_id' =>  $creative_id),
  'ad_format' => 'DESKTOP_FEED_STANDARD',
);
$ad = (new AdAccount(<ACCOUNT_ID>))->createAd(
  $fields,
  $params
);
$ad_id = $ad->id;

$fields = array(
);
$params = array(
  'ad_format' => 'DESKTOP_FEED_STANDARD',
);
$ad_previews = (new Ad($ad_id))->getPreviews(
  $fields,
  $params
);
fields = [
]
params = {
    'objective': 'PAGE_LIKES',
    'status': 'PAUSED',
    'buying_type': 'AUCTION',
    'name': 'My Campaign',
}
campaign = AdAccount(<ACCOUNT_ID>).create_campaign(
    fields=fields,
    params=params,
)
campaign_id = campaign.get_id()

fields = [
]
params = {
    'status': 'PAUSED',
    'targeting': {'geo_locations': {'countries': ['US']}},
    'daily_budget': '1000',
    'billing_event': 'IMPRESSIONS',
    'bid_amount': '20',
    'campaign_id': campaign_id,
    'optimization_goal': 'PAGE_LIKES',
    'promoted_object': {'page_id': <PAGE_ID>},
    'name': 'My AdSet',
}
ad_set = AdAccount(<ACCOUNT_ID>).create_ad_set(
    fields=fields,
    params=params,
)
ad_set_id = ad_set.get_id()

fields = [
]
params = {
    'body': 'Like My Page',
    'image_url': 'https://static.xx.fbcdn.net/rsrc.php/v3/yu/r/66zXtGTxCWr.png',
    'name': 'My Creative',
    'object_id': <PAGE_ID>,
    'title': 'My Page Like Ad',
}
creative = AdAccount(<ACCOUNT_ID>).create_ad_creative(
    fields=fields,
    params=params,
)
creative_id = creative.get_id()

fields = [
]
params = {
    'status': 'PAUSED',
    'adset_id': ad_set_id,
    'name': 'My Ad',
    'creative': {'creative_id': creative_id},
    'ad_format': 'DESKTOP_FEED_STANDARD',
}
ad = AdAccount(<ACCOUNT_ID>).create_ad(
    fields=fields,
    params=params,
)
ad_id = ad.get_id()

fields = [
]
params = {
    'ad_format': 'DESKTOP_FEED_STANDARD',
}
ad_previews = Ad(ad_id).get_previews(
    fields=fields,
    params=params,
)
Campaign campaign = new AdAccount(<ACCOUNT_ID>, context).createCampaign()
  .setObjective(Campaign.EnumObjective.VALUE_PAGE_LIKES)
  .setStatus(Campaign.EnumStatus.VALUE_PAUSED)
  .setBuyingType("AUCTION")
  .setName("My Campaign")
  .execute();
String campaign_id = campaign.getId();
AdSet adSet = new AdAccount(<ACCOUNT_ID>, context).createAdSet()
  .setStatus(AdSet.EnumStatus.VALUE_PAUSED)
  .setTargeting(
    new Targeting()
      .setFieldGeoLocations(
        new TargetingGeoLocation()
          .setFieldCountries(Arrays.asList("US"))
      )
  )
  .setDailyBudget(1000L)
  .setBillingEvent(AdSet.EnumBillingEvent.VALUE_IMPRESSIONS)
  .setBidAmount(20L)
  .setCampaignId(campaign_id)
  .setOptimizationGoal(AdSet.EnumOptimizationGoal.VALUE_PAGE_LIKES)
  .setPromotedObject("{\"page_id\": \"" + <PAGE_ID> + "\"}")
  .setName("My AdSet")
  .execute();
String ad_set_id = adSet.getId();
AdCreative creative = new AdAccount(<ACCOUNT_ID>, context).createAdCreative()
  .setBody("Like My Page")
  .setImageUrl("https://static.xx.fbcdn.net/rsrc.php/v3/yu/r/66zXtGTxCWr.png")
  .setName("My Creative")
  .setObjectId(<PAGE_ID>)
  .setTitle("My Page Like Ad")
  .execute();
String creative_id = creative.getId();
Ad ad = new AdAccount(<ACCOUNT_ID>, context).createAd()
  .setStatus(Ad.EnumStatus.VALUE_PAUSED)
  .setAdsetId(ad_set_id)
  .setName("My Ad")
  .setCreative(
    new AdCreative()
      .setFieldId(creative_id)
  )
  .setParam("ad_format", "DESKTOP_FEED_STANDARD")
  .execute();
String ad_id = ad.getId();
APINodeList<AdPreview> adPreviews = new Ad(ad_id, context).getPreviews()
  .setAdFormat(AdPreview.EnumAdFormat.VALUE_DESKTOP_FEED_STANDARD)
  .execute();
ad_account = FacebookAds::AdAccount.get(<ACCOUNT_ID>)
campaign = ad_account.campaigns.create({
objective: 'PAGE_LIKES',
status: 'PAUSED',
buying_type: 'AUCTION',
name: 'My Campaign',
})
campaign_id = campaign.id
print 'campaign_id:', campaign_id
ad_set = ad_account.adsets.create({
status: 'PAUSED',
targeting: {'geo_locations': {'countries': ['US']}},
daily_budget: '1000',
billing_event: 'IMPRESSIONS',
bid_amount: '20',
campaign_id: campaign_id,
optimization_goal: 'PAGE_LIKES',
promoted_object: {'page_id': <PAGE_ID>},
name: 'My AdSet',
})
ad_set_id = ad_set.id
print 'ad_set_id:', ad_set_id
creative = ad_account.adcreatives.create({
body: 'Like My Page',
image_url: 'https://static.xx.fbcdn.net/rsrc.php/v3/yu/r/66zXtGTxCWr.png',
name: 'My Creative',
object_id: <PAGE_ID>,
title: 'My Page Like Ad',
})
creative_id = creative.id
print 'creative_id:', creative_id
ad = ad_account.ads.create({
status: 'PAUSED',
adset_id: ad_set_id,
name: 'My Ad',
creative: {'creative_id': creative_id},
ad_format: 'DESKTOP_FEED_STANDARD',
})
ad_id = ad.id
print 'ad_id:', ad_id
ad = FacebookAds::Ad.get(ad_id)
ad_previews = ad.previews({
ad_format: 'DESKTOP_FEED_STANDARD',
})
fields = [
];
params = {
  'objective' : 'PAGE_LIKES',
  'status' : 'PAUSED',
  'buying_type' : 'AUCTION',
  'name' : 'My Campaign',
};
let campaign = await (new AdAccount(<ACCOUNT_ID>)).createCampaign(
  fields,
  params
);
let campaign_id = campaign.id;

fields = [
];
params = {
  'status' : 'PAUSED',
  'targeting' : {'geo_locations': {'countries': ['US']}},
  'daily_budget' : '1000',
  'billing_event' : 'IMPRESSIONS',
  'bid_amount' : '20',
  'campaign_id' : campaign_id,
  'optimization_goal' : 'PAGE_LIKES',
  'promoted_object' : {'<PAGE_ID>': <PAGE_ID>},
  'name' : 'My AdSet',
};
let ad_set = await (new AdAccount(<ACCOUNT_ID>)).createAdSet(
  fields,
  params
);
let ad_set_id = ad_set.id;

fields = [
];
params = {
  'body' : 'Like My Page',
  'image_url' : 'https://static.xx.fbcdn.net/rsrc.php/v3/yu/r/66zXtGTxCWr.png',
  'name' : 'My Creative',
  'object_id' : <PAGE_ID>,
  'title' : 'My Page Like Ad',
};
let creative = await (new AdAccount(<ACCOUNT_ID>)).createAdCreative(
  fields,
  params
);
let creative_id = creative.id;

fields = [
];
params = {
  'status' : 'PAUSED',
  'adset_id' : ad_set_id,
  'name' : 'My Ad',
  'creative' : {'creative_id': creative_id},
  'ad_format' : 'DESKTOP_FEED_STANDARD',
};
let ad = await (new AdAccount(<ACCOUNT_ID>)).createAd(
  fields,
  params
);
let ad_id = ad.id;

fields = [
];
params = {
  'ad_format' : 'DESKTOP_FEED_STANDARD',
};
let ad_previews = await (new Ad(ad_id)).getPreviews(
  fields,
  params
);

Learn More