Skip to content

Commit b18269c

Browse files
authored
E2-10023 add trackGeofence (#237)
* add trackGeofence * add test * make trackGeofence event enum * enumConstants m and h * add license * sendIfConnected instead of send
1 parent 074c165 commit b18269c

File tree

7 files changed

+166
-15
lines changed

7 files changed

+166
-15
lines changed

Example/Tests/Classes/LeanplumTest.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#import "LPFeatureFlagManager.h"
3838
#import "Constants.h"
3939
#import "LPRegisterDevice.h"
40+
#import "Leanplum.h"
4041

4142
/**
4243
* Tests leanplum public methods, we seed predefined response that comes from backend
@@ -49,6 +50,8 @@ @interface Leanplum (Test)
4950
+ (NSSet<NSString *> *)parseEnabledFeatureFlagsFromResponse:(NSDictionary *)response;
5051
+ (void)triggerMessageDisplayed:(LPActionContext *)context;
5152

53+
+ (void)trackGeofence:(LPGeofenceEventType *)event withValue:(double)value andInfo:(NSString *)info andArgs:(NSDictionary *)args andParameters:(NSDictionary *)params;
54+
5255
@end
5356

5457
@interface LeanplumTest : XCTestCase
@@ -888,6 +891,19 @@ - (void) test_track
888891
andCurrencyCode:@"USD"
889892
andParameters:trackParams];
890893
[Leanplum forceContentUpdate];
894+
895+
// Validate track geofence with info request.
896+
[LeanplumRequest validate_request:^BOOL(NSString *method, NSString *apiMethod,
897+
NSDictionary *params) {
898+
// Check api method first.
899+
XCTAssertEqualObjects(apiMethod, @"trackGeofence");
900+
// Check if request has all params.
901+
XCTAssertTrue([params[@"event"] isEqualToString:@"enter_region"]);
902+
XCTAssertTrue([params[@"info"] isEqualToString:trackInfo]);
903+
return YES;
904+
}];
905+
[Leanplum trackGeofence:LPEnterRegion withValue:0.0 andInfo:trackInfo andArgs:nil andParameters:nil];
906+
[Leanplum forceContentUpdate];
891907

892908
XCTAssertTrue([Leanplum hasStarted]);
893909
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// EnumConstants.h
3+
// Leanplum-SDK
4+
//
5+
// Created by Grace Gu on 12/19/18.
6+
// Copyright © 2018 Leanplum. All rights reserved.
7+
//
8+
// Licensed to the Apache Software Foundation (ASF) under one
9+
// or more contributor license agreements. See the NOTICE file
10+
// distributed with this work for additional information
11+
// regarding copyright ownership. The ASF licenses this file
12+
// to you under the Apache License, Version 2.0 (the “License”);
13+
// you may not use this file except in compliance with the License.
14+
// You may obtain a copy of the License at
15+
//
16+
// http://www.apache.org/licenses/LICENSE-2.0
17+
//
18+
// Unless required by applicable law or agreed to in writing,
19+
// software distributed under the License is distributed on an
20+
// “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21+
// KIND, either express or implied. See the License for the
22+
// specific language governing permissions and limitations
23+
// under the License.
24+
25+
26+
#ifndef EnumConstants_h
27+
#define EnumConstants_h
28+
29+
@interface LPEnumConstants : NSObject {}
30+
31+
typedef enum {
32+
LPEnterRegion,
33+
LPExitRegion
34+
} LPGeofenceEventType;
35+
36+
37+
+ (NSString *)getEventNameFromGeofenceType:(LPGeofenceEventType)event;
38+
@end
39+
40+
#endif /* EnumConstants_h */
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// EnumConstants.m
3+
// Leanplum-SDK
4+
//
5+
// Created by Grace Gu on 12/19/18.
6+
// Copyright © 2018 Leanplum. All rights reserved.
7+
//
8+
// Licensed to the Apache Software Foundation (ASF) under one
9+
// or more contributor license agreements. See the NOTICE file
10+
// distributed with this work for additional information
11+
// regarding copyright ownership. The ASF licenses this file
12+
// to you under the Apache License, Version 2.0 (the “License”);
13+
// you may not use this file except in compliance with the License.
14+
// You may obtain a copy of the License at
15+
//
16+
// http://www.apache.org/licenses/LICENSE-2.0
17+
//
18+
// Unless required by applicable law or agreed to in writing,
19+
// software distributed under the License is distributed on an
20+
// “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21+
// KIND, either express or implied. See the License for the
22+
// specific language governing permissions and limitations
23+
// under the License.
24+
25+
#import "EnumConstants.h"
26+
27+
@implementation LPEnumConstants
28+
29+
+ (NSString *) getEventNameFromGeofenceType:(LPGeofenceEventType)event {
30+
NSString *result = nil;
31+
32+
switch(event) {
33+
case LPEnterRegion:
34+
result = @"enter_region";
35+
break;
36+
case LPExitRegion:
37+
result = @"exit_region";
38+
break;
39+
default:
40+
[NSException raise:NSGenericException format:@"Unexpected geofenceEventType."];
41+
}
42+
43+
return result;
44+
}
45+
46+
@end

Leanplum-SDK/Classes/Internal/Leanplum.m

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,23 +1915,10 @@ + (void)trackInAppPurchase:(SKPaymentTransaction *)transaction
19151915
LP_END_TRY
19161916
}
19171917

