Skip to content
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
13 changes: 13 additions & 0 deletions Amplitude/Amplitude.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,19 @@

- (void)clearUserProperties;

/*!
@method

@abstract
Removes properties that are tracked on the user level.

@param userProperties An NSArray containing the keys of the properties to be removed.

@discussion
Property keys must be <code>NSString</code> objects.
*/
- (void)unsetUserProperties:(NSArray*) userProperties;

/*!
@method

Expand Down
20 changes: 20 additions & 0 deletions Amplitude/Amplitude.m
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ + (void)setUserProperties:(NSDictionary*) userProperties {
[[Amplitude instance] setUserProperties:userProperties];
}

+ (void)unsetUserProperties:(NSArray*) userProperties {
[[Amplitude instance] unsetUserProperties:userProperties];
}

+ (void)setUserId:(NSString*) userId {
[[Amplitude instance] setUserId:userId];
}
Expand Down Expand Up @@ -1182,6 +1186,22 @@ - (void)clearUserProperties
[self identify:identify];
}

- (void)unsetUserProperties:(NSArray*) userProperties
{
if (userProperties == nil || ![self isArgument:userProperties validType:[NSArray class] methodName:@"unsetUserProperties:"] || [userProperties count] == 0) {
return;
}

NSArray *copy = [userProperties copy];
[self runOnBackgroundQueue:^{
AMPIdentify *identify = [AMPIdentify identify];
for (NSString *key in copy) {
[identify unset:key];
}
[self identify:identify];
}];
}

- (void)setUserId:(NSString*) userId
{
if (!(userId == nil || [self isArgument:userId validType:[NSString class] methodName:@"setUserId:"])) {
Expand Down
26 changes: 26 additions & 0 deletions AmplitudeTests/SetupTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,32 @@ - (void)testUserPropertiesSet {
XCTAssertEqual(1, [[event objectForKey:@"sequence_number"] intValue]);
}

- (void)testUserPropertiesUnset {
[self.amplitude initializeApiKey:apiKey];
AMPDatabaseHelper *dbHelper = [AMPDatabaseHelper getDatabaseHelper];
XCTAssertEqual([dbHelper getEventCount], 0);

NSDictionary *properties = @{
@"shoeSize": @"-",
@"hatSize": @"-",
@"name": @"-"
};

[self.amplitude unsetUserProperties:[properties allKeys]];
[self.amplitude flushQueue];
XCTAssertEqual([dbHelper getEventCount], 0);
XCTAssertEqual([dbHelper getIdentifyCount], 1);
XCTAssertEqual([dbHelper getTotalEventCount], 1);

NSDictionary *expected = [NSDictionary dictionaryWithObject:properties forKey:AMP_OP_UNSET];

NSDictionary *event = [self.amplitude getLastIdentify];
XCTAssertEqualObjects([event objectForKey:@"event_type"], IDENTIFY_EVENT);
XCTAssertEqualObjects([event objectForKey:@"user_properties"], expected);
XCTAssertEqualObjects([event objectForKey:@"event_properties"], [NSDictionary dictionary]); // event properties should be empty
XCTAssertEqual(1, [[event objectForKey:@"sequence_number"] intValue]);
}

- (void)testSetDeviceId {
AMPDatabaseHelper *dbHelper = [AMPDatabaseHelper getDatabaseHelper];

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Unreleased

* Add support for logging events to multiple Amplitude apps. See [Readme](https://github.com/amplitude/Amplitude-iOS#tracking-events-to-multiple-amplitude-apps) for details.
* Add shorthand method to unset user properties

## 3.5.0 (January 15, 2016)

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ You may use `clearUserProperties` to clear all user properties at once. Note: th
[[Amplitude instance] clearUserProperties];
```

### Clearing User Properties with `unsetUserProperties` ###

You may use `unsetUserProperties` shorthand to unset multiple user properties at once. This method is simply a wrapper around `AMPIDentify unset` and `identify`.

# Allowing Users to Opt Out

To stop all event and session logging for a user, call setOptOut:
Expand Down