-
Notifications
You must be signed in to change notification settings - Fork 299
/
Copy pathADJAttributionHandler.m
276 lines (222 loc) · 9.74 KB
/
ADJAttributionHandler.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//
// ADJAttributionHandler.m
// adjust
//
// Created by Pedro Filipe on 29/10/14.
// Copyright (c) 2014 adjust GmbH. All rights reserved.
//
#import "ADJAttributionHandler.h"
#import "ADJAdjustFactory.h"
#import "ADJUtil.h"
#import "ADJActivityHandler.h"
#import "ADJAdditions.h"
#import "ADJTimerOnce.h"
#import "ADJPackageBuilder.h"
#import "ADJUtil.h"
static const char * const kInternalQueueName = "com.adjust.AttributionQueue";
static NSString * const kAttributionTimerName = @"Attribution timer";
@interface ADJAttributionHandler()
@property (nonatomic, strong) dispatch_queue_t internalQueue;
@property (nonatomic, strong) ADJRequestHandler *requestHandler;
@property (nonatomic, weak) id<ADJActivityHandler> activityHandler;
@property (nonatomic, weak) id<ADJLogger> logger;
@property (nonatomic, strong) ADJTimerOnce *attributionTimer;
@property (atomic, assign) BOOL paused;
@property (nonatomic, copy) NSString *lastInitiatedBy;
@end
@implementation ADJAttributionHandler
- (id)initWithActivityHandler:(id<ADJActivityHandler>) activityHandler
startsSending:(BOOL)startsSending
urlStrategy:(ADJUrlStrategy *)urlStrategy
{
self = [super init];
if (self == nil) return nil;
self.internalQueue = dispatch_queue_create(kInternalQueueName, DISPATCH_QUEUE_SERIAL);
self.requestHandler = [[ADJRequestHandler alloc]
initWithResponseCallback:self
urlStrategy:urlStrategy
requestTimeout:[ADJAdjustFactory requestTimeout]];
self.activityHandler = activityHandler;
self.logger = ADJAdjustFactory.logger;
self.paused = !startsSending;
__weak __typeof__(self) weakSelf = self;
self.attributionTimer = [ADJTimerOnce timerWithBlock:^{
__typeof__(self) strongSelf = weakSelf;
if (strongSelf == nil) return;
[strongSelf requestAttributionI:strongSelf];
}
queue:self.internalQueue
name:kAttributionTimerName];
return self;
}
- (void)checkSessionResponse:(ADJSessionResponseData *)sessionResponseData {
[ADJUtil launchInQueue:self.internalQueue
selfInject:self
block:^(ADJAttributionHandler* selfI) {
[selfI checkSessionResponseI:selfI
sessionResponseData:sessionResponseData];
}];
}
- (void)checkSdkClickResponse:(ADJSdkClickResponseData *)sdkClickResponseData {
[ADJUtil launchInQueue:self.internalQueue
selfInject:self
block:^(ADJAttributionHandler* selfI) {
[selfI checkSdkClickResponseI:selfI
sdkClickResponseData:sdkClickResponseData];
}];
}
- (void)checkAttributionResponse:(ADJAttributionResponseData *)attributionResponseData {
[ADJUtil launchInQueue:self.internalQueue
selfInject:self
block:^(ADJAttributionHandler* selfI) {
[selfI checkAttributionResponseI:selfI
attributionResponseData:attributionResponseData];
}];
}
- (void)getAttribution {
[ADJUtil launchInQueue:self.internalQueue
selfInject:self
block:^(ADJAttributionHandler* selfI) {
selfI.lastInitiatedBy = @"sdk";
[selfI waitRequestAttributionWithDelayI:selfI
milliSecondsDelay:0];
}];
}
- (void)pauseSending {
[ADJUtil launchInQueue:self.internalQueue
selfInject:self
block:^(ADJAttributionHandler* selfI) {
selfI.paused = YES;
}];
}
- (void)resumeSending {
[ADJUtil launchInQueue:self.internalQueue
selfInject:self
block:^(ADJAttributionHandler* selfI) {
selfI.paused = NO;
}];
}
#pragma mark - internal
- (void)checkSessionResponseI:(ADJAttributionHandler*)selfI
sessionResponseData:(ADJSessionResponseData *)sessionResponseData {
[selfI checkAttributionI:selfI responseData:sessionResponseData];
[selfI.activityHandler launchSessionResponseTasks:sessionResponseData];
}
- (void)checkSdkClickResponseI:(ADJAttributionHandler*)selfI
sdkClickResponseData:(ADJSdkClickResponseData *)sdkClickResponseData {
[selfI checkAttributionI:selfI responseData:sdkClickResponseData];
[selfI.activityHandler launchSdkClickResponseTasks:sdkClickResponseData];
}
- (void)checkAttributionResponseI:(ADJAttributionHandler*)selfI
attributionResponseData:(ADJAttributionResponseData *)attributionResponseData {
[selfI checkAttributionI:selfI responseData:attributionResponseData];
[selfI checkDeeplinkI:selfI attributionResponseData:attributionResponseData];
[selfI.activityHandler launchAttributionResponseTasks:attributionResponseData];
}
- (void)checkAttributionI:(ADJAttributionHandler*)selfI
responseData:(ADJResponseData *)responseData {
if (responseData.jsonResponse == nil) {
return;
}
NSNumber *timerMilliseconds = [responseData.jsonResponse objectForKey:@"ask_in"];
if (timerMilliseconds != nil) {
[selfI.activityHandler setAskingAttribution:YES];
selfI.lastInitiatedBy = @"backend";
[selfI waitRequestAttributionWithDelayI:selfI
milliSecondsDelay:[timerMilliseconds intValue]];
return;
}
[selfI.activityHandler setAskingAttribution:NO];
NSDictionary *jsonAttribution = [responseData.jsonResponse objectForKey:@"attribution"];
responseData.attribution = [[ADJAttribution alloc] initWithJsonDict:jsonAttribution];
}
- (void)checkDeeplinkI:(ADJAttributionHandler*)selfI
attributionResponseData:(ADJAttributionResponseData *)attributionResponseData {
if (attributionResponseData.jsonResponse == nil) {
return;
}
NSDictionary * jsonAttribution = [attributionResponseData.jsonResponse objectForKey:@"attribution"];
if (jsonAttribution == nil) {
return;
}
NSString *deepLink = [jsonAttribution objectForKey:@"deeplink"];
if (deepLink == nil) {
return;
}
attributionResponseData.deeplink = [NSURL URLWithString:deepLink];
}
- (void)requestAttributionI:(ADJAttributionHandler*)selfI {
if (selfI.paused) {
[selfI.logger debug:@"Attribution handler is paused"];
return;
}
if ([selfI.activityHandler isGdprForgotten]) {
[selfI.logger debug:@"Attribution request won't be fired for forgotten user"];
return;
}
ADJActivityPackage* attributionPackage = [selfI buildAndGetAttributionPackageI:selfI];
[selfI.logger verbose:@"%@", attributionPackage.extendedString];
NSDictionary *sendingParameters = @{
@"sent_at": [ADJUtil formatSeconds1970:[NSDate.date timeIntervalSince1970]]
};
[selfI.requestHandler sendPackageByGET:attributionPackage
sendingParameters:sendingParameters];
}
- (void)responseCallback:(ADJResponseData *)responseData {
if (responseData.jsonResponse) {
[self.logger debug:
@"Got attribution JSON response with message: %@", responseData.message];
} else {
[self.logger error:
@"Could not get attribution JSON response with message: %@", responseData.message];
}
// Check if any package response contains information that user has opted out.
// If yes, disable SDK and flush any potentially stored packages that happened afterwards.
if (responseData.trackingState == ADJTrackingStateOptedOut) {
[self.activityHandler setTrackingStateOptedOut];
return;
}
if ([responseData isKindOfClass:[ADJAttributionResponseData class]]) {
[self checkAttributionResponse:(ADJAttributionResponseData*)responseData];
}
}
- (void)waitRequestAttributionWithDelayI:(ADJAttributionHandler*)selfI
milliSecondsDelay:(int)milliSecondsDelay {
NSTimeInterval secondsDelay = milliSecondsDelay / 1000;
NSTimeInterval nextAskIn = [selfI.attributionTimer fireIn];
if (nextAskIn > secondsDelay) {
return;
}
if (milliSecondsDelay > 0) {
[selfI.logger debug:@"Waiting to query attribution in %d milliseconds", milliSecondsDelay];
}
// set the new time the timer will fire in
[selfI.attributionTimer startIn:secondsDelay];
}
- (ADJActivityPackage *)buildAndGetAttributionPackageI:(ADJAttributionHandler*)selfI
{
double now = [NSDate.date timeIntervalSince1970];
ADJPackageBuilder *attributionBuilder = [[ADJPackageBuilder alloc]
initWithPackageParams:selfI.activityHandler.packageParams
activityState:selfI.activityHandler.activityState
config:selfI.activityHandler.adjustConfig
globalParameters:selfI.activityHandler.globalParameters
trackingStatusManager:selfI.activityHandler.trackingStatusManager
createdAt:now];
ADJActivityPackage *attributionPackage = [attributionBuilder buildAttributionPackage:selfI.lastInitiatedBy];
selfI.lastInitiatedBy = nil;
return attributionPackage;
}
#pragma mark - private
- (void)teardown {
[ADJAdjustFactory.logger verbose:@"ADJAttributionHandler teardown"];
if (self.attributionTimer != nil) {
[self.attributionTimer cancel];
}
self.internalQueue = nil;
self.activityHandler = nil;
self.logger = nil;
self.attributionTimer = nil;
self.requestHandler = nil;
}
@end