Skip to content

Commit cb518e0

Browse files
authored
Add Options#putContext method (#595)
Ref: LIB-634
1 parent dc99957 commit cb518e0

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,14 @@ private void waitForAdvertisingId() {
781781
void fillAndEnqueue(BasePayload.Builder<?, ?> builder, Options options) {
782782
waitForAdvertisingId();
783783

784-
AnalyticsContext contextCopy = analyticsContext.unmodifiableCopy();
784+
AnalyticsContext contextCopy = new AnalyticsContext(analyticsContext);
785+
786+
for (Map.Entry<String, Object> pair : options.context().entrySet()) {
787+
contextCopy.put(pair.getKey(), pair.getValue());
788+
}
789+
790+
contextCopy = contextCopy.unmodifiableCopy();
791+
785792
builder.context(contextCopy);
786793
builder.anonymousId(contextCopy.traits().anonymousId());
787794
builder.integrations(options.integrations());

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ public class Options {
4141
public static final String ALL_INTEGRATIONS_KEY = "All";
4242

4343
private final Map<String, Object> integrations; // passed in by the user
44+
private final Map<String, Object> context;
4445

4546
public Options() {
4647
integrations = new ConcurrentHashMap<>();
48+
context = new ConcurrentHashMap<>();
4749
}
4850

4951
/**
@@ -114,8 +116,26 @@ public Options setIntegrationOptions(
114116
return this;
115117
}
116118

119+
/**
120+
* Attach some additional context information. Unlike with {@link
121+
* com.segment.analytics.Analytics#getAnalyticsContext()}, this only has effect for this call.
122+
*
123+
* @param key The key of the extra context data
124+
* @param value The value of the extra context data
125+
* @return This options object for chaining
126+
*/
127+
public Options putContext(String key, Object value) {
128+
context.put(key, value);
129+
return this;
130+
}
131+
117132
/** Returns a copy of settings for integrations. */
118133
public Map<String, Object> integrations() {
119134
return new LinkedHashMap<>(integrations);
120135
}
136+
137+
/** Returns a copy of the context. */
138+
public Map<String, Object> context() {
139+
return new LinkedHashMap<>(context);
140+
}
121141
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,21 @@ public void optionsDisableIntegrations() {
348348
verifyNoMoreInteractions(integration);
349349
}
350350

351+
@Test
352+
public void optionsCustomContext() {
353+
analytics.track("foo", null, new Options().putContext("from_tests", true));
354+
355+
verify(integration)
356+
.track(
357+
argThat(
358+
new NoDescriptionMatcher<TrackPayload>() {
359+
@Override
360+
protected boolean matchesSafely(TrackPayload payload) {
361+
return payload.context().get("from_tests") == Boolean.TRUE;
362+
}
363+
}));
364+
}
365+
351366
@Test
352367
public void optOutDisablesEvents() throws IOException {
353368
analytics.optOut(true);

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,23 @@ public void setIntegration() throws Exception {
7979
new ImmutableMap.Builder<String, Object>().put("appId", "bar").build())
8080
.build());
8181
}
82+
83+
@Test
84+
public void setOptions() {
85+
options.putContext("foo", "bar");
86+
options.putContext(
87+
"library",
88+
new ImmutableMap.Builder<String, Object>().put("name", "analytics-test").build());
89+
90+
assertThat(options.context())
91+
.isEqualTo(
92+
new ImmutableMap.Builder<String, Object>()
93+
.put("foo", "bar")
94+
.put(
95+
"library",
96+
new ImmutableMap.Builder<String, Object>()
97+
.put("name", "analytics-test")
98+
.build())
99+
.build());
100+
}
82101
}

0 commit comments

Comments
 (0)