@snowplow/react-native-tracker 0.2.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,116 +1,339 @@
1
1
  package com.snowplowanalytics.react.util;
2
2
 
3
+ import androidx.annotation.NonNull;
4
+
3
5
  import com.facebook.react.bridge.ReadableMap;
4
6
  import com.facebook.react.bridge.ReadableArray;
5
- import com.snowplowanalytics.snowplow.tracker.events.SelfDescribing;
6
- import com.snowplowanalytics.snowplow.tracker.events.Structured;
7
- import com.snowplowanalytics.snowplow.tracker.payload.SelfDescribingJson;
8
- import com.snowplowanalytics.snowplow.tracker.events.ScreenView;
9
- import com.snowplowanalytics.snowplow.tracker.events.PageView;
7
+ import com.snowplowanalytics.snowplow.event.DeepLinkReceived;
8
+ import com.snowplowanalytics.snowplow.event.MessageNotification;
9
+ import com.snowplowanalytics.snowplow.event.MessageNotificationAttachment;
10
+ import com.snowplowanalytics.snowplow.event.MessageNotificationTrigger;
11
+ import com.snowplowanalytics.snowplow.event.Structured;
12
+ import com.snowplowanalytics.snowplow.payload.SelfDescribingJson;
13
+ import com.snowplowanalytics.snowplow.event.ScreenView;
14
+ import com.snowplowanalytics.snowplow.event.PageView;
15
+ import com.snowplowanalytics.snowplow.event.Timing;
16
+ import com.snowplowanalytics.snowplow.event.ConsentGranted;
17
+ import com.snowplowanalytics.snowplow.event.ConsentWithdrawn;
18
+ import com.snowplowanalytics.snowplow.event.EcommerceTransactionItem;
19
+ import com.snowplowanalytics.snowplow.event.EcommerceTransaction;
10
20
 
11
21
  import java.util.ArrayList;
12
22
  import java.util.List;
23
+ import java.util.Objects;
24
+ import java.util.UUID;
13
25
 
