I am working on a project that requires obtaining the "scheduled_total" metric from the Facebook Insights API for statistical purposes. However, I have noticed that the results I get through the API do not match what is displayed in the Facebook Business interface. I am currently using Python to make requests to the API. Below is the code I am currently using, along with other metrics:
` import time from facebook_business.api import FacebookAdsApi from facebook_business.adobjects.adaccount import AdAccount from facebook_business.adobjects.adreportrun import AdReportRun
def get_async_insights(ad_account, fields, params):
async_job = ad_account.get_insights(fields=fields, params=params, is_async=True)
async_job.api_get()
# Wait for the async job to complete
while async_job[AdReportRun.Field.async_status] != "Job Completed":
time.sleep(1)
print("> Running extraction...")
async_job.api_get()
return async_job.get_result()
def cast_variable(variable, type):
if type == "int":
return int(variable)
elif type == "float":
return float(variable)
def get_ad_action(ad, action_name, val_type):
if "actions" in ad:
result = 0
for action in ad["actions"]:
if action["action_type"] == action_name:
result += cast_variable(action["value"], val_type)
return result
else:
return 0
if __name__ == "__main__":
# Credentials
access_token = "REDACTED"
app_id = "REDACTED"
app_secret = "REDACTED"
ad_account_id = "REDACTED"
# Perform request
date = "2024-01-01"
FacebookAdsApi.init(app_id, app_secret, access_token, api_version="v20.0")
fields = [
"date_start",
"date_stop",
"ad_id",
"ad_name",
"campaign_id",
"campaign_name",
"impressions",
"clicks",
"actions",
"action_values"
]
params = {
"level": "ad",
"time_range": {"since": date, "until": date},
"breakdowns": ["device_platform"],
"limit": 1000,
"action_attribution_windows": ['1d_click', "7d_click", "1d_view"]
}
# Data extraction
ad_account = AdAccount(ad_account_id)
data = get_async_insights(ad_account=ad_account, fields=fields, params=params)
# Process the data
# See https://developers.facebook.com/docs/marketing-api/reference/ads-insights
# Actions and Action values should be a list<AdsActionStats>:
# See https://developers.facebook.com/docs/marketing-api/reference/ads-action-stats
data_list = []
for ad in data:
row = {
"date": ad["date_start"],
"ad_id": ad["ad_id"],
"ad_name": ad["ad_name"],
"campaign_id": ad["campaign_id"],
"campaign_name": ad["campaign_name"],
"device": ad["device_platform"],
"impressions": int(ad.get("impressions", 0)),
"clicks": int(ad.get("clicks", 0)),
"scheduled_appointments": get_ad_action(ad, "schedule_total", "int")
}
data_list.append(row)
print(data_list)
`
Specifically, the result I am getting is 0, which should not be the case. Has anyone faced a similar issue and found a solution? Additionally, could this discrepancy be due to some additional configuration that needs to be enabled in the Facebook App settings?