I am creating a toolbar for myself that helps me keep track of the Facebook ads that I run.
What I could not figure out is:
How can I get an array of ad IDs for all active ads or can it be active after any further action on my part?
In other words, I want all the ads that I have installed for Active, and which exist in active and active advertisements (and therefore, these ads right now) ... plus all the ads that in my opinion are Active, but Facebook has set another status, such as Awaiting Review (and will return to active soon).
I have the code below, but the problem is that it also randomly includes pending ads that, once they are reviewed and approved by Facebook, will be inactive and not active (because I set them that way). And I do NOT want this type of ad to be included in my report.
My report should only show those where I actively spend money or have the opportunity to spend money as soon as they are approved by FB.
I think I understand the difference between configured_status
and effective_status
in AbstractArchivableCrudObjectFields
, but I don’t know that this is enough to help me, because I have a lot of ads configured to Active that are within Addresses that are inactive, and I don’t want to see those that are listed in my report.
Any recommendations?
public function getActiveAdIds() {
$key = 'activeAdIds';
$adIdsJson = Cache::get($key);
if ($adIdsJson) {
$adIds = json_decode($adIdsJson);
} else {
$adsResponse = $this->getAdsByStatus([ArchivableCrudObjectEffectiveStatuses::ACTIVE, ArchivableCrudObjectEffectiveStatuses::PENDING_REVIEW]);
$ads = $adsResponse->data;
$adIds = [];
foreach ($ads as $ad) {
$adIds[] = $ad->id;
}
$adIdsJson = json_encode($adIds);
Cache::put($key, $adIdsJson, 1);
}
return $adIds;
}
public function getAdsByStatus($statuses) {
$params = [\FacebookAds\Object\Fields\AbstractArchivableCrudObjectFields::EFFECTIVE_STATUS => $statuses];
$adAccount = new AdAccount(self::ACT_PREPEND . $this->fbConfig['account_id']);
$cursor = $adAccount->getAds([], $params);
$response = $cursor->getResponse();
$jsonString = $response->getBody();
return json_decode($jsonString);
}