Skip to content

Adds functionality to increment traits if set #45

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

Merged
merged 3 commits into from
Oct 26, 2017
Merged

Conversation

ladanazita
Copy link
Contributor

Supports Amplitude's add functionality. Will add a new setting traitsToIncrement to UI, which will accept an array of traits (of type NSString) to check in identify.traits. If the trait is present, it will increment the trait given the value passed in.

This will increment a user property by some numerical value. If the user property does not have a value set yet, it will be initialized to 0 before being incremented.


- (void)incrementTrait:(NSString *)trait andValue:(NSString *)value
{
NSArray *increments = [self.settings objectForKey:@"traitsToIncrement"];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The long form and short form usage seems to be inconsistent, we're using both for the changes in this PR - can we stick to one (preferably short form)?

- (void)incrementTrait:(NSString *)trait andValue:(NSString *)value
{
NSArray *increments = [self.settings objectForKey:@"traitsToIncrement"];
for (NSString *increment in increments) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems inefficient. The runtime is O(n*m) where n = size of payload.traits and m = size of settings.traitsToIncrement.

You should remove the case insensitivity and we can cut the runtime to O(n).

Copy link
Contributor

@f2prateek f2prateek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be faster and run in o(n). Looks good otherwise.

@ladanazita
Copy link
Contributor Author

Updates so that for each trait passed in identify and appears in traitsToIncrement, use add otherwise, use set. if traitsToIncrement is not configured, fallback to setUserTraits, which wraps the set functionality, allowing you to set an NSDictionary of user traits at once

[self.amplitude identify:[self.identify set:trait value:value]];
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't we have to remove incremented traits from the traits object? From Amplitude's docs: "Note: If a user property is used in multiple operations on the same Identify object, then only the first operation will be saved and the rest will be ignored. " setUserProperties isn't called on an Identify object, so I'd expect that traits set via this method would override any traits already incremented on the Identify object.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clarification from Amplitude's docs: "You may use setUserProperties shorthand to set multiple user properties at once. This method is simply a wrapper around Identify.set and identify." That said, calling setUserProperties creates its own instance of the Identify class in Android.

Copy link
Contributor Author

@ladanazita ladanazita Oct 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I don't call setUserProperties if settings has values in traitsToIncrement . Instead it moves into the incrementTrait block and uses set. Yup, setUserProperties is just a wrapper around the logic of using set on AMPIdentify.

@ladanazita ladanazita merged commit 7a72a2e into master Oct 26, 2017
@ladanazita ladanazita deleted the adds/increment branch October 26, 2017 03:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants