Skip to content

KeyError: 'fbtrace_id' when sending event with ActionSource.APP #709

@bdoms

Description

@bdoms

Everything is working well for us with this SDK, except that we encounter a bug every time we try to set the action source as ActionSource.APP. Changing all the other fields doesn't produce this problem and we can see events coming through correctly.

Given a setup like this:

import os
import uuid

from facebook_business.adobjects.serverside.action_source import ActionSource
from facebook_business.adobjects.serverside.custom_data import CustomData
from facebook_business.adobjects.serverside.event import Event
from facebook_business.adobjects.serverside.event_request_async import EventRequestAsync
from facebook_business.adobjects.serverside.user_data import UserData
from facebook_business.api import FacebookAdsApi

FacebookAdsApi.init(access_token=os.environ['FACEBOOK_ACCESS_TOKEN'])

And then a function that does this:

user_data = UserData(email='[email protected]', phone='+15555550000',
    client_ip_address='127.0.0.1', client_user_agent='a valid user agent')

custom_data = CustomData(currency='usd', value=9.99)

event = Event(
    event_name='Purchase',
    event_time=int(time.time()),
    event_id=f'{event_name}-{uuid.uuid4()}',
    user_data=user_data,
    custom_data=custom_data,
    data_processing_options=[],
    event_source_url='https://www.example.com/checkout',
    action_source=ActionSource.APP
)

event_request_async = EventRequestAsync(events=[event], pixel_id=os.environ['FACEBOOK_PIXEL_ID'])
await event_request_async.execute()

That last lines fails with

File "[redacted]/python3.13/site-packages/facebook_business/adobjects/serverside/event_request_async.py", line 99, in execute
    fbtrace_id=response['fbtrace_id'],
               ~~~~~~~~^^^^^^^^^^^^^^
KeyError: 'fbtrace_id'

Again, it seems like none of the data we're including matters except for the action_source. If it's ActionSource.WEBSITE then this will work correctly without an error every time.

Looking at that line of code here: https://github.com/facebook/facebook-python-business-sdk/blob/main/facebook_business/adobjects/serverside/event_request_async.py#L99

It seems like it's making some assumptions about what's included in the response, when maybe it should be more careful?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions