1- # Conversions API parameter builder for NodeJS
1+ # Conversions API parameter builder feature for NodeJS
22
33[ ![ npm] ( https://img.shields.io/npm/v/capi-param-builder-nodejs )] ( https://www.npmjs.com/package/capi-param-builder-nodejs )
44[ ![ License] ( https://img.shields.io/badge/license-Facebook%20Platform-blue.svg?style=flat-square )] ( https://github.com/facebook/capi-param-builder/blob/main/nodejs/LICENSE )
55
66## Introduction
77
8- Conversions API parameter builder SDK is a lightweight tool for improving
8+ The Conversions API parameter builder SDK is a lightweight tool for improving
99Conversions API parameter retrieval and quality.
1010
1111[ Server-Side Parameter Builder Onboarding Guide] ( https://developers.facebook.com/docs/marketing-api/conversions-api/parameter-builder-feature-library/server-side-onboarding )
1212
1313## Quick Start
1414
15- This is a quick start guide for integrating parameter builder with NodeJS. You
16- can also find a demo in the next section.
15+ This is a quick start guide for integrating the parameter builder feature with
16+ NodeJS. You can also find a demo in the next section.
1717
1818### Setup
1919
@@ -22,14 +22,14 @@ can also find a demo in the next section.
22222 . Update in your package.json with the latest version.
2323
2424```
25- "dependencies": {
26- "capi-param-builder-nodejs": {version}
27- }
25+ "dependencies": {
26+ "capi-param-builder-nodejs": {version}
27+ }
2828```
2929
30- 3 . Run ` npm install ` in your application(if you don't have an application, check
31- #demo section for a demo application) . You should see the capi-param-builder
32- installed under your nodejs folder.
30+ 3 . Run ` npm install ` in your application. If you don't have an application,
31+ check #demo section for a demo application. You should see the
32+ capi-param-builder installed under your nodejs folder.
3333
3434Please check the next section for a local demo.
3535
@@ -57,7 +57,7 @@ Following are some further validation:
5757
5858- The printed getFbc value will change to a string containing the ` test ` .
5959- Verify the browser's cookie section. You should see ` _fbc ` and ` _fbp ` exist.
60- And the ` _fbc ` value contains ` test ` string.
60+ And the ` _fbc ` value contains the ` test ` string.
6161
6262 4.2 Then change the URL back to http://localhost:3000 in the same browser.
6363
@@ -74,6 +74,7 @@ This section covers how to use the SDK and provide suggestions on the API usage.
7474```
7575const {ParamBuilder} = require('capi-param-builder');
7676
77+
7778// [Recommended] Option 1: List of ETLD+1 for your website domains.
7879// This helps provide suggestions for your cookie saving domain.
7980const builder = new ParamBuilder(["example.com", "localhost"]);
@@ -87,88 +88,125 @@ Optional constructors:
8788const etldPlus1Resolver = new DefaultETLDPlus1Resolver();
8889const builder = new ParamBuilder(etldPlus1Resolver);
8990
91+
9092// Option 3: Not recommended. Empty input.
9193// Not recommended. We will return a best guess on your domain by one level down your input host.
9294const builder = new ParamBuilder();
9395```
9496
95- 3 . Call ` processRequest ` to process the fbc, fbp.
97+ 3 . Call ` processRequest ` to process the fbc, fbp and fbi(client_ip_address) .
9698
9799```
98100builder.processRequest(
99- req.headers.host, // host full URL.
100- params, // query params
101- requestCookies, // current cookie
102- req.headers.referer // optional, help enhance the accurancy
103- );
101+ req.headers.host, // host full URL.
102+ params, // query params
103+ requestCookies, // current cookie
104+ req.headers.referer // optional, help enhance the accuracy
105+ req.headers['x-forwarded-for'] ?? null, // optional, help to select the best client_ip_address
106+ req.socket.remoteAddress ?? null // optional, help to select the best client_ip_address
107+ );
108+
104109
105110or
106111
112+
107113const cookiesToSet = builder.processRequest(
108- req.headers.host, // host
109- params, // query params
110- requestCookies, // current cookie
111- req.headers.referer // optional, help enhance the accurancy
112- );
114+ req.headers.host, // host
115+ params, // query params
116+ requestCookies, // current cookie
117+ req.headers.referer, // optional, help enhance the accuracy
118+ req.headers['x-forwarded-for'] ?? null, // optional, help to select the best client_ip_address
119+ req.socket.remoteAddress ?? null // optional, help to select the best client_ip_address
120+ );
113121```
114122
115- 4 . [ Recommended] Save the ` cookiesToSet ` as first-party cookies. This helps
116- keep consistent fbc and fbp for your event. Based on your webserver
117- framework, the save cookie API may vary. Feel free to choose the best fit
118- for your use case.
119-
120- Below uses the example from the demo application.
123+ 4 . [ Recommended] Save the ` cookiesToSet ` as first-party cookies to keep fbc and
124+ fbp consistent across all events. Based on your webserver framework, the
125+ save cookie API may vary. Feel free to choose the best fit for your use
126+ case.Below is one example excerpted from the demo application.
121127
122- Recommended: get the list of ` cookiesToSet ` from API call in step 3.
128+ Recommended: get the list of ` cookiesToSet ` from API call in step 3.
123129
124- ```
125- // Call processRequest in above step 3.
126- // The returned cookiesToSet is the recommended list of cookies to be saved.
127- const cookiesToSet = builder.processRequest(...);
128- for (const cookie of cookiesToSet) {
129- responseCookies.push(cookie.name + '=' + cookie.value + '; Max-Age=' + cookie.maxAge + '; Domain=' + cookie.domain + '; Path=/');
130- }
131- res.setHeader('Set-Cookie', responseCookies);
132- ```
130+ ```
131+ // Call processRequest in above step 3.
132+ // The returned cookiesToSet is the recommended list of cookies to be saved.
133+ const cookiesToSet = builder.processRequest(...);
134+ for (const cookie of cookiesToSet) {
135+ responseCookies.push(cookie.name + '=' + cookie.value + '; Max-Age=' + cookie.maxAge + '; Domain=' + cookie.domain + '; Path=/');
136+ }
137+ res.setHeader('Set-Cookie', responseCookies);
138+ ```
133139
134- Optional: call `builder.getCookiesToSet()` to get the list of cookies to be
135- saved.
140+ Optional: call ` builder.getCookiesToSet() ` to get the list of cookies to be
141+ saved.
136142
137- ```
138- for (const cookie of builder.getCookiesToSet()) {
139- responseCookies.push(cookie.name + '=' + cookie.value + '; Max-Age=' +
140- cookie.maxAge + '; Domain=' + cookie.domain + '; Path=/');
141- }
142- res.setHeader('Set-Cookie', responseCookies);
143- ```
143+ ```
144+ for (const cookie of builder.getCookiesToSet()) {
145+ responseCookies.push(cookie.name + '=' + cookie.value + '; Max-Age=' +
146+ cookie.maxAge + '; Domain=' + cookie.domain + '; Path=/');
147+ }
148+ res.setHeader('Set-Cookie', responseCookies);
149+ ```
144150
145- 5. Get fbc and fbp value
151+ 5 . Get fbc/fbp, client_ip_address, normalized and hashed PII values
146152
147153```
154+
155+
148156const fbc = builder.getFbc();
149157
150- ```
151158
152159```
160+
153161const fbp = builder.getFbp();
162+
163+ ```
164+
165+
166+ const client_ip_address = builder.getClientIpAddress();
167+
168+
169+ ```
170+
171+ const email = builder.getNormalizedAndHashedPII('
172+ John_Smith@gmail.com ','email');
173+
174+ ```
175+
176+
177+ const phone = builder.getNormalizedAndHashedPII('+1 (616) 954-78 88','phone');
178+
179+
180+ ```
181+
182+ getNormalizedAndHashedPII(piiValue, dataType)
183+
184+ ```
185+ API is to get normalized and hashed (sha256) PII from input piiValue, supported dataType include 'phone', 'email','first_name', 'last_name', 'date_of_birth', 'gender', 'city', 'state', 'zip_code', 'country' and 'external_id'.
186+
187+
154188```
155189
156- 6. Send fbc and fbp back with Conversions API under UserData section:
190+ 6 . Send fbc, fbp, client_ip_address, email and phone back through Conversions
191+ API under UserData section:
157192
158193```
159194data=[
160- 'event_name: '...',
161- 'event_tme': <your_time>,
162- 'user_data': {
163- 'fbc': fbc, // The value provided in step 5
164- 'fbp': fbp, // The value provided in step 5
165- ...
166- }
167- ...
195+ 'event_name: '...',
196+ 'event_time': <your_time>,
197+ 'user_data': {
198+ 'fbc': fbc, // The value provided in step 5
199+ 'fbp': fbp, // The value provided in step 5
200+ 'client_ip_address': client_ip_address // The value provided in step 5
201+ 'em': email // The value provided in step 5
202+ 'ph': phone // The value provided in step 5
203+ ...
204+ }
205+ ...
168206]
169207```
170208
171209## License
172210
173- Conversions API parameter builder for NodeJS is licensed under the LICENSE file
174- in the root directory of this source tree.
211+ The Conversions API parameter builder feature for NodeJS is licensed under the
212+ LICENSE file in the root directory of this source tree.
0 commit comments