Skip to content

Commit

Permalink
feat: Support seamless integration with amplitude experiment SDK (#306)
Browse files Browse the repository at this point in the history
  • Loading branch information
bgiori authored Feb 4, 2022
1 parent 67a0138 commit 277b0fb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ android {
}

dependencies {
implementation 'com.amplitude:analytics-connector:1.0.0'
implementation 'com.squareup.okhttp3:okhttp:4.2.2'
testImplementation 'com.squareup.okhttp3:mockwebserver:4.2.2'
testImplementation 'org.robolectric:robolectric:4.3.1'
Expand Down
43 changes: 42 additions & 1 deletion src/main/java/com/amplitude/api/AmplitudeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import android.os.Build;
import android.util.Pair;


import com.amplitude.analytics.connector.AnalyticsConnector;
import com.amplitude.analytics.connector.Identity;
import com.amplitude.analytics.connector.util.JSONUtil;
import com.amplitude.eventexplorer.EventExplorer;
import com.amplitude.security.MD5;
import com.amplitude.util.DoubleCheck;
Expand All @@ -20,13 +24,15 @@
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;

import kotlin.Unit;
import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
Expand Down Expand Up @@ -201,9 +207,13 @@ public class AmplitudeClient {
* The background event uploading worker thread instance.
*/
WorkerThread httpThread = new WorkerThread("httpThread");
/**
* The core package for integrating with the Experiment SDK.
*/
final AnalyticsConnector connector;
/**
* The runner for middleware
* */
*/
MiddlewareRunner middlewareRunner = new MiddlewareRunner();

/**
Expand All @@ -221,6 +231,7 @@ public AmplitudeClient(String instance) {
this.instanceName = Utils.normalizeInstanceName(instance);
logThread.start();
httpThread.start();
this.connector = AnalyticsConnector.getInstance(this.instanceName);
}

/**
Expand Down Expand Up @@ -402,7 +413,21 @@ public void onDatabaseReset(SQLiteDatabase db) {
}
});

// set up listener to core package to receive exposure events from Experiment
connector.getEventBridge().setEventReceiver(analyticsEvent -> {
String eventType = analyticsEvent.getEventType();
JSONObject eventProperties = JSONUtil.toJSONObject(analyticsEvent.getEventProperties());
JSONObject userProperties = JSONUtil.toJSONObject(analyticsEvent.getUserProperties());
logEventAsync(eventType, eventProperties, null, userProperties,
null, null, getCurrentTimeMillis(), false);
return Unit.INSTANCE;
});

// Set user ID and device ID in core identity store for use in Experiment SDK
connector.getIdentityStore().setIdentity(new Identity(userId, deviceId, new HashMap<>()));

initialized = true;

} catch (CursorWindowAllocationException e) { // treat as uninitialized SDK
logger.e(TAG, String.format(
"Failed to initialize Amplitude SDK due to: %s", e.getMessage()
Expand Down Expand Up @@ -1258,6 +1283,14 @@ protected long logEvent(String eventType, JSONObject eventProperties, JSONObject
event.put("group_properties", (groupProperties == null) ? new JSONObject()
: truncate(groupProperties));
result = saveEvent(eventType, event, extra);

// If the the event is an identify, update the user properties to the core identity
// for experiment SDK to consume.
if (eventType.equals(Constants.IDENTIFY_EVENT) && userProperties != null) {
connector.getIdentityStore().editIdentity()
.updateUserProperties(JSONUtil.toUpdateUserPropertiesMap(userProperties))
.commit();
}
} catch (JSONException e) {
logger.e(TAG, String.format(
"JSON Serialization of event type %s failed, skipping: %s", eventType, e.toString()
Expand Down Expand Up @@ -1923,6 +1956,10 @@ public void run() {
sendSessionEvent(START_SESSION_EVENT);
}
}

// update the user in the core identity store to notify
// experiment to re-fetch variants with the new identity
client.connector.getIdentityStore().editIdentity().setUserId(userId).commit();
}
});
return this;
Expand Down Expand Up @@ -1950,6 +1987,10 @@ public void run() {
}
client.deviceId = deviceId;
saveDeviceId(deviceId);

// update the user in the core identity store to notify
// experiment to re-fetch variants with the new identity
client.connector.getIdentityStore().editIdentity().setDeviceId(deviceId).commit();
}
});
return this;
Expand Down

0 comments on commit 277b0fb

Please sign in to comment.