Ronak Shah
Ronak Shah

Reputation: 420

Fb Ads api How to get Active only FB Campaigns?

I am trying to get the campaigns list from facebook ads api I am using below code

        $account = new AdAccount('act_' . $account_id);
        $campaignSets = $account->getCampaigns(array(
            CampaignFields::ID,
            CampaignFields::NAME,
            CampaignFields::START_TIME,
            CampaignFields::STOP_TIME,
            CampaignFields::SPEND_CAP,
            'effective_status'
                ));

But I need only the active campaigns list so how can I filter the list by Camapign Status = Active only

Thanks, Ronak Shah

Upvotes: 2

Views: 4167

Answers (2)

Luke Madhanga
Luke Madhanga

Reputation: 7447

I can show you how to achieve this using the raw REST-API (I opted not to use FB's unnecessarily convoluted SDK (Python)).

https://graph.facebook.com/v2.11/act_99999999999999/campaigns?status=["ACTIVE","PAUSED","ARCHIVED","PENDING_REVIEW","DISAPPROVED","PREAPPROVED","PENDING_BILLING_INFO","CAMPAIGN_PAUSED","ARCHIVED","ADSET_PAUSED"]&format=json&method=get&pretty=1&limit=250&fields=field1,field2&time_range={"since": "2017-11-01", "until": "2017-12-05"}&access_token=XXX

The key here is the status=[] GET parameter.

Upvotes: 4

wai
wai

Reputation: 9123

If you check the method declaration for AdAccount->getCampaigns, you will see that it accepts two parameters $fields and $params. $fields for the fields that you want to retrieve, and $params as a filter. Example in your case (tested with v2.5):-

$account = new AdAccount('act_' . $account_id);

$fields = array(
    CampaignFields::ID,
    CampaignFields::NAME,
    CampaignFields::START_TIME,
    CampaignFields::STOP_TIME,
    CampaignFields::SPEND_CAP,
    'effective_status'
);

$params = array(
    'effective_status' => array(
        Campaign::STATUS_ACTIVE
    ),
);

$campaignSets = $account->getCampaigns($fields, $params);

Hope this helps! :)

Upvotes: 7

Related Questions