-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathOSChannelTracker.m
More file actions
168 lines (138 loc) · 7.25 KB
/
OSChannelTracker.m
File metadata and controls
168 lines (138 loc) · 7.25 KB
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
/**
Modified MIT License
Copyright 2020 OneSignal
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
1. The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2. All copies of substantial portions of the Software may only be used in connection
with services provided by OneSignal.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#import <Foundation/Foundation.h>
#import <OneSignalCore/OneSignalCore.h>
#import "OSMacros.h"
#import "OSChannelTracker.h"
#import "OSIndirectInfluence.h"
@implementation OSChannelTracker
- (id)initWithRepository:(OSInfluenceDataRepository *)dataRepository {
self = [super init];
if (self) {
_dataRepository = dataRepository;
}
return self;
}
/*
Start Methods implemented by childrens
*/
- (NSString * _Nonnull)idTag { mustOverride(); }
- (OSInfluenceChannel)channelType { mustOverride(); }
- (NSArray * _Nullable)lastChannelObjectsReceivedByNewId:(NSString *)identifier { mustOverride(); }
- (NSArray * _Nullable)lastChannelObjects { mustOverride(); }
- (NSInteger)channelLimit { mustOverride(); }
- (NSInteger)indirectAttributionWindow { mustOverride(); }
- (void)saveChannelObjects:(NSArray * _Nonnull)channelObjects { mustOverride(); }
- (void)initInfluencedTypeFromCache { mustOverride(); }
- (void)cacheState { mustOverride(); }
/*
End Methods implemented by childrens
*/
- (BOOL)isDirectSessionEnabled {
return [self.dataRepository isDirectInfluenceEnabled];
}
- (BOOL)isIndirectSessionEnabled {
return [self.dataRepository isIndirectInfluenceEnabled];
}
- (BOOL)isUnattributedSessionEnabled {
return [self.dataRepository isUnattributedInfluenceEnabled];
}
- (void)resetAndInitInfluence {
_directId = nil;
_indirectIds = [self lastReceivedIds];
_influenceType = _indirectIds != nil && _indirectIds.count > 0 ? INDIRECT : UNATTRIBUTED;
[self cacheState];
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:[NSString stringWithFormat:@"OSChannelTracker resetAndInitInfluence for: %@ finish with influenceType: %@", [self idTag], OS_INFLUENCE_TYPE_TO_STRING(_influenceType)]];
}
- (NSArray * _Nonnull)lastReceivedIds {
NSMutableArray *ids = [NSMutableArray new];
NSArray *lastChannelObjectReceived = [self lastChannelObjects];
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:[NSString stringWithFormat:@"OSChannelTracker for: %@ lastChannelObjectReceived: %@", [self idTag], lastChannelObjectReceived]];
if (!lastChannelObjectReceived || lastChannelObjectReceived.count == 0)
return ids; // Unattributed session
NSInteger attributionWindowInSeconds = [self indirectAttributionWindow];
NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
// Add only valid indirectInfluences within the attribution window
for (var i = 0; i < lastChannelObjectReceived.count; i++) {
OSIndirectInfluence* indirectInfluence = [lastChannelObjectReceived objectAtIndex:i];
long difference = currentTime - indirectInfluence.timestamp;
if (difference <= attributionWindowInSeconds)
[ids addObject:indirectInfluence.influenceId];
}
return ids;
}
- (void)saveLastId:(NSString *)lastId {
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:[NSString stringWithFormat:@"OSChannelTracker for: %@ saveLastId id: %@", [self idTag], lastId]];
let channelLimit = [self channelLimit];
let lastChannelObjectsReceived = [self lastChannelObjectsReceivedByNewId:lastId];
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:[NSString stringWithFormat:@"OSChannelTracker for: %@ saveLastId with lastChannelObjectReceived: %@", [self idTag], lastChannelObjectsReceived]];
let timestamp = [NSDate date].timeIntervalSince1970;
let indirectInfluence = [[OSIndirectInfluence alloc] initWithParamsInfluenceId:lastId forChannel:[self idTag] timestamp:timestamp];
// Create channelObjectToSave to be saved at a limited size, removing any old notifications
NSArray *channelObjectToSave;
if (!lastChannelObjectsReceived || lastChannelObjectsReceived.count == 0) {
channelObjectToSave = [NSArray arrayWithObject:indirectInfluence];
} else if (lastChannelObjectsReceived.count < channelLimit) {
NSMutableArray *lastChannelObjectsReceivedMutable = [lastChannelObjectsReceived mutableCopy];
[lastChannelObjectsReceivedMutable addObject:indirectInfluence];
channelObjectToSave = lastChannelObjectsReceivedMutable;
} else {
NSMutableArray *lastChannelObjectsReceivedMutable = [lastChannelObjectsReceived mutableCopy];
[lastChannelObjectsReceivedMutable addObject:indirectInfluence];
// Remove old indirectInfluences to keep final indirectInfluence at a limited size
let lengthDifference = lastChannelObjectsReceivedMutable.count - channelLimit;
for (int i = 0; i < lengthDifference; i++)
[lastChannelObjectsReceivedMutable removeObjectAtIndex:0];
channelObjectToSave = lastChannelObjectsReceivedMutable;
}
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:[NSString stringWithFormat:@"OSChannelTracker for: %@ with channelObjectToSave: %@", [self idTag], channelObjectToSave]];
[self saveChannelObjects:channelObjectToSave];
}
- (OSInfluence *)currentSessionInfluence {
OSInfluenceBuilder *builder = [OSInfluenceBuilder new];
builder.influenceType = DISABLED;
builder.influenceChannel = [self channelType];
if (_influenceType == DIRECT) {
if ([self isDirectSessionEnabled]) {
if (_directId) {
NSArray *ids = [NSArray arrayWithObject:_directId];
builder.ids = ids;
builder.influenceType = DIRECT;
} else {
[OneSignalLog onesignalLog:ONE_S_LL_ERROR message:@"OSChannelTracker:currentSessionInfluence found a direct influence without a direct id."];
}
}
} else if (_influenceType == INDIRECT) {
if ([self isIndirectSessionEnabled]) {
builder.ids = _indirectIds;
builder.influenceType = INDIRECT;
}
} else if ([self isUnattributedSessionEnabled]) {
builder.influenceType = UNATTRIBUTED;
}
return [[OSInfluence alloc] initWithBuilder:builder];
}
- (NSString *)description {
return [NSString stringWithFormat:
@"OSChannelTracker tag: %@ influenceType: %@ indirectIds: %@ directIds: %@", [self idTag], OS_INFLUENCE_TYPE_TO_STRING(_influenceType), _indirectIds, _directId];
}
@end