Skip to content

Commit b9acece

Browse files
authored
E2-1605 implement counters SDK (#226)
* added some counters * add more counters * bring back verify and add some stopMocking calls * revert to previous successful build * dependency in LPRequest * add sendallcounts test, remove count aggregator from leanplumtest * dependency in LPInbox * dependency in LPActionContext * bring back countaggregator test
1 parent 33e7066 commit b9acece

31 files changed

+274
-1
lines changed

Example/Tests/Classes/LPCountAggregatorTest.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424

2525

2626
#import <XCTest/XCTest.h>
27+
#import <OCMock/OCMock.h>
2728
#import "LPCountAggregator.h"
2829
#import "Constants.h"
30+
#import "LeanplumRequest.h"
2931

3032
/**
3133
* Expose private class methods
@@ -128,4 +130,19 @@ - (void)test_makeParams {
128130
XCTAssert([params[LP_PARAM_COUNT] intValue] == 2);
129131
}
130132

133+
- (void)test_sendAllCounts {
134+
LPCountAggregator *countAggregator = [[LPCountAggregator alloc] init];
135+
NSString *testString = @"test";
136+
countAggregator.enabledCounters = [NSSet setWithObjects:testString, nil];
137+
[countAggregator incrementCount:testString];
138+
139+
id leanplumRequestMock = OCMClassMock([LeanplumRequest class]);
140+
141+
OCMStub([leanplumRequestMock post:LP_METHOD_LOG params:[OCMArg any]]).andReturn(leanplumRequestMock);
142+
143+
[countAggregator sendAllCounts];
144+
145+
OCMVerify([leanplumRequestMock sendEventually]);
146+
}
147+
131148
@end

Example/Tests/Classes/LPRequestSenderTest.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ - (void)testSendEventually {
8787
[requestSender sendEventually:request];
8888

8989
OCMVerify([eventDataManagerMock addEvent:[OCMArg isNotNil]]);
90+
91+
[eventDataManagerMock stopMocking];
9092
}
9193

9294
- (void)testSendIfConnected {
@@ -99,6 +101,8 @@ - (void)testSendIfConnected {
99101
[requestSender sendIfConnected:request];
100102

101103
OCMVerify([requestSenderMock sendNow:request]);
104+
105+
[requestSenderMock stopMocking];
102106
}
103107

104108
- (void)testSendIfNotConnected {
@@ -126,6 +130,8 @@ - (void)testSendNowWithData {
126130
[requestSender sendNow:request withData:data[@"key"] forKey:@"key"];
127131

128132
OCMVerify([requestSenderMock sendNow:request withDatas:data]);
133+
134+
[requestSenderMock stopMocking];
129135
}
130136

131137
- (void)testSendNowWithDatas {
@@ -170,7 +176,10 @@ - (void) testSendRequestsSync {
170176
OCMVerify([requestSender.engine operationWithPath:[OCMArg any] params:[OCMArg any] httpMethod:[OCMArg any] ssl:[OCMArg any] timeoutSeconds:timeout]);
171177
OCMVerify([opMock addCompletionHandler:[OCMArg any] errorHandler:[OCMArg any]]);
172178
OCMVerify([requestSender.engine enqueueOperation:opMock]);
179+
180+
[requestOperationMock stopMocking];
173181
[countAggregatorMock stopMocking];
182+
[eventDataManagerMock stopMocking];
174183
}
175184

176185
- (void) testSendRequestsAsync {
@@ -183,5 +192,8 @@ - (void) testSendRequestsAsync {
183192

184193
OCMVerify([requestOperationMock addExecutionBlock:[OCMArg any]]);
185194
OCMVerify([mockQueue addOperation:requestOperationMock]);
195+
196+
[requestOperationMock stopMocking];
197+
[mockQueue stopMocking];
186198
}
187199
@end

Leanplum-SDK/Classes/Features/Actions/LPActionArg.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#import "LeanplumInternal.h"
99
#import "Utils.h"
1010
#import "LPVarCache.h"
11+
#import "LPCountAggregator.h"
1112

1213
@interface LPActionArg (PrivateProperties)
1314

@@ -35,6 +36,8 @@ + (LPActionArg *)argNamed:(NSString *)name with:(NSObject *)defaultValue kind:(N
3536
withDefaultValue:(NSString *) defaultValue];
3637
}
3738
LP_END_TRY
39+
40+
[[LPCountAggregator sharedAggregator] incrementCount:@"arg_named"];
3841
return arg;
3942
}
4043

Leanplum-SDK/Classes/Features/Actions/LPActionContext.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#import "LPVarCache.h"
1010
#import "LPFileManager.h"
1111
#import "Utils.h"
12+
#import "LPCountAggregator.h"
1213

1314
typedef void (^LPFileCallback)(NSString* value, NSString *defaultValue);
1415

@@ -26,6 +27,13 @@ @interface LPActionContext (PrivateProperties)
2627

2728
@end
2829

30+
@interface LPActionContext()
31+
32+
@property (nonatomic, strong) LPCountAggregator *countAggregator;
33+
34+
@end
35+
36+
2937
@implementation LPActionContext
3038

3139
@synthesize name=_name;
@@ -38,6 +46,7 @@ @implementation LPActionContext
3846
@synthesize key=_key;
3947
@synthesize preventRealtimeUpdating=_preventRealtimeUpdating;
4048
@synthesize contextualValues=_contextualValues;
49+
@synthesize countAggregator=_countAggregator;
4150

4251
+ (LPActionContext *)actionContextWithName:(NSString *)name
4352
args:(NSDictionary *)args
@@ -67,6 +76,7 @@ + (LPActionContext *)actionContextWithName:(NSString *)name
6776
context->_isRooted = YES;
6877
context->_isPreview = NO;
6978
context->_priority = priority;
79+
context->_countAggregator = [LPCountAggregator sharedAggregator];
7080
return context;
7181
}
7282

@@ -511,6 +521,8 @@ - (void)runActionNamed:(NSString *)name
511521
[Leanplum triggerAction:childContext];
512522
});
513523
LP_END_TRY
524+
525+
[self.countAggregator incrementCount:@"run_action_named"];
514526
}
515527

516528
- (void)runTrackedActionNamed:(NSString *)name
@@ -524,6 +536,8 @@ - (void)runTrackedActionNamed:(NSString *)name
524536
[self trackMessageEvent:name withValue:0.0 andInfo:nil andParameters:nil];
525537
}
526538
[self runActionNamed:name];
539+
540+
[self.countAggregator incrementCount:@"run_tracked_action_named"];
527541
}
528542

529543
- (void)trackMessageEvent:(NSString *)event withValue:(double)value andInfo:(NSString *)info

Leanplum-SDK/Classes/Features/Actions/LPActionManager.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#import "LPRequestFactory.h"
3636
#import "LPRequestSender.h"
3737
#import "LPAPIConfig.h"
38+
#import "LPCountAggregator.h"
3839

3940
#import <objc/runtime.h>
4041
#import <objc/message.h>
@@ -270,6 +271,7 @@ @interface LPActionManager()
270271
@property (nonatomic, strong) LeanplumShouldHandleNotificationBlock shouldHandleNotification;
271272
@property (nonatomic, strong) NSString *displayedTracked;
272273
@property (nonatomic, strong) NSDate *displayedTrackedTime;
274+
@property (nonatomic, strong) LPCountAggregator *countAggregator;
273275

274276
@end
275277

@@ -303,6 +305,7 @@ - (id)init
303305
_sessionOccurrences = [NSMutableDictionary dictionary];
304306
_messageImpressionOccurrences = [NSMutableDictionary dictionary];
305307
_messageTriggerOccurrences = [NSMutableDictionary dictionary];
308+
_countAggregator = [LPCountAggregator sharedAggregator];
306309
}
307310
return self;
308311
}
@@ -590,6 +593,8 @@ - (void)didReceiveRemoteNotification:(NSDictionary *)userInfo
590593
withAction:(NSString *)action
591594
fetchCompletionHandler:(LeanplumFetchCompletionBlock)completionHandler
592595
{
596+
[self.countAggregator incrementCount:@"did_receive_remote_notification"];
597+
593598
// If app was inactive, then handle notification because the user tapped it.
594599
if ([[UIApplication sharedApplication] applicationState] != UIApplicationStateActive) {
595600
[self handleNotification:userInfo
@@ -1217,6 +1222,8 @@ - (BOOL)matchesLimitTimes:(int)amount
12171222
- (void)recordMessageTrigger:(NSString *)messageId
12181223
{
12191224
[self incrementMessageTriggerOccurrences:messageId];
1225+
1226+
[self.countAggregator incrementCount:@"record_message_trigger"];
12201227
}
12211228

12221229
/**

Leanplum-SDK/Classes/Features/Actions/LPUIAlert.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#import "LPUIAlert.h"
2727
#import "Constants.h"
28+
#import "LPCountAggregator.h"
2829

2930
@implementation LPUIAlert
3031

@@ -84,6 +85,7 @@ + (void)showWithTitle:(NSString *)title
8485
[alertView show];
8586
#endif
8687
}
88+
[[LPCountAggregator sharedAggregator] incrementCount:@"show_With_title"];
8789
}
8890

8991
@end

Leanplum-SDK/Classes/Features/Events/LPEventCallback.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@
2525
#import "LPEventCallback.h"
2626
#import "LeanplumRequest.h"
2727
#import "LPResponse.h"
28+
#import "LPCountAggregator.h"
29+
30+
@interface LPEventCallback()
31+
32+
@property (strong, nonatomic) LPCountAggregator *countAggregator;
33+
34+
@end
2835

2936
@implementation LPEventCallback
3037

@@ -34,6 +41,7 @@ - (id)initWithResponseBlock:(LPNetworkResponseBlock)responseBlock
3441
if (self = [super init]) {
3542
self.responseBlock = [responseBlock copy];
3643
self.errorBlock = [errorBlock copy];
44+
_countAggregator = [LPCountAggregator sharedAggregator];
3745
}
3846
return self;
3947
}
@@ -45,6 +53,8 @@ - (void)invokeResponseWithOperation:(id<LPNetworkOperationProtocol>)operation
4553
return;
4654
}
4755

56+
[self.countAggregator incrementCount:@"invoke_response_with_operation"];
57+
4858
// Ensure all callbacks are on main thread.
4959
if (![NSThread isMainThread]) {
5060
dispatch_async(dispatch_get_main_queue(), ^{

Leanplum-SDK/Classes/Features/Events/LPEventCallbackManager.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#import "LeanplumRequest.h"
2727
#import "LPEventCallback.h"
2828
#import "LPResponse.h"
29+
#import "LPCountAggregator.h"
2930

3031
@implementation LPEventCallbackManager
3132

@@ -55,6 +56,7 @@ + (void)addEventCallbackAt:(NSInteger)index
5556
if (callbackMap && callback && atIndex) {
5657
callbackMap[atIndex] = callback;
5758
}
59+
[[LPCountAggregator sharedAggregator] incrementCount:@"add_event_callback_at"];
5860
}
5961

6062
+ (void)invokeSuccessCallbacksOnResponses:(id)responses
@@ -90,6 +92,7 @@ + (void)invokeSuccessCallbacksOnResponses:(id)responses
9092
id response = [LPResponse getResponseAt:index fromDictionary:responses];
9193
responseBlock(operation, response);
9294
}];
95+
[[LPCountAggregator sharedAggregator] incrementCount:@"invoke_success_callbacks_on_responses"];
9396
}
9497

9598
+ (void)invokeErrorCallbacksOnResponses:(id)responses
@@ -117,6 +120,7 @@ + (void)invokeErrorCallbacksOnResponses:(id)responses
117120
[callback invokeError:error];
118121
}
119122
}
123+
[[LPCountAggregator sharedAggregator] incrementCount:@"invoke_error_callbacks_on_responses"];
120124
}
121125

122126
+ (void)invokeErrorCallbacksWithError:(NSError *)error

Leanplum-SDK/Classes/Features/Events/LPEventDataManager.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#import "LPEventDataManager.h"
2626
#import "LPDatabase.h"
2727
#import "LPJSON.h"
28+
#import "LPCountAggregator.h"
2829

2930
@implementation LPEventDataManager
3031

@@ -33,6 +34,8 @@ + (void)addEvent:(NSDictionary *)event
3334
NSString *query = @"INSERT INTO event (data) VALUES (?);";
3435
NSArray *objectsToBind = @[[LPJSON stringFromJSON:event]];
3536
[[LPDatabase sharedDatabase] runQuery:query bindObjects:objectsToBind];
37+
38+
[[LPCountAggregator sharedAggregator] incrementCount:@"add_event"];
3639
}
3740

3841
+ (void)addEvents:(NSArray *)events
@@ -52,6 +55,8 @@ + (void)addEvents:(NSArray *)events
5255
[objectsToBind addObject:dataString];
5356
}];
5457
[[LPDatabase sharedDatabase] runQuery:query bindObjects:objectsToBind];
58+
59+
[[LPCountAggregator sharedAggregator] incrementCount:@"add_events"];
5560
}
5661

5762
+ (NSArray *)eventsWithLimit:(NSInteger)limit
@@ -70,6 +75,8 @@ + (NSArray *)eventsWithLimit:(NSInteger)limit
7075
[events addObject:[event mutableCopy]];
7176
}
7277

78+
[[LPCountAggregator sharedAggregator] incrementCount:@"events_with_limit"];
79+
7380
return events;
7481
}
7582

@@ -82,6 +89,8 @@ + (void)deleteEventsWithLimit:(NSInteger)limit
8289
"(SELECT rowid FROM event ORDER BY rowid "
8390
"LIMIT %ld);", (long)limit];
8491
[[LPDatabase sharedDatabase] runQuery:query];
92+
93+
[[LPCountAggregator sharedAggregator] incrementCount:@"delete_events_with_limit"];
8594
}
8695

8796
+ (NSInteger)count

Leanplum-SDK/Classes/Features/Inbox/LPInbox.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#import "Utils.h"
3535
#import "LPRequestFactory.h"
3636
#import "LPRequestSender.h"
37+
#import "LPCountAggregator.h"
3738

3839
static NSObject *updatingLock;
3940

@@ -260,6 +261,7 @@ + (LPInbox *)sharedState {
260261
- (id)init {
261262
if (self = [super init]) {
262263
[self reset];
264+
_countAggregator = [LPCountAggregator sharedAggregator];
263265
}
264266
return self;
265267
}
@@ -508,6 +510,9 @@ - (NSArray *)allMessages
508510
[messages addObject:[self messageForId:messageId]];
509511
}
510512
LP_END_TRY
513+
514+
[self.countAggregator incrementCount:@"all_messages_inbox"];
515+
511516
return messages;
512517
}
513518

0 commit comments

Comments
 (0)