Conversions API parameter builder for Ruby
June 9, 2026 · View on GitHub
Introduction
The Conversions API param builder is a light weighted SDK to help improve the Conversions API params' retrieval and quality.
Server-Side Parameter Builder Onboarding Guide
Quick Start
-
Check the latest version in CHANGELOG.
-
Install the library.
gem install capi_param_builder_ruby
- Verify by run
gem list | grep capi_param_builder_ruby
Verify the SDK name and version.
Demo
-
Checkout demo application ./example
-
Install the capi_param_builder library, from above #Quick Start. Also install optional 3rd party library(sinatra, public_suffix) to run the demo.
2.1 sinatra. Install by running gem install sinatra. An easy web framework.
2.2 (optional) public_suffix. Install by running gem install public_suffix. It
will help resolve etld+1, if you'd prefer to use this option to get your etld+1
domain. It's used under./resolver/default_etld_plus_one_resolver.rb as one
example to use etld+1 resolver. For usage, please check in #Usage section below.
- Run local demo server. Once you have capi_param_builder, sinatra library installed. Run the demo application.
cd ./example
ruby app.rb
-
Verification.
4.1 Visit
http://localhost:4567/. You'll see the demo page withfbcandfbpvalue printed on main page. Open dev inspector to check the cookies, same value should be in_fbcand_fbp.fbcmight be null if this is your first time visiting.4.2 Visit
http://localhost:4567/?fbclid=this_is_my_test. You'll see the_fbcandfbcvalue changed. The new value should contain the stringthis_is_my_test. The_fbpvalue should be the same as 4.1.4.3 Visit
http://localhost:4567again. You'll see the_fbcand_fbpstays the same as 4.1 and 4.2.
API usage
This section explains how to use the SDK. And provide suggestions on the API usage.
- Install the capi_param_builder library, from above #Quick Start.
- Import the class as
require 'capi_param_builder' - Construct your ParamBuilder. We provide 3 options.
[Recommended] Option 1: Provide a list of etld+1. We'll use the etld+1 to match your current hostname, then provide recommended update cookies' domain.
builder = ParamBuilder.new(["localhost", "example.com"])
Option 2: Provide an ETLD+1 resolver. Implement a customized ETLD+1 resolver to provide the preferred etld+1 to save the cookies to.
# example implementation DefaultEtldPlusOneResolver from example/resolver. Feel free to provide your owne resolver.
builder = ParamBuilder.new(DefaultEtldPlusOneResolver.new())
Option 3: [Not recommended] empty input. We'll return one level down from your input host. May miss some accuracy.
builder = ParamBuilder.new()
- [Recommended] Call
process_request_from_contextto process fbc, fbp, event_source_url and referrer_url. Pass your framework's request object (or Rackenvhash) 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.
cookies_to_be_updated = builder.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.
cookies_to_be_updated = builder.process_request(
domain, # str: current host
name query_params, #dict[str, List[str]]: query params as hash type
cookie_dict,#dict[str, str]: current cookies as hash type
referral_link) #Optional[str]: optional current referer
- [Recommended] Save
cookies_to_be_updatedas 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: Save the cookies_to_be_updated cookies from
process_request_from_context to your response.
# Get the recommended saved cookie from step 4 above
cookies_to_be_updated = builder.process_request_from_context(request)
for cookie in cookies_to_be_updated do response.set_cookie(
cookie.name,
value: cookie.value,
domain: cookie.domain, path: "/",
# for sinatra the expires is an absolute ts
# Check your web framework to have the correct expires.
expires: Time.now + cookie.max_age)
end
Option 2: Save the recommended cookies from get_cookies_to_set to your
response.
# Get the recommended saved cookie from step 4 above
builder.process_request_from_context(request)
# `cookies_to_be_updated` from get_cookies_to_set()
for cookie in builder.get_cookies_to_set() do
response.set_cookie(
cookie.name,
value: cookie.value,
domain: cookie.domain,
path: "/",
# for sinatra the expire time, is an absolute ts
# Check your web framework to have the correct expires.
expires: Time.now + cookie.max_age)
end
- get correct fbc, fbp, event_source_url, and referrer_url.
fbc = builder.get_fbc()
fbp = builder.get_fbp()
event_source_url = builder.get_event_source_url()
referrer_url = builder.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 ...
}
...
]
Framework support
builder.process_request_from_context(request) accepts a request object
directly and extracts host / cookies / query / referer for you. It supports out
of the box any Rack-based framework:
- Rails (
ActionDispatch::Request), Sinatra, Hanami, Roda, Cuba — anything exposing#envthat returns a Rack-style hash - Raw Rack
envHash
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.
URL support
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):
builder.process_request_from_context(request)
event_source_url = builder.get_event_source_url()
referrer_url = builder.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
nilwhen the required information is not available.
Send them with your Conversions API payload:
data = {
event_name: '...',
event_time: Time.now.to_i,
event_source_url: builder.get_event_source_url(),
referrer_url: builder.get_referrer_url(),
user_data: {
fbc: builder.get_fbc(),
fbp: builder.get_fbp(),
},
}
License
Conversions API parameter builder for Ruby is licensed under the LICENSE file in the root directory of this source tree.