Reputation: 9241
I am trying to set up an API in Lumen which fetches data from Facebooks Ads API and stores it in the database.
I am having trouble understanding the Facebook API. I want to fetch data for ad accounts, ad campaigns, ad sets and ads.
Correct me if I am wrong but based on Facebooks ad structure, I should be able to get ad accounts and then get ad campaigns that belong to that ad account and then ad sets which belong to the campaign and then ads that belong to that ad set. Is this correct?
Thanks
Upvotes: 0
Views: 2593
Reputation: 1133
Yes, that's correct. Facebook ads are represented as several objects in the Facebook Graph accessible via the Ads API (aka Marketing API).
Given an ad account you can retrieve the ad objects that belong to it by querying the ad account edges adcampaign_groups (campaigns), adcampaigns (adsets), and adgroups (ads).
curl https://graph.facebook.com/v2.4/act_<AD_ACCOUNT_ID>/adcampaign_groups?access_token=<ACCESS_TOKEN>
curl https://graph.facebook.com/v2.4/act_<AD_ACCOUNT_ID>/adcampaigns?access_token=<ACCESS_TOKEN>
curl https://graph.facebook.com/v2.4/act_<AD_ACCOUNT_ID>/adgroups?access_token=<ACCESS_TOKEN>
Note that Graph results are paged so depending on how many objects exist you may need to query across pages. Object reference documentation will tell you what fields can be accessed, note that Facebook only returns id field information by default, other fields must be explicitly queried for.
For example, to read the name and account status of an ad account in curl use:
curl -G \
-d "fields=name,account_status" \
-d "access_token=<ACCESS_TOKEN>" \
"https://graph.facebook.com/v2.4/act_<AD_ACCOUNT_ID>"
For more information on the Facebook ad object structure see this Developers documentation page https://developers.facebook.com/docs/marketing-api/getting-started#structure and detailed object reference at https://developers.facebook.com/docs/marketing-api/reference
Upvotes: 3