-1

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?

1 Answer 1

0

Well, we finally know why, and it's about the business location.

If we go to the explanation of the variable "Scheduled Appointments" in this guide: https://www.facebook.com/business/help/512698555829471

There is a small notice that says: Some metrics and ad campaigns related to the messaging feature, as well as organic messaging functions, will not be available for businesses or ads whose destination or origin is Europe or Japan.

Let's click on the link that appears about metrics, which should take us here: https://www.facebook.com/business/help/384262082655927

And I emphasize the following message: Due to new privacy regulations in Europe and Japan, some message metrics may not be available. It doesn't say all, of course, but one we are trying to extract is affected in our case. Let's pay attention to the notice, and which functionalities are affected, one of them is "Messaging functions via API". That means that even though the data appears on the dashboard, we will not be able to get it via the API.

If we go to the "Meta Ads Reporting and Ads Manager" section and expand it, we will see that one of the affected variables is "Appointments Scheduled".

That is why, even if it appears in the metrics to be extracted: https://developers.facebook.com/docs/marketing-api/reference/ads-action-stats, we will not be able to obtain this variable in any way

Not the answer you're looking for? Browse other questions tagged or ask your own question.