Skip to content

Commit

Permalink
feat: Add versionId to tracking plan data (#392)
Browse files Browse the repository at this point in the history
  • Loading branch information
falconandy authored Mar 31, 2022
1 parent e8a9722 commit a09f022
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 32 deletions.
1 change: 1 addition & 0 deletions Sources/Amplitude/AMPConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,4 @@ extern NSString *const AMP_TRACKING_OPTION_VERSION_NAME;
extern NSString *const AMP_PLAN_BRANCH;
extern NSString *const AMP_PLAN_SOURCE;
extern NSString *const AMP_PLAN_VERSION;
extern NSString *const AMP_PLAN_VERSION_ID;
1 change: 1 addition & 0 deletions Sources/Amplitude/AMPConstants.m
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@
NSString *const AMP_PLAN_BRANCH = @"branch";
NSString *const AMP_PLAN_SOURCE = @"source";
NSString *const AMP_PLAN_VERSION = @"version";
NSString *const AMP_PLAN_VERSION_ID = @"versionId";
13 changes: 13 additions & 0 deletions Sources/Amplitude/AMPPlan.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ - (AMPPlan *)setVersion:(NSString *)version {
return self;
}

- (AMPPlan *)setVersionId:(NSString *)versionId {
if ([AMPUtils isEmptyString:versionId]) {
AMPLITUDE_LOG(@"Invalid empty versionId");
return self;
}

_versionId = versionId;
return self;
}

- (NSDictionary *)toNSDictionary {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
if (_branch) {
Expand All @@ -83,6 +93,9 @@ - (NSDictionary *)toNSDictionary {
if (_version) {
[dict setValue:_version forKey:AMP_PLAN_VERSION];
}
if (_versionId) {
[dict setValue:_versionId forKey:AMP_PLAN_VERSION_ID];
}
return dict;
}

Expand Down
4 changes: 4 additions & 0 deletions Sources/Amplitude/Public/AMPPlan.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

@property (nonatomic, strong, readonly) NSString *version;

@property (nonatomic, strong, readonly) NSString *versionId;

+ (instancetype)plan;

- (AMPPlan *)setBranch:(NSString *)branch;
Expand All @@ -38,6 +40,8 @@

- (AMPPlan *)setVersion:(NSString *)version;

- (AMPPlan *)setVersionId:(NSString *)versionId;

- (NSDictionary *)toNSDictionary;

@end
62 changes: 32 additions & 30 deletions Tests/AmplitudeTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -862,16 +862,16 @@ -(void)testRegenerateDeviceId {

-(void)testTrackIdfa {
NSString *value = @"12340000-0000-0000-0000-000000000000";

self.amplitude.adSupportBlock = ^NSString * _Nonnull{
return value;
};

[self.amplitude logEvent:@"test"];
[self.amplitude flushQueue];

self.amplitude.adSupportBlock = nil;

NSDictionary *apiProps = [self.amplitude getLastEvent][@"api_properties"];
XCTAssertTrue([[apiProps objectForKey:@"ios_idfa"] isEqualToString:value]);
}
Expand All @@ -883,9 +883,9 @@ -(void)testIdfaAsDeviceId {
if (dbHelper != nil) {
[dbHelper deleteDB];
}

NSString *value = @"12340000-0000-0000-0000-000000000000";

Amplitude *client = [Amplitude instanceWithName:@"idfa"];
client.adSupportBlock = ^NSString * _Nonnull{
return value;
Expand All @@ -906,9 +906,9 @@ -(void)testDisableIdfaAsDeviceId {
if (dbHelper != nil) {
[dbHelper deleteDB];
}

NSString *value = @"12340000-0000-0000-0000-000000000000";

Amplitude *client = [Amplitude instanceWithName:@"disable_idfa"];
client.adSupportBlock = ^NSString * _Nonnull{
return value;
Expand All @@ -930,9 +930,9 @@ -(void)testIdfvAsDeviceId {
if (dbHelper != nil) {
[dbHelper deleteDB];
}

Amplitude *client = [Amplitude instanceWithName:@"idfv"];

AMPDeviceInfo * deviceInfo = [[AMPDeviceInfo alloc] init];

[client flushQueueWithQueue:client.initializerQueue];
Expand Down Expand Up @@ -987,23 +987,23 @@ -(void)testSetTrackingConfig {
- (void)testEnableCoppaControl {
NSDictionary *event = nil;
NSDictionary *apiProperties = nil;

[self.amplitude disableCoppaControl];

[self.amplitude logEvent:@"test"];
[self.amplitude flushQueue];
event = [self.amplitude getLastEvent];

apiProperties = [event objectForKey:@"api_properties"];
XCTAssertNotNil([apiProperties objectForKey:@"ios_idfv"]);

[self.amplitude enableCoppaControl];
[self.amplitude logEvent:@"test"];
[self.amplitude flushQueue];
event = [self.amplitude getLastEvent];
apiProperties = [event objectForKey:@"api_properties"];
XCTAssertNil([apiProperties objectForKey:@"ios_idfv"]);

// Minor guard covers 3 server configs. city, lat_lng, ip
NSDictionary *trackingOptions = [apiProperties objectForKey:@"tracking_options"];
XCTAssertEqual(3, trackingOptions.count);
Expand All @@ -1015,75 +1015,75 @@ - (void)testEnableCoppaControl {
- (void)testCustomizedLibrary {
Amplitude *client = [Amplitude instanceWithName:@"custom_lib"];
[client initializeApiKey:@"blah"];

client.libraryName = @"amplitude-unity";
client.libraryVersion = @"1.0.0";

[client logEvent:@"test"];
[client flushQueue];

NSDictionary *event = [client getLastEventFromInstanceName:@"custom_lib"];
NSDictionary *targetLibraryValue = @{ @"name" : @"amplitude-unity",
@"version" : @"1.0.0"
};

NSDictionary *currentLibraryValue = event[@"library"];
XCTAssertEqualObjects(currentLibraryValue, targetLibraryValue);
}

- (void)testCustomizedLibraryWithNilVersion {
Amplitude *client = [Amplitude instanceWithName:@"custom_lib"];
[client initializeApiKey:@"blah"];

client.libraryName = @"amplitude-unity";
client.libraryVersion = nil;

[client logEvent:@"test"];
[client flushQueue];

NSDictionary *event = [client getLastEventFromInstanceName:@"custom_lib"];
NSDictionary *targetLibraryValue = @{ @"name" : @"amplitude-unity",
@"version" : kAMPUnknownVersion
};

NSDictionary *currentLibraryValue = event[@"library"];
XCTAssertEqualObjects(currentLibraryValue, targetLibraryValue);
}

- (void)testCustomizedLibraryWithNilLibrary {
Amplitude *client = [Amplitude instanceWithName:@"custom_lib"];
[client initializeApiKey:@"blah"];

client.libraryName = nil;
client.libraryVersion = @"1.0.0";

[client logEvent:@"test"];
[client flushQueue];

NSDictionary *event = [client getLastEventFromInstanceName:@"custom_lib"];
NSDictionary *targetLibraryValue = @{ @"name" : kAMPUnknownLibrary,
@"version" : @"1.0.0"
};

NSDictionary *currentLibraryValue = event[@"library"];
XCTAssertEqualObjects(currentLibraryValue, targetLibraryValue);
}

- (void)testCustomizedLibraryWithNilLibraryAndVersion {
Amplitude *client = [Amplitude instanceWithName:@"custom_lib"];
[client initializeApiKey:@"blah"];

client.libraryName = nil;
client.libraryVersion = nil;

[client logEvent:@"test"];
[client flushQueue];

NSDictionary *event = [client getLastEventFromInstanceName:@"custom_lib"];
NSDictionary *targetLibraryValue = @{ @"name" : kAMPUnknownLibrary,
@"version" : kAMPUnknownVersion
};

NSDictionary *currentLibraryValue = event[@"library"];
XCTAssertEqualObjects(currentLibraryValue, targetLibraryValue);
}
Expand All @@ -1094,17 +1094,19 @@ - (void)testSetPlan {
NSString *branch = @"main";
NSString *source = @"mobile";
NSString *version = @"1.0.0";

AMPPlan *plan = [[[[AMPPlan plan] setBranch:branch] setSource:source] setVersion:version];
NSString *versionId = @"9ec23ba0-275f-468f-80d1-66b88bff9529";

AMPPlan *plan = [[[[[AMPPlan plan] setBranch:branch] setSource:source] setVersion:version] setVersionId:versionId];
[client setPlan:plan];
[client logEvent:@"test"];
[client flushQueue];
NSDictionary *event = [client getLastEventFromInstanceName:@"observe_plan"];

NSDictionary *planValue = event[@"plan"];
XCTAssertEqualObjects(branch, planValue[@"branch"]);
XCTAssertEqualObjects(source, planValue[@"source"]);
XCTAssertEqualObjects(version, planValue[@"version"]);
XCTAssertEqualObjects(versionId, planValue[@"versionId"]);
}

- (void)testSetServerZone {
Expand Down Expand Up @@ -1153,7 +1155,7 @@ - (void)testSwallowMiddleware {
[client initializeApiKey:@"middleware_api_key"];
[client logEvent:@"test"];
[client flushQueue];

XCTAssertNil([client getLastEventFromInstanceName:@"middleware_swallow"]);
}

Expand Down
24 changes: 22 additions & 2 deletions Tests/PlanTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,37 @@ - (void)testSetVersion {
XCTAssertEqualObjects([dict objectForKey:@"version"], version);
}

- (void)testSetVersionId {
AMPPlan *plan = [AMPPlan plan];
XCTAssertNil(plan.versionId);

NSString *versionId = @"9ec23ba0-275f-468f-80d1-66b88bff9529";
[plan setVersionId:versionId];
XCTAssertEqualObjects(plan.versionId, versionId);

// test that ignore empty inputs
[plan setVersionId:nil];
XCTAssertEqualObjects(plan.versionId, versionId);
[plan setVersionId:@""];
XCTAssertEqualObjects(plan.versionId, versionId);

NSDictionary *dict = [plan toNSDictionary];
XCTAssertEqualObjects([dict objectForKey:@"versionId"], versionId);
}

- (void)testToNSDictionary {
NSString *branch = @"main";
NSString *source = @"mobile";
NSString *version = @"1.0.0";

AMPPlan *plan = [[[[AMPPlan plan] setBranch:branch] setSource:source] setVersion:version];
NSString *versionId = @"9ec23ba0-275f-468f-80d1-66b88bff9529";

AMPPlan *plan = [[[[[AMPPlan plan] setBranch:branch] setSource:source] setVersion:version] setVersionId:versionId];

NSDictionary *dict = [plan toNSDictionary];
XCTAssertEqualObjects([dict objectForKey:@"branch"], branch);
XCTAssertEqualObjects([dict objectForKey:@"source"], source);
XCTAssertEqualObjects([dict objectForKey:@"version"], version);
XCTAssertEqualObjects([dict objectForKey:@"versionId"], versionId);
}

@end

0 comments on commit a09f022

Please sign in to comment.