public function testCrud() { $account = new AdAccount($this->getConfig()->accountId); $job = $account->getInsightsAsync(); $this->assertTrue($job instanceof AsyncJobInsights); $this->waitTillJobComplete($job); $job->getResult(); }
/** * @param mixed $objectId * @param array $params * * @return \Illuminate\Support\Collection */ public function getInsights($objectId, $params = []) { $fields = $params['fields']; unset($params['fields']); $account = new AdAccount($objectId); $insights = $account->getInsights($fields, $params); return $this->response($insights); }
public function testCrud() { $account = new AdAccount($this->getConfig()->accountId); $report_stats_params = array('date_preset' => 'yesterday', 'data_columns' => "['campaign_name','reach','actions','spend', 'clicks']"); $job = $account->getReportStatsAsync(array(), $report_stats_params); $this->assertTrue($job instanceof AsyncJobReportStats); $this->waitTillJobComplete($job); $job->getResult(); }
/** * @depends testCreate */ public function testCrud() { $account = new AdAccount($this->getConfig()->accountId); $pixel = $account->getAdsPixels()->current(); $pixelId = $pixel->{AdsPixelsFields::ID}; $pixel = new AdsPixel($pixelId); $this->assertCanRead($pixel); $this->assertCanUpdate($pixel, array(AdsPixelsFields::NAME => $this->getConfig()->testRunId . ' updated')); $this->assertCannotDelete($pixel); }
public function testFields() { $account = new AdAccount($this->getConfig()->accountId); $fields = array(AdAccountFields::NAME); $account->read($fields); $fields = array(InsightsFields::ACCOUNT_NAME); $job = $account->getInsightsAsync($fields); $this->assertTrue($job instanceof AsyncJobInsights); $this->waitTillJobComplete($job); $result = $job->getResult(); $this->assertEquals($result[0]->{InsightsFields::ACCOUNT_NAME}, $account->{AdAccountFields::NAME}); }
public function setup() { parent::setup(); $this->account = new AdAccount($this->getConfig()->accountId); $cursor = $this->account->getAdsPixels(); $this->adsPixel = $cursor->current(); $this->productCatalog = new ProductCatalog(null, $this->getConfig()->businessId); $this->productCatalog->setData(array(ProductCatalogFields::NAME => $this->getConfig()->testRunId)); $this->productCatalog->create(); $this->productSet = new ProductSet(null, $this->productCatalog->{ProductCatalogFields::ID}); $this->productSet->setData(array(ProductSetFields::NAME => $this->getConfig()->testRunId, ProductSetFields::FILTER => array('retailer_id' => array('is_any' => array('pid1', 'pid2'))))); $this->productSet->create(); }
public function setup() { parent::setup(); $this->account = new AdAccount($this->getActId()); $cursor = $this->account->getAdsPixels(); $this->adsPixel = $cursor->current(); $this->productCatalog = new ProductCatalog(null, $this->getBusinessManagerId()); $this->productCatalog->setData(array(ProductCatalogFields::NAME => 'Test Catalog ' . $this->getTestRunId())); $this->productCatalog->create(); $this->productSet = new ProductSet(null, $this->productCatalog->{ProductCatalogFields::ID}); $this->productSet->setData(array(ProductSetFields::NAME => 'Test Set ' . $this->getTestRunId(), ProductSetFields::FILTER => array('retailer_id' => array('is_any' => array('pid1', 'pid2'))))); $this->productSet->create(); }
public function testCrud() { $account = new AdAccount($this->getActId()); $pixels = $account->getAdsPixels(); $this->assertEquals(1, $pixels->count()); /** @var AdsPixel $pixel */ $pixel = $pixels->current(); $this->assertTrue($pixel instanceof AdsPixel); $pixelId = $pixel->{AdsPixelsFields::ID}; $pixel = new AdsPixel($pixelId); $this->assertCanRead($pixel); $this->assertCannotDelete($pixel); $this->assertCannotUpdate($pixel); }
public function testCrudAccess() { $account = new AdAccount($this->getConfig()->accountId); $cursor = $account->getPartnerCategories(); if (!$cursor->count()) { $this->markTestSkipped(); } /* @var $category AbstractCrudObject */ $category = $cursor->current(); $this->assertTrue($category instanceof PartnerCategory); $this->assertCanRead($category); $this->assertCannotUpdate($category); $this->assertCannotDelete($category); $this->assertCannotCreate(new PartnerCategory(null, $this->getConfig()->accountId)); }
function queryTotal(array $dimensions, $column, \DateTime $date) { $account = new AdAccount(requireEnv('FACEBOOK_ACCOUNT_ID')); $collapsedDimensions = implode(',', $dimensions); $params = ['breakdowns' => $collapsedDimensions, 'level' => InsightsLevels::ADSET, 'time_range' => ['since' => $date->format('Y-m-d'), 'until' => $date->format('Y-m-d')]]; $asyncJob = $account->getInsightsAsync([], $params); echo sprintf("Created async job %s (%s)\n", $asyncJob->getData()['id'], $collapsedDimensions); $result = waitForJobToComplete($asyncJob); $total = 0; foreach ($result as $row) { $data = $row->getData(); $total += $data['spend']; } return $total; }
public function testCrudAccess() { $ad_account = new AdAccount($this->getConfig()->accountId); $ad_users = $ad_account->getAdUsers(); $this->assertGreaterThan(0, $ad_users->count()); $uid = (new AdUser('me'))->read()->{AdUserFields::ID}; $found = false; foreach ($ad_users as $ad_user) { if ($ad_user->id === $uid) { $this->assertCanFetchConnection($ad_user, 'getAdAccounts'); $this->assertCanFetchConnection($ad_user, 'getAdAccountGroups'); $found = true; break; } } $this->assertTrue($found); }
/** * AdsPixels can be created but only one per account can exist */ public function testCreate() { // make sure there's at least one pixel $account = new AdAccount($this->getConfig()->accountId); $pixels = $account->getAdsPixels(); $pixel = null; if (!$pixels->count()) { $pixel = new AdsPixel(null, $this->getConfig()->accountId); $pixel->{AdsPixelsFields::NAME} = 'WCA Pixel'; $this->assertCanCreate($pixel); } else { $pixel = $pixels->current(); } $pixel = new AdsPixel(null, $this->getConfig()->accountId); $pixel->{AdsPixelsFields::NAME} = $this->getConfig()->testRunId; $this->assertCannotCreate($pixel); }
public function testCrudAccess() { $ad_account = new AdAccount($this->getActId()); $ad_users = $ad_account->getAdUsers(); $this->assertNotNull($ad_users); $this->assertGreaterThan(0, $ad_users->count()); $me = (new FacebookRequest($this->getSession(), Api::HTTP_METHOD_GET, '/me'))->execute()->getResponse(); $this->assertNotNull($me); $uid = $me->{'id'}; $found = false; foreach ($ad_users as $ad_user) { if ($ad_user->id === $uid) { $this->assertCanFetchConnection($ad_user, 'getAdAccounts'); $found = true; break; } } $this->assertTrue($found); }
public function createAdAccount(array $fields = array(), array $params = array(), $pending = false) { $this->assureId(); $param_types = array('adaccounts' => 'list<string>'); $enums = array(); $request = new ApiRequest($this->api, $this->data['id'], RequestInterface::METHOD_POST, '/adaccounts', new AdAccount(), 'EDGE', AdAccount::getFieldsEnum()->getValues(), new TypeChecker($param_types, $enums)); $request->addParams($params); $request->addFields($fields); return $pending ? $request : $request->execute(); }
public function testCrudAccess() { $ad_account = new AdAccount($this->getActId()); $ad_users = $ad_account->getAdUsers(); $this->assertNotNull($ad_users); $this->assertGreaterThan(0, $ad_users->count()); $me = $this->getApi()->call('/me')->getContent(); $this->assertNotNull($me); $uid = $me['id']; $found = false; foreach ($ad_users as $ad_user) { if ($ad_user->id === $uid) { $this->assertCanFetchConnection($ad_user, 'getAdAccounts'); $found = true; break; } } $this->assertTrue($found); }
/** * Method to get all active adgroups from Facebook account */ private function get_active_adgroups() { $account = new AdAccount('act_' . self::$accountId); $adgroups = $account->getAds(array(AdFields::ID, AdFields::ADSET_ID, AdFields::EFFECTIVE_STATUS), array('limit' => 500)); $loopCount = 0; while (true) { $num = $adgroups->count(); $adgroups->fetchAfter(); if ($num == $adgroups->count() || $loopCount == 10) { break; } $loopCount = $loopCount + 1; } $finalAdgroups = []; foreach ($adgroups as $adgroup) { $adgroupData = (array) $adgroup->getData(); if ($adgroupData['effective_status'] != 'ACTIVE') { continue; } $adgroupObject = array('id' => $adgroupData['id'], 'adset_id' => $adgroupData['adset_id']); array_push($finalAdgroups, $adgroupObject); } return $finalAdgroups; }
$audience->setData(array(CustomAudienceFields::NAME => 'My new CA', CustomAUdienceFields::DESCRIPTION => 'Docsmith', CustomAudienceFields::SUBTYPE => CustomAudienceSubtypes::CUSTOM)); $audience->create(); $custom_audience_id = $audience->{CustomAudienceFields::ID}; $user_id_1 = 1234; $user_id_2 = 12345; // _DOC open [CUSTOM_AUDIENCE_USERS_ADD_ID] // _DOC vars [custom_audience_id:s, application_id, user_id_1, user_id_2] // use FacebookAds\Object\CustomAudience; // use FacebookAds\Object\Fields\CustomAudienceFields; // use FacebookAds\Object\Values\CustomAudienceTypes; // Add Facebook IDs of users of certain applications $audience = new CustomAudience($custom_audience_id); $audience->addUsers(array($user_id_1, $user_id_2), CustomAudienceTypes::ID, array($application_id)); // _DOC close [CUSTOM_AUDIENCE_USERS_ADD_ID] $audience->delete(); $account = new AdAccount($ad_account_id); $pixels = $account->getAdsPixels(array(AdsPixelsFields::CODE)); if (!$pixels->count()) { $pixel = new AdsPixel(null, $ad_account_id); $pixel->{AdsPixelsFields::NAME} = 'My WCA Pixel'; $pixel->create(); } else { $pixel = $pixels->current(); } $pixel_id = $pixel->{AdsPixelsFields::ID}; // _DOC open [CUSTOM_AUDIENCE_CREATE_WCA] // _DOC vars [ad_account_id:s, pixel_id] // use FacebookAds\Object\CustomAudience; // use FacebookAds\Object\Fields\CustomAudienceFields; // use FacebookAds\Object\Values\CustomAudienceSubtypes; $custom_audience = new CustomAudience(null, $ad_account_id);
public function setup() { parent::setup(); $account = new AdAccount($this->getConfig()->accountId); $this->adsPixel = $account->getAdsPixels()->current(); if ($this->adsPixel === null) { throw new \Exception('Ads Pixel is null'); } $this->productCatalog = new ProductCatalog(null, $this->getConfig()->businessId); $this->productCatalog->setData(array(ProductCatalogFields::NAME => $this->getConfig()->testRunId)); $this->productCatalog->create(); $this->productSet = new ProductSet(null, $this->productCatalog->{ProductCatalogFields::ID}); $this->productSet->setData(array(ProductSetFields::NAME => $this->getConfig()->testRunId, ProductSetFields::FILTER => array('retailer_id' => array('is_any' => array('pid1', 'pid2'))))); $this->productSet->create(); $this->productAudience = new ProductAudience(null, $this->getConfig()->accountId); $this->productAudience->setData(array(ProductAudienceFields::NAME => $this->getConfig()->testRunId, ProductAudienceFields::PRODUCT_SET_ID => $this->productSet->{ProductSetFields::ID}, ProductAudienceFields::PIXEL_ID => $this->adsPixel->{AdsPixelsFields::ID}, ProductAudienceFields::INCLUSIONS => array(array('retention_seconds' => 86400, 'rule' => array('and' => array(array('event' => array('eq' => 'ViewContent')), array('userAgent' => array('i_contains' => 'iPhone')))))))); $this->productAudience->create(); }
$campaign->delete(); $campaign = new AdCampaign(null, $ad_account_id); $campaign->setData(array(AdCampaignFields::NAME => 'My First Homepage Campaign', AdCampaignFields::OBJECTIVE => AdObjectives::NONE, AdCampaignFields::BUYING_TYPE => AdBuyingTypes::FIXED_CPM, AdCampaignFields::STATUS => AdCampaign::STATUS_PAUSED)); $campaign->create(); $campaign_group_id = $campaign->id; // _DOC open [ADSET_CREATE_HOMEPAGE] // _DOC vars [ad_account_id:s, campaign_group_id] // use FacebookAds\Object\AdAccount; // use FacebookAds\Object\AdSet; // use FacebookAds\Object\TargetingSpecs; // use FacebookAds\Object\Fields\AdSetFields; // use FacebookAds\Object\Fields\TargetingSpecsFields; // use FacebookAds\Object\Values\BillingEvents; // use FacebookAds\Object\Values\OptimizationGoals; // use FacebookAds\Object\Values\PageTypes; $account = new AdAccount($ad_account_id); $ratecards = $account->getRateCards(); $country = $ratecards->current()->country; $rate = (int) $ratecards->current()->rate; $impressions = 5000; $lifetime_budget = $rate * $impressions; $adset = new AdSet(null, $ad_account_id); $adset->setData(array(AdSetFields::NAME => 'Adset Homepage Ads', AdSetFields::CAMPAIGN_GROUP_ID => $campaign_group_id, AdSetFields::CAMPAIGN_STATUS => AdSet::STATUS_ACTIVE, AdSetFields::LIFETIME_BUDGET => $lifetime_budget, AdSetFields::LIFETIME_IMPS => $impressions, AdSetFields::OPTIMIZATION_GOAL => OptimizationGoals::REACH, AdSetFields::BILLING_EVENT => BillingEvents::IMPRESSIONS, AdSetFields::BID_AMOUNT => 100, AdSetFields::TARGETING => (new TargetingSpecs())->setData(array(TargetingSpecsFields::PAGE_TYPES => array(PageTypes::LOGOUT), TargetingSpecsFields::GEO_LOCATIONS => array('countries' => array($country)))), AdSetFields::END_TIME => (new \DateTime('+1 week'))->format(\DateTime::ISO8601))); $adset->create(); // _DOC close [ADSET_CREATE_HOMEPAGE] $adset->delete(); $campaign->delete(); $campaign = new AdCampaign(null, $ad_account_id); $campaign->setData(array(AdCampaignFields::NAME => 'My CPC Campaign', AdCampaignFields::OBJECTIVE => AdObjectives::PAGE_LIKES, AdCampaignFields::BUYING_TYPE => AdBuyingTypes::AUCTION, AdCampaignFields::STATUS => AdCampaign::STATUS_PAUSED)); $campaign->create(); $campaign_group_id = $campaign->id;
// use FacebookAds\Object\AdAccount; // use FacebookAds\Object\Fields\AdPreviewFields; $account = new AdAccount($ad_account_id); $account->getAdPreviews(array(), array(AdPreviewFields::CREATIVE => $creative_spec, AdPreviewFields::AD_FORMAT => $format)); // _DOC close [ADACCOUNT_GET_PREVIEWS] // _DOC open [ADACCOUNT_GET_PREVIEWS_WITH_OBJECT_STORY_ID] // _DOC vars [ad_account_id:s, post_id:s, format:s] // use FacebookAds\Object\AdAccount; // use FacebookAds\Object\Fields\AdPreviewFields; // use FacebookAds\Object\Fields\AdCreativeFields; $account = new AdAccount($ad_account_id); $account->getAdPreviews(array(), array(AdPreviewFields::CREATIVE => array(AdCreativeFields::OBJECT_STORY_ID => $post_id), AdPreviewFields::AD_FORMAT => $format)); // _DOC close [ADACCOUNT_GET_PREVIEWS_WITH_OBJECT_STORY_ID] // _DOC open [ADACCOUNT_GET_PREVIEWS_DESKTOP_WITH_STORY_ID] // _DOC vars [ad_account_id:s, post_id:s] // use FacebookAds\Object\AdAccount; // use FacebookAds\Object\Fields\AdPreviewFields; // use FacebookAds\Object\Fields\AdCreativeFields; // use FacebookAds\Object\Values\AdFormats; $account = new AdAccount($ad_account_id); $account->getAdPreviews(array(), array(AdPreviewFields::CREATIVE => array(AdCreativeFields::OBJECT_STORY_ID => $post_id), AdPreviewFields::AD_FORMAT => AdFormats::DESKTOP_FEED_STANDARD)); // _DOC close [ADACCOUNT_GET_PREVIEWS_DESKTOP_WITH_STORY_ID] // _DOC open [ADACCOUNT_GET_PREVIEWS_RHS_WITH_STORY_ID] // _DOC vars [ad_account_id:s, post_id:s] // use FacebookAds\Object\AdAccount; // use FacebookAds\Object\Fields\AdPreviewFields; // use FacebookAds\Object\Fields\AdCreativeFields; // use FacebookAds\Object\Values\AdFormats; $account = new AdAccount($ad_account_id); $account->getAdPreviews(array(), array(AdPreviewFields::CREATIVE => array(AdCreativeFields::OBJECT_STORY_ID => $post_id), AdPreviewFields::AD_FORMAT => AdFormats::RIGHT_COLUMN_STANDARD)); // _DOC close [ADACCOUNT_GET_PREVIEWS_RHS_WITH_STORY_ID]
// _DOC close [ADIMAGE_CREATE] $image_id = $image->{AdImageFields::ID}; $image_hash = $image->{AdImageFields::HASH}; // _DOC open [ADIMAGE_DELETE] // _DOC vars [image_id, ad_account_id:s, image_hash:s] // use FacebookAds\Object\AdImage; // use FacebookAds\Object\Fields\AdImageFields; $image = new AdImage($image_id, $ad_account_id); $image->{AdImageFields::HASH} = $image_hash; $image->delete(); // _DOC close [ADIMAGE_DELETE] // _DOC open [ADIMAGE_CREATE_ZIP] // _DOC vars [zip_path:s, ad_account_id:s] // use FacebookAds\Object\AdImage; $images = AdImage::createFromZip($zip_path, $ad_account_id); // Output image hashes foreach ($images as $image) { echo $image->{AdImageFields::HASH} . PHP_EOL; } // _DOC close [ADIMAGE_CREATE_ZIP] $image_1_hash = $images[0]->{AdImageFields::HASH}; $image_2_hash = $images[1]->{AdImageFields::HASH}; // _DOC open [ADIMAGE_READ_MULTI_WITH_HASH] // _DOC vars [ad_account_id:s, image_1_hash:s, image_2_hash:s] // use FacebookAds\Object\AdAccount; $account = new AdAccount($ad_account_id); $images = $account->getAdImages(array(), array('hashes' => array($image_1_hash, $image_2_hash))); // _DOC close [ADIMAGE_READ_MULTI_WITH_HASH] foreach ($images as $image) { $image->delete(); }
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. * */ namespace FacebookAdsDocs; use FacebookAds\Object\AdAccount; use FacebookAds\Object\AdsPixel; use FacebookAds\Object\Fields\AdsPixelsFields; use FacebookAds\Object\Fields\AdAccountFields; use FacebookAds\Object\Fields\BusinessFields; use FacebookAdsTest\Config\Config; /** @var Config $config */ $ad_account_id = $config->accountId; $business_id = $config->businessId; $account = new AdAccount($ad_account_id); $pixels = $account->getAdsPixels(); if (!$pixels->count()) { // _DOC open [ADSPIXEL_CREATE] // _DOC vars [ad_account_id:s] // use FacebookAds\Object\AdsPixel; // use FacebookAds\Object\Fields\AdsPixelsFields; $pixel = new AdsPixel(null, $ad_account_id); $pixel->{AdsPixelsFields::NAME} = 'My WCA Pixel'; $pixel->create(); // _DOC close [ADSPIXEL_CREATE] } else { $pixel = $pixels->current(); } $pixel_id = $pixel->{AdsPixelsFields::ID}; // _DOC open [ADSPIXEL_READ_PIXEL_CODE]
/** * @param $accountId * @param array $fields * * @return Collection * * @see https://developers.facebook.com/docs/marketing-api/reference/adgroup#Reading */ public function getAds($accountId, $fields = []) { $account = new AdAccount($accountId); $ads = $account->getAds($fields); return $this->response($ads); }
public function updateSelf(array $fields = array(), array $params = array(), $pending = false) { $this->assureId(); $param_types = array('agency_client_declaration' => 'map', 'business_info' => 'map', 'end_advertiser' => 'string', 'is_notifications_enabled' => 'bool', 'media_agency' => 'string', 'name' => 'string', 'partner' => 'string', 'redownload' => 'bool', 'spend_cap' => 'float', 'spend_cap_action' => 'string'); $enums = array(); $request = new ApiRequest($this->api, $this->data['id'], RequestInterface::METHOD_POST, '/', new AdAccount(), 'NODE', AdAccount::getFieldsEnum()->getValues(), new TypeChecker($param_types, $enums)); $request->addParams($params); $request->addFields($fields); return $pending ? $request : $request->execute(); }
/** * Method to get all active and paused adgroups from Facebook account */ private function get_adgroups() { $account = new AdAccount('act_' . self::$accountId); $adgroups = $account->getAds(array(AdFields::ID, AdFields::NAME, AdFields::CONFIGURED_STATUS, AdFields::EFFECTIVE_STATUS), array('limit' => 500)); $loopCount = 0; while (true) { $num = $adgroups->count(); $adgroups->fetchAfter(); if ($num == $adgroups->count() || $loopCount == 10) { break; } $loopCount = $loopCount + 1; } $finalAdgroups = []; foreach ($adgroups as $adgroup) { //if(count($tempAdgroups) > 5)continue; $adgroupData = (array) $adgroup->getData(); //DEBUG if ($adgroupData['id'] == '6038255178364') { self::$testAdgroup = array('id' => $adgroupData['id'], 'name' => $adgroupData['name'], 'configured_status' => $adgroupData['configured_status'], 'effective_status' => $adgroupData['effective_status']); } //only active campaigns if ($adgroupData['effective_status'] != 'ACTIVE' && $adgroupData['effective_status'] != 'PAUSED') { continue; } //only active or paused adsets if ($adgroupData['configured_status'] != 'ACTIVE' && $adgroupData['configured_status'] != 'PAUSED') { continue; } $adgroupObject = array('id' => $adgroupData['id'], 'name' => $adgroupData['name'], 'configured_status' => $adgroupData['configured_status'], 'effective_status' => $adgroupData['effective_status']); array_push($finalAdgroups, $adgroupObject); } return $finalAdgroups; }
/** * @param array $fields * @param array $params * @return Cursor */ public function getAdAccounts(array $fields = array(), array $params = array()) { return $this->getManyByConnection(AdAccount::className(), $fields, $params); }
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}); }