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

(feat): easy event tracking. #359

Merged
merged 6 commits into from
Jan 22, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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: 0 additions & 1 deletion OptimizelySDKCore/OptimizelySDKCore/OPTLYEventBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ NS_ASSUME_NONNULL_END
*/
- (nullable NSDictionary *)buildConversionEventForUser:(nonnull NSString *)userId
thomaszurkan-optimizely marked this conversation as resolved.
Show resolved Hide resolved
event:(nonnull OPTLYEvent *)event
decisions:(nonnull NSArray<NSDictionary *> *)decisions
eventTags:(nullable NSDictionary *)eventTags
attributes:(nullable NSDictionary<NSString *, NSObject *> *)attributes;
@end
Expand Down
7 changes: 3 additions & 4 deletions OptimizelySDKCore/OptimizelySDKCore/OPTLYEventBuilder.m
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ -(NSDictionary *)buildImpressionEventForUser:(NSString *)userId

-(NSDictionary *)buildConversionEventForUser:(NSString *)userId
event:(OPTLYEvent *)event
decisions:(NSArray<NSDictionary *> *)decisions
eventTags:(NSDictionary *)eventTags
attributes:(NSDictionary<NSString *, NSObject *> *)attributes {

Expand All @@ -80,7 +79,7 @@ -(NSDictionary *)buildConversionEventForUser:(NSString *)userId

NSDictionary *commonParams = [self createCommonParamsForUser:userId attributes:attributes];
NSArray *conversionOnlyParams = [self createConversionParamsOfEvent:event userId:userId
decisions:decisions eventTags:eventTags
eventTags:eventTags
thomaszurkan-optimizely marked this conversation as resolved.
Show resolved Hide resolved
attributes:attributes];
NSDictionary *conversionParams = [self createImpressionOrConversionParamsWithCommonParams:commonParams conversionOrImpressionOnlyParams:conversionOnlyParams];

Expand All @@ -99,6 +98,8 @@ - (NSDictionary *)createCommonParamsForUser:(NSString *)userId
commonParams[OPTLYEventParameterKeysVisitors] = @[visitor];
commonParams[OPTLYEventParameterKeysProjectId] = [self.config.projectId getStringOrEmpty];
commonParams[OPTLYEventParameterKeysAccountId] = [self.config.accountId getStringOrEmpty];
commonParams[OPTLYEventParameterKeysEnrichDecisions] = @(true);

commonParams[OPTLYEventParameterKeysClientEngine] = [[self.config clientEngine] getStringOrEmpty];
commonParams[OPTLYEventParameterKeysClientVersion] = [[self.config clientVersion] getStringOrEmpty];
commonParams[OPTLYEventParameterKeysRevision] = [self.config.revision getStringOrEmpty];
Expand Down Expand Up @@ -142,7 +143,6 @@ - (NSDictionary *)createImpressionOrConversionParamsWithCommonParams:(NSDictiona

- (NSArray *)createConversionParamsOfEvent:(OPTLYEvent *)event
userId:(NSString *)userId
decisions:(NSArray<NSDictionary *> *)decisions
eventTags:(NSDictionary *)eventTags
attributes:(NSDictionary *)attributes {

Expand Down Expand Up @@ -175,7 +175,6 @@ - (NSArray *)createConversionParamsOfEvent:(OPTLYEvent *)event
}
}

snapshot[OPTLYEventParameterKeysDecisions] = decisions;
snapshot[OPTLYEventParameterKeysEvents] = @[eventDict];

[conversionEventParams addObject:snapshot];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2016-2018, Optimizely, Inc. and contributors *
* Copyright 2016-2019, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand All @@ -21,6 +21,7 @@ NS_ASSUME_NONNULL_BEGIN
// --- Base Parameters ----
extern NSString * const OPTLYEventParameterKeysAccountId;
extern NSString * const OPTLYEventParameterKeysProjectId;
extern NSString * const OPTLYEventParameterKeysEnrichDecisions;
extern NSString * const OPTLYEventParameterKeysVisitors;
extern NSString * const OPTLYEventParameterKeysAnonymizeIP;
extern NSString * const OPTLYEventParameterKeysClientEngine;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2016-2018, Optimizely, Inc. and contributors *
* Copyright 2016-2019, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand All @@ -19,6 +19,7 @@
// --- Base Parameters ----
NSString * const OPTLYEventParameterKeysAccountId = @"account_id"; // nonnull
NSString * const OPTLYEventParameterKeysProjectId = @"project_id";
NSString * const OPTLYEventParameterKeysEnrichDecisions = @"enrich_decisions";
NSString * const OPTLYEventParameterKeysVisitors = @"visitors"; // nonnull
NSString * const OPTLYEventParameterKeysAnonymizeIP = @"anonymize_ip";
NSString * const OPTLYEventParameterKeysClientEngine = @"client_name";
Expand Down
9 changes: 0 additions & 9 deletions OptimizelySDKCore/OptimizelySDKCore/Optimizely.m
Original file line number Diff line number Diff line change
Expand Up @@ -447,17 +447,8 @@ - (void)track:(NSString *)eventKey
return;
}

