Fetching Facebook Ad spends grouped by campaign names - facebook-graph-api

I wanted to fetch the daily ad spends in Facebook based on campaigns separately, ie, for each campaign with its spend.
I have tried with API call, but that gives state wise classification. How do I group based on campaign names?
"https://graph.facebook.com/v14.0/act_xxxxxx/insights?" + encodeURI("level=ad&fields=spend&access_token=FB_ACCESS_TOKEN&breakdowns=region,country&time_range={'since':'2022-08-09','until':'2022-08-09'}&filtering=[{\"field\":\"country\",\"operator\":\"CONTAIN\",\"value\":\"US\"}]&limit=100")

Campaigns and adsets has an insights field where you can read performance and spending metrics.
Get insights from campaigns endpoint
https://graph.facebook.com/v14.0/act_xxx/campaigns?fields=name,insights{spend}
you can use date filters with you calls to get lifetime spending.
you can also get spending from campaign adset insights field.
Check the official documentation
https://developers.facebook.com/docs/marketing-api/reference/ad-campaign/insights

Related

How to build events aggregation service for high load system with DynamoDB

I'm working on an Ad-tech system which serves millions of users.
Basically users (non anonymous users) can see different Ads that are being created by the marketing team.
Our marketing team want to be able to set some Frequency caps on those Ads (among other targeting rules they already have)
For example:
"We should not show this ad for a user if he already seen/click this ad more than X times in the last Y days"
Also ads can be grouped to campaigns, so rules like that are also possibile:
"We should not show this for a user if he viewed more than X times ads in this campaign in the last Y days".
Also our marketing might wanna know how many people viewed/click a specific add in the last Y days.
We have roughly 200K RPM and our responses should be very fast.
The smallest unit of time for our queries is one day and it will not change.
Few questions and thoughts:
Is DynamoDB a good fit?
I thought about creating a table for each event type (Click/View/Close..)
What is the best way to configure the primary key?
I thought about settings the primary key as the user id and the sort key as a combination of the ad id and the current day {dd/mm/yyyy}
I thought about use "ADD" operation to increase the counter when a user click/view/.. an Ad in a specific date. are they expensive operations? do I have an alternative?
What is the best way I can use to also be able to query per ad and campaigns as well (for example: "all users views for all ads in campaign" or "get all ad views in the last 40 days) ) ?
What other considerations should I take in mind?
Thanks a lot

Facebook marketing API to get campaigns active for particular day

I have script which pulls all campaigns from Facebook marketing API and then respective ads from it.
I have 200 campaigns. I need campaigns which were active on some past date say 12 Nov 2020. The campaign in inactive now, but was active on particular date. So when I query (using REST) for that particular date filtering by start and end date as 12 Nov 2020. It doesn't send me that campaign in results since campaign is not really active NOW.
Is there a way where I could get active campaigns on a particular date? or other way get only campaigns which really have some data on a given date?
Problem with loading all campaigns and then subsequent ads to check for data is huge problem due to throttling when we have good no of campaigns.
Thanks
You need to query with all the effective statuses in the filter object for the campaign. That will provide you with all the data you are after and not just the currently active.
Append the following filter to your graph api call for campaigns:
&filtering[]={"field":"effective_status","operator":"IN","value":["ACTIVE","PAUSED","DELETED","ARCHIVED","IN_PROCESS","WITH_ISSUES"]}

Instagram Graph API Insights - Reach between two dates

In Instagram, the reach metric is a de-duplicated data source. In other words, the sum of 7 days worth of data does not necessarily equal the value for that week. This is similar to the Facebook Marketing reach metric.
I'd like to support this scenario: given two dates, give me the reach between those two dates.
In the FB Marketing API this is easily achieved using the since and until parameters to define the date boundaries, and then using a time_increment of all_days to retrieve the reach summed up across all those days.
For example, using the FB Marketing API Docs here is a FB Marketing url that requests a week's worth of data summed up:
{adaccount_id}/insights?metric=reach&since=2018-02-05&until=2018-02-11&time_increment=all_days
In the Instagram Graph API I can define a since and until parameter to define the date boundaries, but there is no parameter like time_increment.
Using the Instagram Graph API Docs, here is a url that requests a week's worth of data split up by day:
{instagram_business_account_id}/insights?metric=reach&period=day&since=1517788800&until=1518307200
Is there any way to change that around to get the summed up reach between two dates? I had hoped for a period option of all_days similar to the FB Marketing API's time_increment parameter but that doesn't exist.

Facebook Marketing API limits

I use marketing fb graph API through my fb app with developer level access. I need to basically get all data (info about campaigns, ad sets, stats, and so on) as often as possible. There are some limits and I'm reaching them pretty quickly calling graph marketing API. After a few hundred calls I'm stuck with err #17 - request limit reached.
My questions are:
is it possible and how to increase the limit
what's the efficient way of getting all data. I'm mostly interested in tracking changes (smth has been added/ updated/ deleted and stats), but first of course I need to gather all my marketing account data somehow.
Requesting each object for it's info / stats is going to hurt after a while. If you need detailed stats about engagement, social reach, impressions and such, you have a couple options:
The /reportstats endpoint is available for most objects and overs aggregation filters, specificity, column selecting and other useful things and can also be called to asynchronously generate a stats report for all of the objects you requests. This can even be done on an account level. You could easily retrieve a low-level aggregation of delivery stats for the entire tree of objects in your account or campaign.
Another more recent development in their API is the Insights endpoint, which is a centralized endpoint for retrieving stats as well and operates more like the rest of the graph API (request field names, filtering, preset, etc).
I recommend reading up on each for retrieving delivery stats for your ads api objects.
Additionally, if you're looking for the objects' attributes themselves, there are a couple options. The most useful one may be that you can request the graph api with no endpoint and a list of object ids in the params. The request would look like this graph.facebook.com/?ids=000000000. Furthermore, you can use facebook's fields query parameter to return attributes about that object. But, wait; there's more. Facebook's graph api offers field expansion for connected objects through this same query parameter. That is to say, you could request all campaigns' attributes, all ad sets' names and all ad sets' ads' names like so:
curl -G \
-d 'ids=00000000,111111111' \ # ids of campaigns
-d 'fields=name,adcampaigns.limit(50).fields(name),adgroups.limit(50).fields(name)' \
-d 'access_token=xxxxxxxxxx' \
https://graph.facebook.com/v2.4

Bounce rate from the Alexa API

Does the Alexa API give a way to query Bounce Rates? There seems to be no ResponseGroup that returns this information. I've tried all the ResponseGroups and Actions mentioned in the documentation here.
There is no way to get highly accurate data via their API, but the alternative is to use SimilarWeb API for engagement data such as:
Average Page Views
Average number of page views per visitor
Average Time On Site
Bounce Rate
You can read more about the Engagement API in this link: https://developer.similarweb.com/engagement_api