Beispiel #1
0
 private function createAdCreateive($postId)
 {
     $creative = new AdCreative(null, 'act_' . $this->adAccountId);
     $creative->setData(array(AdCreativeFields::NAME => 'Sample Promoted Post ' . date("Y-m-d H:i:s"), AdCreativeFields::OBJECT_STORY_ID => $this->pageId . '_' . $postId));
     $creative->create();
     return $creative->getData()[AdCreative::FIELD_ID];
 }
 public function testDynamicProductAdsCreation()
 {
     $this->adCampaign = new AdCampaign(null, $this->getConfig()->accountId);
     $this->adCampaign->setData(array(AdCampaignFields::NAME => $this->getConfig()->testRunId, AdCampaignFields::OBJECTIVE => AdObjectives::PRODUCT_CATALOG_SALES, AdCampaignFields::PROMOTED_OBJECT => array('product_catalog_id' => $this->productCatalog->{ProductCatalogFields::ID})));
     $this->assertCanCreate($this->adCampaign);
     $targeting = new TargetingSpecs();
     $targeting->{TargetingSpecsFields::GEO_LOCATIONS} = array('countries' => array('US'));
     $targeting->{TargetingSpecsFields::DYNAMIC_AUDIENCE_IDS} = array($this->productAudience->{ProductAudienceFields::ID});
     $this->adSet = new AdSet(null, $this->getConfig()->accountId);
     $this->adSet->setData(array(AdSetFields::NAME => $this->getConfig()->testRunId, AdSetFields::OPTIMIZATION_GOAL => OptimizationGoals::LINK_CLICKS, AdSetFields::BILLING_EVENT => BillingEvents::IMPRESSIONS, AdSetFields::BID_AMOUNT => 2, AdSetFields::DAILY_BUDGET => 2000, AdSetFields::CAMPAIGN_GROUP_ID => $this->adCampaign->{AdCampaignFields::ID}, AdSetFields::TARGETING => $targeting, AdsetFields::PROMOTED_OBJECT => array('product_set_id' => $this->productSet->{ProductSetFields::ID})));
     $this->assertCanCreate($this->adSet);
     $template = new TemplateData();
     $template->setData(array(TemplateDataFields::DESCRIPTION => '{{product.description}}', TemplateDataFields::LINK => 'http://www.example.com/', TemplateDataFields::MESSAGE => 'Test DPA Ad Message', TemplateDataFields::NAME => '{{product.name | titleize}}', TemplateDataFields::MAX_PRODUCT_COUNT => 3, TemplateDataFields::CALL_TO_ACTION => array('type' => CallToActionTypes::SHOP_NOW)));
     $story = new ObjectStorySpec();
     $story->setData(array(ObjectStorySpecFields::PAGE_ID => $this->getConfig()->pageId, ObjectStorySpecFields::TEMPLATE_DATA => $template));
     $this->creative = new AdCreative(null, $this->getConfig()->accountId);
     $this->creative->setData(array(AdCreativeFields::NAME => $this->getConfig()->testRunId, AdCreativeFields::OBJECT_STORY_SPEC => $story, AdCreativeFields::PRODUCT_SET_ID => $this->productSet->{ProductSetFields::ID}));
     $this->assertCanCreate($this->creative);
     $this->adGroup = new AdGroup(null, $this->getConfig()->accountId);
     $this->adGroup->setData(array(AdGroupFields::NAME => 'DPA Test Ad 1 ' . $this->getConfig()->testRunId, AdGroupFields::CAMPAIGN_ID => $this->adSet->{AdSetFields::ID}, AdGroupFields::CREATIVE => array('creative_id' => $this->creative->{AdCreativeFields::ID})));
     $this->assertCanCreate($this->adGroup);
 }
$adset = new AdSet(null, $account->id);
$adset->setData(array(AdSetFields::NAME => 'My First AdSet', AdSetFields::CAMPAIGN_GROUP_ID => $campaign->id, AdSetFields::CAMPAIGN_STATUS => AdSet::STATUS_ACTIVE, AdSetFields::DAILY_BUDGET => '150', AdSetFields::TARGETING => $targeting, AdSetFields::OPTIMIZATION_GOAL => OptimizationGoals::REACH, AdSetFields::BILLING_EVENT => BillingEvents::IMPRESSIONS, AdSetFields::BID_AMOUNT => 2, AdSetFields::START_TIME => (new \DateTime("+1 week"))->format(\DateTime::ISO8601), AdSetFields::END_TIME => (new \DateTime("+2 week"))->format(\DateTime::ISO8601)));
$adset->validate()->create();
echo 'AdSet  ID: ' . $adset->id . "\n";
/**
 * Step 5 Create an AdImage
 */
use FacebookAds\Object\AdImage;
use FacebookAds\Object\Fields\AdImageFields;
$image = new AdImage(null, $account->id);
$image->{AdImageFields::FILENAME} = SDK_DIR . '/test/misc/image.png';
$image->create();
echo 'Image Hash: ' . $image->hash . "\n";
/**
 * Step 6 Create an AdCreative
 */
