From 99bffd001d5ed9605e2a06176c7f6a29acbba4c2 Mon Sep 17 00:00:00 2001 From: William Towns Date: Wed, 24 Feb 2016 14:45:57 -0800 Subject: [PATCH 1/4] add unsetUserProperties to AmplitudeClient --- src/com/amplitude/api/AmplitudeClient.java | 18 ++++++++ .../amplitude/api/AmplitudeClientTest.java | 42 +++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/src/com/amplitude/api/AmplitudeClient.java b/src/com/amplitude/api/AmplitudeClient.java index 9c1f8a46..a9272a63 100644 --- a/src/com/amplitude/api/AmplitudeClient.java +++ b/src/com/amplitude/api/AmplitudeClient.java @@ -670,6 +670,24 @@ public void run() { }); } + public void unsetUserProperties(final String[] userProperties) { + if (userProperties == null || userProperties.length == 0 || + !contextAndApiKeySet("unsetUserProperties")) { + return; + } + + runOnLogThread(new Runnable() { + @Override + public void run() { + Identify identify = new Identify(); + for (int i = 0; i < userProperties.length; i++) { + identify.unset(userProperties[i]); + } + identify(identify); + } + }); + } + public void clearUserProperties() { Identify identify = new Identify().clearAll(); identify(identify); diff --git a/test/com/amplitude/api/AmplitudeClientTest.java b/test/com/amplitude/api/AmplitudeClientTest.java index b7f4eba5..9df5c16a 100644 --- a/test/com/amplitude/api/AmplitudeClientTest.java +++ b/test/com/amplitude/api/AmplitudeClientTest.java @@ -17,6 +17,7 @@ import org.robolectric.shadows.ShadowLooper; import java.util.Arrays; +import java.util.Iterator; import java.util.List; import java.util.UUID; @@ -197,6 +198,47 @@ public void testSetUserProperties() throws JSONException { assertTrue(compareJSONObjects(userProperties, setOperations)); } + @Test + public void testUnsetUserProperties() { + ShadowLooper looper = Shadows.shadowOf(amplitude.logThread.getLooper()); + + // setting null or empty user properties does nothing + amplitude.unsetUserProperties(null); + looper.runToEndOfTasks(); + assertEquals(getUnsentEventCount(), 0); + assertEquals(getUnsentIdentifyCount(), 0); + amplitude.unsetUserProperties(new String[0]); + looper.runToEndOfTasks(); + assertEquals(getUnsentEventCount(), 0); + assertEquals(getUnsentIdentifyCount(), 0); + + String[] userProperties = new String[] { "key1", "key2" }; + amplitude.unsetUserProperties(userProperties); + looper.runToEndOfTasks(); + assertEquals(getUnsentEventCount(), 0); + assertEquals(getUnsentIdentifyCount(), 1); + JSONObject event = getLastUnsentIdentify(); + assertEquals(Constants.IDENTIFY_EVENT, event.optString("event_type")); + assertEquals(event.optJSONObject("event_properties").length(), 0); + + JSONObject userPropertiesOperations = event.optJSONObject("user_properties"); + assertEquals(userPropertiesOperations.length(), 1); + assertTrue(userPropertiesOperations.has(Constants.AMP_OP_UNSET)); + + JSONObject unsetOperations = userPropertiesOperations.optJSONObject(Constants.AMP_OP_UNSET); + boolean matches = true; + Iterator keys = unsetOperations.keys(); + int i = 0; + while (keys.hasNext() && i < userProperties.length) { + if (!keys.next().equals(userProperties[i])) { + matches = false; + break; + } + ++i; + } + assertTrue(matches); + } + @Test public void testIdentifyMultipleOperations() throws JSONException { String property1 = "string value"; From cb86b736b71cce88e341173ece5776f747545e66 Mon Sep 17 00:00:00 2001 From: William Towns Date: Wed, 24 Feb 2016 14:46:22 -0800 Subject: [PATCH 2/4] add unsetUserProperties to AmplitudePlugin --- src/com/amplitude/unity/plugins/AmplitudePlugin.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/com/amplitude/unity/plugins/AmplitudePlugin.java b/src/com/amplitude/unity/plugins/AmplitudePlugin.java index 0fabcced..d23d97e3 100644 --- a/src/com/amplitude/unity/plugins/AmplitudePlugin.java +++ b/src/com/amplitude/unity/plugins/AmplitudePlugin.java @@ -73,6 +73,10 @@ public static void setUserProperties(String jsonProperties) { Amplitude.getInstance().setUserProperties(properties); } + public static void unsetUserProperties(String propertiesList) { + Amplitude.getInstance().unsetUserProperties(propertiesList.split(",")); + } + public static void logRevenue(double amount) { Amplitude.getInstance().logRevenue(amount); } From 4f37389f08f0807a367ce61f74f44c4dcdf2add5 Mon Sep 17 00:00:00 2001 From: William Towns Date: Wed, 24 Feb 2016 14:51:30 -0800 Subject: [PATCH 3/4] update readme --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 107aa449..1076d637 100644 --- a/README.md +++ b/README.md @@ -212,6 +212,15 @@ You may use `clearUserProperties` to clear all user properties at once. Note: th Amplitude.getInstance().clearUserProperties(); ``` +### Clearing Specific User Properties with `unsetUserProperties` ### + +You may use `unsetUserProperties` shorthand to unset multiple user properties at once. This method is simply a wrapper around `Identify.unset` and `identify`. + +```java +String[] userProperties = {"KEY_GOES_HERE", "OTHER_KEY_GOES_HERE"}; +Amplitude.getInstance().unsetUserProperties(userProperties); +``` + # Tracking Revenue # To track revenue from a user, call `logRevenue()` each time a user generates revenue. For example: From dc4c5a88c2ef5b6457d426e9458df98c9486eaaa Mon Sep 17 00:00:00 2001 From: William Towns Date: Fri, 26 Feb 2016 16:48:05 -0800 Subject: [PATCH 4/4] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b53cd2a..25461ac8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ * Add support for logging events to multiple Amplitude apps. See [Readme](https://github.com/amplitude/Amplitude-Android#tracking-events-to-multiple-amplitude-apps) for details. * Update to OKHttp v3.0.1. +* Add wrapper methods to unset user properties from Unity. ## 2.5.0 (January 15, 2016)