1918-
+ (void)track:(NSString *)event withValue:(double)value andInfo:(NSString *)info
1919-
andArgs:(NSDictionary *)args andParameters:(NSDictionary *)params
1920-
{
1921-
RETURN_IF_NOOP;
1922-
LP_TRY
1923-
1924-
// Track should not be called in background.
1925-
if (![NSThread isMainThread]) {
1926-
dispatch_sync(dispatch_get_main_queue(), ^{
1927-
[self track:event withValue:value andInfo:info andArgs:args andParameters:params];
1928-
});
1929-
return;
1930-
}
1931-
1918+
+ (NSMutableDictionary *)makeTrackArgs:(NSString *)event withValue:(double)value andInfo: (NSString *)info andArgs:(NSDictionary *)args andParameters:(NSDictionary *)params {
19321919
NSString *valueStr = [NSString stringWithFormat:@"%f", value];
19331920
NSMutableDictionary *arguments = [NSMutableDictionary dictionaryWithObjectsAndKeys:
1934-
valueStr, LP_PARAM_VALUE, nil];
1921+
valueStr, LP_PARAM_VALUE, nil];
19351922
if (args) {
19361923
[arguments addEntriesFromDictionary:args];
19371924
}
@@ -1948,7 +1935,25 @@ + (void)track:(NSString *)event withValue:(double)value andInfo:(NSString *)info
19481935
if ([[UIApplication sharedApplication] applicationState] != UIApplicationStateActive) {
19491936
arguments[@"allowOffline"] = @YES;
19501937
}
1938+
return arguments;
1939+
}
19511940

1941+
+ (void)track:(NSString *)event withValue:(double)value andInfo:(NSString *)info
1942+
andArgs:(NSDictionary *)args andParameters:(NSDictionary *)params
1943+
{
1944+
RETURN_IF_NOOP;
1945+
LP_TRY
1946+
1947+
// Track should not be called in background.
1948+
if (![NSThread isMainThread]) {
1949+
dispatch_sync(dispatch_get_main_queue(), ^{
1950+
[self track:event withValue:value andInfo:info andArgs:args andParameters:params];
1951+
});
1952+
return;
1953+
}
1954+
1955+
NSMutableDictionary *arguments = [self makeTrackArgs:event withValue:value andInfo:info andArgs:args andParameters:params];
1956+
19521957
[self onStartIssued:^{
19531958
[self trackInternal:event withArgs:arguments andParameters:params];
19541959
}];
@@ -1957,6 +1962,39 @@ + (void)track:(NSString *)event withValue:(double)value andInfo:(NSString *)info
19571962
[[LPCountAggregator sharedAggregator] incrementCount:@"track"];
19581963
}
19591964

