Skip to content

Add Options#putContext method #595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion analytics/src/main/java/com/segment/analytics/Analytics.java
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,14 @@ private void waitForAdvertisingId() {
void fillAndEnqueue(BasePayload.Builder<?, ?> builder, Options options) {
waitForAdvertisingId();

AnalyticsContext contextCopy = analyticsContext.unmodifiableCopy();
AnalyticsContext contextCopy = new AnalyticsContext(analyticsContext);

for (Map.Entry<String, Object> pair : options.context().entrySet()) {
contextCopy.put(pair.getKey(), pair.getValue());
}

contextCopy = contextCopy.unmodifiableCopy();

builder.context(contextCopy);
builder.anonymousId(contextCopy.traits().anonymousId());
builder.integrations(options.integrations());
Expand Down
20 changes: 20 additions & 0 deletions analytics/src/main/java/com/segment/analytics/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ public class Options {
public static final String ALL_INTEGRATIONS_KEY = "All";

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

public Options() {
integrations = new ConcurrentHashMap<>();
context = new ConcurrentHashMap<>();
}

/**
Expand Down Expand Up @@ -114,8 +116,26 @@ public Options setIntegrationOptions(
return this;
}

/**
* Attach some additional context information. Unlike with {@link
* com.segment.analytics.Analytics#getAnalyticsContext()}, this only has effect for this call.
*
* @param key The key of the extra context data
* @param value The value of the extra context data
* @return This options object for chaining
*/
public Options putContext(String key, Object value) {
context.put(key, value);
return this;
}

/** Returns a copy of settings for integrations. */
public Map<String, Object> integrations() {
return new LinkedHashMap<>(integrations);
}

/** Returns a copy of the context. */
public Map<String, Object> context() {
return new LinkedHashMap<>(context);
}
}
15 changes: 15 additions & 0 deletions analytics/src/test/java/com/segment/analytics/AnalyticsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,21 @@ public void optionsDisableIntegrations() {
verifyNoMoreInteractions(integration);
}

@Test
public void optionsCustomContext() {
analytics.track("foo", null, new Options().putContext("from_tests", true));

verify(integration)
.track(
argThat(
new NoDescriptionMatcher<TrackPayload>() {
@Override
protected boolean matchesSafely(TrackPayload payload) {
return payload.context().get("from_tests") == Boolean.TRUE;
}
}));
}

@Test
public void optOutDisablesEvents() throws IOException {
analytics.optOut(true);
Expand Down
19 changes: 19 additions & 0 deletions analytics/src/test/java/com/segment/analytics/OptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,23 @@ public void setIntegration() throws Exception {
new ImmutableMap.Builder<String, Object>().put("appId", "bar").build())
.build());
}

@Test
public void setOptions() {
options.putContext("foo", "bar");
options.putContext(
"library",
new ImmutableMap.Builder<String, Object>().put("name", "analytics-test").build());

assertThat(options.context())
.isEqualTo(
new ImmutableMap.Builder<String, Object>()
.put("foo", "bar")
.put(
"library",
new ImmutableMap.Builder<String, Object>()
.put("name", "analytics-test")
.build())
.build());
}
}