@snowplow/react-native-tracker 1.3.0 → 2.0.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 +150 -0
  8. package/.github/workflows/deploy.yml +98 -0
  9. package/.github/workflows/e2e-android.yml +121 -0
  10. package/.github/workflows/e2e-ios.yml +90 -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 +166 -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 +704 -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 +271 -0
  32. package/android/src/main/java/com/snowplow/reactnativetracker/util/EventUtil.kt +213 -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 +108 -0
  73. package/example/e2e/environment.js +23 -0
  74. package/example/e2e/helpers/microCommands.js +153 -0
  75. package/example/e2e/helpers/microHelpers.js +346 -0
  76. package/example/e2e/helpers/microHelpers.test.js +308 -0
  77. package/example/e2e/helpers/schemas.js +62 -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 +300 -0
  81. package/example/index.js +5 -0
  82. package/example/ios/.xcode.env +11 -0
  83. package/example/ios/Podfile +64 -0
  84. package/example/ios/Podfile.lock +737 -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 +557 -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 +165 -0
  107. package/ios/ReactNativeTracker.swift +757 -0
  108. package/ios/{RNSnowplowTracker.xcodeproj → ReactNativeTracker.xcodeproj}/project.pbxproj +55 -79
  109. package/ios/Util/ConfigUtils.swift +206 -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 +475 -0
  114. package/lib/commonjs/api.js.map +1 -0
  115. package/lib/commonjs/configurations.js +209 -0
  116. package/lib/commonjs/configurations.js.map +1 -0
  117. package/lib/commonjs/constants.js +85 -0
  118. package/lib/commonjs/constants.js.map +1 -0
  119. package/lib/commonjs/events.js +256 -0
  120. package/lib/commonjs/events.js.map +1 -0
  121. package/lib/commonjs/index.js +141 -0
  122. package/lib/commonjs/index.js.map +1 -0
  123. package/lib/commonjs/jsCore.js +393 -0
  124. package/lib/commonjs/jsCore.js.map +1 -0
  125. package/lib/commonjs/native.js +29 -0
  126. package/lib/commonjs/native.js.map +1 -0
  127. package/lib/commonjs/subject.js +248 -0
  128. package/lib/commonjs/subject.js.map +1 -0
  129. package/lib/commonjs/tracker.js +221 -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 +85 -0
  134. package/lib/commonjs/utils.js.map +1 -0
  135. package/lib/commonjs/webViewInterface.js +79 -0
  136. package/lib/commonjs/webViewInterface.js.map +1 -0
  137. package/lib/module/api.js +439 -0
  138. package/lib/module/api.js.map +1 -0
  139. package/lib/module/configurations.js +197 -0
  140. package/lib/module/configurations.js.map +1 -0
  141. package/lib/module/constants.js +80 -0
  142. package/lib/module/constants.js.map +1 -0
  143. package/lib/module/events.js +240 -0
  144. package/lib/module/events.js.map +1 -0
  145. package/lib/module/index.js +129 -0
  146. package/lib/module/index.js.map +1 -0
  147. package/lib/module/jsCore.js +390 -0
  148. package/lib/module/jsCore.js.map +1 -0
  149. package/lib/module/native.js +25 -0
  150. package/lib/module/native.js.map +1 -0
  151. package/lib/module/subject.js +235 -0
  152. package/lib/module/subject.js.map +1 -0
  153. package/lib/module/tracker.js +209 -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 +79 -0
  158. package/lib/module/utils.js.map +1 -0
  159. package/lib/module/webViewInterface.js +75 -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 +226 -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 +59 -0
  178. package/lib/typescript/constants.d.ts.map +1 -0
  179. package/lib/typescript/events.d.ts +101 -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 +140 -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 +93 -0
  190. package/lib/typescript/tracker.d.ts.map +1 -0
  191. package/{dist/index.d.ts → lib/typescript/types.d.ts} +188 -75
  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 +138 -51
  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 +34 -0
  202. package/src/__tests__/configurations.test.ts +620 -0
  203. package/src/__tests__/events.test.ts +749 -0
  204. package/src/__tests__/index.test.ts +27 -0
  205. package/src/__tests__/jsCore.test.ts +169 -0
  206. package/src/__tests__/utils.test.ts +23 -0
  207. package/src/api.ts +535 -0
  208. package/src/configurations.ts +337 -0
  209. package/src/constants.ts +102 -0
  210. package/src/events.ts +330 -0
  211. package/src/index.ts +189 -0
  212. package/src/jsCore.ts +553 -0
  213. package/src/native.ts +34 -0
  214. package/src/subject.ts +315 -0
  215. package/src/tracker.ts +330 -0
  216. package/src/types.ts +1087 -0
  217. package/src/utils.ts +78 -0
  218. package/src/webViewInterface.ts +105 -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 -1650
  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,315 @@