1965+
+ (void)trackGeofence:(LPGeofenceEventType)event withInfo:(NSString *)info {
1966+
if ([[LPFeatureFlagManager sharedManager] isFeatureFlagEnabled:@"track_geofence"]) {
1967+
[self trackGeofence:event withValue:0.0 andInfo:info andArgs:nil andParameters:nil];
1968+
} else {
1969+
[[LPCountAggregator sharedAggregator] incrementCount:@"track_geofence_disabled"];
1970+
}
1971+
}
1972+
1973+
+ (void)trackGeofence:(LPGeofenceEventType)event withValue:(double)value andInfo:(NSString *)info
1974+
andArgs:(NSDictionary *)args andParameters:(NSDictionary *)params
1975+
{
1976+
RETURN_IF_NOOP;
1977+
LP_TRY
1978+
1979+
// TrackGeofence should not be called in background.
1980+
if (![NSThread isMainThread]) {
1981+
dispatch_sync(dispatch_get_main_queue(), ^{
1982+
[self trackGeofence:event withValue:value andInfo:info andArgs:args andParameters:params];
1983+
});
1984+
return;
1985+
}
1986+
1987+
NSString *eventName = [LPEnumConstants getEventNameFromGeofenceType:event];
1988+
1989+
NSMutableDictionary *arguments = [self makeTrackArgs:eventName withValue:value andInfo:info andArgs:args andParameters:params];
1990+
1991+
LPRequestFactory *reqFactory = [[LPRequestFactory alloc]
1992+
initWithFeatureFlagManager:[LPFeatureFlagManager sharedManager]];
1993+
id<LPRequesting> request = [reqFactory trackGeofenceWithParams:arguments];
1994+
[[LPRequestSender sharedInstance] sendIfConnected:request];
1995+
LP_END_TRY
1996+
}
1997+
19601998
+ (void)trackInternal:(NSString *)event withArgs:(NSDictionary *)args
19611999
andParameters:(NSDictionary *)params
19622000
{

Leanplum-SDK/Classes/Leanplum.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#import "LPActionContext.h"
2929
#import "LPVar.h"
3030
#import "LPMessageArchiveData.h"
31+
#import "EnumConstants.h"
3132

3233
#ifndef LP_NOT_TV
3334
#if (!defined(TARGET_OS_TV) || !TARGET_OS_TV)
@@ -610,6 +611,8 @@ typedef NS_ENUM(NSUInteger, LPTrackScreenMode) {
610611
+ (void)track:(NSString *)event withValue:(double)value andInfo:(NSString *)info andParameters:(NSDictionary *)params;
611612
/**@}*/
612613

614+
+ (void)trackGeofence:(LPGeofenceEventType)event withInfo:(NSString *)info;
615+
613616
/**
614617
* @{
615618
* Gets the path for a particular resource. The resource can be overridden by the server.

Leanplum-SDK/Classes/Managers/Networking/LPRequestFactory.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ NS_ASSUME_NONNULL_BEGIN
3838
- (id<LPRequesting>)stopWithParams:(nullable NSDictionary *)params;
3939
- (id<LPRequesting>)restartWithParams:(nullable NSDictionary *)params;
4040
- (id<LPRequesting>)trackWithParams:(nullable NSDictionary *)params;
41+
- (id<LPRequesting>)trackGeofenceWithParams:(nullable NSDictionary *)params;
4142
- (id<LPRequesting>)advanceWithParams:(nullable NSDictionary *)params;
4243
- (id<LPRequesting>)pauseSessionWithParams:(nullable NSDictionary *)params;
4344
- (id<LPRequesting>)pauseStateWithParams:(nullable NSDictionary *)params;

Leanplum-SDK/Classes/Managers/Networking/LPRequestFactory.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
NSString *LP_API_METHOD_STOP = @"stop";
3434
NSString *LP_API_METHOD_RESTART = @"restart";
3535
NSString *LP_API_METHOD_TRACK = @"track";
36+
NSString *LP_API_METHOD_TRACK_GEOFENCE = @"trackGeofence";
3637
NSString *LP_API_METHOD_ADVANCE = @"advance";
3738
NSString *LP_API_METHOD_PAUSE_SESSION = @"pauseSession";
3839
NSString *LP_API_METHOD_PAUSE_STATE = @"pauseState";
@@ -104,6 +105,12 @@ -(instancetype)initWithFeatureFlagManager:(LPFeatureFlagManager *)featureFlagMan
104105
[self.countAggregator incrementCount:@"track_with_params"];
105106
return [self createPostForApiMethod:LP_API_METHOD_TRACK params:params];
106107
}
108+
109+
- (id<LPRequesting>)trackGeofenceWithParams:(NSDictionary *)params;
110+
{
111+
[self.countAggregator incrementCount:@"track_geofence_with_params"];
112+
return [self createPostForApiMethod:LP_API_METHOD_TRACK_GEOFENCE params:params];
113+
}
107114
- (id<LPRequesting>)advanceWithParams:(NSDictionary *)params;
108115
{
109116
[self.countAggregator incrementCount:@"advance_with_params"];

0 commit comments

Comments
 (0)