Skip to content
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

Add DefaultProjectSettings in Analytics.Builder #662

Merged
merged 15 commits into from
May 21, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import android.app.Application;
import android.util.Log;
import com.segment.analytics.Analytics;
import com.segment.analytics.ValueMap;
import io.github.inflationx.calligraphy3.CalligraphyConfig;
import io.github.inflationx.calligraphy3.CalligraphyInterceptor;
import io.github.inflationx.viewpump.ViewPump;
Expand Down Expand Up @@ -54,6 +55,16 @@ public void onCreate() {
new Analytics.Builder(this, ANALYTICS_WRITE_KEY)
.trackApplicationLifecycleEvents()
.trackAttributionInformation()
.defaultProjectSettings(
new ValueMap()
.putValue(
"integrations",
new ValueMap()
.putValue(
"Adjust",
new ValueMap()
.putValue("appToken", "<>")
.putValue("trackAttributionData", true))))
.recordScreenViews();

// Set the initialized instance as a globally accessible instance.
Expand Down
52 changes: 40 additions & 12 deletions analytics/src/main/java/com/segment/analytics/Analytics.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ public static void setSingletonInstance(Analytics analytics) {
final boolean trackDeepLinks,
BooleanPreference optOut,
Crypto crypto,
@NonNull List<Middleware> middlewares) {
@NonNull List<Middleware> middlewares,
@NonNull final ValueMap defaultProjectSettings) {
this.application = application;
this.networkExecutor = networkExecutor;
this.stats = stats;
Expand Down Expand Up @@ -254,23 +255,36 @@ public static void setSingletonInstance(Analytics analytics) {
public void run() {
projectSettings = getSettings();
if (isNullOrEmpty(projectSettings)) {
// Backup mode - Enable just the Segment integration.
// Backup mode - Enable the Segment integration and load the provided
// defaultProjectSettings
// {
// ...defaultProjectSettings
// integrations: {
// ...defaultProjectSettings.integrations
// Segment.io: {
// ...defaultProjectSettings.integrations.Segment.io
// apiKey: "{writeKey}"
// }
// }
// }
projectSettings =
ProjectSettings.create(
new ValueMap() //
.putValue(
"integrations",
new ValueMap()
.putValue(
"Segment.io",
new ValueMap().putValue("apiKey", Analytics.this.writeKey))));
if (!defaultProjectSettings.containsKey("integrations")) {
defaultProjectSettings.put("integrations", new ValueMap());
}
if (!defaultProjectSettings.getValueMap("integrations").containsKey("Segment.io")) {
defaultProjectSettings
.getValueMap("integrations")
.put("Segment.io", new ValueMap());
}
if (!defaultProjectSettings
.getValueMap("integrations")
.getValueMap("Segment.io")
.containsKey("apiKey")) {
defaultProjectSettings
.getValueMap("integrations")
.getValueMap("Segment.io")
.putValue("apiKey", Analytics.this.writeKey);
}
projectSettings = ProjectSettings.create(defaultProjectSettings);
}
HANDLER.post(
new Runnable() {
Expand Down Expand Up @@ -1047,6 +1061,7 @@ public static class Builder {
private boolean trackAttributionInformation = false;
private boolean trackDeepLinks = false;
private Crypto crypto;
private ValueMap defaultProjectSettings = new ValueMap();

/** Start building a new {@link Analytics} instance. */
public Builder(Context context, String writeKey) {
Expand Down Expand Up @@ -1253,6 +1268,18 @@ public Builder middleware(Middleware middleware) {
return this;
}

/**
* Set the default project settings to use, if Segment.com cannot be reached. An example
* configuration can be found here, using your write key: <a
* href="https://cdn-settings.segment.com/v1/projects/YOUR_WRITE_KEY/settings">
* https://cdn-settings.segment.com/v1/projects/YOUR_WRITE_KEY/settings </a>
*/
public Builder defaultProjectSettings(ValueMap defaultProjectSettings) {
assertNotNull(defaultProjectSettings, "defaultProjectSettings");
this.defaultProjectSettings = defaultProjectSettings;
return this;
}

/**
* The executor on which payloads are dispatched asynchronously. This is not exposed publicly.
*/
Expand Down Expand Up @@ -1351,7 +1378,8 @@ public Analytics build() {
trackDeepLinks,
optOut,
crypto,
middlewares);
middlewares,
defaultProjectSettings);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,14 @@ private void mockWriteKeyInResources(Context context, String writeKey) {
//noinspection ResourceType
when(resources.getString(1)).thenReturn(writeKey);
}

@Test
public void invalidDefaultProjectSettingsThrowsException() {
try {
new Builder(context, "foo").defaultProjectSettings(null);
fail("Null defaultProjectSettings should throw exception.");
} catch (NullPointerException expected) {
assertThat(expected).hasMessage("defaultProjectSettings == null");
}
}
}
Loading