Skip to content

Commit f6e6f3a

Browse files
hongj-srcmeta-codesync[bot]
authored andcommitted
Ruby: provide breakdown to differentiate net new and modified new appendix
Summary: This change adds appendix type constants to differentiate between: - APPENDIX_NET_NEW (0x02): Brand new cookies (0→1) - APPENDIX_MODIFIED_NEW (0x03): Updated existing cookies with new payload - APPENDIX_NO_CHANGE (0x00): Existing values without modification TODO: cleanup unused variables Differential Revision: D91720354 fbshipit-source-id: 5849cdc84fc070b62a48d55d91cb6dbaa3020b3a
1 parent 712b8df commit f6e6f3a

2 files changed

Lines changed: 93 additions & 48 deletions

File tree

ruby/capi_param_builder/lib/capi_param_builder.rb

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,17 @@ class ParamBuilder
2929
APPENDIX_LENGTH_V1 = 2
3030
APPENDIX_LENGTH_V2 = 8
3131

32+
# Appendix type constants
33+
APPENDIX_NO_CHANGE = 0x00
34+
APPENDIX_GENERAL_NEW = 0x01
35+
APPENDIX_NET_NEW = 0x02
36+
APPENDIX_MODIFIED_NEW = 0x03
37+
3238
def initialize(input = nil)
3339
@fbc_params_configs = [FbcParamConfigs.new("fbclid", "", "clickID")]
34-
@appendix_new = get_appendix(true)
35-
@appendix_normal = get_appendix(false)
40+
@appendix_net_new = get_appendix(APPENDIX_NET_NEW)
41+
@appendix_modified_new = get_appendix(APPENDIX_MODIFIED_NEW)
42+
@appendix_no_change = get_appendix(APPENDIX_NO_CHANGE)
3643

3744
if input.nil?
3845
return
@@ -48,21 +55,27 @@ def initialize(input = nil)
4855
end
4956
end
5057

51-
private def get_appendix(is_new)
58+
private def get_appendix(appendix_type)
5259
begin
5360
version = ReleaseConfig::VERSION
5461
version_parts = version.split(".")
5562
major = version_parts[0].to_i
5663
minor = version_parts[1].to_i
5764
patch = version_parts[2].to_i
5865

59-
is_new_byte = is_new ? 0x01 : 0x00
66+
# Validate appendix type
67+
valid_types = [APPENDIX_NET_NEW, APPENDIX_GENERAL_NEW, APPENDIX_MODIFIED_NEW]
68+
if valid_types.include?(appendix_type)
69+
type_byte = appendix_type
70+
else
71+
type_byte = APPENDIX_NO_CHANGE
72+
end
6073

