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 Wrapper to Un-Set User Properties #81

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 18 additions & 0 deletions src/com/amplitude/api/AmplitudeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/com/amplitude/unity/plugins/AmplitudePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
42 changes: 42 additions & 0 deletions test/com/amplitude/api/AmplitudeClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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";
Expand Down