14
26
  public class EventUtil {
15
27
 
16
- public static List<SelfDescribingJson> getContexts(ReadableArray contexts) {
28
+ public static SelfDescribingJson createSelfDescribingJson(ReadableMap json) {
29
+ String schema = json.getString("schema");
30
+ ReadableMap dataMap = json.getMap("data");
31
+
32
+ return new SelfDescribingJson(schema, dataMap.toHashMap());
33
+ }
17
34
 
35
+ public static List<SelfDescribingJson> createContexts(ReadableArray contexts) {
18
36
  List<SelfDescribingJson> nativeContexts = new ArrayList<>();
19
37
  for (int i = 0; i < contexts.size(); i++) {
20
- SelfDescribingJson json = EventUtil.getSelfDescribingJson(contexts.getMap(i));
38
+ SelfDescribingJson json = createSelfDescribingJson(contexts.getMap(i));
21
39
  nativeContexts.add(json);
22
40
  }
41
+
23
42
  return nativeContexts;
24
43
  }
25
44
 
26
- public static SelfDescribingJson getSelfDescribingJson(ReadableMap json) {
45
+ public static Structured createStructuredEvent(ReadableMap argmap) {
46
+ Structured event = new Structured(
47
+ Objects.requireNonNull(argmap.getString("category"), "category can't be null"),
48
+ Objects.requireNonNull(argmap.getString("action"), "action can't be null")
49
+ );
27
50
 
28
- String schema = json.getString("schema");
29
- ReadableMap dataMap = json.getMap("data");
30
- if (schema != null && dataMap != null) {
31
- return new SelfDescribingJson(schema, dataMap.toHashMap());
32
- } else {
33
- // log error
51
+ if (argmap.hasKey("label")) {
52
+ event.label(argmap.getString("label"));
53
+ }
54
+ if (argmap.hasKey("property")) {
55
+ event.property(argmap.getString("property"));
56
+ }
57
+ // React Native forces primitive double type - so null "value" parameter is handled by not setting at all
58
+ if (argmap.hasKey("value") && !argmap.isNull("value")) {
59
+ event.value(argmap.getDouble("value"));
34
60
  }
35
- return null;
61
+
62
+ return event;
36
63
  }
37
64
 
38
- public static SelfDescribing getSelfDescribingEvent(ReadableMap event, ReadableArray contexts) {
65
+ public static ScreenView createScreenViewEvent(ReadableMap argmap) {
66
+ @NonNull String name = Objects.requireNonNull(argmap.getString("name"), "name can't be null");
67
+ ScreenView event = (
68
+ argmap.hasKey("id") ?
69
+ new ScreenView(name, UUID.fromString(argmap.getString("id"))) :
70
+ new ScreenView(name)
71
+ );
39
72
 
40
- SelfDescribingJson data = EventUtil.getSelfDescribingJson(event);
41
- List<SelfDescribingJson> nativeContexts = EventUtil.getContexts(contexts);
42
- SelfDescribing.Builder eventBuilder = SelfDescribing.builder();
43
- if (data == null) return null;
44
- eventBuilder.eventData(data);
45
- if (nativeContexts != null) {
46
- eventBuilder.customContext(nativeContexts);
73
+ if (argmap.hasKey("type")) {
74
+ event.type(argmap.getString("type"));
75
+ }
76
+ if (argmap.hasKey("previousName")) {
77
+ event.previousName(argmap.getString("previousName"));
78
+ }
79
+ if (argmap.hasKey("previousType")) {
80
+ event.previousType(argmap.getString("previousType"));
81
+ }
82
+ if (argmap.hasKey("previousId")) {
83
+ event.previousId(argmap.getString("previousId"));
47
84
  }
48
- return eventBuilder.build();
85
+ if (argmap.hasKey("transitionType")) {
86
+ event.transitionType(argmap.getString("transitionType"));
87
+ }
88
+
89
+ return event;
49
90
  }
50
91
 
51
- public static Structured getStructuredEvent(ReadableMap details, ReadableArray contexts) {
92
+ public static PageView createPageViewEvent(ReadableMap argmap) {
93
+ PageView event = new PageView(
94
+ Objects.requireNonNull(argmap.getString("pageUrl"), "pageUrl can't be null")
95
+ );
96
+
97
+ if (argmap.hasKey("pageTitle")) {
98
+ event.pageTitle(argmap.getString("pageTitle"));
99
+ }
100
+ if (argmap.hasKey("referrer")) {
101
+ event.referrer(argmap.getString("referrer"));
102
+ }
103
+
104
+ return event;
105
+ }
52
106
 
53
- // build with required arguments
54
- Structured.Builder eventBuilder = Structured.builder()
55
- .category(details.getString("category"))
56
- .action(details.getString("action"));
107
+ public static Timing createTimingEvent(ReadableMap argmap) {
108
+ Timing event = new Timing(
109
+ Objects.requireNonNull(argmap.getString("category"), "category can't be null"),
110
+ Objects.requireNonNull(argmap.getString("variable"), "variable can't be null"),
111
+ argmap.getInt("timing")
112
+ );
57
113
 
58
- // add any optional arguments
59
- if (details.hasKey("label")) {
60
- eventBuilder.label(details.getString("label"));
114
+ if (argmap.hasKey("label")) {
115
+ event.label(argmap.getString("label"));
61
116
  }
62
- if (details.hasKey("property")) {
63
- eventBuilder.property(details.getString("property"));
117
+
118
+ return event;
119
+ }
120
+
121
+ public static ConsentGranted createConsentGrantedEvent(ReadableMap argmap) {
122
+ ConsentGranted event = new ConsentGranted(
123
+ Objects.requireNonNull(argmap.getString("expiry"), "expiry can't be null"),
124
+ Objects.requireNonNull(argmap.getString("documentId"), "documentId can't be null"),
125
+ Objects.requireNonNull(argmap.getString("version"), "version can't be null")
126
+ );
127
+
128
+ if (argmap.hasKey("name")) {
129
+ event.documentName(argmap.getString("name"));
64
130
  }
65
- // React Native forces primitive double type - so null "value" parameter is handled by not setting at all
66
- if (details.hasKey("value") && !details.isNull("value")) {
67
- eventBuilder.value(details.getDouble("value"));
131
+ if (argmap.hasKey("documentDescription")) {
132
+ event.documentDescription(argmap.getString("documentDescription"));
68
133
  }
69
134
 
70
- List<SelfDescribingJson> nativeContexts = EventUtil.getContexts(contexts);
71
- if (nativeContexts != null) {
72
- eventBuilder.customContext(nativeContexts);
135
+ return event;
136
+ }
137
+
138
+ public static ConsentWithdrawn createConsentWithdrawnEvent(ReadableMap argmap) {
139
+ ConsentWithdrawn event = new ConsentWithdrawn(
140
+ argmap.getBoolean("all"),
141
+ Objects.requireNonNull(argmap.getString("documentId"), "documentId can't be null"),
142
+ Objects.requireNonNull(argmap.getString("version"), "version can't be null")
143
+ );
144
+
145
+ if (argmap.hasKey("name")) {
146
+ event.documentName(argmap.getString("name"));
73
147
  }
74
- return eventBuilder.build();
148
+ if (argmap.hasKey("documentDescription")) {
149
+ event.documentDescription(argmap.getString("documentDescription"));
150
+ }
151
+
152
+ return event;
75
153
  }
76
154
 
77
- public static ScreenView getScreenViewEvent(ReadableMap details, ReadableArray contexts) {
155
+ public static List<EcommerceTransactionItem> createEcommerceTransactionItems(ReadableArray items) {
156
+ List<EcommerceTransactionItem> ecomItems = new ArrayList<>();
157
+ for (int i = 0; i < items.size(); i++) {
158
+ ReadableMap argItem = items.getMap(i);
159
+ EcommerceTransactionItem item = new EcommerceTransactionItem(
160
+ Objects.requireNonNull(argItem.getString("sku"), "sku can't be null"),
161
+ argItem.getDouble("price"),
162
+ argItem.getInt("quantity")
163
+ );
78
164
 
79
- // build with required arguments
80
- ScreenView.Builder eventBuilder = ScreenView.builder()
81
- .name(details.getString("screenName"));
82
- // add any optional arguments
83
- if (details.hasKey("screenType")) {
84
- eventBuilder.type(details.getString("screenType"));
165
+ if (argItem.hasKey("name")) {
166
+ item.name(argItem.getString("name"));
167
+ }
168
+ if (argItem.hasKey("category")) {
169
+ item.category(argItem.getString("category"));
85
170
  }
86
- if (details.hasKey("transitionType")) {
87
- eventBuilder.transitionType(details.getString("transitionType"));
171
+ if (argItem.hasKey("currency")) {
172
+ item.currency(argItem.getString("currency"));
88
173
  }
89
174
 
90
- List<SelfDescribingJson> nativeContexts = EventUtil.getContexts(contexts);
91
- if (nativeContexts != null) {
92
- eventBuilder.customContext(nativeContexts);
175
+ ecomItems.add(item);
93
176
  }
94
- return eventBuilder.build();
177
+
178
+ return ecomItems;
95
179
  }
96
180
 
97
- public static PageView getPageViewEvent(ReadableMap details, ReadableArray contexts) {
181
+ public static EcommerceTransaction createEcommerceTransactionEvent(ReadableMap argmap) {
182
+ List<EcommerceTransactionItem> ecomItems = createEcommerceTransactionItems(
183
+ Objects.requireNonNull(argmap.getArray("items"), "items can't be null")
184
+ );
185
+
186
+ EcommerceTransaction event = new EcommerceTransaction(
187
+ Objects.requireNonNull(argmap.getString("orderId"), "orderId can't be null"),
188
+ argmap.getDouble("totalValue"),
189
+ ecomItems
190
+ );
98
191
 
99
- // build with required arguments
100
- PageView.Builder eventBuilder = PageView.builder()
101
- .pageUrl(details.getString("pageUrl"));
102
- // add any optional arguments
103
- if (details.hasKey("pageTitle")) {
104
- eventBuilder.pageTitle(details.getString("pageTitle"));
192
+ if (argmap.hasKey("affiliation")) {
193
+ event.affiliation(argmap.getString("affiliation"));
194
+ }
195
+ if (argmap.hasKey("taxValue")) {
196
+ event.taxValue(argmap.getDouble("taxValue"));
197
+ }
198
+ if (argmap.hasKey("shipping")) {
199
+ event.shipping(argmap.getDouble("shipping"));
200
+ }
201
+ if (argmap.hasKey("city")) {
202
+ event.city(argmap.getString("city"));
203
+ }
204
+ if (argmap.hasKey("state")) {
205
+ event.state(argmap.getString("state"));
206
+ }
207
+ if (argmap.hasKey("country")) {
208
+ event.country(argmap.getString("country"));
209
+ }
210
+ if (argmap.hasKey("currency")) {
211
+ event.currency(argmap.getString("currency"));
212
+ }
213
+
214
+ return event;
215
+ }
216
+
217
+ public static DeepLinkReceived createDeepLinkReceivedEvent(ReadableMap argmap) {
218
+ DeepLinkReceived event = new DeepLinkReceived(
219
+ Objects.requireNonNull(argmap.getString("url"), "url can't be null")
220
+ );
221
+
222
+ if (argmap.hasKey("referrer")) {
223
+ event.referrer(argmap.getString("referrer"));
224
+ }
225
+
226
+ return event;
227
+ }
228
+
229
+ public static List<MessageNotificationAttachment> createMessageNotificationAttachments(ReadableArray items) {
230
+ List<MessageNotificationAttachment> attachments = new ArrayList<>();
231
+ for (int i = 0; i < items.size(); i++) {
232
+ ReadableMap argItem = items.getMap(i);
233
+ MessageNotificationAttachment attachment = new MessageNotificationAttachment(
234
+ Objects.requireNonNull(argItem.getString("identifier"), "identifier can't be null"),
235
+ Objects.requireNonNull(argItem.getString("type"), "type can't be null"),
236
+ Objects.requireNonNull(argItem.getString("url"), "url can't be null")
237
+ );
238
+ attachments.add(attachment);
239
+ }
240
+
241
+ return attachments;
242
+ }
243
+
244
+ public static List<String> createStrings(ReadableArray items) {
245
+ List<String> results = new ArrayList<>();
246
+ for (int i = 0; i < items.size(); i++) {
247
+ results.add(items.getString(i));
248
+ }
249
+ return results;
250
+ }
251
+
252
+ public static MessageNotification createMessageNotificationEvent(ReadableMap argmap) {
253
+
254
+ MessageNotificationTrigger trigger;
255
+ switch (Objects.requireNonNull(argmap.getString("trigger"), "trigger can't be null")) {
256
+ case "push":
257
+ trigger = MessageNotificationTrigger.push;
258
+ break;
259
+ case "location":
260
+ trigger = MessageNotificationTrigger.location;
261
+ break;
262
+ case "calendar":
263
+ trigger = MessageNotificationTrigger.calendar;
264
+ break;
265
+ case "timeInterval":
266
+ trigger = MessageNotificationTrigger.timeInterval;
267
+ break;
268
+ default:
269
+ trigger = MessageNotificationTrigger.other;
270
+ break;
271
+ }
272
+
273
+ MessageNotification event = new MessageNotification(
274
+ Objects.requireNonNull(argmap.getString("title"), "title can't be null"),
275
+ Objects.requireNonNull(argmap.getString("body"), "body can't be null"),
276
+ trigger
277
+ );
278
+
279
+ if (argmap.hasKey("action")) {
280
+ event.action(argmap.getString("action"));
281
+ }
282
+ if (argmap.hasKey("attachments")) {
283
+ ReadableArray attachments = argmap.getArray("attachments");
284
+ if (attachments != null) {
285
+ event.attachments(createMessageNotificationAttachments(attachments));
105
286
  }
106
- if (details.hasKey("pageReferrer")) {
107
- eventBuilder.referrer(details.getString("pageReferrer"));
287
+ }
288
+ if (argmap.hasKey("bodyLocArgs")) {
289
+ ReadableArray bodyLocArgs = argmap.getArray("bodyLocArgs");
290
+ if (bodyLocArgs != null) {
291
+ event.bodyLocArgs(createStrings(bodyLocArgs));
108
292
  }
109
-
110
- List<SelfDescribingJson> nativeContexts = EventUtil.getContexts(contexts);
111
- if (nativeContexts != null) {
112
- eventBuilder.customContext(nativeContexts);
113
293
  }
114
- return eventBuilder.build();
294
+ if (argmap.hasKey("bodyLocKey")) {
295
+ event.bodyLocKey(argmap.getString("bodyLocKey"));
296
+ }
297
+ if (argmap.hasKey("category")) {
298
+ event.category(argmap.getString("category"));
299
+ }
300
+ if (argmap.hasKey("contentAvailable")) {
301
+ event.contentAvailable(argmap.getBoolean("contentAvailable"));
302
+ }
303
+ if (argmap.hasKey("group")) {
304
+ event.group(argmap.getString("group"));
305
+ }
306
+ if (argmap.hasKey("icon")) {
307
+ event.icon(argmap.getString("icon"));
308
+ }
309
+ if (argmap.hasKey("notificationCount")) {
310
+ event.notificationCount(argmap.getInt("notificationCount"));
311
+ }
312
+ if (argmap.hasKey("notificationTimestamp")) {
313
+ event.notificationTimestamp(argmap.getString("notificationTimestamp"));
314
+ }
315
+ if (argmap.hasKey("sound")) {
316
+ event.sound(argmap.getString("sound"));
317
+ }
318
+ if (argmap.hasKey("subtitle")) {
319
+ event.subtitle(argmap.getString("subtitle"));
320
+ }
321
+ if (argmap.hasKey("tag")) {
322
+ event.tag(argmap.getString("tag"));
323
+ }
324
+ if (argmap.hasKey("threadIdentifier")) {
325
+ event.threadIdentifier(argmap.getString("threadIdentifier"));
326
+ }
327
+ if (argmap.hasKey("titleLocArgs")) {
328
+ ReadableArray titleLocArgs = argmap.getArray("titleLocArgs");
329
+ if (titleLocArgs != null) {
330
+ event.titleLocArgs(createStrings(titleLocArgs));
331
+ }
332
+ }
333
+ if (argmap.hasKey("titleLocKey")) {
334
+ event.titleLocKey(argmap.getString("titleLocKey"));
335
+ }
336
+
337
+ return event;
115
338
  }
116
339
  }
@@ -0,0 +1,7 @@
1
+ package com.snowplowanalytics.react.util;
2
+
3
+ public class TrackerVersion {
4
+
5
+ public final static String RN_TRACKER_VERSION = "rn-1.1.0";
6
+
7
+ }