6174
# Create byte array
6275
bytes_array = [
6376
DEFAULT_FORMAT, # 0x01 = 1
6477
LANGUAGE_TOKEN_INDEX, # 0x05 = 5
65-
is_new_byte, # 0x01 when is_new=true, 0x00 when is_new=false
78+
type_byte, # appendix type
6679
major, # Major version number
6780
minor, # Minor version number
6881
patch # Patch version number
@@ -104,7 +117,7 @@ def initialize(input = nil)
104117
end
105118
# Append language token if not present
106119
if parts.size == MIN_PAYLOAD_SPLIT_LENGTH
107-
updated_cookie_value = cookie_value + "." + @appendix_normal
120+
updated_cookie_value = cookie_value + "." + @appendix_no_change
108121
@cookie_to_set_dict[cookie_name] = CookieSettings.new(
109122
cookie_name, updated_cookie_value, @etld_plus_one, DEFAULT_1PC_AGE)
110123
return updated_cookie_value
@@ -282,25 +295,34 @@ def get_fbp()
282295
return nil
283296
end
284297

298+
cookie_update = false
299+
is_net_new = false
285300
if existing_fbc.nil?
286301
cookie_update = true
302+
is_net_new = true
287303
else
288304
parts = existing_fbc.split(/\./)
289-
cookie_update = new_fbc_payload != parts[3]
305+
if parts.size < MIN_PAYLOAD_SPLIT_LENGTH
306+
cookie_update = true
307+
is_net_new = true
308+
else
309+
cookie_update = new_fbc_payload != parts[3]
310+
end
290311
end
291312
if cookie_update == false
292313
return nil
293314
end
294315

295316
current_ms = (Time.now.to_f * 1000).to_i.to_s
317+
appendix = is_net_new ? @appendix_net_new : @appendix_modified_new
296318
new_fbc = "fb." +
297319
@sub_domain_index.to_s +
298320
"." +
299321
current_ms +
300322
"." +
301323
new_fbc_payload +
302324
"." +
303-
@appendix_new
325+
appendix
304326
updated_cookie_setting = CookieSettings.new(
305327
FBC_NAME, new_fbc, @etld_plus_one, DEFAULT_1PC_AGE)
306328
return updated_cookie_setting
@@ -319,7 +341,7 @@ def get_fbp()
319341
"." +
320342
new_fbp_payload +
321343
"." +
322-
@appendix_new
344+
@appendix_net_new
323345
updated_cookie_setting = CookieSettings.new(
324346
FBP_NAME, new_fbp, @etld_plus_one, DEFAULT_1PC_AGE)
325347
return updated_cookie_setting

ruby/capi_param_builder/test/test_param_builder.rb

Lines changed: 62 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
require 'test_etld_plus_one_resolver'
1111
require_relative '../lib/model/fbc_param_configs'
1212

13-
APPENDIX_IS_NEW = "AQUBAQAB"
14-
APPENDIX_IS_NORMAL = "AQUAAQAB"
13+
APPENDIX_NET_NEW = "AQUCAQAB"
14+
APPENDIX_MODIFIED_NEW = "AQUDAQAB"
15+
APPENDIX_NO_CHANGE = "AQUAAQAB"
1516

1617
# Save the original value before any tests run
1718
ORIGINAL_VERSION = ReleaseConfig::VERSION
@@ -31,10 +32,12 @@ def test_override_different_versions
3132
ReleaseConfig.const_set(:VERSION, "1.15.24")
3233
assert_equal "1.15.24", ReleaseConfig::VERSION
3334
builder = ParamBuilder.new()
34-
appendix_new = builder.send(:get_appendix, true)
35-
assert_equal(appendix_new, "AQUBAQ8Y")
36-
appendix_normal = builder.send(:get_appendix, false)
37-
assert_equal(appendix_normal, "AQUAAQ8Y")
35+
appendix_net_new = builder.send(:get_appendix, ParamBuilder::APPENDIX_NET_NEW)
36+
assert_equal(appendix_net_new, "AQUCAQ8Y")
37+
appendix_modified_new = builder.send(:get_appendix, ParamBuilder::APPENDIX_MODIFIED_NEW)
38+
assert_equal(appendix_modified_new, "AQUDAQ8Y")
39+
appendix_no_change = builder.send(:get_appendix, ParamBuilder::APPENDIX_NO_CHANGE)
40+
assert_equal(appendix_no_change, "AQUAAQ8Y")
3841
ensure
3942
ReleaseConfig.send(:remove_const, :VERSION)
4043
ReleaseConfig.const_set(:VERSION, tmp_original_version)
@@ -45,10 +48,10 @@ def test_override_different_versions_exception
4548
ReleaseConfig.send(:remove_const, :VERSION)
4649
ReleaseConfig.const_set(:VERSION, nil)
4750
builder = ParamBuilder.new()
48-
appendix_new = builder.send(:get_appendix, true)
49-
assert_equal(appendix_new, "BQ")
50-
appendix_normal = builder.send(:get_appendix, false)
51-
assert_equal(appendix_normal, "BQ")
51+
appendix_net_new = builder.send(:get_appendix, ParamBuilder::APPENDIX_NET_NEW)
52+
assert_equal(appendix_net_new, "BQ")
53+
appendix_no_change = builder.send(:get_appendix, ParamBuilder::APPENDIX_NO_CHANGE)
54+
assert_equal(appendix_no_change, "BQ")
5255
ensure
5356
ReleaseConfig.send(:remove_const, :VERSION)
5457
ReleaseConfig.const_set(:VERSION, tmp_original_version)
@@ -62,16 +65,16 @@ def test_process_request_with_fbc_fbp_updated
6265
{"test"=>"value", "_fbc"=>"fb.1.2.test_fbc"},
6366
"example.com?fbclid=test345")
6467
assert_equal(2, cookie_to_update.size())
65-
assert_contains(".test123.#{APPENDIX_IS_NEW}", builder.get_fbc())
68+
assert_contains(".test123.#{APPENDIX_MODIFIED_NEW}", builder.get_fbc())
6669
assert_contains("fb.0.", builder.get_fbp())
6770
for cookie in cookie_to_update do
6871
if cookie.name == '_fbc'
69-
assert cookie.value.end_with?(".test123.#{APPENDIX_IS_NEW}")
72+
assert cookie.value.end_with?(".test123.#{APPENDIX_MODIFIED_NEW}")
7073
assert_contains('fb.0.', cookie.value)
7174
assert_contains("localhost", cookie.domain)
7275
else
7376
assert_equal(ParamBuilder::FBP_NAME, cookie.name)
74-
assert cookie.value.end_with?(APPENDIX_IS_NEW)
77+
assert cookie.value.end_with?(APPENDIX_NET_NEW)
7578
assert_equal("localhost", cookie.domain)
7679
end
7780
end
@@ -86,18 +89,18 @@ def test_process_request_with_fbc_fbp_update_no_cookie
8689
{},
8790
"example.com")
8891
assert_equal(2, cookie_to_update.size())
89-
assert builder.get_fbc().end_with?(".test123.#{APPENDIX_IS_NEW}")
92+
assert builder.get_fbc().end_with?(".test123.#{APPENDIX_NET_NEW}")
9093
assert_contains("fb.1.", builder.get_fbp())
91-
assert builder.get_fbp().end_with?(".#{APPENDIX_IS_NEW}")
94+
assert builder.get_fbp().end_with?(".#{APPENDIX_NET_NEW}")
9295
for cookie in cookie_to_update do
9396
if cookie.name == '_fbc'
94-
assert cookie.value.end_with?(".test123.#{APPENDIX_IS_NEW}")
97+
assert cookie.value.end_with?(".test123.#{APPENDIX_NET_NEW}")
9598
assert_contains('fb.1.', cookie.value)
9699
assert_equal("example.com", cookie.domain)
97100
else
98101
assert_equal(ParamBuilder::FBP_NAME, cookie.name)
99102
assert_contains("fb.1.", cookie.value)
100-
assert cookie.value.end_with?(".#{APPENDIX_IS_NEW}")
103+
assert cookie.value.end_with?(".#{APPENDIX_NET_NEW}")
101104
assert_equal("example.com", cookie.domain)
102105
end
103106
end
@@ -114,12 +117,12 @@ def test_process_request_with_with_resolver
114117
assert_equal(2, cookie_to_update.size())
115118
for cookie in cookie_to_update do
116119
if cookie.name == ParamBuilder::FBC_NAME
117-
assert_contains(".test.#{APPENDIX_IS_NEW}", cookie.value)
120+
assert_contains(".test.#{APPENDIX_MODIFIED_NEW}", cookie.value)
118121
assert_contains("fb.4.", cookie.value)
119122
assert_equal("this.is.a.test.com", cookie.domain)
120123
else
121124
assert_equal(ParamBuilder::FBP_NAME, cookie.name)
122-
assert_equal("fb.1.123.value.#{APPENDIX_IS_NORMAL}", cookie.value)
125+
assert_equal("fb.1.123.value.#{APPENDIX_NO_CHANGE}", cookie.value)
123126
assert_equal("this.is.a.test.com", cookie.domain)
124127
end
125128
end
@@ -135,7 +138,7 @@ def test_process_request_with_with_empty_constructor
135138
"https://example.com?fbclid=wer")
136139
assert_equal(1, cookie_to_update.size())
137140
for cookie in cookie_to_update do
138-
assert_contains(".test.#{APPENDIX_IS_NEW}", cookie.value)
141+
assert_contains(".test.#{APPENDIX_NET_NEW}", cookie.value)
139142
assert_contains("fb.4.", cookie.value)
140143
assert_equal("is.a.test.co.uk", cookie.domain)
141144
end
@@ -153,7 +156,7 @@ def test_process_request_with_with_invali_string_domain_list
153156
"https://example.com?fbclid=wer")
154157
assert_equal(1, cookie_to_update.size())
155158
for cookie in cookie_to_update do
156-
assert_contains(".test.#{APPENDIX_IS_NEW}", cookie.value)
159+
assert_contains(".test.#{APPENDIX_NET_NEW}", cookie.value)
157160
assert_contains("fb.4.", cookie.value)
158161
assert_equal("is.a.test.example.com", cookie.domain)
159162
end
@@ -170,13 +173,13 @@ def test_process_request_with_with_referer
170173
assert_equal(2, cookie_to_update.size())
171174
for cookie in cookie_to_update do
172175
if cookie.name == ParamBuilder::FBC_NAME
173-
assert_contains(".wer.#{APPENDIX_IS_NEW}", cookie.value)
176+
assert_contains(".wer.#{APPENDIX_NET_NEW}", cookie.value)
174177
assert_contains("fb.4.", cookie.value)
175178
assert_equal("is.a.test.co.uk", cookie.domain)
176179
else
177180
assert_equal(ParamBuilder::FBP_NAME, cookie.name)
178181
assert_contains("fb.4.", cookie.value)
179-
assert_contains(".#{APPENDIX_IS_NEW}", cookie.value)
182+
assert_contains(".#{APPENDIX_NET_NEW}", cookie.value)
180183
assert_equal("is.a.test.co.uk", cookie.domain)
181184
end
182185
end
@@ -193,13 +196,13 @@ def test_process_request_with_with_duplicate_referer_params
193196
assert_equal(2, cookie_to_update.size())
194197
for cookie in cookie_to_update do
195198
if cookie.name == ParamBuilder::FBC_NAME
196-
assert_contains(".wer.#{APPENDIX_IS_NEW}", cookie.value)
199+
assert_contains(".wer.#{APPENDIX_NET_NEW}", cookie.value)
197200
assert_contains("fb.4.", cookie.value)
198201
assert_equal("is.a.test.co.uk", cookie.domain)
199202
else
200203
assert_equal(ParamBuilder::FBP_NAME, cookie.name)
201204
assert_contains("fb.4.", cookie.value)
202-
assert_contains(".#{APPENDIX_IS_NEW}", cookie.value)
205+
assert_contains(".#{APPENDIX_NET_NEW}", cookie.value)
203206
end
204207
end
205208
assert_equal(cookie_to_update, builder.get_cookies_to_set())
@@ -220,13 +223,13 @@ def test_process_request_with_param_config_update
220223
assert_equal(2, cookie_to_update.size())
221224
for cookie in cookie_to_update do
222225
if cookie.name == ParamBuilder::FBC_NAME
223-
assert_contains(".test123_test1_test2.#{APPENDIX_IS_NEW}", cookie.value)
226+
assert_contains(".test123_test1_test2.#{APPENDIX_NET_NEW}", cookie.value)
224227
assert_contains("fb.1.", cookie.value)
225228
assert_equal("example.com", cookie.domain)
226229
else
227230
assert_equal(ParamBuilder::FBP_NAME, cookie.name)
228231
assert_contains("fb.1.", cookie.value)
229-
assert_contains(".#{APPENDIX_IS_NEW}", cookie.value)
232+
assert_contains(".#{APPENDIX_NET_NEW}", cookie.value)
230233
end
231234
end
232235
assert_equal(cookie_to_update, builder.get_cookies_to_set())
@@ -246,13 +249,13 @@ def test_process_request_with_param_config_with_customized_config_only
246249
assert_equal(2, cookie_to_update.size())
247250
for cookie in cookie_to_update do
248251
if cookie.name == ParamBuilder::FBC_NAME
249-
assert_contains(".test1_test2.#{APPENDIX_IS_NEW}", cookie.value)
252+
assert_contains(".test1_test2.#{APPENDIX_NET_NEW}", cookie.value)
250253
assert_contains("fb.1.", cookie.value)
251254
assert_equal("example.com", cookie.domain)
252255
else
253256
assert_equal(ParamBuilder::FBP_NAME, cookie.name)
254257
assert_contains("fb.1.", cookie.value)
255-
assert_contains(".#{APPENDIX_IS_NEW}", cookie.value)
258+
assert_contains(".#{APPENDIX_NET_NEW}", cookie.value)
256259
end
257260
end
258261
assert_equal(cookie_to_update, builder.get_cookies_to_set())
@@ -274,15 +277,15 @@ def test_process_request_with_param_config_with_duplication
274277
for cookie in cookie_to_update do
275278
if cookie.name == ParamBuilder::FBC_NAME
276279
assert_contains(
277-
".test123_test1_test456.#{APPENDIX_IS_NEW}",
280+
".test123_test1_test456.#{APPENDIX_NET_NEW}",
278281
cookie.value
279282
)
280283
assert_contains("fb.1.", cookie.value)
281284
assert_equal("example.com", cookie.domain)
282285
else
283286
assert_equal(ParamBuilder::FBP_NAME, cookie.name)
284287
assert_contains("fb.1.", cookie.value)
285-
assert_contains(".#{APPENDIX_IS_NEW}", cookie.value)
288+
assert_contains(".#{APPENDIX_NET_NEW}", cookie.value)
286289
end
287290
end
288291
assert_equal(cookie_to_update, builder.get_cookies_to_set())
@@ -305,13 +308,13 @@ def test_process_request_with_param_config_update_referer
305308
assert_equal(2, cookie_to_update.size())
306309
for cookie in cookie_to_update do
307310
if cookie.name == ParamBuilder::FBC_NAME
308-
assert_contains(".wer_test1_placeholder.#{APPENDIX_IS_NEW}", cookie.value)
311+
assert_contains(".wer_test1_placeholder.#{APPENDIX_NET_NEW}", cookie.value)
309312
assert_contains("fb.1.", cookie.value)
310313
assert_equal("example.com", cookie.domain)
311314
else
312315
assert_equal(ParamBuilder::FBP_NAME, cookie.name)
313316
assert_contains("fb.1.", cookie.value)
314-
assert_contains(".#{APPENDIX_IS_NEW}", cookie.value)
317+
assert_contains(".#{APPENDIX_NET_NEW}", cookie.value)
315318
end
316319
end
317320
assert_equal(cookie_to_update, builder.get_cookies_to_set())
@@ -327,7 +330,7 @@ def test_process_request_with_with_mutiple_domain_list
327330
for cookie in cookie_to_update do
328331
if cookie.name == ParamBuilder::FBC_NAME
329332
assert_equal(
330-
"fb.1.2.test_fbc.#{APPENDIX_IS_NORMAL}",
333+
"fb.1.2.test_fbc.#{APPENDIX_NO_CHANGE}",
331334
cookie.value
332335
)
333336
assert_equal(
@@ -336,7 +339,7 @@ def test_process_request_with_with_mutiple_domain_list
336339
)
337340
else
338341
assert_equal(ParamBuilder::FBP_NAME, cookie.name)
339-
assert_contains(".#{APPENDIX_IS_NEW}", cookie.value)
342+
assert_contains(".#{APPENDIX_NET_NEW}", cookie.value)
340343
assert_equal("example.co.uk", cookie.domain)
341344
end
342345
end
@@ -380,21 +383,41 @@ def test_process_request_with_valie_cookies_with_language_update
380383
{"_fbp"=>"fb.1.123.345", "_fbc"=>"fb.1.2.test_fbc"},
381384
nil)
382385
assert_equal(2, cookie_to_update.size())
383-
assert_equal("fb.1.2.test_fbc.#{APPENDIX_IS_NORMAL}", builder.get_fbc())
384-
assert_equal("fb.1.123.345.#{APPENDIX_IS_NORMAL}", builder.get_fbp())
386+
assert_equal("fb.1.2.test_fbc.#{APPENDIX_NO_CHANGE}", builder.get_fbc())
387+
assert_equal("fb.1.123.345.#{APPENDIX_NO_CHANGE}", builder.get_fbp())
385388
for cookie in cookie_to_update do
386389
if cookie.name == ParamBuilder::FBC_NAME
387-
assert_equal("fb.1.2.test_fbc.#{APPENDIX_IS_NORMAL}", cookie.value)
390+
assert_equal("fb.1.2.test_fbc.#{APPENDIX_NO_CHANGE}", cookie.value)
388391
assert_equal("localhost", cookie.domain)
389392
else
390393
assert_equal(ParamBuilder::FBP_NAME, cookie.name)
391-
assert_equal("fb.1.123.345.#{APPENDIX_IS_NORMAL}", cookie.value)
394+
assert_equal("fb.1.123.345.#{APPENDIX_NO_CHANGE}", cookie.value)
392395
assert_equal("localhost", cookie.domain)
393396
end
394397
end
395398
assert_equal(cookie_to_update, builder.get_cookies_to_set())
396399
end
397400

