@snowplow/react-native-tracker 1.4.0 → 2.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.
Files changed (240) hide show
  1. package/.editorconfig +15 -0
  2. package/.eslintignore +6 -0
  3. package/.gitattributes +3 -0
  4. package/.github/ISSUE_TEMPLATE/bug_report.md +29 -0
  5. package/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  6. package/.github/actions/setup/action.yml +28 -0
  7. package/.github/workflows/build.yml +155 -0
  8. package/.github/workflows/deploy.yml +97 -0
  9. package/.github/workflows/e2e-android.yml +121 -0
  10. package/.github/workflows/e2e-ios.yml +95 -0
  11. package/.github/workflows/snyk.yml +21 -0
  12. package/.gitignore +70 -0
  13. package/.npmignore +60 -0
  14. package/.nvmrc +1 -0
  15. package/.watchmanconfig +1 -0
  16. package/.yarnrc +3 -0
  17. package/CHANGELOG +176 -0
  18. package/CONTRIBUTING.md +80 -0
  19. package/LICENSE +1 -1
  20. package/README.md +24 -111
  21. package/android/build.gradle +93 -37
  22. package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  23. package/android/gradle/wrapper/gradle-wrapper.properties +1 -2
  24. package/android/gradle.properties +5 -2
  25. package/android/gradlew +172 -110
  26. package/android/gradlew.bat +24 -19
  27. package/android/src/main/AndroidManifest.xml +1 -3
  28. package/android/src/main/AndroidManifestNew.xml +2 -0
  29. package/android/src/main/java/com/snowplow/reactnativetracker/ReactNativeTrackerModule.kt +744 -0
  30. package/android/src/main/java/com/snowplow/reactnativetracker/ReactNativeTrackerPackage.kt +17 -0
  31. package/android/src/main/java/com/snowplow/reactnativetracker/util/ConfigUtil.kt +274 -0
  32. package/android/src/main/java/com/snowplow/reactnativetracker/util/EventUtil.kt +233 -0
  33. package/android/src/main/java/com/snowplow/reactnativetracker/util/TrackerVersion.kt +5 -0
  34. package/babel.config.js +3 -0
  35. package/example/.watchmanconfig +1 -0
  36. package/example/Gemfile +6 -0
  37. package/example/android/app/build.gradle +129 -0
  38. package/example/android/app/debug.keystore +0 -0
  39. package/example/android/app/proguard-rules.pro +10 -0
  40. package/example/android/app/src/androidTest/java/com/reactnativetrackerexample/DetoxTest.java +36 -0
  41. package/example/android/app/src/androidTest/java/com/reactnativetrackerexample/DetoxTestAppJUnitRunner.java +21 -0
  42. package/example/android/app/src/debug/AndroidManifest.xml +13 -0
  43. package/example/android/app/src/debug/java/com/reactnativetrackerexample/ReactNativeFlipper.java +75 -0
  44. package/example/android/app/src/main/AndroidManifest.xml +26 -0
  45. package/example/android/app/src/main/java/com/reactnativetrackerexample/MainActivity.java +32 -0
  46. package/example/android/app/src/main/java/com/reactnativetrackerexample/MainApplication.java +62 -0
  47. package/example/android/app/src/main/res/drawable/rn_edit_text_material.xml +36 -0
  48. package/example/android/app/src/main/res/mipmap-hdpi/ic_launcher.png +0 -0
  49. package/example/android/app/src/main/res/mipmap-hdpi/ic_launcher_round.png +0 -0
  50. package/example/android/app/src/main/res/mipmap-mdpi/ic_launcher.png +0 -0
  51. package/example/android/app/src/main/res/mipmap-mdpi/ic_launcher_round.png +0 -0
  52. package/example/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png +0 -0
  53. package/example/android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png +0 -0
  54. package/example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png +0 -0
  55. package/example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png +0 -0
  56. package/example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png +0 -0
  57. package/example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png +0 -0
  58. package/example/android/app/src/main/res/values/strings.xml +3 -0
  59. package/example/android/app/src/main/res/values/styles.xml +9 -0
  60. package/example/android/app/src/release/java/com/reactnativetrackerexample/ReactNativeFlipper.java +20 -0
  61. package/example/android/build.gradle +31 -0
  62. package/example/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  63. package/example/android/gradle/wrapper/gradle-wrapper.properties +6 -0
  64. package/example/android/gradle.properties +44 -0
  65. package/example/android/gradlew +244 -0
  66. package/example/android/gradlew.bat +92 -0
  67. package/example/android/settings.gradle +4 -0
  68. package/example/app.json +4 -0
  69. package/example/babel.config.js +17 -0
  70. package/example/detox.config.js +92 -0
  71. package/example/e2e/config.json +10 -0
  72. package/example/e2e/emitEvents.e2e.detox.js +95 -0
  73. package/example/e2e/environment.js +23 -0
  74. package/example/e2e/helpers/microCommands.js +140 -0
  75. package/example/e2e/helpers/microHelpers.js +333 -0
  76. package/example/e2e/helpers/microHelpers.test.js +295 -0
  77. package/example/e2e/helpers/schemas.js +49 -0
  78. package/example/e2e/jest.config.js +16 -0
  79. package/example/e2e/setup.js +4 -0
  80. package/example/e2e/testEvents.micro.test.js +287 -0
  81. package/example/index.js +5 -0
  82. package/example/ios/.xcode.env +11 -0
  83. package/example/ios/Podfile +62 -0
  84. package/example/ios/Podfile.lock +730 -0
  85. package/example/ios/ReactNativeTrackerExample/AppDelegate.h +6 -0
  86. package/example/ios/ReactNativeTrackerExample/AppDelegate.mm +26 -0
  87. package/example/ios/ReactNativeTrackerExample/Images.xcassets/AppIcon.appiconset/Contents.json +53 -0
  88. package/example/ios/ReactNativeTrackerExample/Images.xcassets/Contents.json +6 -0
  89. package/example/ios/ReactNativeTrackerExample/Info.plist +55 -0
  90. package/example/ios/ReactNativeTrackerExample/LaunchScreen.storyboard +47 -0
  91. package/example/ios/ReactNativeTrackerExample/main.m +10 -0
  92. package/example/ios/ReactNativeTrackerExample-Bridging-Header.h +3 -0
  93. package/example/ios/ReactNativeTrackerExample.xcodeproj/project.pbxproj +722 -0
  94. package/{ios/RNSnowplowTracker.xcodeproj/xcshareddata/xcschemes/RNSnowplowTracker.xcscheme → example/ios/ReactNativeTrackerExample.xcodeproj/xcshareddata/xcschemes/ReactNativeTrackerExample.xcscheme} +29 -21
  95. package/example/ios/ReactNativeTrackerExample.xcworkspace/contents.xcworkspacedata +10 -0
  96. package/example/ios/ReactNativeTrackerExample.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
  97. package/example/ios/ReactNativeTrackerExampleTests/Info.plist +24 -0
  98. package/example/ios/ReactNativeTrackerExampleTests/ReactNativeTrackerExampleTests.m +66 -0
  99. package/example/metro.config.js +45 -0
  100. package/example/package.json +54 -0
  101. package/example/react-native.config.js +10 -0
  102. package/example/src/App.js +566 -0
  103. package/example/tsconfig.json +5 -0
  104. package/example/yarn.lock +7379 -0
  105. package/ios/ReactNativeTracker-Bridging-Header.h +2 -0
  106. package/ios/ReactNativeTracker.mm +175 -0
  107. package/ios/ReactNativeTracker.swift +822 -0
  108. package/ios/{RNSnowplowTracker.xcodeproj → ReactNativeTracker.xcodeproj}/project.pbxproj +55 -79
  109. package/ios/Util/ConfigUtils.swift +209 -0
  110. package/ios/Util/TrackerVersion.swift +3 -0
  111. package/ios/Util/Utilities.swift +37 -0
  112. package/lefthook.yml +11 -0
  113. package/lib/commonjs/api.js +490 -0
  114. package/lib/commonjs/api.js.map +1 -0
  115. package/lib/commonjs/configurations.js +196 -0
  116. package/lib/commonjs/configurations.js.map +1 -0
  117. package/lib/commonjs/constants.js +77 -0
  118. package/lib/commonjs/constants.js.map +1 -0
  119. package/lib/commonjs/events.js +277 -0
  120. package/lib/commonjs/events.js.map +1 -0
  121. package/lib/commonjs/index.js +132 -0
  122. package/lib/commonjs/index.js.map +1 -0
  123. package/lib/commonjs/jsCore.js +427 -0
  124. package/lib/commonjs/jsCore.js.map +1 -0
  125. package/lib/commonjs/native.js +16 -0
  126. package/lib/commonjs/native.js.map +1 -0
  127. package/lib/commonjs/subject.js +235 -0
  128. package/lib/commonjs/subject.js.map +1 -0
  129. package/lib/commonjs/tracker.js +248 -0
  130. package/lib/commonjs/tracker.js.map +1 -0
  131. package/lib/commonjs/types.js +2 -0
  132. package/lib/commonjs/types.js.map +1 -0
  133. package/lib/commonjs/utils.js +72 -0
  134. package/lib/commonjs/utils.js.map +1 -0
  135. package/lib/commonjs/webViewInterface.js +66 -0
  136. package/lib/commonjs/webViewInterface.js.map +1 -0
  137. package/lib/module/api.js +452 -0
  138. package/lib/module/api.js.map +1 -0
  139. package/lib/module/configurations.js +184 -0
  140. package/lib/module/configurations.js.map +1 -0
  141. package/lib/module/constants.js +72 -0
  142. package/lib/module/constants.js.map +1 -0
  143. package/lib/module/events.js +259 -0
  144. package/lib/module/events.js.map +1 -0
  145. package/lib/module/index.js +120 -0
  146. package/lib/module/index.js.map +1 -0
  147. package/lib/module/jsCore.js +424 -0
  148. package/lib/module/jsCore.js.map +1 -0
  149. package/lib/module/native.js +12 -0
  150. package/lib/module/native.js.map +1 -0
  151. package/lib/module/subject.js +222 -0
  152. package/lib/module/subject.js.map +1 -0
  153. package/lib/module/tracker.js +234 -0
  154. package/lib/module/tracker.js.map +1 -0
  155. package/lib/module/types.js +2 -0
  156. package/lib/module/types.js.map +1 -0
  157. package/lib/module/utils.js +66 -0
  158. package/lib/module/utils.js.map +1 -0
  159. package/lib/module/webViewInterface.js +62 -0
  160. package/lib/module/webViewInterface.js.map +1 -0
  161. package/lib/typescript/__tests__/api.test.d.ts +2 -0
  162. package/lib/typescript/__tests__/api.test.d.ts.map +1 -0
  163. package/lib/typescript/__tests__/configurations.test.d.ts +2 -0
  164. package/lib/typescript/__tests__/configurations.test.d.ts.map +1 -0
  165. package/lib/typescript/__tests__/events.test.d.ts +2 -0
  166. package/lib/typescript/__tests__/events.test.d.ts.map +1 -0
  167. package/lib/typescript/__tests__/index.test.d.ts +2 -0
  168. package/lib/typescript/__tests__/index.test.d.ts.map +1 -0
  169. package/lib/typescript/__tests__/jsCore.test.d.ts +2 -0
  170. package/lib/typescript/__tests__/jsCore.test.d.ts.map +1 -0
  171. package/lib/typescript/__tests__/utils.test.d.ts +2 -0
  172. package/lib/typescript/__tests__/utils.test.d.ts.map +1 -0
  173. package/lib/typescript/api.d.ts +240 -0
  174. package/lib/typescript/api.d.ts.map +1 -0
  175. package/lib/typescript/configurations.d.ts +73 -0
  176. package/lib/typescript/configurations.d.ts.map +1 -0
  177. package/lib/typescript/constants.d.ts +64 -0
  178. package/lib/typescript/constants.d.ts.map +1 -0
  179. package/lib/typescript/events.d.ts +115 -0
  180. package/lib/typescript/events.d.ts.map +1 -0
  181. package/lib/typescript/index.d.ts +27 -0
  182. package/lib/typescript/index.d.ts.map +1 -0
  183. package/lib/typescript/jsCore.d.ts +152 -0
  184. package/lib/typescript/jsCore.d.ts.map +1 -0
  185. package/lib/typescript/native.d.ts +4 -0
  186. package/lib/typescript/native.d.ts.map +1 -0
  187. package/lib/typescript/subject.d.ts +91 -0
  188. package/lib/typescript/subject.d.ts.map +1 -0
  189. package/lib/typescript/tracker.d.ts +111 -0
  190. package/lib/typescript/tracker.d.ts.map +1 -0
  191. package/{dist/index.d.ts → lib/typescript/types.d.ts} +264 -73
  192. package/lib/typescript/types.d.ts.map +1 -0
  193. package/lib/typescript/utils.d.ts +35 -0
  194. package/lib/typescript/utils.d.ts.map +1 -0
  195. package/lib/typescript/webViewInterface.d.ts +15 -0
  196. package/lib/typescript/webViewInterface.d.ts.map +1 -0
  197. package/package.json +135 -53
  198. package/scripts/bootstrap.js +29 -0
  199. package/snowplow-react-native-tracker.podspec +42 -0
  200. package/src/__mocks__/react-native.js +7 -0
  201. package/src/__tests__/api.test.ts +21 -0
  202. package/src/__tests__/configurations.test.ts +608 -0
  203. package/src/__tests__/events.test.ts +736 -0
  204. package/src/__tests__/index.test.ts +14 -0
  205. package/src/__tests__/jsCore.test.ts +199 -0
  206. package/src/__tests__/utils.test.ts +10 -0
  207. package/src/api.ts +556 -0
  208. package/src/configurations.ts +325 -0
  209. package/src/constants.ts +97 -0
  210. package/src/events.ts +355 -0
  211. package/src/index.ts +184 -0
  212. package/src/jsCore.ts +603 -0
  213. package/src/native.ts +21 -0
  214. package/src/subject.ts +302 -0
  215. package/src/tracker.ts +377 -0
  216. package/src/types.ts +1162 -0
  217. package/src/utils.ts +65 -0
  218. package/src/webViewInterface.ts +92 -0
  219. package/tsconfig.build.json +5 -0
  220. package/tsconfig.json +28 -0
  221. package/turbo.json +34 -0
  222. package/yarn.lock +9333 -0
  223. package/RNSnowplowTracker.podspec +0 -24
  224. package/android/src/main/java/com/snowplowanalytics/react/tracker/RNSnowplowTrackerModule.java +0 -750
  225. package/android/src/main/java/com/snowplowanalytics/react/tracker/RNSnowplowTrackerPackage.java +0 -29
  226. package/android/src/main/java/com/snowplowanalytics/react/util/ConfigUtil.java +0 -351
  227. package/android/src/main/java/com/snowplowanalytics/react/util/EventUtil.java +0 -339
  228. package/android/src/main/java/com/snowplowanalytics/react/util/TrackerVersion.java +0 -7
  229. package/dist/index.js +0 -2026
  230. package/dist/index.js.map +0 -1
  231. package/ios/RNSnowplowTracker.h +0 -34
  232. package/ios/RNSnowplowTracker.m +0 -911
  233. package/ios/Util/NSDictionary+RNSP_TypeMethods.h +0 -33
  234. package/ios/Util/NSDictionary+RNSP_TypeMethods.m +0 -40
  235. package/ios/Util/RNConfigUtils.h +0 -44
  236. package/ios/Util/RNConfigUtils.m +0 -209
  237. package/ios/Util/RNTrackerVersion.h +0 -27
  238. package/ios/Util/RNTrackerVersion.m +0 -27
  239. package/ios/Util/RNUtilities.h +0 -32
  240. package/ios/Util/RNUtilities.m +0 -68
