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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
//
// PackageBuilder.java
// Adjust
//
// Created by Christian Wellenbrock on 2013-06-25.
// Copyright (c) 2013 adjust GmbH. All rights reserved.
// See the file MIT-LICENSE for copying permission.
//
package com.adjust.sdk;
import android.text.TextUtils;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
class PackageBuilder {
private AdjustConfig adjustConfig;
private DeviceInfo deviceInfo;
private ActivityState activityState;
private long createdAt;
// reattributions
Map<String, String> extraParameters;
AdjustAttribution attribution;
String reftag;
private static ILogger logger = AdjustFactory.getLogger();
public PackageBuilder(AdjustConfig adjustConfig,
DeviceInfo deviceInfo,
ActivityState activityState,
long createdAt) {
this.adjustConfig = adjustConfig;
this.deviceInfo = deviceInfo;
this.activityState = activityState.clone();
this.createdAt = createdAt;
}
public ActivityPackage buildSessionPackage() {
Map<String, String> parameters = getDefaultParameters();
addDuration(parameters, "last_interval", activityState.lastInterval);
addString(parameters, "default_tracker", adjustConfig.defaultTracker);
ActivityPackage sessionPackage = getDefaultActivityPackage();
sessionPackage.setPath("/session");
sessionPackage.setActivityKind(ActivityKind.SESSION);
sessionPackage.setSuffix("");
sessionPackage.setParameters(parameters);
return sessionPackage;
}
public ActivityPackage buildEventPackage(AdjustEvent event) {
Map<String, String> parameters = getDefaultParameters();
addInt(parameters, "event_count", activityState.eventCount);
addString(parameters, "event_token", event.eventToken);
addDouble(parameters, "revenue", event.revenue);
addString(parameters, "currency", event.currency);
addMapJson(parameters, "callback_params", event.callbackParameters);
addMapJson(parameters, "partner_params", event.partnerParameters);
ActivityPackage eventPackage = getDefaultActivityPackage();
eventPackage.setPath("/event");
eventPackage.setActivityKind(ActivityKind.EVENT);
eventPackage.setSuffix(getEventSuffix(event));
eventPackage.setParameters(parameters);
return eventPackage;
}
public ActivityPackage buildClickPackage(String source, long clickTime) {
Map<String, String> parameters = getDefaultParameters();
addString(parameters, "source", source);
addDate(parameters, "click_time", clickTime);
addString(parameters, "reftag", reftag);
addMapJson(parameters, "params", extraParameters);
injectAttribution(parameters);
ActivityPackage clickPackage = getDefaultActivityPackage();
clickPackage.setPath("/sdk_click");
clickPackage.setActivityKind(ActivityKind.CLICK);
clickPackage.setSuffix("");
clickPackage.setParameters(parameters);
return clickPackage;
}
public ActivityPackage buildAttributionPackage() {
Map<String, String> parameters = getIdsParameters();
ActivityPackage attributionPackage = getDefaultActivityPackage();
attributionPackage.setPath("attribution"); // does not contain '/' because of Uri.Builder.appendPath
attributionPackage.setActivityKind(ActivityKind.ATTRIBUTION);
attributionPackage.setSuffix("");
attributionPackage.setParameters(parameters);
return attributionPackage;
}
private ActivityPackage getDefaultActivityPackage() {
ActivityPackage activityPackage = new ActivityPackage();
activityPackage.setClientSdk(deviceInfo.clientSdk);
return activityPackage;
}
private Map<String, String> getDefaultParameters() {
Map<String, String> parameters = new HashMap<String, String>();
injectDeviceInfo(parameters);
injectConfig(parameters);
injectActivityState(parameters);
addDate(parameters, "created_at", createdAt);
// general
checkDeviceIds(parameters);
return parameters;
}
private Map<String, String> getIdsParameters() {
Map<String, String> parameters = new HashMap<String, String>();
injectDeviceInfoIds(parameters);
injectConfig(parameters);
injectActivityStateIds(parameters);
checkDeviceIds(parameters);
return parameters;
}
private void injectDeviceInfo(Map<String, String> parameters) {
injectDeviceInfoIds(parameters);
addString(parameters, "fb_id", deviceInfo.fbAttributionId);
addString(parameters, "package_name", deviceInfo.packageName);
addString(parameters, "app_version", deviceInfo.appVersion);
addString(parameters, "device_type", deviceInfo.deviceType);
addString(parameters, "device_name", deviceInfo.deviceName);
addString(parameters, "device_manufacturer", deviceInfo.deviceManufacturer);
addString(parameters, "os_name", deviceInfo.osName);
addString(parameters, "os_version", deviceInfo.osVersion);
addString(parameters, "language", deviceInfo.language);
addString(parameters, "country", deviceInfo.country);
addString(parameters, "screen_size", deviceInfo.screenSize);
addString(parameters, "screen_format", deviceInfo.screenFormat);
addString(parameters, "screen_density", deviceInfo.screenDensity);
addString(parameters, "display_width", deviceInfo.displayWidth);
addString(parameters, "display_height", deviceInfo.displayHeight);
fillPluginKeys(parameters);
}
private void injectDeviceInfoIds(Map<String, String> parameters) {
addString(parameters, "mac_sha1", deviceInfo.macSha1);
addString(parameters, "mac_md5", deviceInfo.macShortMd5);
addString(parameters, "android_id", deviceInfo.androidId);
}
private void injectConfig(Map<String, String> parameters) {
addString(parameters, "app_token", adjustConfig.appToken);
addString(parameters, "environment", adjustConfig.environment);
addBoolean(parameters, "device_known", adjustConfig.knownDevice);
addBoolean(parameters, "needs_attribution_data", adjustConfig.hasListener());
String playAdId = Util.getPlayAdId(adjustConfig.context);
addString(parameters, "gps_adid", playAdId);
Boolean isTrackingEnabled = Util.isPlayTrackingEnabled(adjustConfig.context);
addBoolean(parameters, "tracking_enabled", isTrackingEnabled);
}
private void injectActivityState(Map<String, String> parameters) {
injectActivityStateIds(parameters);
addInt(parameters, "session_count", activityState.sessionCount);
addInt(parameters, "subsession_count", activityState.subsessionCount);
addDuration(parameters, "session_length", activityState.sessionLength);
addDuration(parameters, "time_spent", activityState.timeSpent);
}
private void injectActivityStateIds(Map<String, String> parameters) {
addString(parameters, "android_uuid", activityState.uuid);
}
private void injectAttribution(Map<String, String> parameters) {
if (attribution == null) {
return;
}
addString(parameters, "tracker", attribution.trackerName);
addString(parameters, "campaign", attribution.campaign);
addString(parameters, "adgroup", attribution.adgroup);
addString(parameters, "creative", attribution.creative);
}
private void checkDeviceIds(Map<String, String> parameters) {
if (!parameters.containsKey("mac_sha1")
&& !parameters.containsKey("mac_md5")
&& !parameters.containsKey("android_id")
&& !parameters.containsKey("gps_adid")) {
logger.error("Missing device id's. Please check if Proguard is correctly set with Adjust SDK");
}
}
private void fillPluginKeys(Map<String, String> parameters) {
if (deviceInfo.pluginKeys == null) {
return;
}
for (Map.Entry<String, String> entry : deviceInfo.pluginKeys.entrySet()) {
addString(parameters, entry.getKey(), entry.getValue());
}
}
private String getEventSuffix(AdjustEvent event) {
if (event.revenue == null) {
return String.format(" '%s'", event.eventToken);
} else {
return String.format(Locale.US, " (%.4f %s, '%s')", event.revenue, event.currency, event.eventToken);
}
}
private void addString(Map<String, String> parameters, String key, String value) {
if (TextUtils.isEmpty(value)) {
return;
}
parameters.put(key, value);
}
private void addInt(Map<String, String> parameters, String key, long value) {
if (value < 0) {
return;
}
String valueString = Long.toString(value);
addString(parameters, key, valueString);
}
private void addDate(Map<String, String> parameters, String key, long value) {
if (value < 0) {
return;
}
String dateString = Util.dateFormat(value);
addString(parameters, key, dateString);
}
private void addDuration(Map<String, String> parameters, String key, long durationInMilliSeconds) {
if (durationInMilliSeconds < 0) {
return;
}
long durationInSeconds = (durationInMilliSeconds + 500) / 1000;
addInt(parameters, key, durationInSeconds);
}
private void addMapJson(Map<String, String> parameters, String key, Map<String, String> map) {
if (map == null) {
return;
}
if (map.size() == 0) {
return;
}
JSONObject jsonObject = new JSONObject(map);
String jsonString = jsonObject.toString();
addString(parameters, key, jsonString);
}
private void addBoolean(Map<String, String> parameters, String key, Boolean value) {
if (value == null) {
return;
}
int intValue = value ? 1 : 0;
addInt(parameters, key, intValue);
}
private void addDouble(Map<String, String> parameters, String key, Double value) {
if (value == null) return;
String doubleString = String.format("%.5f", value);
addString(parameters, key, doubleString);
}
}
|