NSArray *decisions = [self decisionsFor:event userId:userId attributes:attributes];
thomaszurkan-optimizely marked this conversation as resolved.
Show resolved Hide resolved

if ([decisions getValidArray] == nil) {
NSString *logMessage = [NSString stringWithFormat:OPTLYLoggerMessagesConversionFailure, eventKey];
[self handleErrorLogsForTrack:logMessage ofLevel:OptimizelyLogLevelInfo];
return;
}

thomaszurkan-optimizely marked this conversation as resolved.
Show resolved Hide resolved
NSDictionary *conversionEventParams = [self.eventBuilder buildConversionEventForUser:userId
event:event
decisions:decisions
eventTags:eventTags
attributes:attributes];
if ([conversionEventParams getValidDictionary] == nil) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,8 @@ - (void)tearDown {
#pragma mark - Test BuildConversionTicket:... Audiences

- (void)testBuildConversionTicketWithNoAudience {
NSArray *decisions = [self.optimizely decisionsFor:eventWithoutAudience userId:kUserId attributes:nil];
NSDictionary *params = [self.eventBuilder buildConversionEventForUser:kUserId
event:eventWithoutAudience
decisions:decisions
eventTags:nil
attributes:nil];

Expand All @@ -175,10 +173,8 @@ - (void)testBuildConversionTicketWithNoAudience {
}

- (void)testBuildConversionTicketWithValidAudience {
NSArray *decisions = [self.optimizely decisionsFor:eventWithAudience userId:kUserId attributes:self.attributes];
NSDictionary *params = [self.eventBuilder buildConversionEventForUser:kUserId
event:eventWithAudience
decisions:decisions
eventTags:nil
attributes:self.attributes];

Expand All @@ -201,61 +197,51 @@ - (void)testBuildConversionTicketWithInvalidAudience
{
// check without attributes that satisfy audience requirement
NSDictionary *attributes = @{@"browser_type":@"chrome"};
NSArray *decisions = [self.optimizely decisionsFor:eventWithAudience userId:kUserId attributes:attributes];
NSDictionary *conversionTicket = [self.eventBuilder buildConversionEventForUser:kUserId
event:eventWithAudience
decisions:decisions
eventTags:nil
attributes:attributes];

XCTAssertNotNil(conversionTicket, @"Conversion ticket should not be nil.");
NSDictionary *visitor = conversionTicket[OPTLYEventParameterKeysVisitors][0];
NSDictionary *snapshot = visitor[OPTLYEventParameterKeysSnapshots][0];
decisions = snapshot[OPTLYEventParameterKeysDecisions];
XCTAssertEqual([decisions count], 0, @"Conversion ticket should not have any decision");
}

- (void)testBuildConversionTicketWithExperimentNotRunning {
OPTLYEvent *event = [self.config getEventForKey:kEventWithExperimentNotRunningName];
NSArray *decisions = [self.optimizely decisionsFor:event userId:kUserId attributes:nil];
NSDictionary *conversionTicket = [self.eventBuilder buildConversionEventForUser:kUserId
event:event
decisions:decisions
eventTags:nil
attributes:nil];


XCTAssertNotNil(conversionTicket, @"Conversion ticket should not be nil.");
NSDictionary *visitor = conversionTicket[OPTLYEventParameterKeysVisitors][0];
NSDictionary *snapshot = visitor[OPTLYEventParameterKeysSnapshots][0];
decisions = snapshot[OPTLYEventParameterKeysDecisions];
NSDictionary *decisions = snapshot[OPTLYEventParameterKeysDecisions];
XCTAssertEqual([decisions count], 0, @"Conversion ticket should not have any decision");
}

- (void)testBuildConversionTicketWithInvalidExperiment {
OPTLYEvent *event = [self.config getEventForKey:kEventWithInvalidExperimentName];
NSArray *decisions = [self.optimizely decisionsFor:event userId:kUserId attributes:nil];
NSDictionary *conversionTicket = [self.eventBuilder buildConversionEventForUser:kUserId
event:event
decisions:decisions
eventTags:nil
attributes:nil];


XCTAssertNotNil(conversionTicket, @"Conversion ticket should not be nil.");
NSDictionary *visitor = conversionTicket[OPTLYEventParameterKeysVisitors][0];
NSDictionary *snapshot = visitor[OPTLYEventParameterKeysSnapshots][0];
decisions = snapshot[OPTLYEventParameterKeysDecisions];
NSDictionary *decisions = snapshot[OPTLYEventParameterKeysDecisions];
XCTAssertEqual([decisions count], 0, @"Conversion ticket should not have any decision");
}

- (void)testBuildConversionTicketWithNoConfig {
self.config = nil;
self.eventBuilder = [[OPTLYEventBuilderDefault alloc] initWithConfig:self.config];
NSArray *decisions = [self.optimizely decisionsFor:eventWithAudience userId:kUserId attributes:self.attributes];
NSDictionary *conversionTicket = [self.eventBuilder buildConversionEventForUser:kUserId
event:eventWithAudience
decisions:decisions
eventTags:nil
attributes:self.attributes];

Expand Down Expand Up @@ -679,10 +665,8 @@ - (void)testBuildConversionTicketWithRevenueAndEventTags
- (void)testBuildConversionTicketWithAnonymizeIPFalse {
OPTLYProjectConfig *config = [self setUpForAnonymizeIPFalse];
OPTLYEventBuilderDefault *eventBuilder = [[OPTLYEventBuilderDefault alloc] initWithConfig:config];
NSArray *decisions = [self.optimizely decisionsFor:eventWithoutAudience userId:kUserId attributes:nil];
NSDictionary *params = [eventBuilder buildConversionEventForUser:kUserId
event:eventWithoutAudience
decisions:decisions
eventTags:nil
attributes:nil];

Expand All @@ -694,10 +678,8 @@ - (void)testBuildConversionTicketWithAnonymizeIPFalse {

- (void)testCreateConversionEventWithEmptyAttributeValue {
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary:@{kAttributeKeyBrowserType : @""}];
NSArray *decisions = [self.optimizely decisionsFor:eventWithoutAudience userId:kUserId attributes:attributes];
NSDictionary *params = [self.eventBuilder buildConversionEventForUser:kUserId
event:eventWithoutAudience
decisions:decisions
eventTags:nil
attributes:attributes];

Expand All @@ -724,11 +706,9 @@ - (void)testCreateConversionEventWithEmptyAttributeId {
NSData *data = [NSJSONSerialization dataWithJSONObject:datafile options:0 error:NULL];
self.config = [[OPTLYProjectConfig alloc] initWithDatafile:data];
self.eventBuilder = [[OPTLYEventBuilderDefault alloc] initWithConfig:self.config];
NSArray *decisions = [self.optimizely decisionsFor:eventWithoutAudience userId:kUserId attributes:self.attributes];

NSDictionary *params = [self.eventBuilder buildConversionEventForUser:kUserId
event:eventWithoutAudience
decisions:decisions
eventTags:nil
attributes:self.attributes];

Expand Down Expand Up @@ -810,11 +790,9 @@ - (void)testConversionEventWithoutBotFiltering {
NSData *data = [NSJSONSerialization dataWithJSONObject:datafile options:0 error:NULL];
self.config = [[OPTLYProjectConfig alloc] initWithDatafile:data];
self.eventBuilder = [[OPTLYEventBuilderDefault alloc] initWithConfig:self.config];
NSArray *decisions = [self.optimizely decisionsFor:eventWithAudience userId:kUserId attributes:self.attributes];

NSDictionary *params = [self.eventBuilder buildConversionEventForUser:kUserId
event:eventWithAudience
decisions:decisions
eventTags:nil
attributes:self.attributes];

Expand All @@ -835,11 +813,9 @@ - (void)testConversionEventWithBotFilteringFalse {
NSData *data = [NSJSONSerialization dataWithJSONObject:datafile options:0 error:NULL];
self.config = [[OPTLYProjectConfig alloc] initWithDatafile:data];
self.eventBuilder = [[OPTLYEventBuilderDefault alloc] initWithConfig:self.config];
NSArray *decisions = [self.optimizely decisionsFor:eventWithAudience userId:kUserId attributes:self.attributes];

NSDictionary *params = [self.eventBuilder buildConversionEventForUser:kUserId
event:eventWithAudience
decisions:decisions
eventTags:nil
attributes:self.attributes];

Expand Down Expand Up @@ -962,10 +938,8 @@ - (void)commonBuildConversionTicketTest:(NSDictionary*)eventTags
// Common subroutine for many of the testBuildEventXxx test methods.
// Generally, a testBuildEventXxx should make at most one call
// to commonBuildConversionTicketTest:sentEventTags:sentTags: .
NSArray *decisions = [self.optimizely decisionsFor:eventWithAudience userId:kUserId attributes:self.attributes];
NSDictionary *params = [self.eventBuilder buildConversionEventForUser:kUserId
event:eventWithAudience
decisions:decisions
eventTags:eventTags
attributes:self.attributes];
[self.attributes addEntriesFromDictionary:self.reservedAttributes];
Expand Down Expand Up @@ -1062,22 +1036,7 @@ - (void)checkConversionTicket:(NSDictionary *)params

// check conversion if payload available
XCTAssertNotNil(params, @"Invalid Conversion ticket");

NSArray *decisions = params[OPTLYEventParameterKeysDecisions];

XCTAssertGreaterThan([decisions count], 0, @"Didn't find any decision");
XCTAssertGreaterThan([experimentKeys count], 0, @"Didn't find any experiment key");

for (int i=0; i < [decisions count]; i++) {
NSDictionary *decision = decisions[i];
OPTLYExperiment *experiment = [config getExperimentForId:experimentKeys[i]];
OPTLYVariation *bucketedVariation = [config getVariationForExperiment:experiment.experimentKey
userId:userId
attributes:attributes
bucketer:bucketer];
[self checkDecision:decision campaignId:experiment.layerId experimentId:experiment.experimentId variationId:bucketedVariation.variationId];
}


NSArray *events = params[OPTLYEventParameterKeysEvents];
XCTAssertGreaterThan([events count], 0, @"Didn't find any event");

Expand Down Expand Up @@ -1189,6 +1148,10 @@ - (void)checkCommonParams:(NSDictionary *)params withAttributes:(NSDictionary *)
NSString *clientVersion = params[OPTLYEventParameterKeysClientVersion];
XCTAssert([clientVersion isEqualToString:[self.config clientVersion]], @"Incorrect client version.");

// check enrich_decisions
NSNumber *enrich = params[OPTLYEventParameterKeysEnrichDecisions];
XCTAssert([enrich boolValue] == true, @"Incorrect value for enrich decisions.");

// check anonymizeIP
NSNumber *anonymizeIP = params[OPTLYEventParameterKeysAnonymizeIP];
XCTAssert([anonymizeIP boolValue] == true, @"Incorrect value for IP anonymization.");
Expand Down
Loading