package/src/subject.ts ADDED
@@ -0,0 +1,302 @@
1
+ 'use strict';
2
+
3
+ import { RNSnowplowTracker } from './native';
4
+ import { logMessages } from './constants';
5
+ import { isScreenSize, isValidSubjectConf } from './configurations';
6
+ import type { ScreenSize, SubjectConfiguration } from './types';
7
+
8
+ /**
9
+ * Sets the userId of the tracker subject
10
+ *
11
+ * @param namespace {string} - the tracker namespace
12
+ * @param newUid {string | null} - the new userId
13
+ * @returns - Promise
14
+ */
15
+ function setUserId(namespace: string, newUid: string | null): Promise<void> {
16
+ if (!(newUid === null || typeof newUid === 'string')) {
17
+ return Promise.reject(new Error(logMessages.setUserId));
18
+ }
19
+ return <Promise<void>>Promise.resolve(
20
+ RNSnowplowTracker.setUserId({
21
+ tracker: namespace,
22
+ userId: newUid,
23
+ })
24
+ );
25
+ }
26
+
27
+ /**
28
+ * Sets the networkUserId of the tracker subject
29
+ *
30
+ * @param namespace {string} - the tracker namespace
31
+ * @param newNuid {string | null} - the new networkUserId
32
+ * @returns - Promise
33
+ */
34
+ function setNetworkUserId(
35
+ namespace: string,
36
+ newNuid: string | null
37
+ ): Promise<void> {
38
+ if (!(newNuid === null || typeof newNuid === 'string')) {
39
+ return Promise.reject(new Error(logMessages.setNetworkUserId));
40
+ }
41
+ return <Promise<void>>Promise.resolve(
42
+ RNSnowplowTracker.setNetworkUserId({
43
+ tracker: namespace,
44
+ networkUserId: newNuid,
45
+ })
46
+ );
47
+ }
48
+
49
+ /**
50
+ * Sets the domainUserId of the tracker subject
51
+ *
52
+ * @param namespace {string} - the tracker namespace
53
+ * @param newDuid {string | null} - the new domainUserId
54
+ * @returns - Promise
55
+ */
56
+ function setDomainUserId(
57
+ namespace: string,
58
+ newDuid: string | null
59
+ ): Promise<void> {
60
+ if (!(newDuid === null || typeof newDuid === 'string')) {
61
+ return Promise.reject(new Error(logMessages.setDomainUserId));
62
+ }
63
+ return <Promise<void>>Promise.resolve(
64
+ RNSnowplowTracker.setDomainUserId({
65
+ tracker: namespace,
66
+ domainUserId: newDuid,
67
+ })
68
+ );
69
+ }
70
+
71
+ /**
72
+ * Sets the ipAddress of the tracker subject
73
+ *
74
+ * @param namespace {string} - the tracker namespace
75
+ * @param newIp {string | null} - the new ipAddress
76
+ * @returns - Promise
77
+ */
78
+ function setIpAddress(namespace: string, newIp: string | null): Promise<void> {
79
+ if (!(newIp === null || typeof newIp === 'string')) {
80
+ return Promise.reject(new Error(logMessages.setIpAddress));
81
+ }
82
+ return <Promise<void>>Promise.resolve(
83
+ RNSnowplowTracker.setIpAddress({
84
+ tracker: namespace,
85
+ ipAddress: newIp,
86
+ })
87
+ );
88
+ }
89
+
90
+ /**
91
+ * Sets the useragent of the tracker subject
92
+ *
93
+ * @param namespace {string} - the tracker namespace
94
+ * @param newUagent {string | null} - the new useragent
95
+ * @returns - Promise
96
+ */
97
+ function setUseragent(
98
+ namespace: string,
99
+ newUagent: string | null
100
+ ): Promise<void> {
101
+ if (!(newUagent === null || typeof newUagent === 'string')) {
102
+ return Promise.reject(new Error(logMessages.setUseragent));
103
+ }
104
+ return <Promise<void>>Promise.resolve(
105
+ RNSnowplowTracker.setUseragent({
106
+ tracker: namespace,
107
+ useragent: newUagent,
108
+ })
109
+ );
110
+ }
111
+
112
+ /**
113
+ * Sets the timezone of the tracker subject
114
+ *
115
+ * @param namespace {string} - the tracker namespace
116
+ * @param newTz {string | null} - the new timezone
117
+ * @returns - Promise
118
+ */
119
+ function setTimezone(namespace: string, newTz: string | null): Promise<void> {
120
+ if (!(newTz === null || typeof newTz === 'string')) {
121
+ return Promise.reject(new Error(logMessages.setTimezone));
122
+ }
123
+ return <Promise<void>>Promise.resolve(
124
+ RNSnowplowTracker.setTimezone({
125
+ tracker: namespace,
126
+ timezone: newTz,
127
+ })
128
+ );
129
+ }
130
+
131
+ /**
132
+ * Sets the language of the tracker subject
133
+ *
134
+ * @param namespace {string} - the tracker namespace
135
+ * @param newLang {string | null} - the new language
136
+ * @returns - Promise
137
+ */
138
+ function setLanguage(namespace: string, newLang: string | null): Promise<void> {
139
+ if (!(newLang === null || typeof newLang === 'string')) {
140
+ return Promise.reject(new Error(logMessages.setLanguage));
141
+ }
142
+ return <Promise<void>>Promise.resolve(
143
+ RNSnowplowTracker.setLanguage({
144
+ tracker: namespace,
145
+ language: newLang,
146
+ })
147
+ );
148
+ }
149
+
150
+ /**
151
+ * Sets the screenResolution of the tracker subject
152
+ *
153
+ * @param namespace {string} - the tracker namespace
154
+ * @param newRes {ScreenSize | null} - the new screenResolution
155
+ * @returns - Promise
156
+ */
157
+ function setScreenResolution(
158
+ namespace: string,
159
+ newRes: ScreenSize | null
160
+ ): Promise<void> {
161
+ if (!(newRes === null || isScreenSize(newRes))) {
162
+ return Promise.reject(new Error(logMessages.setScreenResolution));
163
+ }
164
+ return <Promise<void>>Promise.resolve(
165
+ RNSnowplowTracker.setScreenResolution({
166
+ tracker: namespace,
167
+ screenResolution: newRes,
168
+ })
169
+ );
170
+ }
171
+
172
+ /**
173
+ * Sets the screenViewport of the tracker subject
174
+ *
175
+ * @param namespace {string} - the tracker namespace
176
+ * @param newView {ScreenSize | null} - the new screenViewport
177
+ * @returns - Promise
178
+ */
179
+ function setScreenViewport(
180
+ namespace: string,
181
+ newView: ScreenSize | null
182
+ ): Promise<void> {
183
+ if (!(newView === null || isScreenSize(newView))) {
184
+ return Promise.reject(new Error(logMessages.setScreenViewport));
185
+ }
186
+ return <Promise<void>>Promise.resolve(
187
+ RNSnowplowTracker.setScreenViewport({
188
+ tracker: namespace,
189
+ screenViewport: newView,
190
+ })
191
+ );
192
+ }
193
+
194
+ /**
195
+ * Sets the colorDepth of the tracker subject
196
+ *
197
+ * @param namespace {string} - the tracker namespace
198
+ * @param newColorD {number | null} - the new colorDepth
199
+ * @returns - Promise
200
+ */
201
+ function setColorDepth(
202
+ namespace: string,
203
+ newColorD: number | null
204
+ ): Promise<void> {
205
+ if (!(newColorD === null || typeof newColorD === 'number')) {
206
+ return Promise.reject(new Error(logMessages.setColorDepth));
207
+ }
208
+ return <Promise<void>>Promise.resolve(
209
+ RNSnowplowTracker.setColorDepth({
210
+ tracker: namespace,
211
+ colorDepth: newColorD,
212
+ })
213
+ );
214
+ }
215
+
216
+ const setterMap = {
217
+ userId: setUserId,
218
+ networkUserId: setNetworkUserId,
219
+ domainUserId: setDomainUserId,
220
+ ipAddress: setIpAddress,
221
+ useragent: setUseragent,
222
+ timezone: setTimezone,
223
+ language: setLanguage,
224
+ screenResolution: setScreenResolution,
225
+ screenViewport: setScreenViewport,
226
+ colorDepth: setColorDepth,
227
+ } as Record<
228
+ keyof SubjectConfiguration,
229
+ <Type>(namespace: string, x: Type | null) => Promise<void>
230
+ >;
231
+
232
+ /**
233
+ * Sets the tracker subject
234
+ *
235
+ * @param namespace {string} - the tracker namespace
236
+ * @param config {SubjectConfiguration} - the new subject data
237
+ * @returns - Promise
238
+ */
239
+ function setSubjectData(
240
+ namespace: string,
241
+ config: SubjectConfiguration
242
+ ): Promise<void> {
243
+ if (!isValidSubjectConf(config)) {
244
+ return Promise.reject(
245
+ new Error(`${logMessages.setSubjectData} ${logMessages.subject}`)
246
+ );
247
+ }
248
+
249
+ const promises = Object.keys(config)
250
+ .map((k) => {
251
+ const fun = setterMap[k];
252
+ return fun ? fun(namespace, config[k]) : undefined;
253
+ })
254
+ .filter((f) => f !== undefined);
255
+
256
+ // to use Promise.all (Promise.allSettled not supported in all RN versions)
257
+ const safePromises = promises.map((p) =>
258
+ (p as Promise<void>)
259
+ .then(
260
+ (x) =>
261
+ Object.assign({
262
+ status: 'fulfilled',
263
+ value: x,
264
+ }) as PromiseFulfilledResult<void>
265
+ )
266
+ .catch(
267
+ (err) =>
268
+ Object.assign({
269
+ status: 'rejected',
270
+ reason: err.message as string,
271
+ }) as PromiseRejectedResult
272
+ )
273
+ );
274
+
275
+ return <Promise<void>>Promise.all(safePromises).then((outcomes) => {
276
+ const anyReasons = outcomes.filter(
277
+ (res) => res.status === 'rejected'
278
+ ) as PromiseRejectedResult[];
279
+ if (anyReasons.length > 0) {
280
+ const allReasons = anyReasons.reduce(
281
+ (acc, curr) => acc + ':' + curr.reason,
282
+ logMessages.setSubjectData
283
+ );
284
+ throw new Error(allReasons);
285
+ }
286
+ return true;
287
+ });
288
+ }
289
+
290
+ export {
291
+ setUserId,
292
+ setNetworkUserId,
293
+ setDomainUserId,
294
+ setIpAddress,
295
+ setUseragent,
296
+ setTimezone,
297
+ setLanguage,
298
+ setScreenResolution,
299
+ setScreenViewport,
300
+ setColorDepth,
301
+ setSubjectData,
302
+ };
package/src/tracker.ts ADDED
@@ -0,0 +1,377 @@
1
+ 'use strict';
2
+
3
+ import { RNSnowplowTracker } from './native';
4
+ import { logMessages } from './constants';
5
+ import {
6
+ validateContexts,
7
+ validateSelfDesc,
8
+ validateScreenView,
9
+ validateScrollChanged,
10
+ validateListItemView,
11
+ validateStructured,
12
+ validatePageView,
13
+ validateTiming,
14
+ validateConsentGranted,
15
+ validateConsentWithdrawn,
16
+ validateEcommerceTransaction,
17
+ validateDeepLinkReceived,
18
+ validateMessageNotification,
19
+ } from './events';
20
+ import type {
21
+ SelfDescribing,
22
+ EventContext,
23
+ ScreenViewProps,
24
+ StructuredProps,
25
+ PageViewProps,
26
+ TimingProps,
27
+ ConsentGrantedProps,
28
+ ConsentWithdrawnProps,
29
+ EcommerceTransactionProps,
30
+ DeepLinkReceivedProps,
31
+ MessageNotificationProps,
32
+ ScrollChangedProps,
33
+ ListItemViewProps,
34
+ } from './types';
35
+
36
+ /**
37
+ * Tracks a self-describing event
38
+ *
39
+ * @param namespace {string} - the tracker namespace
40
+ * @param argmap {Object} - the event data
41
+ * @param contexts {Array}- the event contexts
42
+ * @returns {Promise}
43
+ */
44
+ function trackSelfDescribingEvent(
45
+ namespace: string | null,
46
+ argmap: SelfDescribing,
47
+ contexts: EventContext[] = []
48
+ ): Promise<void> {
49
+ return <Promise<void>>validateSelfDesc(argmap)
50
+ .then(() => validateContexts(contexts))
51
+ .then(
52
+ () => <Promise<void>>RNSnowplowTracker.trackSelfDescribingEvent({
53
+ tracker: namespace,
54
+ eventData: argmap,
55
+ contexts: contexts,
56
+ })
57
+ )
58
+ .catch((error) => {
59
+ throw new Error(`${logMessages.trackSelfDesc} ${error.message}`);
60
+ });
61
+ }
62
+
63
+ /**
64
+ * Tracks a screen-view event
65
+ *
66
+ * @param namespace {string} - the tracker namespace
67
+ * @param argmap {Object} - the event data
68
+ * @param contexts {Array}- the event contexts
69
+ * @returns {Promise}
70
+ */
71
+ function trackScreenViewEvent(
72
+ namespace: string | null,
73
+ argmap: ScreenViewProps,
74
+ contexts: EventContext[] = []
75
+ ): Promise<void> {
76
+ return <Promise<void>>validateScreenView(argmap)
77
+ .then(() => validateContexts(contexts))
78
+ .then(
79
+ () => <Promise<void>>RNSnowplowTracker.trackScreenViewEvent({
80
+ tracker: namespace,
81
+ eventData: argmap,
82
+ contexts: contexts,
83
+ })
84
+ )
85
+ .catch((error) => {
86
+ throw new Error(`${logMessages.trackScreenView} ${error.message}`);
87
+ });
88
+ }
89
+
90
+ /**
91
+ * Tracks a scroll-changed event
92
+ *
93
+ * @param namespace {string} - the tracker namespace
94
+ * @param argmap {Object} - the event data
95
+ * @param contexts {Array}- the event contexts
96
+ * @returns {Promise}
97
+ */
98
+ function trackScrollChangedEvent(
99
+ namespace: string | null,
100
+ argmap: ScrollChangedProps,
101
+ contexts: EventContext[] = []
102
+ ): Promise<void> {
103
+ return <Promise<void>>validateScrollChanged(argmap)
104
+ .then(() => validateContexts(contexts))
105
+ .then(
106
+ () => <Promise<void>>RNSnowplowTracker.trackScrollChangedEvent({
107
+ tracker: namespace,
108
+ eventData: argmap,
109
+ contexts: contexts,
110
+ })
111
+ )
112
+ .catch((error) => {
113
+ throw new Error(`${logMessages.trackScrollChanged} ${error.message}`);
114
+ });
115
+ }
116
+
117
+ /**
118
+ * Tracks a list item view event
119
+ *
120
+ * @param namespace {string} - the tracker namespace
121
+ * @param argmap {Object} - the event data
122
+ * @param contexts {Array}- the event contexts
123
+ * @returns {Promise}
124
+ */
125
+ function trackListItemViewEvent(
126
+ namespace: string | null,
127
+ argmap: ListItemViewProps,
128
+ contexts: EventContext[] = []
129
+ ): Promise<void> {
130
+ return <Promise<void>>validateListItemView(argmap)
131
+ .then(() => validateContexts(contexts))
132
+ .then(
133
+ () => <Promise<void>>RNSnowplowTracker.trackListItemViewEvent({
134
+ tracker: namespace,
135
+ eventData: argmap,
136
+ contexts: contexts,
137
+ })
138
+ )
139
+ .catch((error) => {
140
+ throw new Error(`${logMessages.trackListItemView} ${error.message}`);
141
+ });
142
+ }
143
+
144
+ /**
145
+ * Tracks a structured event
146
+ *
147
+ * @param namespace {string} - the tracker namespace
148
+ * @param argmap {Object} - the event data
149
+ * @param contexts {Array}- the event contexts
150
+ * @returns {Promise}
151
+ */
152
+ function trackStructuredEvent(
153
+ namespace: string | null,
154
+ argmap: StructuredProps,
155
+ contexts: EventContext[] = []
156
+ ): Promise<void> {
157
+ return <Promise<void>>validateStructured(argmap)
158
+ .then(() => validateContexts(contexts))
159
+ .then(
160
+ () => <Promise<void>>RNSnowplowTracker.trackStructuredEvent({
161
+ tracker: namespace,
162
+ eventData: argmap,
163
+ contexts: contexts,
164
+ })
165
+ )
166
+ .catch((error) => {
167
+ throw new Error(`${logMessages.trackStructured} ${error.message}`);
168
+ });
169
+ }
170
+
171
+ /**
172
+ * Tracks a page-view event
173
+ *
174
+ * @param namespace {string} - the tracker namespace
175
+ * @param argmap {Object} - the event data
176
+ * @param contexts {Array}- the event contexts
177
+ * @returns {Promise}
178
+ */
179
+ function trackPageViewEvent(
180
+ namespace: string | null,
181
+ argmap: PageViewProps,
182
+ contexts: EventContext[] = []
183
+ ): Promise<void> {
184
+ return <Promise<void>>validatePageView(argmap)
185
+ .then(() => validateContexts(contexts))
186
+ .then(
187
+ () => <Promise<void>>RNSnowplowTracker.trackPageViewEvent({
188
+ tracker: namespace,
189
+ eventData: argmap,
190
+ contexts: contexts,
191
+ })
192
+ )
193
+ .catch((error) => {
194
+ throw new Error(`${logMessages.trackPageView} ${error.message}`);
195
+ });
196
+ }
197
+
198
+ /**
199
+ * Tracks a timing event
200
+ *
201
+ * @param namespace {string} - the tracker namespace
202
+ * @param argmap {Object} - the event data
203
+ * @param contexts {Array}- the event contexts
204
+ * @returns {Promise}
205
+ */
206
+ function trackTimingEvent(
207
+ namespace: string | null,
208
+ argmap: TimingProps,
209
+ contexts: EventContext[] = []
210
+ ): Promise<void> {
211
+ return <Promise<void>>validateTiming(argmap)
212
+ .then(() => validateContexts(contexts))
213
+ .then(
214
+ () => <Promise<void>>RNSnowplowTracker.trackTimingEvent({
215
+ tracker: namespace,
216
+ eventData: argmap,
217
+ contexts: contexts,
218
+ })
219
+ )
220
+ .catch((error) => {
221
+ throw new Error(`${logMessages.trackTiming} ${error.message}`);
222
+ });
223
+ }
224
+
225
+ /**
226
+ * Tracks a consent-granted event
227
+ *
228
+ * @param namespace {string} - the tracker namespace
229
+ * @param argmap {Object} - the event data
230
+ * @param contexts {Array}- the event contexts
231
+ * @returns {Promise}
232
+ */
233
+ function trackConsentGrantedEvent(
234
+ namespace: string | null,
235
+ argmap: ConsentGrantedProps,
236
+ contexts: EventContext[] = []
237
+ ): Promise<void> {
238
+ return <Promise<void>>validateConsentGranted(argmap)
239
+ .then(() => validateContexts(contexts))
240
+ .then(
241
+ () => <Promise<void>>RNSnowplowTracker.trackConsentGrantedEvent({
242
+ tracker: namespace,
243
+ eventData: argmap,
244
+ contexts: contexts,
245
+ })
246
+ )
247
+ .catch((error) => {
248
+ throw new Error(`${logMessages.trackConsentGranted} ${error.message}`);
249
+ });
250
+ }
251
+
252
+ /**
253
+ * Tracks a consent-withdrawn event
254
+ *
255
+ * @param namespace {string} - the tracker namespace
256
+ * @param argmap {Object} - the event data
257
+ * @param contexts {Array}- the event contexts
258
+ * @returns {Promise}
259
+ */
260
+ function trackConsentWithdrawnEvent(
261
+ namespace: string | null,
262
+ argmap: ConsentWithdrawnProps,
263
+ contexts: EventContext[] = []
264
+ ): Promise<void> {
265
+ return <Promise<void>>validateConsentWithdrawn(argmap)
266
+ .then(() => validateContexts(contexts))
267
+ .then(
268
+ () => <Promise<void>>RNSnowplowTracker.trackConsentWithdrawnEvent({
269
+ tracker: namespace,
270
+ eventData: argmap,
271
+ contexts: contexts,
272
+ })
273
+ )
274
+ .catch((error) => {
275
+ throw new Error(`${logMessages.trackConsentWithdrawn} ${error.message}`);
276
+ });
277
+ }
278
+
279
+ /**
280
+ * Tracks an ecommerce-transaction event
281
+ *
282
+ * @param namespace {string} - the tracker namespace
283
+ * @param argmap {Object} - the event data
284
+ * @param contexts {Array}- the event contexts
285
+ * @returns {Promise}
286
+ */
287
+ function trackEcommerceTransactionEvent(
288
+ namespace: string | null,
289
+ argmap: EcommerceTransactionProps,
290
+ contexts: EventContext[] = []
291
+ ): Promise<void> {
292
+ return <Promise<void>>validateEcommerceTransaction(argmap)
293
+ .then(() => validateContexts(contexts))
294
+ .then(
295
+ () => <Promise<void>>RNSnowplowTracker.trackEcommerceTransactionEvent({
296
+ tracker: namespace,
297
+ eventData: argmap,
298
+ contexts: contexts,
299
+ })
300
+ )
301
+ .catch((error) => {
302
+ throw new Error(
303
+ `${logMessages.trackEcommerceTransaction} ${error.message}`
304
+ );
305
+ });
306
+ }
307
+
308
+ /**
309
+ * Tracks a deep link received event
310
+ *
311
+ * @param namespace {string} - the tracker namespace
312
+ * @param argmap {Object} - the event data
313
+ * @param contexts {Array}- the event contexts
314
+ * @returns {Promise}
315
+ */
316
+ function trackDeepLinkReceivedEvent(
317
+ namespace: string | null,
318
+ argmap: DeepLinkReceivedProps,
319
+ contexts: EventContext[] = []
320
+ ): Promise<void> {
321
+ return <Promise<void>>validateDeepLinkReceived(argmap)
322
+ .then(() => validateContexts(contexts))
323
+ .then(
324
+ () => <Promise<void>>RNSnowplowTracker.trackDeepLinkReceivedEvent({
325
+ tracker: namespace,
326
+ eventData: argmap,
327
+ contexts: contexts,
328
+ })
329
+ )
330
+ .catch((error) => {
331
+ throw new Error(`${logMessages.trackDeepLinkReceived} ${error.message}`);
332
+ });
333
+ }
334
+
335
+ /**
336
+ * Tracks a message notification event
337
+ *
338
+ * @param namespace {string} - the tracker namespace
339
+ * @param argmap {Object} - the event data
340
+ * @param contexts {Array}- the event contexts
341
+ * @returns {Promise}
342
+ */
343
+ function trackMessageNotificationEvent(
344
+ namespace: string | null,
345
+ argmap: MessageNotificationProps,
346
+ contexts: EventContext[] = []
347
+ ): Promise<void> {
348
+ return <Promise<void>>validateMessageNotification(argmap)
349
+ .then(() => validateContexts(contexts))
350
+ .then(
351
+ () => <Promise<void>>RNSnowplowTracker.trackMessageNotificationEvent({
352
+ tracker: namespace,
353
+ eventData: argmap,
354
+ contexts: contexts,
355
+ })
356
+ )
357
+ .catch((error) => {
358
+ throw new Error(
359
+ `${logMessages.trackMessageNotification} ${error.message}`
360
+ );
361
+ });
362
+ }
363
+
364
+ export {
365
+ trackSelfDescribingEvent,
366
+ trackScreenViewEvent,
367
+ trackScrollChangedEvent,
368
+ trackListItemViewEvent,
369
+ trackStructuredEvent,
370
+ trackPageViewEvent,
371
+ trackTimingEvent,
372
+ trackConsentGrantedEvent,
373
+ trackConsentWithdrawnEvent,
374
+ trackEcommerceTransactionEvent,
375
+ trackDeepLinkReceivedEvent,
376
+ trackMessageNotificationEvent,
377
+ };