use FacebookAds\Object\AdCreative;
use FacebookAds\Object\Fields\AdCreativeFields;
$creative = new AdCreative(null, $account->id);
$creative->setData(array(AdCreativeFields::NAME => 'Sample Creative', AdCreativeFields::TITLE => 'Welcome to the Jungle', AdCreativeFields::BODY => 'We\'ve got fun \'n\' games', AdCreativeFields::IMAGE_HASH => $image->hash, AdCreativeFields::OBJECT_URL => 'http://www.example.com/'));
$creative->create();
echo 'Creative ID: ' . $creative->id . "\n";
/**
 * Step 7 Create an AdGroup
 */
use FacebookAds\Object\AdGroup;
use FacebookAds\Object\Fields\AdGroupFields;
$adgroup = new AdGroup(null, $account->id);
$adgroup->setData(array(AdGroupFields::CREATIVE => array('creative_id' => $creative->id), AdGroupFields::NAME => 'My First AdGroup', AdGroupFields::CAMPAIGN_ID => $adset->id));
$adgroup->create();
echo 'AdGroup ID:' . $adgroup->id . "\n";
$image->{AdImageFields::FILENAME} = $image_path;
$image->create();
$image_hash = $image->{AdImageFields::HASH};
// _DOC open [ADGROUP_CREATE_INLINE_CREATIVE]
// _DOC vars [ad_account_id:s, image_hash:s, ad_set_id:s]
// use FacebookAds\Object\AdCreative;
// use FacebookAds\Object\AdGroup;
// use FacebookAds\Object\TargetingSpecs;
// use FacebookAds\Object\Fields\AdCreativeFields;
// use FacebookAds\Object\Fields\AdImageFields;
// use FacebookAds\Object\Fields\AdGroupFields;
// First, upload the ad image that you will use in your ad creative
// Please refer to Ad Image Create for details.
// Then, use the image hash returned from above
$creative = new AdCreative(null, $ad_account_id);
$creative->setData(array(AdCreativeFields::TITLE => 'My Test Creative', AdCreativeFields::BODY => 'My Test Ad Creative Body', AdCreativeFields::OBJECT_URL => 'https://www.facebook.com/facebook', AdCreativeFields::IMAGE_HASH => $image_hash));
// 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
$adgroup = new AdGroup(null, $ad_account_id);
$adgroup->setData(array(AdGroupFields::NAME => 'My Ad', AdGroupFields::CAMPAIGN_ID => $ad_set_id, AdGroupFields::CREATIVE => $creative, AdGroupFields::ADGROUP_STATUS => AdGroup::STATUS_PAUSED));
$adgroup->create();
// _DOC close [ADGROUP_CREATE_INLINE_CREATIVE]
// fetch the id of the generated creative
$adgroup->read(array(AdGroupFields::CREATIVE));
$creative->{AdCreativeFields::ID} = $adgroup->{AdGroupFields::CREATIVE}['id'];
// _DOC open [ADGROUP_GET_TARGETING_DESCRIPTION]
// _DOC vars [ad_group_id]
// use FacebookAds\Object\AdGroup;
$adgroup = new AdGroup($ad_group_id);
$targeting_description = $adgroup->getTargetingDescription();
$creative->setData(array(AdCreativeFields::NAME => 'Dynamic Ad Template Creative Sample', AdCreativeFields::APPLINK_TREATMENT => 'deeplink_with_web_fallback', AdCreativeFields::OBJECT_STORY_SPEC => $object_story_spec, AdCreativeFields::PRODUCT_SET_ID => $product_set_id));
$creative->create();
// _DOC close [ADCREATIVE_CREATE_DPA_DEEPLINK]
$creative->delete();
$product_set_id_1 = $product_set_id;
// _DOC open [ADCREATIVE_CREATE_DPA_RETARGETING]
// _DOC vars [ad_account_id:s, page_id, link:s, product_set_id_1]
// use FacebookAds\Object\AdCreative;
// use FacebookAds\Object\ObjectStorySpec;
// use FacebookAds\Object\ObjectStory\TemplateData;
// use FacebookAds\Object\Fields\AdCreativeFields;
// use FacebookAds\Object\Fields\ObjectStorySpecFields;
$object_story_spec = new ObjectStorySpec();
$object_story_spec->setData(array(ObjectStorySpecFields::PAGE_ID => $page_id, ObjectStorySpecFields::TEMPLATE_DATA => (new TemplateData())->setData(array('message' => 'Test {{product.name | titleize}}', 'link' => $link, 'name' => 'Headline {{product.price}}', 'description' => 'Description {{product.description}}', 'max_product_count' => 3))));
$creative = new AdCreative(null, $ad_account_id);
$creative->setData(array(AdCreativeFields::NAME => 'Dynamic Ad Template Creative Sample', AdCreativeFields::OBJECT_STORY_SPEC => $object_story_spec, AdCreativeFields::PRODUCT_SET_ID => $product_set_id_1));
$creative->create();
// _DOC close [ADCREATIVE_CREATE_DPA_RETARGETING]
$creative_id = $creative->{AdCreativeFields::ID};
$creative->delete();
$feed->delete();
$product_set->delete();
$product_catalog->delete();
// FIXME excluded from runtime
return;
$retailer_id = 'rid';
// _DOC open [ADCREATIVE_READ_DPA_PREVIEW]
// _DOC vars [creative_id, catalog_id, retailer_id:s]
// use FacebookAds\Object\AdCreative;
// use FacebookAds\Object\AdPreview;
// use FacebookAds\Object\Fields\AdCreativeFields;