public function testAdPreviews() { // Preview with actual creative $previews = $this->adCreative->getAdPreviews(array(), array(AdPreviewFields::AD_FORMAT => AdFormats::RIGHT_COLUMN_STANDARD)); $this->assertNotEquals(0, $previews->count()); $preview = $previews->offsetGet(0); $this->assertRegExp('/iframe/', $preview->{AdPreviewFields::BODY}); // Preview with actual adgroup $previews = $this->adGroup->getAdPreviews(array(), array(AdPreviewFields::AD_FORMAT => AdFormats::RIGHT_COLUMN_STANDARD)); $this->assertNotEquals(0, $previews->count()); $preview = $previews->offsetGet(0); $this->assertRegExp('/iframe/', $preview->{AdPreviewFields::BODY}); // Preview with creative specs $account = new AdAccount($this->getActId()); $previews = $account->getAdPreviews(array(), array(AdPreviewFields::CREATIVE => array(AdCreativeFields::BODY => 'Testing the creative preview', AdCreativeFields::OBJECT_ID => $this->getPageId()), AdPreviewFields::AD_FORMAT => AdFormats::RIGHT_COLUMN_STANDARD)); $this->assertNotEquals(0, $previews->count()); $preview = $previews->offsetGet(0); $this->assertRegExp('/iframe/', $preview->{AdPreviewFields::BODY}); }
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); }
public function tearDown() { if ($this->adGroup) { $this->adGroup->delete(); $this->adGroup = null; } if ($this->adSet) { $this->adSet->delete(); $this->adSet = null; } if ($this->adCampaign) { $this->adCampaign->delete(); $this->adCampaign = null; } if ($this->adImage) { $this->adImage->delete(); $this->adImage = null; } if ($this->adCreative) { $this->adCreative->delete(); $this->adCreative = null; } parent::tearDown(); }
/** * @param array $fields * @param array $params * @return Cursor */ public function getAdGroups(array $fields = array(), array $params = array()) { return $this->getManyByConnection(AdGroup::className(), $fields, $params); }
$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->create(); echo 'Image Hash: ' . $image->hash . "\n"; /** * Step 5 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 6 Search Targeting */ use FacebookAds\Object\TargetingSearch; use FacebookAds\Object\Search\TargetingSearchTypes; $results = TargetingSearch::search($type = TargetingSearchTypes::INTEREST, $class = null, $query = 'facebook'); // we'll take the top result for now $target = count($results) ? $results->getObjects()[0] : null; echo "Using target: " . $target->name . "\n"; $targeting = array('geo_locations' => array('countries' => array('GB')), 'interests' => array(array('id' => $target->id, 'name' => $target->name))); /** * Step 7 Create an AdGroup */ use FacebookAds\Object\AdGroup; use FacebookAds\Object\Fields\AdGroupFields; use FacebookAds\Object\Fields\AdGroupBidInfoFields; $adgroup = new AdGroup(null, $account->id); $adgroup->setData(array(AdGroupFields::CREATIVE => array('creative_id' => $creative->id), AdGroupFields::NAME => 'My First AdGroup', AdGroupFields::BID_TYPE => AdGroup::BID_TYPE_CPM, AdGroupFields::BID_INFO => array(AdGroupBidInfoFields::IMPRESSIONS => '2'), AdGroupFields::CAMPAIGN_ID => $adset->id, AdGroupFields::TARGETING => $targeting)); $adgroup->create(); echo 'AdGroup ID:' . $adgroup->id . "\n";
/** * @param array $fields * @param array $params * @return Cursor */ public function getAdGroupsByLabel(array $fields = array(), array $params = array()) { return $this->getManyByConnection(AdGroup::classname(), $fields, $params, 'adgroupsbylabels'); }
// 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(); // Output targeting description foreach ($targeting_description->targetingsentencelines as $description) { echo $description['content'] . PHP_EOL; foreach ($description['children'] as $child) { echo "\t" . $child . PHP_EOL; } } // _DOC close [ADGROUP_GET_TARGETING_DESCRIPTION] $adgroup->delete(); $adset->delete(); $campaign->delete(); $creative->delete(); $image->delete();