Conversions API parameter builder SDK is a lightweight tool for improving Conversions API parameter retrieval and quality.
Server-Side Parameter Builder Onboarding Guide
This is the quick start guide to help you integrate with the param builder with a demo example.
-
Check the latest version in CHANGELOG.
-
Install the library via pip CLI:
pip install capi_param_builder_python
Verify if the library install successfully by run
pip list
Verify the library name and version.
-
Checkout the demo application under ./example
-
Go to ./example. Run
python server.pyto get the localhost demo started. -
Visit
http://localhost:8000/to view your local demo page. You should seefbcandfbpvalue printed on the home page. Open your browser's dev tool to check the cookies. You should seefbcandfbpcookies are the same value as printed. -
[Optional] You could visit a website like:
http://localhost:8000/?fbclid=testto further verify your cookie settings. You'll see thefbccookie value containstest. -
[Optional] Visit the
http://localhost:8000/again. Thefbcandfbpvalue should be the same as step 4.
This section explains how to use the SDK. And provide suggestions on the API usage.
- Install the library dependency, from above #Quick Start
- Import the class as
from capi_param_builder import ParamBuilder - Construct your ParamBuilder. We provide 3 options to resolve your etld+1. The reason we need etld+1 is to help get the best domain to have the cookie saved to.
[Recommended] Option 1: Provide a list of etld+1 to the ParamBuilder.
paramBuilder = ParamBuilder(["example.com"])
Option 2: Provide a customized ETLD+1 resolver.
/**
In the demo, if you'd like to try DefaultEtldPlusOneResolver
option(check API usage below). Under ./resolver/default_etld_plus_one_resolver.py is an example
of the implementation.
**/
paramBuilder = ParamBuilder(DefaultEtldPlusOneResolver())
[Not recommended] Option 3: no input for constructor. We'll return one level down from your input URL. This may miss some accuracy.
paramBuilder = ParamBuilder() # Not recommended.
- [Recommended] Call
process_request_from_contextto process fbc, fbp, event_source_url and referrer_url. Pass your framework's request object (or WSGI/ASGI environ) directly — the SDK extracts host / cookies / query / referer (and the request URL forevent_source_url) for you, and returns the recommended cookies to set. See the Framework support section for exactly what to pass for your framework.
updated_cookies = paramBuilder.process_request_from_context(request)
Deprecated: process_request is still supported but deprecated. It does not
construct event_source_url. Prefer process_request_from_context above.
updated_cookies = paramBuilder.process_request(
domain, # str: current full domain url
query_params, #dict[str, List[str]]: query params
cookie_dict, # dict[str, str]: cookies dict
referral_link, #Optional[str]: optional string for full url with query params )
- [Recommended] Save
updated_cookiesas first-party cookies. This helps keep consistent fbc and fbp among your events. Based on your webserver framework, the save cookie API may vary. Feel free to choose the best fit for your use case. Below uses the example from demo application.
Option 1: Get the recommended saved cookie from step 4
process_request_from_context above.
# Get the recommended saved cookie from step 4 API
updated_cookies = paramBuilder.process_request_from_context(request)
for cookie in updated_cookies:
self.send_header( "Set-Cookie",
f"{cookie.name}={cookie.value};Max-Age={cookie.max_age};path=/;domain={cookie.domain}",)
Option 2: Get the recommended saved cookie from
paramBuilder.get_cookies_to_set()
# process_request_from_context should be always called
paramBuilder.process_request_from_context(request)
for cookie in paramBuilder.get_cookies_to_set():
self.send_header( "Set-Cookie",
f"{cookie.name}={cookie.value};Max-Age={cookie.max_age};path=/;domain={cookie.domain}",)
- Get fbc, fbp, event_source_url, and referrer_url
fbc = paramBuilder.get_fbc()
fbp = paramBuilder.get_fbp()
event_source_url = paramBuilder.get_event_source_url()
referrer_url = paramBuilder.get_referrer_url()
- Send the parameters back to the Conversions API.
event_source_urlandreferrer_urlare sent at the event level;fbcandfbpare sent insideuser_data.
data=[
'event_name': '...',
'event_time': <your_time>,
'event_source_url': event_source_url, # The value provided in step 6
'referrer_url': referrer_url, # The value provided in step 6
'user_data': {
'fbc': fbc, # The value provided in step 6
'fbp': fbp, # The value provided in step 6
...
}
...
]
paramBuilder.process_request_from_context(request) accepts a request object
directly and extracts host / cookies / query / referer for you. It supports out
of the box:
- ASGI: FastAPI, Starlette, Quart, raw ASGI scope dicts (HTTP/2 falls back to
:authoritywhenhostis absent) - WSGI: Django (
request.META), Flask / Pyramid / Bottle (request.environ), raw WSGI environ dicts
Frameworks that need a small adjustment:
- Tornado — Tornado's
RequestHandler.requestis not WSGI/ASGI shaped. Build an environ-style dict and pass it:paramBuilder.process_request_from_context({ "HTTP_HOST": request.host, "HTTP_REFERER": request.headers.get("Referer"), "QUERY_STRING": request.query, "HTTP_COOKIE": request.headers.get("Cookie", ""), "REMOTE_ADDR": request.remote_ip, })
For any other framework, you can build a PlainDataObject directly and pass it
in, or fall back to the original process_request(host, queries, cookies, referer)
call.
The SDK can extract event_source_url and referrer_url from the incoming
HTTP request. These values help improve Conversions API event matching and
attribution quality.
Using process_request_from_context (recommended):
paramBuilder.process_request_from_context(request)
event_source_url = paramBuilder.get_event_source_url()
referrer_url = paramBuilder.get_referrer_url()Using process_request (deprecated):
process_request does not construct event_source_url — only referrer_url
is available via the referer parameter. Use process_request_from_context if
you need event_source_url.
How it works
event_source_urlis constructed from the scheme, host, and request URI of the incoming HTTP request.referrer_urlis captured from theRefererheader before any fbclid extraction, with an SDK version appendix added.- Both return
Nonewhen the required information is not available.
Send them with your Conversions API payload:
data = {
"event_name": "...",
"event_time": int(time.time()),
"event_source_url": paramBuilder.get_event_source_url(),
"referrer_url": paramBuilder.get_referrer_url(),
"user_data": {
"fbc": paramBuilder.get_fbc(),
"fbp": paramBuilder.get_fbp(),
},
}Conversions API parameter builder for Python is licensed under the LICENSE file in the root directory of this source tree.