forked from segmentio/analytics-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdjustDestination.swift
187 lines (157 loc) · 6.66 KB
/
AdjustDestination.swift
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
//
// AdjustDestination.swift
// DestinationsExample
//
// Created by Brandon Sneed on 5/27/21.
//
// NOTE: You can see this plugin in use in the DestinationsExample application.
//
// This plugin is NOT SUPPORTED by Segment. It is here merely as an example,
// and for your convenience should you find it useful.
//
// Adjust SPM package can be found here: https://github.com/adjust/ios_sdk
// MIT License
//
// Copyright (c) 2021 Segment
//
// 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:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// 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
import Segment
import Adjust
@objc
class AdjustDestination: NSObject, DestinationPlugin, RemoteNotifications {
let timeline = Timeline()
let type = PluginType.destination
let key = "Adjust"
var analytics: Analytics? = nil
private var settings: AdjustSettings? = nil
public func update(settings: Settings, type: UpdateType) {
// we've already set up this singleton SDK, can't do it again, so skip.
guard type == .initial else { return }
guard let settings: AdjustSettings = settings.integrationSettings(forPlugin: self) else { return }
self.settings = settings
var environment = ADJEnvironmentSandbox
if let _ = settings.setEnvironmentProduction {
environment = ADJEnvironmentProduction
}
let adjustConfig = ADJConfig(appToken: settings.appToken, environment: environment)
if let bufferingEnabled = settings.setEventBufferingEnabled {
adjustConfig?.eventBufferingEnabled = bufferingEnabled
}
if let _ = settings.trackAttributionData {
adjustConfig?.delegate = self
}
if let useDelay = settings.setDelay, useDelay == true, let delayTime = settings.delayTime {
adjustConfig?.delayStart = delayTime
}
Adjust.appDidLaunch(adjustConfig)
}
public func identify(event: IdentifyEvent) -> IdentifyEvent? {
if let userId = event.userId, userId.count > 0 {
Adjust.addSessionPartnerParameter("user_id", value: userId)
}
if let anonId = event.anonymousId, anonId.count > 0 {
Adjust.addSessionPartnerParameter("anonymous_id", value: anonId)
}
return event
}
public func track(event: TrackEvent) -> TrackEvent? {
if let anonId = event.anonymousId, anonId.count > 0 {
Adjust.addSessionPartnerParameter("anonymous_id", value: anonId)
}
if let token = mappedCustomEventToken(eventName: event.event) {
let adjEvent = ADJEvent(eventToken: token)
let properties = event.properties?.dictionaryValue
if let properties = properties {
for (key, value) in properties {
adjEvent?.addCallbackParameter(key, value: "\(value)")
}
}
let revenue: Double? = extract(key: "revenue", from: properties)
let currency: String? = extract(key: "currency", from: properties, withDefault: "USD")
let orderId: String? = extract(key: "orderId", from: properties)
if let revenue = revenue, let currency = currency {
adjEvent?.setRevenue(revenue, currency: currency)
}
if let orderId = orderId {
adjEvent?.setTransactionId(orderId)
}
}
return event
}
public func reset() {
Adjust.resetSessionPartnerParameters()
}
public func registeredForRemoteNotifications(deviceToken: Data) {
Adjust.setDeviceToken(deviceToken)
}
}
// MARK: - Adjust Delegate conformance
extension AdjustDestination: AdjustDelegate {
public func adjustAttributionChanged(_ attribution: ADJAttribution?) {
let campaign: [String: Any] = [
"source": attribution?.network ?? NSNull(),
"name": attribution?.campaign ?? NSNull(),
"content": attribution?.clickLabel ?? NSNull(),
"adCreative": attribution?.creative ?? NSNull(),
"adGroup": attribution?.adgroup ?? NSNull()
]
let properties: [String: Any] = [
"provider": "Adjust",
"trackerToken": attribution?.trackerToken ?? NSNull(),
"trackerName": attribution?.trackerName ?? NSNull(),
"campaign": campaign
]
analytics?.track(name: "Install Attributed", properties: properties)
}
}
// MARK: - Support methods
extension AdjustDestination {
internal func mappedCustomEventToken(eventName: String) -> String? {
var result: String? = nil
if let tokens = settings?.customEvents?.dictionaryValue {
result = tokens[eventName] as? String
}
return result
}
internal func extract<T>(key: String, from properties: [String: Any]?, withDefault value: T? = nil) -> T? {
var result: T? = value
guard let properties = properties else { return result }
for (propKey, propValue) in properties {
// not sure if this comparison is actually necessary,
// but existed in the old destination so ...
if key.lowercased() == propKey.lowercased() {
if let value = propValue as? T {
result = value
break
}
}
}
return result
}
}
private struct AdjustSettings: Codable {
let appToken: String
let setEnvironmentProduction: Bool?
let setEventBufferingEnabled: Bool?
let trackAttributionData: Bool?
let setDelay: Bool?
let customEvents: JSON?
let delayTime: Double?
}