Skip to content

Commit

Permalink
Supports setting to set traits once
Browse files Browse the repository at this point in the history
  • Loading branch information
ladannasserian committed Oct 27, 2017
1 parent 7652ff0 commit 6e94ca6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
52 changes: 50 additions & 2 deletions Example/Tests/Tests.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,23 @@
describe(@"Identify", ^{

it(@"identify without traits", ^{
integration = [[SEGAmplitudeIntegration alloc] initWithSettings:@{ @"traitsToIncrement" : [NSNull null] } andAmplitude:amplitude andAmpRevenue:amprevenue andAmpIdentify:identify];
integration = [[SEGAmplitudeIntegration alloc] initWithSettings:@{ @"traitsToIncrement" : [NSNull null],
@"traitsToSetOnce" : [NSNull null] }
andAmplitude:amplitude
andAmpRevenue:amprevenue
andAmpIdentify:identify];
SEGIdentifyPayload *payload = [[SEGIdentifyPayload alloc] initWithUserId:@"1111" anonymousId:nil traits:@{} context:@{} integrations:@{}];

[integration identify:payload];
[verify(amplitude) setUserId:@"1111"];
});

it(@"identify with traits", ^{
integration = [[SEGAmplitudeIntegration alloc] initWithSettings:@{ @"traitsToIncrement" : @[] } andAmplitude:amplitude andAmpRevenue:amprevenue andAmpIdentify:identify];
integration = [[SEGAmplitudeIntegration alloc] initWithSettings:@{ @"traitsToIncrement" : @[],
@"traitsToSetOnce" : @[] }
andAmplitude:amplitude
andAmpRevenue:amprevenue
andAmpIdentify:identify];
SEGIdentifyPayload *payload = [[SEGIdentifyPayload alloc] initWithUserId:@"7891" anonymousId:nil traits:@{
@"name" : @"George Costanza",
@"gender" : @"male",
Expand Down Expand Up @@ -97,6 +105,46 @@
[verify(amplitude) identify:[identify set:@"gender" value:@"female"]];
});

it(@"sets identify trait once", ^{
integration = [[SEGAmplitudeIntegration alloc] initWithSettings:@{ @"traitsToSetOnce" : @[ @"sign_up_date" ] } andAmplitude:amplitude andAmpRevenue:amprevenue andAmpIdentify:identify];

SEGIdentifyPayload *payload = [[SEGIdentifyPayload alloc] initWithUserId:@"3290842" anonymousId:nil traits:@{ @"sign_up_date" : @"2015-08-24",
@"city" : @"los angeles" }
context:@{}
integrations:@{}];

[integration identify:payload];
[verify(amplitude) identify:[identify setOnce:@"sign_up_date" value:@"2015-08-24"]];
[verify(amplitude) identify:[identify set:@"city" value:@"los angeles"]];
});

it(@"sets identify trait once and increments trait", ^{
integration = [[SEGAmplitudeIntegration alloc] initWithSettings:@{ @"traitsToSetOnce" : @[ @"birthdate" ],
@"traitsToIncrement" : @[ @"age" ] }
andAmplitude:amplitude
andAmpRevenue:amprevenue
andAmpIdentify:identify];

SEGIdentifyPayload *payload = [[SEGIdentifyPayload alloc] initWithUserId:@"3290842" anonymousId:nil traits:@{ @"address" : @{
@"street" : @"California st",
@"city" : @"San Francisco"
},
@"birthdate" : @"1989-07-10",
@"name" : @"ladan",
@"age" : @28 }
context:@{}
integrations:@{}];

[integration identify:payload];
[verify(amplitude) identify:[identify setOnce:@"birthdate" value:@"1989-07-10"]];
[verify(amplitude) identify:[identify add:@"age" value:@28]];
[verify(amplitude) identify:[identify set:@"name" value:@"ladan"]];
[verify(amplitude) identify:[identify set:@"address" value:@{
@"street" : @"California st",
@"city" : @"San Francisco"
}]];
});

});

describe(@"Screen", ^{
Expand Down
1 change: 1 addition & 0 deletions Pod/Classes/SEGAmplitudeIntegration.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
@property (strong) AMPRevenue *amprevenue;
@property AMPIdentify *identify;
@property NSSet *traitsToIncrement;
@property NSSet *traitsToSetOnce;

- (id)initWithSettings:(NSDictionary *)settings;
- (id)initWithSettings:(NSDictionary *)settings andAmplitude:(Amplitude *)amplitude andAmpRevenue:(AMPRevenue *)amprevenue andAmpIdentify:(AMPIdentify *)identify;
Expand Down
8 changes: 7 additions & 1 deletion Pod/Classes/SEGAmplitudeIntegration.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ - (id)initWithSettings:(NSDictionary *)settings andAmplitude:(Amplitude *)amplit
self.traitsToIncrement = [NSSet setWithArray:self.settings[@"traitsToIncrement"]];
}

if (self.settings[@"traitsToSetOnce"] != (id)[NSNull null]) {
self.traitsToSetOnce = [NSSet setWithArray:self.settings[@"traitsToSetOnce"]];
}

NSString *apiKey = self.settings[@"apiKey"];
[self.amplitude initializeApiKey:apiKey];
SEGLog(@"[Amplitude initializeApiKey:%@]", apiKey);
Expand Down Expand Up @@ -73,7 +77,7 @@ - (void)identify:(SEGIdentifyPayload *)payload
[self.amplitude setUserId:payload.userId];
SEGLog(@"[Amplitude setUserId:%@]", payload.userId);

if ([self.traitsToIncrement count] > 0) {
if ([self.traitsToIncrement count] > 0 || [self.traitsToSetOnce count] > 0) {
[self incrementOrSetTraits:payload.traits];
} else {
[self.amplitude setUserProperties:payload.traits];
Expand Down Expand Up @@ -212,6 +216,8 @@ - (void)incrementOrSetTraits:(NSDictionary *)traits
if ([self.traitsToIncrement member:trait]) {
[self.amplitude identify:[self.identify add:trait value:value]];
SEGLog(@"[Amplitude add:%@ value:%@]", trait, value);
} else if ([self.traitsToSetOnce member:trait]) {
[self.amplitude identify:[self.identify setOnce:trait value:value]];
} else {
[self.amplitude identify:[self.identify set:trait value:value]];
SEGLog(@"[Amplitude set:%@ value:%@]", trait, value);
Expand Down

0 comments on commit 6e94ca6

Please sign in to comment.