Meta has provided a list of library SDKs in both client-side (JavaScript) and server-side (PHP, Java, Python, NodeJS, Ruby). These SDK libraries are intended to help developers improve the quality of Conversions API event parameters (for example, fbc and fbp), and enable advertisers to adhere to Meta’s best practices around generating these parameters.
Below are step-by-step guides for integrating with your application directly. If you are interested in running a demo, please refer to the quick start guides.
Currently, the parameter builder feature manages the storing and retrieval of:
It also provides the normalization and hashing functionality of the following customer information parameters:
This document describes the parameter builder library from the server side.
For information about the client-side library, please refer to the Client-Side Parameter Builder Library Onboarding Guide. Our recommendation is to implement both the client- and server- side solutions.
Check our Github for the example demo and code resources: github.com/facebook/capi-param-builder
// Check the latest version. Add the dependency in your composer.json.
{
"require": {
"php": ">=7.4",
"facebook/capi-param-builder-php": "{current_version}"
},
"minimum-stability": "dev"
}Note: We are updating the API to be more flexible to fit your case. Please check each language’s README for the most specific option. Below is the general guidance for all languages.
The parameter builder library provides functionality to process incoming HTTP requests and extract param values such as fbc/fbp and client IP address. It also provides customer information parameters (PII) normalization and hashing functionality. If you’d like to review existing examples, see the quick start guides for more information.
Here are the steps:
_fbc and _fbp cookies returned by the libraryExample of server-side integration pseudocode:
// Pseudocode on client's server:
// Call library. Param is list of etld+1 for your websites.
$param_builder = new ParamBuilder(arrays['example.com', 'test.com']);
// Get fbc, fbp
$cookies = $param_builder->processRequest($host, $url, $cookies, $referer, $x_forwarded_for, $remote_address);
// Save fbc and fbp provided by $cookies
set_cookie($cookies);
// Get fbc
$fbc = $param_builder->getFbc();
// Get fbp
$fbp = $param_build->getFbp();
// Get client ip address
$client_ip_address = $param_builder->getClientIpAddress();
// Get normalized and hashed email
$email = $param_builder->getNormalizedAndHashedPII(‘John_Smith@gmail.com’,’email’);
// Get normalized and hashed phone
$phone = $param_builder->getNormalizedAndHashedPII(‘+1 (616) 954-78 88’,’phone’);
// Call Conversion API and pass $fbc, $fbp, $client_ip_address, $email and $phone
We currently support five major APIs:
Different languages may have different name conventions. Below are examples for Java.
This is the core API to process fbc, fbp and client_ip_address. The first 3 parameters for processRequest API are mandatory and the rest are optional, but we encourage you to set all parameters for optimized performance.
/**
* @param host: host or domain this request is coming from
* @param query_params: url query params in Map format. Eg. Example.com?test=123&name=hello, then the pass-in query params will be {“test”:”123”, “name”:”hello”}
* @param cookies: current cookies in Map format. Eg.{“my_cookie1”:”test”, “_fbc”:”xxxxx”} etc.
* @param referer: http referrer request header. Eg.https://example.com/page?q=123 etc.
* @param x_forwarded_for: http x_forwarded_for request header. Eg.203.0.113.195,2001:db8:85a3:8d3:1319:8a2e:370:7348 etc.
* @param remote_address: the remote_address variable in http request. Eg.2001:db8:85a3:8d3:1319:8a2e:370:7348 returned from request.getRemoteAddr() etc.
* @return List of CookieSetting. You could set your user cookies based on the returned value.
*/
List<CookieSetting> processRequest(String host, Map<String, String> query_params, Map<String, String> cookies, String referer, String x_forwarded_for, String remote_address)
CookieSetting is a model Meta provides.
@Getter
public class CookieSetting {
public String name;
public String value;
public String domain;
public int maxAge;
}
Application usage:
import com.facebook.capi.paramsdk.ParamBuilder;
import com.facebook.capi.paramsdk.model.CookieSetting;
...
// Calling the API
// The list would be a list of preferred domains. We'll match it with your input domain and return in the CookieSetting's domain
ParamBuilder paramBuilder = new ParamBuilder(Arrays.asList("example.com", ..));
..
// Currently only contains _fbc and _fbp
List<CookieSetting> updatedCookieList = paramBuilder.processRequest(domainName, queryParamsMap, cookieMap, referer, x_forwarded_for, remote_address);
// Set cookie based on your language and frameworks
...
This API returns the normalized and hashed (sha256) customer information parameters (PII). If input is invalid, this API will return null. This method takes two parameters. The first parameter is the PII value to normalize and hash. The second parameter is the type of PII you want to normalize as. Available options are: 'phone', 'email', 'first_name', 'last_name', 'date_of_birth', 'gender', 'city', 'state', 'zip_code', 'country' and 'external_id'.
// @params piiValue: string. The PII value we want to normalize and hash.
// @params dataType: string. The type of PII you want to normalize as. Available options are: 'phone', 'email','first_name','last_name','date_of_birth','gender','city','state', 'zip_code', 'country' and 'external_id'
// @return NormalizedAndHashedPII: string or null, the normalized and hashed PII. We will return null with invalid input.
getNormalizedAndHashedPII(piiValue,dataType)
Usage example:
getNormalizedAndHashedPII(‘John_Smith@gmail.com’,’email’);
getNormalizedAndHashedPII(‘+1 (616) 954-78 88’,’phone’);
You will need to call processRequest above first in order to get the accurate fbc. If fbc is not available, it will return null.
String getFbc();
Application usage:
String fbc = paramBuilder.getFbc();
You will need to call processRequest above first in order to get the accurate fbp. If fbp is not available, it will return null.
String getFbp();
Application usage:
String fbp = paramBuilder.getFbp();
Return client_ip_address value from cookie. You will need to call processRequest above first in order to get the accurate client_ip_address. If client_ip_address is not available, it will return null.
String getClientIpAddress();
Application usage:
String client_ip_address = paramBuilder.getClientIpAddress();
This section includes some examples to read and store cookies. The server side library doesn’t save cookies; it’s recommended that advertisers save the suggested cookies list. As for user’s cookie and consent, please refer to Meta’s Business Tools Terms for details.
This section includes examples to read and store cookies.
Businesses can implement code that creates a banner and requires affirmative consent (for example, an “I agree” checkbox at the top of the page) to allow cookie saving actions for fbc and fbp. If you already have a system in place that addresses this need, such as a tag manager, you can make this code optional.
Below are examples of how to read and set the cookies. If you are unsure about any portion, see our quick start guides for more detailed examples.
$param_builder = new FacebookAds\ParamBuilder(arrays['example.com', 'test.com']);
$cookies_to_set = $param_builder->processRequest(
$_SERVER['HTTP_HOST'],
$_GET,
$_COOKIE,
$_SERVER['HTTP_REFERER'] ?? null, // Optional field. If you input, could help improve the quality
$_SERVER['HTTP_X_FORWARDED_FOR'] ?? null,
$_SERVER['REMOTE_ADDR'] ?? null
);
foreach ($cookies_to_set as $cookie) {
setcookie(
$cookie->name,
$cookie->value,
time() + $cookie->max_age,
'/',
$cookie->domain
);
}After the cookie is updated, you will need to send fbc/fbp, client_ip_address and PII (email, phone, etc.) back to the Conversions API using the common API. The following are examples of how to get fbc/fbp, client_ip_address and PII (email, phone, etc.) in each language.
// Initialize the builder.
$param_builder = new FacebookAds\ParamBuilder(array('example.com', 'test.com'));
// Process the request
$cookies_to_set = $param_builder->processRequest(
$_SERVER['HTTP_HOST'],
$_GET,
$_COOKIE,
$_SERVER['HTTP_REFERER'] ?? null,
$_SERVER['HTTP_X_FORWARDED_FOR'] ?? null,
$_SERVER['REMOTE_ADDR'] ?? null
);
// Get fbc
$fbc = builder->getFbc();
// Get fbp
$fbp = builder->getFbc();
// Get client_ip_address
$client_ip_address = builder->getClientIpAddress();
// Get normalized and hashed email (sha256)
$email = builder->getNormalizedAndHashedPII(' John_Smith@gmail.com ','email');
// Get normalized and hashed phone (sha256)
$phone = builder->getNormalizedAndHashedPII('1(650)123-4567','phone');
// Pass the fbc, fbp, client_ip_address, email and phone number to the Conversions APIYou may consider this option as you are constructing the parameter builder library. We recommend using the domain list option mentioned in the Using the APIs section above.
To achieve the best result, fbc and fbp cookies should be written to the top level domain as much as possible. Internally, the parameter builder libraries use a public suffix list to determine which domain to set cookies to. If you know the eTLD+1 of your web domain, you could implement a more optimized resolver by implementing the ETLDPlus1Resolver interface.
Examples from each language are shown below. You may also review the demo examples in the quick start guides for existing solutions.
class SimpleETLDPlus1Resolver implements ETLDPlus1Resolver {
public function resolveETLDPlus1($domain) {
if (isSubdomain($domain, "example.com")) {
return "example.com";
}
throw new InvalidArgumentException("only example.com is supported");
}
}