401+
def test_process_request_fbc_modified
402+
# Test that modifying an existing fbc with a different payload uses MODIFIED_NEW appendix
403+
builder = ParamBuilder.new()
404+
cookie_to_update = builder.process_request(
405+
"localhost",
406+
{"fbclid"=>"new_click_id"},
407+
{"_fbc"=>"fb.0.123.old_click_id.BQ", "_fbp"=>"fb.0.456.123456789.BQ"})
408+
assert_equal(1, cookie_to_update.size())
409+
fbc = builder.get_fbc()
410+
assert fbc.end_with?(".new_click_id.#{APPENDIX_MODIFIED_NEW}")
411+
assert_contains("fb.0.", fbc)
412+
for cookie in cookie_to_update do
413+
assert_equal(ParamBuilder::FBC_NAME, cookie.name)
414+
assert cookie.value.end_with?(".new_click_id.#{APPENDIX_MODIFIED_NEW}")
415+
assert_equal("localhost", cookie.domain)
416+
end
417+
assert_equal("fb.0.456.123456789.BQ", builder.get_fbp())
418+
assert_equal(cookie_to_update, builder.get_cookies_to_set())
419+
end
420+
398421
def test_extract_host_from_http_host_with_protocol_and_port
399422
builder = ParamBuilder.new()
400423
host_name = builder.send(:extract_host_from_http_host, "https://example.com:3030")

0 commit comments

Comments
 (0)