1
+ /*
2
+ * Copyright (c) 2020-2023 Snowplow Analytics Ltd. All rights reserved.
3
+ *
4
+ * This program is licensed to you under the Apache License Version 2.0,
5
+ * and you may not use this file except in compliance with the Apache License Version 2.0.
6
+ * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
7
+ *
8
+ * Unless required by applicable law or agreed to in writing,
9
+ * software distributed under the Apache License Version 2.0 is distributed on an
10
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
12
+ */
13
+
14
+ 'use strict';
15
+
16
+ import { RNSnowplowTracker } from './native';
17
+ import { logMessages } from './constants';
18
+ import { isScreenSize, isValidSubjectConf } from './configurations';
19
+ import type { ScreenSize, SubjectConfiguration } from './types';
20
+
21
+ /**
22
+ * Sets the userId of the tracker subject
23
+ *
24
+ * @param namespace {string} - the tracker namespace
25
+ * @param newUid {string | null} - the new userId
26
+ * @returns - Promise
27
+ */
28
+ function setUserId(namespace: string, newUid: string | null): Promise<void> {
29
+ if (!(newUid === null || typeof newUid === 'string')) {
30
+ return Promise.reject(new Error(logMessages.setUserId));
31
+ }
32
+ return <Promise<void>>Promise.resolve(
33
+ RNSnowplowTracker.setUserId({
34
+ tracker: namespace,
35
+ userId: newUid,
36
+ })
37
+ );
38
+ }
39
+
40
+ /**
41
+ * Sets the networkUserId of the tracker subject
42
+ *
43
+ * @param namespace {string} - the tracker namespace
44
+ * @param newNuid {string | null} - the new networkUserId
45
+ * @returns - Promise
46
+ */
47
+ function setNetworkUserId(
48
+ namespace: string,
49
+ newNuid: string | null
50
+ ): Promise<void> {
51
+ if (!(newNuid === null || typeof newNuid === 'string')) {
52
+ return Promise.reject(new Error(logMessages.setNetworkUserId));
53
+ }
54
+ return <Promise<void>>Promise.resolve(
55
+ RNSnowplowTracker.setNetworkUserId({
56
+ tracker: namespace,
57
+ networkUserId: newNuid,
58
+ })
59
+ );
60
+ }
61
+
62
+ /**
63
+ * Sets the domainUserId of the tracker subject
64
+ *
65
+ * @param namespace {string} - the tracker namespace
66
+ * @param newDuid {string | null} - the new domainUserId
67
+ * @returns - Promise
68
+ */
69
+ function setDomainUserId(
70
+ namespace: string,
71
+ newDuid: string | null
72
+ ): Promise<void> {
73
+ if (!(newDuid === null || typeof newDuid === 'string')) {
74
+ return Promise.reject(new Error(logMessages.setDomainUserId));
75
+ }
76
+ return <Promise<void>>Promise.resolve(
77
+ RNSnowplowTracker.setDomainUserId({
78
+ tracker: namespace,
79
+ domainUserId: newDuid,
80
+ })
81
+ );
82
+ }
83
+
84
+ /**
85
+ * Sets the ipAddress of the tracker subject
86
+ *
87
+ * @param namespace {string} - the tracker namespace
88
+ * @param newIp {string | null} - the new ipAddress
89
+ * @returns - Promise
90
+ */
91
+ function setIpAddress(namespace: string, newIp: string | null): Promise<void> {
92
+ if (!(newIp === null || typeof newIp === 'string')) {
93
+ return Promise.reject(new Error(logMessages.setIpAddress));
94
+ }
95
+ return <Promise<void>>Promise.resolve(
96
+ RNSnowplowTracker.setIpAddress({
97
+ tracker: namespace,
98
+ ipAddress: newIp,
99
+ })
100
+ );
101
+ }
102
+
103
+ /**
104
+ * Sets the useragent of the tracker subject
105
+ *
106
+ * @param namespace {string} - the tracker namespace
107
+ * @param newUagent {string | null} - the new useragent
108
+ * @returns - Promise
109
+ */
110
+ function setUseragent(
111
+ namespace: string,
112
+ newUagent: string | null
113
+ ): Promise<void> {
114
+ if (!(newUagent === null || typeof newUagent === 'string')) {
115
+ return Promise.reject(new Error(logMessages.setUseragent));
116
+ }
117
+ return <Promise<void>>Promise.resolve(
118
+ RNSnowplowTracker.setUseragent({
119
+ tracker: namespace,
120
+ useragent: newUagent,
121
+ })
122
+ );
123
+ }
124
+
125
+ /**
126
+ * Sets the timezone of the tracker subject
127
+ *
128
+ * @param namespace {string} - the tracker namespace
129
+ * @param newTz {string | null} - the new timezone
130
+ * @returns - Promise
131
+ */
132
+ function setTimezone(namespace: string, newTz: string | null): Promise<void> {
133
+ if (!(newTz === null || typeof newTz === 'string')) {
134
+ return Promise.reject(new Error(logMessages.setTimezone));
135
+ }
136
+ return <Promise<void>>Promise.resolve(
137
+ RNSnowplowTracker.setTimezone({
138
+ tracker: namespace,
139
+ timezone: newTz,
140
+ })
141
+ );
142
+ }
143
+
144
+ /**
145
+ * Sets the language of the tracker subject
146
+ *
147
+ * @param namespace {string} - the tracker namespace
148
+ * @param newLang {string | null} - the new language
149
+ * @returns - Promise
150
+ */
151
+ function setLanguage(namespace: string, newLang: string | null): Promise<void> {
152
+ if (!(newLang === null || typeof newLang === 'string')) {
153
+ return Promise.reject(new Error(logMessages.setLanguage));
154
+ }
155
+ return <Promise<void>>Promise.resolve(
156
+ RNSnowplowTracker.setLanguage({
157
+ tracker: namespace,
158
+ language: newLang,
159
+ })
160
+ );
161
+ }
162
+
163
+ /**
164
+ * Sets the screenResolution of the tracker subject
165
+ *
166
+ * @param namespace {string} - the tracker namespace
167
+ * @param newRes {ScreenSize | null} - the new screenResolution
168
+ * @returns - Promise
169
+ */
170
+ function setScreenResolution(
171
+ namespace: string,
172
+ newRes: ScreenSize | null
173
+ ): Promise<void> {
174
+ if (!(newRes === null || isScreenSize(newRes))) {
175
+ return Promise.reject(new Error(logMessages.setScreenResolution));
176
+ }
177
+ return <Promise<void>>Promise.resolve(
178
+ RNSnowplowTracker.setScreenResolution({
179
+ tracker: namespace,
180
+ screenResolution: newRes,
181
+ })
182
+ );
183
+ }
184
+
185
+ /**
186
+ * Sets the screenViewport of the tracker subject
187
+ *
188
+ * @param namespace {string} - the tracker namespace
189
+ * @param newView {ScreenSize | null} - the new screenViewport
190
+ * @returns - Promise
191
+ */
192
+ function setScreenViewport(
193
+ namespace: string,
194
+ newView: ScreenSize | null
195
+ ): Promise<void> {
196
+ if (!(newView === null || isScreenSize(newView))) {
197
+ return Promise.reject(new Error(logMessages.setScreenViewport));
198
+ }
199
+ return <Promise<void>>Promise.resolve(
200
+ RNSnowplowTracker.setScreenViewport({
201
+ tracker: namespace,
202
+ screenViewport: newView,
203
+ })
204
+ );
205
+ }
206
+
207
+ /**
208
+ * Sets the colorDepth of the tracker subject
209
+ *
210
+ * @param namespace {string} - the tracker namespace
211
+ * @param newColorD {number | null} - the new colorDepth
212
+ * @returns - Promise
213
+ */
214
+ function setColorDepth(
215
+ namespace: string,
216
+ newColorD: number | null
217
+ ): Promise<void> {
218
+ if (!(newColorD === null || typeof newColorD === 'number')) {
219
+ return Promise.reject(new Error(logMessages.setColorDepth));
220
+ }
221
+ return <Promise<void>>Promise.resolve(
222
+ RNSnowplowTracker.setColorDepth({
223
+ tracker: namespace,
224
+ colorDepth: newColorD,
225
+ })
226
+ );
227
+ }
228
+
229
+ const setterMap = {
230
+ userId: setUserId,
231
+ networkUserId: setNetworkUserId,
232
+ domainUserId: setDomainUserId,
233
+ ipAddress: setIpAddress,
234
+ useragent: setUseragent,
235
+ timezone: setTimezone,
236
+ language: setLanguage,
237
+ screenResolution: setScreenResolution,
238
+ screenViewport: setScreenViewport,
239
+ colorDepth: setColorDepth,
240
+ } as Record<
241
+ keyof SubjectConfiguration,
242
+ <Type>(namespace: string, x: Type | null) => Promise<void>
243
+ >;
244
+
245
+ /**
246
+ * Sets the tracker subject
247
+ *
248
+ * @param namespace {string} - the tracker namespace
249
+ * @param config {SubjectConfiguration} - the new subject data
250
+ * @returns - Promise
251
+ */
252
+ function setSubjectData(
253
+ namespace: string,
254
+ config: SubjectConfiguration
255
+ ): Promise<void> {
256
+ if (!isValidSubjectConf(config)) {
257
+ return Promise.reject(
258
+ new Error(`${logMessages.setSubjectData} ${logMessages.subject}`)
259
+ );
260
+ }
261
+
262
+ const promises = Object.keys(config)
263
+ .map((k) => {
264
+ const fun = setterMap[k];
265
+ return fun ? fun(namespace, config[k]) : undefined;
266
+ })
267
+ .filter((f) => f !== undefined);
268
+
269
+ // to use Promise.all (Promise.allSettled not supported in all RN versions)
270
+ const safePromises = promises.map((p) =>
271
+ (p as Promise<void>)
272
+ .then(
273
+ (x) =>
274
+ Object.assign({
275
+ status: 'fulfilled',
276
+ value: x,
277
+ }) as PromiseFulfilledResult<void>
278
+ )
279
+ .catch(
280
+ (err) =>
281
+ Object.assign({
282
+ status: 'rejected',
283
+ reason: err.message as string,
284
+ }) as PromiseRejectedResult
285
+ )
286
+ );
287
+
288
+ return <Promise<void>>Promise.all(safePromises).then((outcomes) => {
289
+ const anyReasons = outcomes.filter(
290
+ (res) => res.status === 'rejected'
291
+ ) as PromiseRejectedResult[];
292
+ if (anyReasons.length > 0) {
293
+ const allReasons = anyReasons.reduce(
294
+ (acc, curr) => acc + ':' + curr.reason,
295
+ logMessages.setSubjectData
296
+ );
297
+ throw new Error(allReasons);
298
+ }
299
+ return true;
300
+ });
301
+ }
302
+
303
+ export {
304
+ setUserId,
305
+ setNetworkUserId,
306
+ setDomainUserId,
307
+ setIpAddress,
308
+ setUseragent,
309
+ setTimezone,
310
+ setLanguage,
311
+ setScreenResolution,
312
+ setScreenViewport,
313
+ setColorDepth,
314
+ setSubjectData,
315
+ };
package/src/tracker.ts ADDED
@@ -0,0 +1,330 @@
1
+ /*
2
+ * Copyright (c) 2020-2023 Snowplow Analytics Ltd. All rights reserved.
3
+ *
4
+ * This program is licensed to you under the Apache License Version 2.0,
5
+ * and you may not use this file except in compliance with the Apache License Version 2.0.
6
+ * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
7
+ *
8
+ * Unless required by applicable law or agreed to in writing,
9
+ * software distributed under the Apache License Version 2.0 is distributed on an
10
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
12
+ */
13
+
14
+ 'use strict';
15
+
16
+ import { RNSnowplowTracker } from './native';
17
+ import { logMessages } from './constants';
18
+ import {
19
+ validateContexts,
20
+ validateSelfDesc,
21
+ validateScreenView,
22
+ validateStructured,
23
+ validatePageView,
24
+ validateTiming,
25
+ validateConsentGranted,
26
+ validateConsentWithdrawn,
27
+ validateEcommerceTransaction,
28
+ validateDeepLinkReceived,
29
+ validateMessageNotification,
30
+ } from './events';
31
+ import type {
32
+ SelfDescribing,
33
+ EventContext,
34
+ ScreenViewProps,
35
+ StructuredProps,
36
+ PageViewProps,
37
+ TimingProps,
38
+ ConsentGrantedProps,
39
+ ConsentWithdrawnProps,
40
+ EcommerceTransactionProps,
41
+ DeepLinkReceivedProps,
42
+ MessageNotificationProps,
43
+ } from './types';
44
+
45
+ /**
46
+ * Tracks a self-describing event
47
+ *
48
+ * @param namespace {string} - the tracker namespace
49
+ * @param argmap {Object} - the event data
50
+ * @param contexts {Array}- the event contexts
51
+ * @returns {Promise}
52
+ */
53
+ function trackSelfDescribingEvent(
54
+ namespace: string | null,
55
+ argmap: SelfDescribing,
56
+ contexts: EventContext[] = []
57
+ ): Promise<void> {
58
+ return <Promise<void>>validateSelfDesc(argmap)
59
+ .then(() => validateContexts(contexts))
60
+ .then(
61
+ () => <Promise<void>>RNSnowplowTracker.trackSelfDescribingEvent({
62
+ tracker: namespace,
63
+ eventData: argmap,
64
+ contexts: contexts,
65
+ })
66
+ )
67
+ .catch((error) => {
68
+ throw new Error(`${logMessages.trackSelfDesc} ${error.message}`);
69
+ });
70
+ }
71
+
72
+ /**
73
+ * Tracks a screen-view event
74
+ *
75
+ * @param namespace {string} - the tracker namespace
76
+ * @param argmap {Object} - the event data
77
+ * @param contexts {Array}- the event contexts
78
+ * @returns {Promise}
79
+ */
80
+ function trackScreenViewEvent(
81
+ namespace: string | null,
82
+ argmap: ScreenViewProps,
83
+ contexts: EventContext[] = []
84
+ ): Promise<void> {
85
+ return <Promise<void>>validateScreenView(argmap)
86
+ .then(() => validateContexts(contexts))
87
+ .then(
88
+ () => <Promise<void>>RNSnowplowTracker.trackScreenViewEvent({
89
+ tracker: namespace,
90
+ eventData: argmap,
91
+ contexts: contexts,
92
+ })
93
+ )
94
+ .catch((error) => {
95
+ throw new Error(`${logMessages.trackScreenView} ${error.message}`);
96
+ });
97
+ }
98
+
99
+ /**
100
+ * Tracks a structured event
101
+ *
102
+ * @param namespace {string} - the tracker namespace
103
+ * @param argmap {Object} - the event data
104
+ * @param contexts {Array}- the event contexts
105
+ * @returns {Promise}
106
+ */
107
+ function trackStructuredEvent(
108
+ namespace: string | null,
109
+ argmap: StructuredProps,
110
+ contexts: EventContext[] = []
111
+ ): Promise<void> {
112
+ return <Promise<void>>validateStructured(argmap)
113
+ .then(() => validateContexts(contexts))
114
+ .then(
115
+ () => <Promise<void>>RNSnowplowTracker.trackStructuredEvent({
116
+ tracker: namespace,
117
+ eventData: argmap,
118
+ contexts: contexts,
119
+ })
120
+ )
121
+ .catch((error) => {
122
+ throw new Error(`${logMessages.trackStructured} ${error.message}`);
123
+ });
124
+ }
125
+
126
+ /**
127
+ * Tracks a page-view event
128
+ *
129
+ * @param namespace {string} - the tracker namespace
130
+ * @param argmap {Object} - the event data
131
+ * @param contexts {Array}- the event contexts
132
+ * @returns {Promise}
133
+ */
134
+ function trackPageViewEvent(
135
+ namespace: string | null,
136
+ argmap: PageViewProps,
137
+ contexts: EventContext[] = []
138
+ ): Promise<void> {
139
+ return <Promise<void>>validatePageView(argmap)
140
+ .then(() => validateContexts(contexts))
141
+ .then(
142
+ () => <Promise<void>>RNSnowplowTracker.trackPageViewEvent({
143
+ tracker: namespace,
144
+ eventData: argmap,
145
+ contexts: contexts,
146
+ })
147
+ )
148
+ .catch((error) => {
149
+ throw new Error(`${logMessages.trackPageView} ${error.message}`);
150
+ });
151
+ }
152
+
153
+ /**
154
+ * Tracks a timing event
155
+ *
156
+ * @param namespace {string} - the tracker namespace
157
+ * @param argmap {Object} - the event data
158
+ * @param contexts {Array}- the event contexts
159
+ * @returns {Promise}
160
+ */
161
+ function trackTimingEvent(
162
+ namespace: string | null,
163
+ argmap: TimingProps,
164
+ contexts: EventContext[] = []
165
+ ): Promise<void> {
166
+ return <Promise<void>>validateTiming(argmap)
167
+ .then(() => validateContexts(contexts))
168
+ .then(
169
+ () => <Promise<void>>RNSnowplowTracker.trackTimingEvent({
170
+ tracker: namespace,
171
+ eventData: argmap,
172
+ contexts: contexts,
173
+ })
174
+ )
175
+ .catch((error) => {
176
+ throw new Error(`${logMessages.trackTiming} ${error.message}`);
177
+ });
178
+ }
179
+
180
+ /**
181
+ * Tracks a consent-granted event
182
+ *
183
+ * @param namespace {string} - the tracker namespace
184
+ * @param argmap {Object} - the event data
185
+ * @param contexts {Array}- the event contexts
186
+ * @returns {Promise}
187
+ */
188
+ function trackConsentGrantedEvent(
189
+ namespace: string | null,
190
+ argmap: ConsentGrantedProps,
191
+ contexts: EventContext[] = []
192
+ ): Promise<void> {
193
+ return <Promise<void>>validateConsentGranted(argmap)
194
+ .then(() => validateContexts(contexts))
195
+ .then(
196
+ () => <Promise<void>>RNSnowplowTracker.trackConsentGrantedEvent({
197
+ tracker: namespace,
198
+ eventData: argmap,
199
+ contexts: contexts,
200
+ })
201
+ )
202
+ .catch((error) => {
203
+ throw new Error(`${logMessages.trackConsentGranted} ${error.message}`);
204
+ });
205
+ }
206
+
207
+ /**
208
+ * Tracks a consent-withdrawn event
209
+ *
210
+ * @param namespace {string} - the tracker namespace
211
+ * @param argmap {Object} - the event data
212
+ * @param contexts {Array}- the event contexts
213
+ * @returns {Promise}
214
+ */
215
+ function trackConsentWithdrawnEvent(
216
+ namespace: string | null,
217
+ argmap: ConsentWithdrawnProps,
218
+ contexts: EventContext[] = []
219
+ ): Promise<void> {
220
+ return <Promise<void>>validateConsentWithdrawn(argmap)
221
+ .then(() => validateContexts(contexts))
222
+ .then(
223
+ () => <Promise<void>>RNSnowplowTracker.trackConsentWithdrawnEvent({
224
+ tracker: namespace,
225
+ eventData: argmap,
226
+ contexts: contexts,
227
+ })
228
+ )
229
+ .catch((error) => {
230
+ throw new Error(`${logMessages.trackConsentWithdrawn} ${error.message}`);
231
+ });
232
+ }
233
+
234
+ /**
235
+ * Tracks an ecommerce-transaction event
236
+ *
237
+ * @param namespace {string} - the tracker namespace
238
+ * @param argmap {Object} - the event data
239
+ * @param contexts {Array}- the event contexts
240
+ * @returns {Promise}
241
+ */
242
+ function trackEcommerceTransactionEvent(
243
+ namespace: string | null,
244
+ argmap: EcommerceTransactionProps,
245
+ contexts: EventContext[] = []
246
+ ): Promise<void> {
247
+ return <Promise<void>>validateEcommerceTransaction(argmap)
248
+ .then(() => validateContexts(contexts))
249
+ .then(
250
+ () => <Promise<void>>RNSnowplowTracker.trackEcommerceTransactionEvent({
251
+ tracker: namespace,
252
+ eventData: argmap,
253
+ contexts: contexts,
254
+ })
255
+ )
256
+ .catch((error) => {
257
+ throw new Error(
258
+ `${logMessages.trackEcommerceTransaction} ${error.message}`
259
+ );
260
+ });
261
+ }
262
+
263
+ /**
264
+ * Tracks a deep link received event
265
+ *
266
+ * @param namespace {string} - the tracker namespace
267
+ * @param argmap {Object} - the event data
268
+ * @param contexts {Array}- the event contexts
269
+ * @returns {Promise}
270
+ */
271
+ function trackDeepLinkReceivedEvent(
272
+ namespace: string | null,
273
+ argmap: DeepLinkReceivedProps,
274
+ contexts: EventContext[] = []
275
+ ): Promise<void> {
276
+ return <Promise<void>>validateDeepLinkReceived(argmap)
277
+ .then(() => validateContexts(contexts))
278
+ .then(
279
+ () => <Promise<void>>RNSnowplowTracker.trackDeepLinkReceivedEvent({
280
+ tracker: namespace,
281
+ eventData: argmap,
282
+ contexts: contexts,
283
+ })
284
+ )
285
+ .catch((error) => {
286
+ throw new Error(`${logMessages.trackDeepLinkReceived} ${error.message}`);
287
+ });
288
+ }
289
+
290
+ /**
291
+ * Tracks a message notification event
292
+ *
293
+ * @param namespace {string} - the tracker namespace
294
+ * @param argmap {Object} - the event data
295
+ * @param contexts {Array}- the event contexts
296
+ * @returns {Promise}
297
+ */
298
+ function trackMessageNotificationEvent(
299
+ namespace: string | null,
300
+ argmap: MessageNotificationProps,
301
+ contexts: EventContext[] = []
302
+ ): Promise<void> {
303
+ return <Promise<void>>validateMessageNotification(argmap)
304
+ .then(() => validateContexts(contexts))
305
+ .then(
306
+ () => <Promise<void>>RNSnowplowTracker.trackMessageNotificationEvent({
307
+ tracker: namespace,
308
+ eventData: argmap,
309
+ contexts: contexts,
310
+ })
311
+ )
312
+ .catch((error) => {
313
+ throw new Error(
314
+ `${logMessages.trackMessageNotification} ${error.message}`
315
+ );
316
+ });
317
+ }
318
+
319
+ export {
320
+ trackSelfDescribingEvent,
321
+ trackScreenViewEvent,
322
+ trackStructuredEvent,
323
+ trackPageViewEvent,
324
+ trackTimingEvent,
325
+ trackConsentGrantedEvent,
326
+ trackConsentWithdrawnEvent,
327
+ trackEcommerceTransactionEvent,
328
+ trackDeepLinkReceivedEvent,
329
+ trackMessageNotificationEvent,
330
+ };