David
David

Reputation: 553

Get ads Insights and AdCreatives data for an AdAccount on Facebook Ads PHP SDK

I want transform this Facebook Graph API request:

act_xyz/ads?fields=insights.date_preset(yesterday){ad_name,adset_name,campaign_name,account_name,account_id,impressions,inline_link_clicks,spend,ad_id},adcreatives{object_story_spec}

to use Facebook Ads PHP SDK. Now I have this code:

$account = new AdAccount('act_xyz');

$fields = array(
    InsightsFields::AD_NAME,
    InsightsFields::ADSET_NAME,
    InsightsFields::CAMPAIGN_NAME,
    InsightsFields::ACCOUNT_NAME,
    InsightsFields::ACCOUNT_ID,
    InsightsFields::IMPRESSIONS,
    InsightsFields::INLINE_LINK_CLICKS,
    InsightsFields::SPEND,
    InsightsFields::AD_ID
);

$params = array(
    'date_preset' => InsightsPresets::YESTERDAY
);

$account->getInsights($fields, $params);

[...]

$fields = array(
    AdCreativeFields::OBJECT_STORY_SPEC
);

$account->getAdCreatives($fields);

I can get all data separately, but I want obtain AdCreatives and Insights for each Ad on my AdAccount like Graph API does. Thanks.

Upvotes: 2

Views: 1392

Answers (1)

wai
wai

Reputation: 9143

You still can perform Graph API calls to Ads related endpoint if you have the right access token. Using the latest Facebook PHP SDK & Facebook Ads PHP SDK:-

$fb = new Facebook(['app_id' => {APP_ID}, 'app_secret' => {APP_SECRET}]);

$account = new AdAccount('act_xyz');

$data = $fb->get('/' . $account->id . '/ads?fields=insights.date_preset(yesterday){ad_name,adset_name,campaign_name,account_name,account_id,impressions,inline_link_clicks,spend,ad_id},adcreatives{object_story_spec}', $access_token);

I've tested this and it works. I am using Facebook PHP SDK v5.1.2 and Facebook Ads PHP SDK v2.5.1.

Hope this helps! :D

Upvotes: 1

Related Questions