Skip to content

Commit 1425d4b

Browse files
(feat): easy event tracking. (#359)
1 parent 7f47f68 commit 1425d4b

File tree

7 files changed

+20
-120
lines changed

7 files changed

+20
-120
lines changed

OptimizelySDKCore/OptimizelySDKCore/OPTLYEventBuilder.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2016,2018, Optimizely, Inc. and contributors *
2+
* Copyright 2016-2019 Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -57,7 +57,6 @@ NS_ASSUME_NONNULL_END
5757
*/
5858
- (nullable NSDictionary *)buildConversionEventForUser:(nonnull NSString *)userId
5959
event:(nonnull OPTLYEvent *)event
60-
decisions:(nonnull NSArray<NSDictionary *> *)decisions
6160
eventTags:(nullable NSDictionary *)eventTags
6261
attributes:(nullable NSDictionary<NSString *, NSObject *> *)attributes;
6362
@end

OptimizelySDKCore/OptimizelySDKCore/OPTLYEventBuilder.m

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2016,2018, Optimizely, Inc. and contributors *
2+
* Copyright 2016,2018-2019, Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -70,7 +70,6 @@ -(NSDictionary *)buildImpressionEventForUser:(NSString *)userId
7070

7171
-(NSDictionary *)buildConversionEventForUser:(NSString *)userId
7272
event:(OPTLYEvent *)event
73-
decisions:(NSArray<NSDictionary *> *)decisions
7473
eventTags:(NSDictionary *)eventTags
7574
attributes:(NSDictionary<NSString *, NSObject *> *)attributes {
7675

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

8180
NSDictionary *commonParams = [self createCommonParamsForUser:userId attributes:attributes];
8281
NSArray *conversionOnlyParams = [self createConversionParamsOfEvent:event userId:userId
83-
decisions:decisions eventTags:eventTags
82+
eventTags:eventTags
8483
attributes:attributes];
8584
NSDictionary *conversionParams = [self createImpressionOrConversionParamsWithCommonParams:commonParams conversionOrImpressionOnlyParams:conversionOnlyParams];
8685

@@ -99,6 +98,8 @@ - (NSDictionary *)createCommonParamsForUser:(NSString *)userId
9998
commonParams[OPTLYEventParameterKeysVisitors] = @[visitor];
10099
commonParams[OPTLYEventParameterKeysProjectId] = [self.config.projectId getStringOrEmpty];
101100
commonParams[OPTLYEventParameterKeysAccountId] = [self.config.accountId getStringOrEmpty];
101+
commonParams[OPTLYEventParameterKeysEnrichDecisions] = @YES;
102+
102103
commonParams[OPTLYEventParameterKeysClientEngine] = [[self.config clientEngine] getStringOrEmpty];
103104
commonParams[OPTLYEventParameterKeysClientVersion] = [[self.config clientVersion] getStringOrEmpty];
104105
commonParams[OPTLYEventParameterKeysRevision] = [self.config.revision getStringOrEmpty];
@@ -142,7 +143,6 @@ - (NSDictionary *)createImpressionOrConversionParamsWithCommonParams:(NSDictiona
142143

143144
- (NSArray *)createConversionParamsOfEvent:(OPTLYEvent *)event
144145
userId:(NSString *)userId
145-
decisions:(NSArray<NSDictionary *> *)decisions
146146
eventTags:(NSDictionary *)eventTags
147147
attributes:(NSDictionary *)attributes {
148148

@@ -175,7 +175,6 @@ - (NSArray *)createConversionParamsOfEvent:(OPTLYEvent *)event
175175
}
176176
}
177177

178-
snapshot[OPTLYEventParameterKeysDecisions] = decisions;
179178
snapshot[OPTLYEventParameterKeysEvents] = @[eventDict];
180179

181180
[conversionEventParams addObject:snapshot];

OptimizelySDKCore/OptimizelySDKCore/OPTLYEventParameterKeys.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2016-2018, Optimizely, Inc. and contributors *
2+
* Copyright 2016-2019, Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -21,6 +21,7 @@ NS_ASSUME_NONNULL_BEGIN
2121
// --- Base Parameters ----
2222
extern NSString * const OPTLYEventParameterKeysAccountId;
2323
extern NSString * const OPTLYEventParameterKeysProjectId;
24+
extern NSString * const OPTLYEventParameterKeysEnrichDecisions;
2425
extern NSString * const OPTLYEventParameterKeysVisitors;
2526
extern NSString * const OPTLYEventParameterKeysAnonymizeIP;
2627
extern NSString * const OPTLYEventParameterKeysClientEngine;

OptimizelySDKCore/OptimizelySDKCore/OPTLYEventParameterKeys.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2016-2018, Optimizely, Inc. and contributors *
2+
* Copyright 2016-2019, Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -19,6 +19,7 @@
1919
// --- Base Parameters ----
2020
NSString * const OPTLYEventParameterKeysAccountId = @"account_id"; // nonnull
2121
NSString * const OPTLYEventParameterKeysProjectId = @"project_id";
22+
NSString * const OPTLYEventParameterKeysEnrichDecisions = @"enrich_decisions";
2223
NSString * const OPTLYEventParameterKeysVisitors = @"visitors"; // nonnull
2324
NSString * const OPTLYEventParameterKeysAnonymizeIP = @"anonymize_ip";
2425
NSString * const OPTLYEventParameterKeysClientEngine = @"client_name";

OptimizelySDKCore/OptimizelySDKCore/Optimizely.m

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2017-2018, Optimizely, Inc. and contributors *
2+
* Copyright 2017-2019, Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -447,17 +447,8 @@ - (void)track:(NSString *)eventKey
447447
return;
448448
}
449449

450-
NSArray *decisions = [self decisionsFor:event userId:userId attributes:attributes];
451-
452-
if ([decisions getValidArray] == nil) {
453-
NSString *logMessage = [NSString stringWithFormat:OPTLYLoggerMessagesConversionFailure, eventKey];
454-
[self handleErrorLogsForTrack:logMessage ofLevel:OptimizelyLogLevelInfo];
455-
return;
456-
}
457-
458450
NSDictionary *conversionEventParams = [self.eventBuilder buildConversionEventForUser:userId
459451
event:event
460-
decisions:decisions
461452
eventTags:eventTags
462453
attributes:attributes];
463454
if ([conversionEventParams getValidDictionary] == nil) {
@@ -909,57 +900,6 @@ - (OPTLYVariation *)sendImpressionEventFor:(OPTLYExperiment *)experiment
909900
return variation;
910901
}
911902

912-
/**
913-
Helper method to retrieve decisions for the user from the provided event.
914-
915-
@param event The event which needs to be recorded.
916-
@param userId Id for user.
917-
@param attributes The user's attributes.
918-
@return Array of dictionaries containing valid experiment Ids, variation Ids and layer Ids into which the user is bucketed.
919-
*/
920-
- (NSArray<NSDictionary *> *)decisionsFor:(OPTLYEvent *)event
921-
userId:(NSString *)userId
922-
attributes:(NSDictionary<NSString *, NSObject *> *)attributes {
923-
924-
NSArray *experimentIds = event.experimentIds;
925-
926-
if ([experimentIds count] == 0) {
927-
NSString *logMessage = [NSString stringWithFormat:OPTLYLoggerMessagesTrackEventNoAssociation, event.eventKey];
928-
[self handleErrorLogsForTrack:logMessage ofLevel:OptimizelyLogLevelDebug];
929-
return nil;
930-
}
931-
932-
NSMutableArray *decisions = [NSMutableArray new];
933-
934-
for (NSString *experimentId in experimentIds) {
935-
OPTLYExperiment *experiment = [self.config getExperimentForId:experimentId];
936-
937-
// if the experiment is nil, then it is not part of the project's list of experiments
938-
if (!experiment) {
939-
NSString *logMessage = [NSString stringWithFormat:OPTLYLoggerMessagesTrackExperimentNoAssociation, experiment.experimentKey, event.eventKey];
940-
[self handleErrorLogsForTrack:logMessage ofLevel:OptimizelyLogLevelDebug];
941-
continue;
942-
}
943-
944-
// bucket user into a variation
945-
OPTLYVariation *variation = [self variation:experiment.experimentKey userId:userId attributes:attributes];
946-
947-
// if the variation is nil, then experiment should not be tracked
948-
if (!variation) {
949-
NSString *logMessage = [NSString stringWithFormat:OPTLYLoggerMessagesTrackExperimentNotTracked, userId, experiment.experimentKey];
950-
[self handleErrorLogsForTrack:logMessage ofLevel:OptimizelyLogLevelDebug];
951-
continue;
952-
}
953-
954-
NSMutableDictionary *decision = [NSMutableDictionary new];
955-
decision[OPTLYEventParameterKeysDecisionCampaignId] = [experiment.layerId getStringOrEmpty];
956-
decision[OPTLYEventParameterKeysDecisionExperimentId] = experiment.experimentId;
957-
decision[OPTLYEventParameterKeysDecisionVariationId] = variation.variationId;
958-
[decisions addObject:decision];
959-
}
960-
return decisions;
961-
}
962-
963903
+ (BOOL)isEmptyArray:(NSObject*)array {
964904
return (!array
965905
|| ![array isKindOfClass:[NSArray class]]

0 commit comments

Comments
 (0)