Skip to content

Commit 21cf543

Browse files
Jiaming Youmeta-codesync[bot]
authored andcommitted
Auto-populate event_source_url from CAPI ParamBuilder
Summary: # Problem `com.facebook.capi.sdk:capi-param-builder` 1.3.0 (D106968654) exposes `ParamBuilder.getEventSourceUrl()`, and the Java SDK's `Preference` now has the `is_event_source_url_allowed` gate (D106971535). What is still missing is the wiring in `Event` itself: callers who do `event.setRequestContext(request)` get fbc / fbp handled by `applyParamBuilderDefaults()` but `event_source_url` is never auto-populated. This mirrors the Node.js wiring in D106966732, the PHP wiring in D106907292, the Ruby wiring in D106704065, and the Python wiring in D106698649. # Solution Extend `Event.applyParamBuilderDefaults()` so that after the existing UserData fbc/fbp handling it also reads `paramBuilder.getEventSourceUrl()` and assigns it to the event's `eventSourceUrl` when: - `preference.isEventSourceUrlAllowed()` is `true` (default), AND - `eventSourceUrl` is currently null or empty (caller-supplied values always win, matching the precedence rule for fbc/fbp), AND - the builder returned a non-empty URL. Update the method docstring — it now fills empty `UserData` fields AND empty `Event` fields. Consistent with the existing fbc/fbp handling, this is wired and unit-tested via a mocked `ParamBuilder`, but is a runtime no-op until the deferred `paramBuilder.processRequestFromContext(context)` call in `setRequestContext` is re-enabled (see the NOTE there). That deferral was waiting on the Maven Central release of `capi-param-builder` to publish the request-context methods, which the 1.3.0 bump (D106968654) now satisfies; re-enabling the call (and the matching real-builder guardrail assertions) is a separate follow-up, so this diff keeps the event_source_url wiring in lock-step with how fbc/fbp are handled today rather than changing that behavior here. Differential Revision: D106972051 fbshipit-source-id: ee214cff0fcc2ee11a36e363b5d6431b7b4e40bb
1 parent cbbe155 commit 21cf543

2 files changed

Lines changed: 69 additions & 3 deletions

File tree

src/main/java/com/facebook/ads/sdk/serverside/Event.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -638,9 +638,9 @@ public Event setRequestContext(Object context, Preference preference) {
638638
}
639639

640640
/**
641-
* Fills empty UserData fields from the ParamBuilder-extracted values, gated by Preference.
642-
* No-op when {@link #setRequestContext} was never called. Idempotent: only fills fields that
643-
* are currently empty, so the caller's explicit UserData values always take precedence
641+
* Fills empty UserData and Event fields from the ParamBuilder-extracted values, gated by
642+
* Preference. No-op when {@link #setRequestContext} was never called. Idempotent: only fills
643+
* fields that are currently empty, so the caller's explicit values always take precedence
644644
* regardless of call order.
645645
*
646646
* <p>Invoked by {@link EventRequest} just before serializing the wire payload.
@@ -667,6 +667,13 @@ public Event setRequestContext(Object context, Preference preference) {
667667
}
668668

669669
this.userData = ud;
670+
671+
String builderEventSourceUrl = this.paramBuilder.getEventSourceUrl();
672+
if (Boolean.TRUE.equals(this.preference.isEventSourceUrlAllowed())
673+
&& (this.eventSourceUrl == null || this.eventSourceUrl.isEmpty())
674+
&& builderEventSourceUrl != null && !builderEventSourceUrl.isEmpty()) {
675+
this.eventSourceUrl = builderEventSourceUrl;
676+
}
670677
}
671678

672679
/**

src/test/java/com/facebook/ads/sdk/serverside/EventRequestContextTest.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public void testDefaultsToAllowAllPreferenceWhenNull() {
8484
assertTrue(pref.isFbpAllowed());
8585
assertTrue(pref.isClientIpAddressAllowed());
8686
assertTrue(pref.isReferrerUrlAllowed());
87+
assertTrue(pref.isEventSourceUrlAllowed());
8788
}
8889
}
8990

@@ -203,6 +204,7 @@ public void testPreferenceAllFalseSuppressesEveryAutoField() {
203204
mockConstruction(ParamBuilder.class, (pb, ctx) -> {
204205
when(pb.getFbc()).thenReturn("XX");
205206
when(pb.getFbp()).thenReturn("YY");
207+
when(pb.getEventSourceUrl()).thenReturn("https://shop.example.com/cart");
206208
})) {
207209
Preference pref = new Preference(false, false, false, false, false);
208210
Event event = new Event()
@@ -217,6 +219,63 @@ public void testPreferenceAllFalseSuppressesEveryAutoField() {
217219
assertNull(ud.getFbc());
218220
assertNull(ud.getFbp());
219221
}
222+
assertNull(event.getEventSourceUrl());
223+
}
224+
}
225+
226+
// ----------------------------------------------------------------
227+
// event_source_url auto-population (mocked ParamBuilder)
228+
// ----------------------------------------------------------------
229+
230+
@Test
231+
public void testAutoPopulatesEventSourceUrl() {
232+
try (MockedConstruction<ParamBuilder> mocked =
233+
mockConstruction(ParamBuilder.class, (pb, ctx) -> {
234+
when(pb.getEventSourceUrl()).thenReturn("https://shop.example.com/cart");
235+
})) {
236+
Event event = new Event()
237+
.eventName("PageView")
238+
.eventTime(1700000060L)
239+
.setRequestContext(new Object());
240+
241+
event.applyParamBuilderDefaults();
242+
assertEquals("https://shop.example.com/cart", event.getEventSourceUrl());
243+
}
244+
}
245+
246+
@Test
247+
public void testCallerSuppliedEventSourceUrlTakesPrecedence() {
248+
try (MockedConstruction<ParamBuilder> mocked =
249+
mockConstruction(ParamBuilder.class, (pb, ctx) -> {
250+
when(pb.getEventSourceUrl()).thenReturn("https://from-builder/");
251+
})) {
252+
Event event = new Event()
253+
.eventName("Lead")
254+
.eventTime(1700000061L)
255+
.eventSourceUrl("https://from-caller/")
256+
.setRequestContext(new Object());
257+
258+
event.applyParamBuilderDefaults();
259+
assertEquals("https://from-caller/", event.getEventSourceUrl());
260+
}
261+
}
262+
263+
@Test
264+
public void testPreferenceGatesEventSourceUrl() {
265+
try (MockedConstruction<ParamBuilder> mocked =
266+
mockConstruction(ParamBuilder.class, (pb, ctx) -> {
267+
when(pb.getFbc()).thenReturn("WITHFBC");
268+
when(pb.getEventSourceUrl()).thenReturn("https://from-builder/");
269+
})) {
270+
Preference pref = new Preference(true, true, true, true, false);
271+
Event event = new Event()
272+
.eventName("PageView")
273+
.eventTime(1700000062L)
274+
.setRequestContext(new Object(), pref);
275+
276+
event.applyParamBuilderDefaults();
277+
assertEquals("WITHFBC", event.getUserData().getFbc());
278+
assertNull(event.getEventSourceUrl());
220279
}
221280
}
222281

0 commit comments

Comments
 (0)