forked from singular-labs/segment-singular-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingularIntegation.m
98 lines (75 loc) · 3.29 KB
/
SingularIntegation.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
// SingularIntegation.m
// Segment-Singular-iOS
//
// Created by Eyal Rabinovich on 29/05/2019.
// Copyright © 2019 Singular Labs. All rights reserved.
//
#import "SingularIntegation.h"
#import <Segment/SEGAnalyticsConfiguration.h>
#import <Segment/SEGAnalytics.h>
#import "Singular.h"
#import "SingularConfig.h"
#define SEGMENT_WRAPPER_NAME @"Segment"
#define SEGMENT_WRAPPER_VERSION @"1.2.0"
#define SEGMENT_REVENUE_KEY @"revenue"
#define SEGMENT_CURRENCY_KEY @"currency"
#define DEFAULT_CURRENCY @"USD"
@implementation SingularIntegation
static bool isSKANEnabled = NO;
static bool isManualMode = NO;
static void(^conversionValueUpdatedCallback)(NSInteger);
static int waitForTrackingAuthorizationWithTimeoutInterval = 0;
static bool isInitialized = NO;
- (instancetype)initWithSettings:(NSDictionary *)settings {
self = [super init];
if (!self) {
return self;
}
[Singular setWrapperName:SEGMENT_WRAPPER_NAME andVersion:SEGMENT_WRAPPER_VERSION];
NSString* apiKey = [settings objectForKey:@"apiKey"];
NSString* secret = [settings objectForKey:@"secret"];
if (!apiKey || !secret){
return nil;
}
SingularConfig* config = [[SingularConfig alloc] initWithApiKey:apiKey andSecret:secret];
config.skAdNetworkEnabled = isSKANEnabled;
config.manualSkanConversionManagement = isManualMode;
config.conversionValueUpdatedCallback = conversionValueUpdatedCallback;
config.waitForTrackingAuthorizationWithTimeoutInterval = waitForTrackingAuthorizationWithTimeoutInterval;
[Singular start:config];
isInitialized = YES;
return self;
}
-(void)track:(SEGTrackPayload *)payload{
if([[payload properties] objectForKey:SEGMENT_REVENUE_KEY] ||
[[[payload properties] objectForKey:SEGMENT_REVENUE_KEY] doubleValue] != 0) {
double revenue = [[[payload properties] objectForKey:SEGMENT_REVENUE_KEY] doubleValue];
NSString* currency = DEFAULT_CURRENCY;
if([[payload properties] objectForKey:SEGMENT_CURRENCY_KEY] &&
[[[payload properties] objectForKey:SEGMENT_CURRENCY_KEY] length] > 0){
currency = [[payload properties] objectForKey:SEGMENT_CURRENCY_KEY];
}
[Singular customRevenue:[payload event] currency:currency amount:revenue];
} else {
[Singular event:[payload event]];
}
}
-(void)identify:(SEGIdentifyPayload *)payload{
if([payload userId] && [[payload userId] length] > 0){
[Singular setCustomUserId:[payload userId]];
}
}
- (void)reset{
[Singular unsetCustomUserId];
}
+ (void)setSKANOptions:(BOOL)skAdNetworkEnabled isManualSkanConversionManagementMode:(BOOL)manualMode withWaitForTrackingAuthorizationWithTimeoutInterval:(NSNumber* _Nullable)waitTrackingAuthorizationWithTimeoutInterval withConversionValueUpdatedHandler:(void(^_Nullable)(NSInteger))conversionValueUpdatedHandler {
if (isInitialized) {
NSLog(@"Singular Warning: setSKANOptions should be called before init");
}
isSKANEnabled = skAdNetworkEnabled;
isManualMode = manualMode;
conversionValueUpdatedCallback = conversionValueUpdatedHandler;
waitForTrackingAuthorizationWithTimeoutInterval = waitTrackingAuthorizationWithTimeoutInterval ? [waitTrackingAuthorizationWithTimeoutInterval intValue] : 0;
}
@end