Skip to content

Commit 943dbfb

Browse files
committed
Support schema defaults for new events.
If an event is not present in the tracking plan, we'll now lookup the schema defaults for the project. If the event is disabled in the schema default, it will only be sent to the Segment integration (i.e. api.segment.io). If the event is enabled in the schema default, it will be sent to all integrations. Ref: https://paper.dropbox.com/doc/Schema-Client-Side-Defaults-DufdS8Ej43mnFXvvMqm1b
1 parent 8b2ee96 commit 943dbfb

File tree

2 files changed

+135
-3
lines changed

2 files changed

+135
-3
lines changed

analytics/src/main/java/com/segment/analytics/IntegrationOperation.java

+22-2
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,32 @@ public void run(String key, Integration<?> integration, ProjectSettings projectS
179179
}
180180

181181
String event = trackPayload.event();
182+
182183
ValueMap eventPlan = trackingPlan.getValueMap(event);
183184
if (isNullOrEmpty(eventPlan)) {
184-
// No event plan, use options provided.
185-
if (isIntegrationEnabled(integrationOptions, key)) {
185+
if (!isNullOrEmpty(integrationOptions)) {
186+
// No event plan, use options provided.
187+
if (isIntegrationEnabled(integrationOptions, key)) {
188+
integration.track(trackPayload);
189+
}
190+
return;
191+
}
192+
193+
// Use schema defaults if no options are provided.
194+
ValueMap defaultPlan = trackingPlan.getValueMap("__default");
195+
196+
// No defaults, send the event.
197+
if (isNullOrEmpty(defaultPlan)) {
198+
integration.track(trackPayload);
199+
return;
200+
}
201+
202+
// Send the event if new events are enabled or if this is the Segment integration.
203+
boolean defaultEventsEnabled = defaultPlan.getBoolean("enabled", true);
204+
if (defaultEventsEnabled || SegmentIntegration.SEGMENT_KEY.equals(key)) {
186205
integration.track(trackPayload);
187206
}
207+
188208
return;
189209
}
190210

analytics/src/test/java/com/segment/analytics/IntegrationOperationTest.java

+113-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void trackAllDisabledInOptionsButIntegrationEnabledWithOptions() {
8888
}
8989

9090
@Test
91-
public void trackNoEventPlan() throws IOException {
91+
public void trackPlanForEvent() throws IOException {
9292
TrackPayload payload = new TrackPayload.Builder()
9393
.event("Install Attributed")
9494
.userId("userId")
@@ -107,6 +107,27 @@ public void trackNoEventPlan() throws IOException {
107107
verify(integration).track(payload);
108108
}
109109

110+
@Test
111+
public void trackPlanForEventWithOptions() throws IOException {
112+
TrackPayload payload = new TrackPayload.Builder()
113+
.event("Install Attributed")
114+
.userId("userId")
115+
.integrations(Collections.singletonMap("All", false))
116+
.build();
117+
track(
118+
payload,
119+
"Mixpanel",
120+
Cartographer.INSTANCE.fromJson(
121+
"{\n"
122+
+ " \"plan\": {\n"
123+
+ " \"track\": {\n"
124+
+ " \"Completed Order\": {}\n"
125+
+ " }\n"
126+
+ " }\n"
127+
+ "}"));
128+
verify(integration, never()).track(payload);
129+
}
130+
110131
@Test
111132
public void trackPlanDisabledEvent() throws IOException {
112133
TrackPayload payload = new TrackPayload.Builder()
@@ -222,4 +243,95 @@ public void ignoresSegment() throws IOException {
222243
+ "}"));
223244
verify(integration).track(payload);
224245
}
246+
247+
@Test
248+
public void defaultNewEventsEnabled() throws IOException {
249+
TrackPayload payload = new TrackPayload.Builder()
250+
.event("Install Attributed")
251+
.userId("userId")
252+
.build();
253+
track(
254+
payload,
255+
"Segment.io",
256+
Cartographer.INSTANCE.fromJson(
257+
"{\n" +
258+
" \"plan\": {\n" +
259+
" \"track\": {\n" +
260+
" \"__default\": {\n" +
261+
" \"enabled\": true\n" +
262+
" }\n" +
263+
" }\n" +
264+
" }\n" +
265+
"}"));
266+
verify(integration).track(payload);
267+
}
268+
269+
@Test
270+
public void defaultNewEventsDisabled() throws IOException {
271+
TrackPayload payload = new TrackPayload.Builder()
272+
.event("Install Attributed")
273+
.userId("userId")
274+
.build();
275+
track(
276+
payload,
277+
"Mixpanel",
278+
Cartographer.INSTANCE.fromJson(
279+
"{\n" +
280+
" \"plan\": {\n" +
281+
" \"track\": {\n" +
282+
" \"__default\": {\n" +
283+
" \"enabled\": false\n" +
284+
" }\n" +
285+
" }\n" +
286+
" }\n" +
287+
"}"));
288+
verify(integration, never()).track(payload);
289+
}
290+
291+
@Test
292+
public void defaultNewEventsDisabledSendToSegment() throws IOException {
293+
TrackPayload payload = new TrackPayload.Builder()
294+
.event("Install Attributed")
295+
.userId("userId")
296+
.build();
297+
track(
298+
payload,
299+
"Segment.io",
300+
Cartographer.INSTANCE.fromJson(
301+
"{\n" +
302+
" \"plan\": {\n" +
303+
" \"track\": {\n" +
304+
" \"__default\": {\n" +
305+
" \"enabled\": false\n" +
306+
" }\n" +
307+
" }\n" +
308+
" }\n" +
309+
"}"));
310+
verify(integration).track(payload);
311+
}
312+
313+
@Test
314+
public void eventPlanOverridesSchemaDefault() throws IOException {
315+
TrackPayload payload = new TrackPayload.Builder()
316+
.event("Install Attributed")
317+
.userId("userId")
318+
.build();
319+
track(
320+
payload,
321+
"Mixpanel",
322+
Cartographer.INSTANCE.fromJson(
323+
"{\n" +
324+
" \"plan\": {\n" +
325+
" \"track\": {\n" +
326+
" \"__default\": {\n" +
327+
" \"enabled\": true\n" +
328+
" },\n" +
329+
" \"Install Attributed\": {\n" +
330+
" \"enabled\": false\n" +
331+
" }\n" +
332+
" }\n" +
333+
" }\n" +
334+
"}"));
335+
verify(integration, never()).track(payload);
336+
}
225337
}

0 commit comments

Comments
 (0)