@jmartins-tillster/analytics-react-native 2.21.3

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 (602) hide show
  1. package/.editorconfig +10 -0
  2. package/.eslintignore +11 -0
  3. package/.eslintrc.js +60 -0
  4. package/.gitattributes +4 -0
  5. package/.github/ISSUE_TEMPLATE/BUG_REPORT.md +23 -0
  6. package/.github/ISSUE_TEMPLATE/FEATURE_REQUEST.md +18 -0
  7. package/.github/workflows/ci.yml +158 -0
  8. package/.github/workflows/create_jira.yml +41 -0
  9. package/.github/workflows/publish.yml +57 -0
  10. package/.husky/commit-msg +4 -0
  11. package/.husky/pre-commit +4 -0
  12. package/.prettierrc.js +7 -0
  13. package/.yarnrc.yml +2 -0
  14. package/CODE_OF_CONDUCT.md +84 -0
  15. package/CONTRIBUTING.md +141 -0
  16. package/LICENSE +21 -0
  17. package/MIGRATION_GUIDE.md +185 -0
  18. package/README.md +944 -0
  19. package/babel.config.js +3 -0
  20. package/commitlint.config.js +13 -0
  21. package/examples/AnalyticsReactNativeExample/.bundle/config +2 -0
  22. package/examples/AnalyticsReactNativeExample/.eslintrc.js +4 -0
  23. package/examples/AnalyticsReactNativeExample/.prettierrc.js +7 -0
  24. package/examples/AnalyticsReactNativeExample/.watchmanconfig +1 -0
  25. package/examples/AnalyticsReactNativeExample/App.tsx +164 -0
  26. package/examples/AnalyticsReactNativeExample/Gemfile +9 -0
  27. package/examples/AnalyticsReactNativeExample/Gemfile.lock +106 -0
  28. package/examples/AnalyticsReactNativeExample/Home.tsx +206 -0
  29. package/examples/AnalyticsReactNativeExample/Modal.tsx +25 -0
  30. package/examples/AnalyticsReactNativeExample/README.md +79 -0
  31. package/examples/AnalyticsReactNativeExample/SecondPage.tsx +25 -0
  32. package/examples/AnalyticsReactNativeExample/android/app/build.gradle +119 -0
  33. package/examples/AnalyticsReactNativeExample/android/app/debug.keystore +0 -0
  34. package/examples/AnalyticsReactNativeExample/android/app/proguard-rules.pro +10 -0
  35. package/examples/AnalyticsReactNativeExample/android/app/src/debug/AndroidManifest.xml +9 -0
  36. package/examples/AnalyticsReactNativeExample/android/app/src/main/AndroidManifest.xml +26 -0
  37. package/examples/AnalyticsReactNativeExample/android/app/src/main/java/com/analyticsreactnativeexample/MainActivity.kt +22 -0
  38. package/examples/AnalyticsReactNativeExample/android/app/src/main/java/com/analyticsreactnativeexample/MainApplication.kt +44 -0
  39. package/examples/AnalyticsReactNativeExample/android/app/src/main/res/drawable/rn_edit_text_material.xml +37 -0
  40. package/examples/AnalyticsReactNativeExample/android/app/src/main/res/mipmap-hdpi/ic_launcher.png +0 -0
  41. package/examples/AnalyticsReactNativeExample/android/app/src/main/res/mipmap-hdpi/ic_launcher_round.png +0 -0
  42. package/examples/AnalyticsReactNativeExample/android/app/src/main/res/mipmap-mdpi/ic_launcher.png +0 -0
  43. package/examples/AnalyticsReactNativeExample/android/app/src/main/res/mipmap-mdpi/ic_launcher_round.png +0 -0
  44. package/examples/AnalyticsReactNativeExample/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png +0 -0
  45. package/examples/AnalyticsReactNativeExample/android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png +0 -0
  46. package/examples/AnalyticsReactNativeExample/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png +0 -0
  47. package/examples/AnalyticsReactNativeExample/android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png +0 -0
  48. package/examples/AnalyticsReactNativeExample/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png +0 -0
  49. package/examples/AnalyticsReactNativeExample/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png +0 -0
  50. package/examples/AnalyticsReactNativeExample/android/app/src/main/res/values/strings.xml +3 -0
  51. package/examples/AnalyticsReactNativeExample/android/app/src/main/res/values/styles.xml +9 -0
  52. package/examples/AnalyticsReactNativeExample/android/build.gradle +21 -0
  53. package/examples/AnalyticsReactNativeExample/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  54. package/examples/AnalyticsReactNativeExample/android/gradle/wrapper/gradle-wrapper.properties +8 -0
  55. package/examples/AnalyticsReactNativeExample/android/gradle.properties +39 -0
  56. package/examples/AnalyticsReactNativeExample/android/gradlew +252 -0
  57. package/examples/AnalyticsReactNativeExample/android/gradlew.bat +94 -0
  58. package/examples/AnalyticsReactNativeExample/android/settings.gradle +6 -0
  59. package/examples/AnalyticsReactNativeExample/app.json +4 -0
  60. package/examples/AnalyticsReactNativeExample/babel.config.js +3 -0
  61. package/examples/AnalyticsReactNativeExample/index.js +9 -0
  62. package/examples/AnalyticsReactNativeExample/ios/.xcode.env +11 -0
  63. package/examples/AnalyticsReactNativeExample/ios/AnalyticsReactNativeExample/AppDelegate.h +6 -0
  64. package/examples/AnalyticsReactNativeExample/ios/AnalyticsReactNativeExample/AppDelegate.mm +31 -0
  65. package/examples/AnalyticsReactNativeExample/ios/AnalyticsReactNativeExample/Images.xcassets/AppIcon.appiconset/Contents.json +53 -0
  66. package/examples/AnalyticsReactNativeExample/ios/AnalyticsReactNativeExample/Images.xcassets/Contents.json +6 -0
  67. package/examples/AnalyticsReactNativeExample/ios/AnalyticsReactNativeExample/Info.plist +52 -0
  68. package/examples/AnalyticsReactNativeExample/ios/AnalyticsReactNativeExample/LaunchScreen.storyboard +47 -0
  69. package/examples/AnalyticsReactNativeExample/ios/AnalyticsReactNativeExample/PrivacyInfo.xcprivacy +37 -0
  70. package/examples/AnalyticsReactNativeExample/ios/AnalyticsReactNativeExample/main.m +10 -0
  71. package/examples/AnalyticsReactNativeExample/ios/AnalyticsReactNativeExample.xcodeproj/project.pbxproj +705 -0
  72. package/examples/AnalyticsReactNativeExample/ios/AnalyticsReactNativeExample.xcodeproj/xcshareddata/xcschemes/AnalyticsReactNativeExample.xcscheme +88 -0
  73. package/examples/AnalyticsReactNativeExample/ios/AnalyticsReactNativeExample.xcworkspace/contents.xcworkspacedata +10 -0
  74. package/examples/AnalyticsReactNativeExample/ios/AnalyticsReactNativeExample.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
  75. package/examples/AnalyticsReactNativeExample/ios/AnalyticsReactNativeExampleTests/AnalyticsReactNativeExampleTests.m +66 -0
  76. package/examples/AnalyticsReactNativeExample/ios/AnalyticsReactNativeExampleTests/Info.plist +24 -0
  77. package/examples/AnalyticsReactNativeExample/ios/Podfile +40 -0
  78. package/examples/AnalyticsReactNativeExample/ios/Podfile.lock +1961 -0
  79. package/examples/AnalyticsReactNativeExample/metro.config.js +11 -0
  80. package/examples/AnalyticsReactNativeExample/package.json +55 -0
  81. package/examples/AnalyticsReactNativeExample/plugins/ConsentManager.tsx +75 -0
  82. package/examples/AnalyticsReactNativeExample/plugins/Logger.ts +16 -0
  83. package/examples/AnalyticsReactNativeExample/react-native.config.js +9 -0
  84. package/examples/AnalyticsReactNativeExample/tsconfig.json +3 -0
  85. package/examples/AnalyticsReactNativeExample/workspace.js +5 -0
  86. package/examples/AnalyticsReactNativeExample/yarn.lock +11110 -0
  87. package/examples/E2E/.bundle/config +2 -0
  88. package/examples/E2E/.detoxrc.js +96 -0
  89. package/examples/E2E/.mise.toml +5 -0
  90. package/examples/E2E/.prettierrc.js +7 -0
  91. package/examples/E2E/.watchmanconfig +1 -0
  92. package/examples/E2E/App.tsx +176 -0
  93. package/examples/E2E/Gemfile +7 -0
  94. package/examples/E2E/Gemfile.lock +101 -0
  95. package/examples/E2E/Home.tsx +206 -0
  96. package/examples/E2E/Modal.tsx +25 -0
  97. package/examples/E2E/README.md +79 -0
  98. package/examples/E2E/SecondPage.tsx +25 -0
  99. package/examples/E2E/android/app/build.gradle +128 -0
  100. package/examples/E2E/android/app/debug.keystore +0 -0
  101. package/examples/E2E/android/app/proguard-rules.pro +10 -0
  102. package/examples/E2E/android/app/src/androidTest/java/com/segmentanalyticsreactnativeexample/DetoxTest.java +29 -0
  103. package/examples/E2E/android/app/src/debug/AndroidManifest.xml +13 -0
  104. package/examples/E2E/android/app/src/debug/java/com/analyticsreactnativeexample/ReactNativeFlipper.java +75 -0
  105. package/examples/E2E/android/app/src/main/AndroidManifest.xml +26 -0
  106. package/examples/E2E/android/app/src/main/java/com/analyticsreactnativeexample/MainActivity.java +32 -0
  107. package/examples/E2E/android/app/src/main/java/com/analyticsreactnativeexample/MainApplication.java +62 -0
  108. package/examples/E2E/android/app/src/main/res/drawable/rn_edit_text_material.xml +36 -0
  109. package/examples/E2E/android/app/src/main/res/mipmap-hdpi/ic_launcher.png +0 -0
  110. package/examples/E2E/android/app/src/main/res/mipmap-hdpi/ic_launcher_round.png +0 -0
  111. package/examples/E2E/android/app/src/main/res/mipmap-mdpi/ic_launcher.png +0 -0
  112. package/examples/E2E/android/app/src/main/res/mipmap-mdpi/ic_launcher_round.png +0 -0
  113. package/examples/E2E/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png +0 -0
  114. package/examples/E2E/android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png +0 -0
  115. package/examples/E2E/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png +0 -0
  116. package/examples/E2E/android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png +0 -0
  117. package/examples/E2E/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png +0 -0
  118. package/examples/E2E/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png +0 -0
  119. package/examples/E2E/android/app/src/main/res/values/strings.xml +3 -0
  120. package/examples/E2E/android/app/src/main/res/values/styles.xml +9 -0
  121. package/examples/E2E/android/app/src/main/res/xml/network_security_config.xml +7 -0
  122. package/examples/E2E/android/app/src/release/java/com/analyticsreactnativeexample/ReactNativeFlipper.java +20 -0
  123. package/examples/E2E/android/build.gradle +37 -0
  124. package/examples/E2E/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  125. package/examples/E2E/android/gradle/wrapper/gradle-wrapper.properties +6 -0
  126. package/examples/E2E/android/gradle.properties +44 -0
  127. package/examples/E2E/android/gradlew +244 -0
  128. package/examples/E2E/android/gradlew.bat +92 -0
  129. package/examples/E2E/android/settings.gradle +4 -0
  130. package/examples/E2E/app.json +4 -0
  131. package/examples/E2E/babel.config.js +14 -0
  132. package/examples/E2E/e2e/jest.config.js +13 -0
  133. package/examples/E2E/e2e/main.e2e.js +192 -0
  134. package/examples/E2E/e2e/matchers.js +34 -0
  135. package/examples/E2E/e2e/mockServer.js +56 -0
  136. package/examples/E2E/index.js +11 -0
  137. package/examples/E2E/ios/.xcode.env +11 -0
  138. package/examples/E2E/ios/AnalyticsReactNativeE2E/AppDelegate.h +6 -0
  139. package/examples/E2E/ios/AnalyticsReactNativeE2E/AppDelegate.mm +26 -0
  140. package/examples/E2E/ios/AnalyticsReactNativeE2E/Images.xcassets/AppIcon.appiconset/Contents.json +53 -0
  141. package/examples/E2E/ios/AnalyticsReactNativeE2E/Images.xcassets/Contents.json +6 -0
  142. package/examples/E2E/ios/AnalyticsReactNativeE2E/Info.plist +55 -0
  143. package/examples/E2E/ios/AnalyticsReactNativeE2E/LaunchScreen.storyboard +47 -0
  144. package/examples/E2E/ios/AnalyticsReactNativeE2E/main.m +10 -0
  145. package/examples/E2E/ios/AnalyticsReactNativeE2E.xcodeproj/project.pbxproj +721 -0
  146. package/examples/E2E/ios/AnalyticsReactNativeE2E.xcworkspace/contents.xcworkspacedata +10 -0
  147. package/examples/E2E/ios/AnalyticsReactNativeE2E.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
  148. package/examples/E2E/ios/AnalyticsReactNativeE2ETests/AnalyticsReactNativeExampleTests.m +66 -0
  149. package/examples/E2E/ios/AnalyticsReactNativeE2ETests/Info.plist +24 -0
  150. package/examples/E2E/ios/Podfile +62 -0
  151. package/examples/E2E/ios/Podfile.lock +763 -0
  152. package/examples/E2E/jest.config.js +3 -0
  153. package/examples/E2E/metro.config.js +45 -0
  154. package/examples/E2E/package.json +65 -0
  155. package/examples/E2E/plugins/Logger.ts +16 -0
  156. package/examples/E2E/react-native.config.js +7 -0
  157. package/examples/E2E/workspace.js +5 -0
  158. package/examples/E2E/yarn.lock +10898 -0
  159. package/examples/E2E-73/.bundle/config +2 -0
  160. package/examples/E2E-73/.detoxrc.js +96 -0
  161. package/examples/E2E-73/.eslintrc.js +4 -0
  162. package/examples/E2E-73/.mise.toml +5 -0
  163. package/examples/E2E-73/.prettierrc.js +7 -0
  164. package/examples/E2E-73/.watchmanconfig +1 -0
  165. package/examples/E2E-73/App.tsx +175 -0
  166. package/examples/E2E-73/Gemfile +7 -0
  167. package/examples/E2E-73/Gemfile.lock +99 -0
  168. package/examples/E2E-73/Home.tsx +206 -0
  169. package/examples/E2E-73/Modal.tsx +25 -0
  170. package/examples/E2E-73/README.md +79 -0
  171. package/examples/E2E-73/SecondPage.tsx +25 -0
  172. package/examples/E2E-73/android/app/build.gradle +119 -0
  173. package/examples/E2E-73/android/app/debug.keystore +0 -0
  174. package/examples/E2E-73/android/app/proguard-rules.pro +10 -0
  175. package/examples/E2E-73/android/app/src/debug/AndroidManifest.xml +9 -0
  176. package/examples/E2E-73/android/app/src/main/AndroidManifest.xml +25 -0
  177. package/examples/E2E-73/android/app/src/main/java/com/analyticsreactnativee2e73/MainActivity.kt +22 -0
  178. package/examples/E2E-73/android/app/src/main/java/com/analyticsreactnativee2e73/MainApplication.kt +45 -0
  179. package/examples/E2E-73/android/app/src/main/res/drawable/rn_edit_text_material.xml +36 -0
  180. package/examples/E2E-73/android/app/src/main/res/mipmap-hdpi/ic_launcher.png +0 -0
  181. package/examples/E2E-73/android/app/src/main/res/mipmap-hdpi/ic_launcher_round.png +0 -0
  182. package/examples/E2E-73/android/app/src/main/res/mipmap-mdpi/ic_launcher.png +0 -0
  183. package/examples/E2E-73/android/app/src/main/res/mipmap-mdpi/ic_launcher_round.png +0 -0
  184. package/examples/E2E-73/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png +0 -0
  185. package/examples/E2E-73/android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png +0 -0
  186. package/examples/E2E-73/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png +0 -0
  187. package/examples/E2E-73/android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png +0 -0
  188. package/examples/E2E-73/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png +0 -0
  189. package/examples/E2E-73/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png +0 -0
  190. package/examples/E2E-73/android/app/src/main/res/values/strings.xml +3 -0
  191. package/examples/E2E-73/android/app/src/main/res/values/styles.xml +9 -0
  192. package/examples/E2E-73/android/build.gradle +21 -0
  193. package/examples/E2E-73/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  194. package/examples/E2E-73/android/gradle/wrapper/gradle-wrapper.properties +7 -0
  195. package/examples/E2E-73/android/gradle.properties +41 -0
  196. package/examples/E2E-73/android/gradlew +249 -0
  197. package/examples/E2E-73/android/gradlew.bat +92 -0
  198. package/examples/E2E-73/android/settings.gradle +4 -0
  199. package/examples/E2E-73/app.json +4 -0
  200. package/examples/E2E-73/babel.config.js +14 -0
  201. package/examples/E2E-73/e2e/jest.config.js +13 -0
  202. package/examples/E2E-73/e2e/main.e2e.js +192 -0
  203. package/examples/E2E-73/e2e/matchers.js +34 -0
  204. package/examples/E2E-73/e2e/mockServer.js +56 -0
  205. package/examples/E2E-73/index.js +9 -0
  206. package/examples/E2E-73/ios/.xcode.env +11 -0
  207. package/examples/E2E-73/ios/AnalyticsReactNativeE2E73/AppDelegate.h +6 -0
  208. package/examples/E2E-73/ios/AnalyticsReactNativeE2E73/AppDelegate.mm +31 -0
  209. package/examples/E2E-73/ios/AnalyticsReactNativeE2E73/Images.xcassets/AppIcon.appiconset/Contents.json +53 -0
  210. package/examples/E2E-73/ios/AnalyticsReactNativeE2E73/Images.xcassets/Contents.json +6 -0
  211. package/examples/E2E-73/ios/AnalyticsReactNativeE2E73/Info.plist +51 -0
  212. package/examples/E2E-73/ios/AnalyticsReactNativeE2E73/LaunchScreen.storyboard +47 -0
  213. package/examples/E2E-73/ios/AnalyticsReactNativeE2E73/main.m +10 -0
  214. package/examples/E2E-73/ios/AnalyticsReactNativeE2E73.xcodeproj/project.pbxproj +700 -0
  215. package/examples/E2E-73/ios/AnalyticsReactNativeE2E73.xcodeproj/xcshareddata/xcschemes/AnalyticsReactNativeE2E73.xcscheme +88 -0
  216. package/examples/E2E-73/ios/AnalyticsReactNativeE2E73.xcworkspace/contents.xcworkspacedata +10 -0
  217. package/examples/E2E-73/ios/AnalyticsReactNativeE2E73Tests/AnalyticsReactNativeE2E73Tests.m +66 -0
  218. package/examples/E2E-73/ios/AnalyticsReactNativeE2E73Tests/Info.plist +24 -0
  219. package/examples/E2E-73/ios/Podfile +55 -0
  220. package/examples/E2E-73/ios/Podfile.lock +1425 -0
  221. package/examples/E2E-73/jest.config.js +3 -0
  222. package/examples/E2E-73/metro.config.js +45 -0
  223. package/examples/E2E-73/package.json +62 -0
  224. package/examples/E2E-73/plugins/Logger.ts +16 -0
  225. package/examples/E2E-73/react-native.config.js +12 -0
  226. package/examples/E2E-73/tsconfig.json +3 -0
  227. package/examples/E2E-73/workspace.js +5 -0
  228. package/examples/E2E-73/yarn.lock +10789 -0
  229. package/examples/README.md +142 -0
  230. package/examples/linkHelper.js +45 -0
  231. package/jest.config.js +7 -0
  232. package/multi-release.config.js +7 -0
  233. package/package.json +79 -0
  234. package/packages/core/CHANGELOG.md +274 -0
  235. package/packages/core/LICENSE +21 -0
  236. package/packages/core/README.md +3 -0
  237. package/packages/core/android/build.gradle +96 -0
  238. package/packages/core/android/gradle.properties +5 -0
  239. package/packages/core/android/src/main/AndroidManifest.xml +4 -0
  240. package/packages/core/android/src/main/AndroidManifestNew.xml +3 -0
  241. package/packages/core/android/src/main/java/com/segmentanalyticsreactnative/AnalyticsReactNativeModule.kt +265 -0
  242. package/packages/core/android/src/main/java/com/segmentanalyticsreactnative/AnalyticsReactNativePackage.kt +43 -0
  243. package/packages/core/babel.config.js +3 -0
  244. package/packages/core/constants-generator.js +17 -0
  245. package/packages/core/ios/AnalyticsReactNative-Bridging-Header.h +1 -0
  246. package/packages/core/ios/AnalyticsReactNative.m +7 -0
  247. package/packages/core/ios/AnalyticsReactNative.swift +142 -0
  248. package/packages/core/ios/AnalyticsReactNative.xcodeproj/project.pbxproj +281 -0
  249. package/packages/core/ios/PrivacyInfo.xcprivacy +70 -0
  250. package/packages/core/jest.config.js +5 -0
  251. package/packages/core/package.json +85 -0
  252. package/packages/core/release.config.js +3 -0
  253. package/packages/core/segment-analytics-react-native.podspec +35 -0
  254. package/packages/core/src/__tests__/__snapshots__/mapTransform.test.ts.snap +20 -0
  255. package/packages/core/src/__tests__/analytics.test.ts +252 -0
  256. package/packages/core/src/__tests__/api.test.ts +91 -0
  257. package/packages/core/src/__tests__/client.test.ts +27 -0
  258. package/packages/core/src/__tests__/context.test.ts +122 -0
  259. package/packages/core/src/__tests__/events.test.ts +234 -0
  260. package/packages/core/src/__tests__/internal/checkInstalledVersion.test.ts +221 -0
  261. package/packages/core/src/__tests__/internal/fetchSettings.test.ts +438 -0
  262. package/packages/core/src/__tests__/internal/handleAppStateChange.test.ts +178 -0
  263. package/packages/core/src/__tests__/internal/trackDeepLinks.test.ts +121 -0
  264. package/packages/core/src/__tests__/logger.test.ts +83 -0
  265. package/packages/core/src/__tests__/mapTransform.test.ts +63 -0
  266. package/packages/core/src/__tests__/methods/alias.test.ts +89 -0
  267. package/packages/core/src/__tests__/methods/flush.test.ts +120 -0
  268. package/packages/core/src/__tests__/methods/group.test.ts +48 -0
  269. package/packages/core/src/__tests__/methods/identify.test.ts +132 -0
  270. package/packages/core/src/__tests__/methods/process.test.ts +162 -0
  271. package/packages/core/src/__tests__/methods/screen.test.ts +48 -0
  272. package/packages/core/src/__tests__/methods/track.test.ts +49 -0
  273. package/packages/core/src/__tests__/timeline.test.ts +130 -0
  274. package/packages/core/src/__tests__/util.test.ts +200 -0
  275. package/packages/core/src/analytics.ts +1030 -0
  276. package/packages/core/src/api.ts +23 -0
  277. package/packages/core/src/client.tsx +76 -0
  278. package/packages/core/src/constants.ts +18 -0
  279. package/packages/core/src/context.ts +107 -0
  280. package/packages/core/src/errors.ts +123 -0
  281. package/packages/core/src/events.ts +75 -0
  282. package/packages/core/src/flushPolicies/__tests__/background-flush-policy.test.ts +39 -0
  283. package/packages/core/src/flushPolicies/__tests__/count-flush-policy.test.ts +28 -0
  284. package/packages/core/src/flushPolicies/__tests__/flush-policy-executer.test.ts +85 -0
  285. package/packages/core/src/flushPolicies/__tests__/timer-flush-policy.test.ts +30 -0
  286. package/packages/core/src/flushPolicies/background-flush-policy.ts +21 -0
  287. package/packages/core/src/flushPolicies/count-flush-policy.ts +31 -0
  288. package/packages/core/src/flushPolicies/flush-policy-executer.ts +100 -0
  289. package/packages/core/src/flushPolicies/index.ts +5 -0
  290. package/packages/core/src/flushPolicies/startup-flush-policy.ts +20 -0
  291. package/packages/core/src/flushPolicies/timer-flush-policy.ts +45 -0
  292. package/packages/core/src/flushPolicies/types.ts +93 -0
  293. package/packages/core/src/index.ts +28 -0
  294. package/packages/core/src/logger.ts +37 -0
  295. package/packages/core/src/mapTransform.ts +48 -0
  296. package/packages/core/src/plugin.ts +212 -0
  297. package/packages/core/src/plugins/ConsentPlugin.ts +200 -0
  298. package/packages/core/src/plugins/DestinationMetadataEnrichment.ts +67 -0
  299. package/packages/core/src/plugins/QueueFlushingPlugin.ts +149 -0
  300. package/packages/core/src/plugins/SegmentDestination.ts +153 -0
  301. package/packages/core/src/plugins/__tests__/QueueFlushingPlugin.test.ts +143 -0
  302. package/packages/core/src/plugins/__tests__/SegmentDestination.test.ts +549 -0
  303. package/packages/core/src/plugins/__tests__/consent/consentNotEnabledAtSegment.test.ts +123 -0
  304. package/packages/core/src/plugins/__tests__/consent/destinationMultipleCategories.test.ts +147 -0
  305. package/packages/core/src/plugins/__tests__/consent/idfa.test.ts +101 -0
  306. package/packages/core/src/plugins/__tests__/consent/mockSettings/ConsentNotEnabledAtSegment.json +70 -0
  307. package/packages/core/src/plugins/__tests__/consent/mockSettings/DestinationsMultipleCategories.json +73 -0
  308. package/packages/core/src/plugins/__tests__/consent/mockSettings/NoUnmappedDestinations.json +105 -0
  309. package/packages/core/src/plugins/__tests__/consent/mockSettings/UnmappedDestinations.json +101 -0
  310. package/packages/core/src/plugins/__tests__/consent/noUnmapped.test.ts +234 -0
  311. package/packages/core/src/plugins/__tests__/consent/unmapped.test.ts +265 -0
  312. package/packages/core/src/plugins/__tests__/consent/utils.ts +75 -0
  313. package/packages/core/src/storage/__tests__/sovranStorage.test.ts +153 -0
  314. package/packages/core/src/storage/helpers.ts +25 -0
  315. package/packages/core/src/storage/index.ts +3 -0
  316. package/packages/core/src/storage/sovranStorage.ts +558 -0
  317. package/packages/core/src/storage/types.ts +106 -0
  318. package/packages/core/src/test-helpers/index.ts +7 -0
  319. package/packages/core/src/test-helpers/mockDestinationPlugin.ts +7 -0
  320. package/packages/core/src/test-helpers/mockEventStore.ts +35 -0
  321. package/packages/core/src/test-helpers/mockLogger.ts +10 -0
  322. package/packages/core/src/test-helpers/mockSegmentStore.ts +266 -0
  323. package/packages/core/src/test-helpers/mockTimeline.ts +9 -0
  324. package/packages/core/src/test-helpers/setupSegmentClient.ts +59 -0
  325. package/packages/core/src/test-helpers/utils.ts +23 -0
  326. package/packages/core/src/timeline.ts +136 -0
  327. package/packages/core/src/types.ts +392 -0
  328. package/packages/core/src/util.ts +288 -0
  329. package/packages/core/src/uuid.ts +7 -0
  330. package/packages/core/tsconfig.json +10 -0
  331. package/packages/core/tsconfig.linter.json +11 -0
  332. package/packages/plugins/plugin-adjust/CHANGELOG.md +60 -0
  333. package/packages/plugins/plugin-adjust/LICENSE +21 -0
  334. package/packages/plugins/plugin-adjust/README.md +72 -0
  335. package/packages/plugins/plugin-adjust/babel.config.js +3 -0
  336. package/packages/plugins/plugin-adjust/jest.config.js +5 -0
  337. package/packages/plugins/plugin-adjust/package.json +72 -0
  338. package/packages/plugins/plugin-adjust/release.config.js +3 -0
  339. package/packages/plugins/plugin-adjust/src/AdjustPlugin.tsx +92 -0
  340. package/packages/plugins/plugin-adjust/src/index.ts +1 -0
  341. package/packages/plugins/plugin-adjust/src/methods/__mocks__/react-native-adjust.ts +19 -0
  342. package/packages/plugins/plugin-adjust/src/methods/__tests__/identify.test.ts +34 -0
  343. package/packages/plugins/plugin-adjust/src/methods/__tests__/reset.test.ts +15 -0
  344. package/packages/plugins/plugin-adjust/src/methods/__tests__/track.test.ts +148 -0
  345. package/packages/plugins/plugin-adjust/src/methods/identify.ts +14 -0
  346. package/packages/plugins/plugin-adjust/src/methods/reset.ts +9 -0
  347. package/packages/plugins/plugin-adjust/src/methods/track.ts +47 -0
  348. package/packages/plugins/plugin-adjust/src/util.ts +29 -0
  349. package/packages/plugins/plugin-adjust/tsconfig.json +10 -0
  350. package/packages/plugins/plugin-advertising-id/CHANGELOG.md +101 -0
  351. package/packages/plugins/plugin-advertising-id/CONTRIBUTING.md +196 -0
  352. package/packages/plugins/plugin-advertising-id/LICENSE +21 -0
  353. package/packages/plugins/plugin-advertising-id/README.md +40 -0
  354. package/packages/plugins/plugin-advertising-id/android/build.gradle +96 -0
  355. package/packages/plugins/plugin-advertising-id/android/gradle.properties +5 -0
  356. package/packages/plugins/plugin-advertising-id/android/src/main/AndroidManifest.xml +4 -0
  357. package/packages/plugins/plugin-advertising-id/android/src/main/AndroidManifestNew.xml +3 -0
  358. package/packages/plugins/plugin-advertising-id/android/src/main/java/com/reactnativeanalyticsreactnativepluginadvertisingid/AnalyticsReactNativePluginAdvertisingIdModule.kt +64 -0
  359. package/packages/plugins/plugin-advertising-id/android/src/main/java/com/reactnativeanalyticsreactnativepluginadvertisingid/AnalyticsReactNativePluginAdvertisingIdPackage.kt +17 -0
  360. package/packages/plugins/plugin-advertising-id/babel.config.js +3 -0
  361. package/packages/plugins/plugin-advertising-id/jest.config.js +5 -0
  362. package/packages/plugins/plugin-advertising-id/package.json +70 -0
  363. package/packages/plugins/plugin-advertising-id/release.config.js +3 -0
  364. package/packages/plugins/plugin-advertising-id/src/AdvertisingIdPlugin.ts +172 -0
  365. package/packages/plugins/plugin-advertising-id/src/index.ts +1 -0
  366. package/packages/plugins/plugin-advertising-id/src/types.ts +3 -0
  367. package/packages/plugins/plugin-advertising-id/tsconfig.json +10 -0
  368. package/packages/plugins/plugin-amplitudeSession/CHANGELOG.md +46 -0
  369. package/packages/plugins/plugin-amplitudeSession/LICENSE +21 -0
  370. package/packages/plugins/plugin-amplitudeSession/README.md +66 -0
  371. package/packages/plugins/plugin-amplitudeSession/babel.config.js +3 -0
  372. package/packages/plugins/plugin-amplitudeSession/jest.config.js +5 -0
  373. package/packages/plugins/plugin-amplitudeSession/package.json +71 -0
  374. package/packages/plugins/plugin-amplitudeSession/release.config.js +3 -0
  375. package/packages/plugins/plugin-amplitudeSession/src/AmplitudeSessionPlugin.tsx +327 -0
  376. package/packages/plugins/plugin-amplitudeSession/src/__tests__/AmplitudeSessionPlugin.test.ts +769 -0
  377. package/packages/plugins/plugin-amplitudeSession/src/index.ts +1 -0
  378. package/packages/plugins/plugin-amplitudeSession/tsconfig.json +10 -0
  379. package/packages/plugins/plugin-appsflyer/CHANGELOG.md +64 -0
  380. package/packages/plugins/plugin-appsflyer/LICENSE +21 -0
  381. package/packages/plugins/plugin-appsflyer/README.md +90 -0
  382. package/packages/plugins/plugin-appsflyer/babel.config.js +3 -0
  383. package/packages/plugins/plugin-appsflyer/jest.config.js +5 -0
  384. package/packages/plugins/plugin-appsflyer/package.json +74 -0
  385. package/packages/plugins/plugin-appsflyer/release.config.js +3 -0
  386. package/packages/plugins/plugin-appsflyer/src/AppsflyerPlugin.tsx +218 -0
  387. package/packages/plugins/plugin-appsflyer/src/__tests__/AppsflyerPlugin.test.ts +20 -0
  388. package/packages/plugins/plugin-appsflyer/src/index.ts +1 -0
  389. package/packages/plugins/plugin-appsflyer/src/methods/__mocks__/react-native-appsflyer.ts +11 -0
  390. package/packages/plugins/plugin-appsflyer/src/methods/__tests__/identify.test.ts +57 -0
  391. package/packages/plugins/plugin-appsflyer/src/methods/__tests__/track.test.ts +99 -0
  392. package/packages/plugins/plugin-appsflyer/src/methods/identify.ts +42 -0
  393. package/packages/plugins/plugin-appsflyer/src/methods/track.ts +61 -0
  394. package/packages/plugins/plugin-appsflyer/src/types.ts +9 -0
  395. package/packages/plugins/plugin-appsflyer/tsconfig.json +10 -0
  396. package/packages/plugins/plugin-branch/CHANGELOG.md +51 -0
  397. package/packages/plugins/plugin-branch/LICENSE +21 -0
  398. package/packages/plugins/plugin-branch/README.md +71 -0
  399. package/packages/plugins/plugin-branch/babel.config.js +3 -0
  400. package/packages/plugins/plugin-branch/jest.config.js +5 -0
  401. package/packages/plugins/plugin-branch/package.json +74 -0
  402. package/packages/plugins/plugin-branch/release.config.js +3 -0
  403. package/packages/plugins/plugin-branch/src/BranchPlugin.ts +42 -0
  404. package/packages/plugins/plugin-branch/src/index.ts +1 -0
  405. package/packages/plugins/plugin-branch/src/methods/__mocks__/react-native-branch.ts +16 -0
  406. package/packages/plugins/plugin-branch/src/methods/__tests__/alias.test.ts +23 -0
  407. package/packages/plugins/plugin-branch/src/methods/__tests__/identify.test.ts +23 -0
  408. package/packages/plugins/plugin-branch/src/methods/__tests__/screen.test.ts +38 -0
  409. package/packages/plugins/plugin-branch/src/methods/__tests__/track.test.ts +73 -0
  410. package/packages/plugins/plugin-branch/src/methods/alias.ts +9 -0
  411. package/packages/plugins/plugin-branch/src/methods/identify.ts +9 -0
  412. package/packages/plugins/plugin-branch/src/methods/parameterMapping.ts +109 -0
  413. package/packages/plugins/plugin-branch/src/methods/reset.ts +5 -0
  414. package/packages/plugins/plugin-branch/src/methods/screen.ts +17 -0
  415. package/packages/plugins/plugin-branch/src/methods/track.ts +18 -0
  416. package/packages/plugins/plugin-branch/src/methods/util.ts +64 -0
  417. package/packages/plugins/plugin-branch/tsconfig.json +10 -0
  418. package/packages/plugins/plugin-braze/CHANGELOG.md +101 -0
  419. package/packages/plugins/plugin-braze/LICENSE +21 -0
  420. package/packages/plugins/plugin-braze/README.md +72 -0
  421. package/packages/plugins/plugin-braze/babel.config.js +3 -0
  422. package/packages/plugins/plugin-braze/jest.config.js +5 -0
  423. package/packages/plugins/plugin-braze/package.json +74 -0
  424. package/packages/plugins/plugin-braze/release.config.js +3 -0
  425. package/packages/plugins/plugin-braze/src/BrazePlugin.tsx +346 -0
  426. package/packages/plugins/plugin-braze/src/index.ts +1 -0
  427. package/packages/plugins/plugin-braze/src/methods/__mocks__/@braze/react-native-sdk.ts +31 -0
  428. package/packages/plugins/plugin-braze/src/methods/__tests__/flush.test.ts +15 -0
  429. package/packages/plugins/plugin-braze/src/methods/__tests__/identify.test.ts +114 -0
  430. package/packages/plugins/plugin-braze/src/methods/__tests__/track.test.ts +321 -0
  431. package/packages/plugins/plugin-braze/src/methods/flush.ts +5 -0
  432. package/packages/plugins/plugin-braze/tsconfig.json +10 -0
  433. package/packages/plugins/plugin-braze-middleware/CHANGELOG.md +32 -0
  434. package/packages/plugins/plugin-braze-middleware/LICENSE +21 -0
  435. package/packages/plugins/plugin-braze-middleware/README.md +67 -0
  436. package/packages/plugins/plugin-braze-middleware/babel.config.js +3 -0
  437. package/packages/plugins/plugin-braze-middleware/jest.config.js +5 -0
  438. package/packages/plugins/plugin-braze-middleware/package.json +72 -0
  439. package/packages/plugins/plugin-braze-middleware/release.config.js +3 -0
  440. package/packages/plugins/plugin-braze-middleware/src/BrazeMiddlewarePlugin.tsx +38 -0
  441. package/packages/plugins/plugin-braze-middleware/src/index.ts +1 -0
  442. package/packages/plugins/plugin-braze-middleware/tsconfig.json +10 -0
  443. package/packages/plugins/plugin-clevertap/CHANGELOG.md +32 -0
  444. package/packages/plugins/plugin-clevertap/LICENSE +21 -0
  445. package/packages/plugins/plugin-clevertap/README.md +71 -0
  446. package/packages/plugins/plugin-clevertap/babel.config.js +3 -0
  447. package/packages/plugins/plugin-clevertap/jest.config.js +5 -0
  448. package/packages/plugins/plugin-clevertap/package.json +72 -0
  449. package/packages/plugins/plugin-clevertap/release.config.js +3 -0
  450. package/packages/plugins/plugin-clevertap/src/ClevertapPlugin.ts +75 -0
  451. package/packages/plugins/plugin-clevertap/src/__mocks__/clevertap-react-native.ts +5 -0
  452. package/packages/plugins/plugin-clevertap/src/__tests__/ClevertapPlugin.test.ts +164 -0
  453. package/packages/plugins/plugin-clevertap/src/index.ts +1 -0
  454. package/packages/plugins/plugin-clevertap/src/parameterMapping.ts +19 -0
  455. package/packages/plugins/plugin-clevertap/tsconfig.json +10 -0
  456. package/packages/plugins/plugin-destination-filters/CHANGELOG.md +39 -0
  457. package/packages/plugins/plugin-destination-filters/LICENSE +21 -0
  458. package/packages/plugins/plugin-destination-filters/README.md +71 -0
  459. package/packages/plugins/plugin-destination-filters/babel.config.js +3 -0
  460. package/packages/plugins/plugin-destination-filters/jest.config.js +5 -0
  461. package/packages/plugins/plugin-destination-filters/package.json +77 -0
  462. package/packages/plugins/plugin-destination-filters/release.config.js +3 -0
  463. package/packages/plugins/plugin-destination-filters/src/DestinationFilters.ts +90 -0
  464. package/packages/plugins/plugin-destination-filters/src/__tests__/DestinationFilters.test.ts +102 -0
  465. package/packages/plugins/plugin-destination-filters/src/index.tsx +1 -0
  466. package/packages/plugins/plugin-destination-filters/tsconfig.json +10 -0
  467. package/packages/plugins/plugin-device-token/CHANGELOG.md +71 -0
  468. package/packages/plugins/plugin-device-token/LICENSE +21 -0
  469. package/packages/plugins/plugin-device-token/README.md +98 -0
  470. package/packages/plugins/plugin-device-token/babel.config.js +3 -0
  471. package/packages/plugins/plugin-device-token/jest.config.js +5 -0
  472. package/packages/plugins/plugin-device-token/package.json +76 -0
  473. package/packages/plugins/plugin-device-token/release.config.js +3 -0
  474. package/packages/plugins/plugin-device-token/src/DeviceTokenPlugin.tsx +94 -0
  475. package/packages/plugins/plugin-device-token/src/index.ts +1 -0
  476. package/packages/plugins/plugin-device-token/src/methods/___tests__/DeviceTokenPlugin.test.ts +94 -0
  477. package/packages/plugins/plugin-device-token/tsconfig.json +10 -0
  478. package/packages/plugins/plugin-facebook-app-events/CHANGELOG.md +78 -0
  479. package/packages/plugins/plugin-facebook-app-events/LICENSE +21 -0
  480. package/packages/plugins/plugin-facebook-app-events/README.md +73 -0
  481. package/packages/plugins/plugin-facebook-app-events/babel.config.js +3 -0
  482. package/packages/plugins/plugin-facebook-app-events/jest.config.js +5 -0
  483. package/packages/plugins/plugin-facebook-app-events/package.json +72 -0
  484. package/packages/plugins/plugin-facebook-app-events/release.config.js +3 -0
  485. package/packages/plugins/plugin-facebook-app-events/src/FacebookAppEventsPlugin.ts +192 -0
  486. package/packages/plugins/plugin-facebook-app-events/src/__tests__/FacebookAppEventsPlugin.test.ts +284 -0
  487. package/packages/plugins/plugin-facebook-app-events/src/index.ts +1 -0
  488. package/packages/plugins/plugin-facebook-app-events/src/methods/__tests__/screen.test.ts +33 -0
  489. package/packages/plugins/plugin-facebook-app-events/src/methods/screen.ts +35 -0
  490. package/packages/plugins/plugin-facebook-app-events/src/parameterMapping.ts +46 -0
  491. package/packages/plugins/plugin-facebook-app-events/src/utils.ts +12 -0
  492. package/packages/plugins/plugin-facebook-app-events/tsconfig.json +10 -0
  493. package/packages/plugins/plugin-firebase/CHANGELOG.md +102 -0
  494. package/packages/plugins/plugin-firebase/LICENSE +21 -0
  495. package/packages/plugins/plugin-firebase/README.md +72 -0
  496. package/packages/plugins/plugin-firebase/babel.config.js +3 -0
  497. package/packages/plugins/plugin-firebase/jest.config.js +5 -0
  498. package/packages/plugins/plugin-firebase/package.json +74 -0
  499. package/packages/plugins/plugin-firebase/release.config.js +3 -0
  500. package/packages/plugins/plugin-firebase/src/FirebasePlugin.tsx +96 -0
  501. package/packages/plugins/plugin-firebase/src/index.ts +1 -0
  502. package/packages/plugins/plugin-firebase/src/methods/___tests__/identify.test.ts +73 -0
  503. package/packages/plugins/plugin-firebase/src/methods/___tests__/reset.test.ts +20 -0
  504. package/packages/plugins/plugin-firebase/src/methods/___tests__/screen.test.ts +33 -0
  505. package/packages/plugins/plugin-firebase/src/methods/___tests__/track.test.ts +85 -0
  506. package/packages/plugins/plugin-firebase/src/methods/parameterMapping.ts +45 -0
  507. package/packages/plugins/plugin-firebase/src/methods/reset.ts +5 -0
  508. package/packages/plugins/plugin-firebase/src/methods/screen.ts +12 -0
  509. package/packages/plugins/plugin-firebase/src/methods/track.ts +26 -0
  510. package/packages/plugins/plugin-firebase/tsconfig.json +10 -0
  511. package/packages/plugins/plugin-idfa/CHANGELOG.md +62 -0
  512. package/packages/plugins/plugin-idfa/LICENSE +21 -0
  513. package/packages/plugins/plugin-idfa/README.md +105 -0
  514. package/packages/plugins/plugin-idfa/babel.config.js +3 -0
  515. package/packages/plugins/plugin-idfa/ios/AnalyticsReactNativePluginIdfa-Bridging-Header.h +2 -0
  516. package/packages/plugins/plugin-idfa/ios/AnalyticsReactNativePluginIdfa.m +11 -0
  517. package/packages/plugins/plugin-idfa/ios/AnalyticsReactNativePluginIdfa.swift +68 -0
  518. package/packages/plugins/plugin-idfa/ios/AnalyticsReactNativePluginIdfa.xcodeproj/project.pbxproj +293 -0
  519. package/packages/plugins/plugin-idfa/jest.config.js +5 -0
  520. package/packages/plugins/plugin-idfa/package.json +70 -0
  521. package/packages/plugins/plugin-idfa/release.config.js +3 -0
  522. package/packages/plugins/plugin-idfa/segment-analytics-react-native-plugin-idfa.podspec +19 -0
  523. package/packages/plugins/plugin-idfa/src/AnalyticsReactNativePluginIdfa.e2e.mock.ts +12 -0
  524. package/packages/plugins/plugin-idfa/src/AnalyticsReactNativePluginIdfa.ts +18 -0
  525. package/packages/plugins/plugin-idfa/src/IdfaPlugin.tsx +80 -0
  526. package/packages/plugins/plugin-idfa/src/index.ts +1 -0
  527. package/packages/plugins/plugin-idfa/src/types.ts +13 -0
  528. package/packages/plugins/plugin-idfa/tsconfig.json +10 -0
  529. package/packages/plugins/plugin-mixpanel/CHANGELOG.md +73 -0
  530. package/packages/plugins/plugin-mixpanel/LICENSE +21 -0
  531. package/packages/plugins/plugin-mixpanel/README.md +71 -0
  532. package/packages/plugins/plugin-mixpanel/babel.config.js +3 -0
  533. package/packages/plugins/plugin-mixpanel/jest.config.js +5 -0
  534. package/packages/plugins/plugin-mixpanel/package.json +72 -0
  535. package/packages/plugins/plugin-mixpanel/release.config.js +3 -0
  536. package/packages/plugins/plugin-mixpanel/src/MixpanelPlugin.ts +106 -0
  537. package/packages/plugins/plugin-mixpanel/src/index.ts +1 -0
  538. package/packages/plugins/plugin-mixpanel/src/methods/__mocks__/mixpanel-react-native.ts +67 -0
  539. package/packages/plugins/plugin-mixpanel/src/methods/__tests__/MixpanelPlugin.test.ts +57 -0
  540. package/packages/plugins/plugin-mixpanel/src/methods/__tests__/__helpers__/constants.ts +18 -0
  541. package/packages/plugins/plugin-mixpanel/src/methods/__tests__/alias.test.ts +47 -0
  542. package/packages/plugins/plugin-mixpanel/src/methods/__tests__/group.test.ts +49 -0
  543. package/packages/plugins/plugin-mixpanel/src/methods/__tests__/identify.test.ts +103 -0
  544. package/packages/plugins/plugin-mixpanel/src/methods/__tests__/screen.test.ts +119 -0
  545. package/packages/plugins/plugin-mixpanel/src/methods/__tests__/track.test.ts +110 -0
  546. package/packages/plugins/plugin-mixpanel/src/methods/alias.ts +28 -0
  547. package/packages/plugins/plugin-mixpanel/src/methods/group.ts +26 -0
  548. package/packages/plugins/plugin-mixpanel/src/methods/identify.ts +72 -0
  549. package/packages/plugins/plugin-mixpanel/src/methods/screen.ts +43 -0
  550. package/packages/plugins/plugin-mixpanel/src/methods/track.ts +60 -0
  551. package/packages/plugins/plugin-mixpanel/src/types.ts +15 -0
  552. package/packages/plugins/plugin-mixpanel/tsconfig.json +10 -0
  553. package/packages/plugins/plugin-onetrust/CHANGELOG.md +16 -0
  554. package/packages/plugins/plugin-onetrust/LICENSE +21 -0
  555. package/packages/plugins/plugin-onetrust/README.md +155 -0
  556. package/packages/plugins/plugin-onetrust/babel.config.js +3 -0
  557. package/packages/plugins/plugin-onetrust/jest.config.js +6 -0
  558. package/packages/plugins/plugin-onetrust/package.json +75 -0
  559. package/packages/plugins/plugin-onetrust/release.config.js +3 -0
  560. package/packages/plugins/plugin-onetrust/src/OneTrustProvider.ts +71 -0
  561. package/packages/plugins/plugin-onetrust/src/index.tsx +1 -0
  562. package/packages/plugins/plugin-onetrust/tsconfig.json +10 -0
  563. package/packages/shared/__mocks__/context.ts +35 -0
  564. package/packages/shared/__mocks__/react-native-get-random-values.ts +1 -0
  565. package/packages/shared/__mocks__/react-native.ts +53 -0
  566. package/packages/shared/__mocks__/uuid.ts +3 -0
  567. package/packages/shared/jest.config.base.js +38 -0
  568. package/packages/shared/package.json +19 -0
  569. package/packages/shared/src/setup.ts +7 -0
  570. package/packages/shared/tsconfig.json +15 -0
  571. package/packages/sovran/CHANGELOG.md +42 -0
  572. package/packages/sovran/CONTRIBUTING.md +196 -0
  573. package/packages/sovran/LICENSE +21 -0
  574. package/packages/sovran/README.md +174 -0
  575. package/packages/sovran/android/build.gradle +94 -0
  576. package/packages/sovran/android/gradle.properties +5 -0
  577. package/packages/sovran/android/src/main/AndroidManifest.xml +4 -0
  578. package/packages/sovran/android/src/main/AndroidManifestNew.xml +3 -0
  579. package/packages/sovran/android/src/main/java/com/reactnativesovran/Sovran.kt +45 -0
  580. package/packages/sovran/android/src/main/java/com/reactnativesovran/SovranModule.kt +55 -0
  581. package/packages/sovran/babel.config.js +3 -0
  582. package/packages/sovran/ios/Sovran-Bridging-Header.h +3 -0
  583. package/packages/sovran/ios/Sovran.m +8 -0
  584. package/packages/sovran/ios/Sovran.swift +67 -0
  585. package/packages/sovran/ios/Sovran.xcodeproj/project.pbxproj +279 -0
  586. package/packages/sovran/jest.config.js +5 -0
  587. package/packages/sovran/package.json +89 -0
  588. package/packages/sovran/release.config.js +3 -0
  589. package/packages/sovran/sovran-react-native.podspec +33 -0
  590. package/packages/sovran/src/__tests__/bridge.test.ts +39 -0
  591. package/packages/sovran/src/__tests__/index.test.tsx +1 -0
  592. package/packages/sovran/src/__tests__/store.test.ts +397 -0
  593. package/packages/sovran/src/bridge.ts +48 -0
  594. package/packages/sovran/src/index.tsx +52 -0
  595. package/packages/sovran/src/persistor/async-storage-persistor.ts +63 -0
  596. package/packages/sovran/src/persistor/index.ts +2 -0
  597. package/packages/sovran/src/persistor/persistor.ts +31 -0
  598. package/packages/sovran/src/store.ts +232 -0
  599. package/packages/sovran/tsconfig.json +9 -0
  600. package/release.config.js +13 -0
  601. package/tsconfig.json +44 -0
  602. package/tsconfig.linter.json +36 -0
@@ -0,0 +1,1030 @@
1
+ import type { Rule } from '@segment/tsub/dist/store';
2
+ import deepmerge from 'deepmerge';
3
+ import {
4
+ AppState,
5
+ AppStateStatus,
6
+ NativeEventSubscription,
7
+ } from 'react-native';
8
+ import {
9
+ settingsCDN,
10
+ workspaceDestinationFilterKey,
11
+ defaultFlushInterval,
12
+ defaultFlushAt,
13
+ } from './constants';
14
+ import { getContext } from './context';
15
+ import {
16
+ createAliasEvent,
17
+ createGroupEvent,
18
+ createIdentifyEvent,
19
+ createScreenEvent,
20
+ createTrackEvent,
21
+ } from './events';
22
+ import {
23
+ CountFlushPolicy,
24
+ Observable,
25
+ TimerFlushPolicy,
26
+ } from './flushPolicies';
27
+ import { FlushPolicyExecuter } from './flushPolicies/flush-policy-executer';
28
+ import { DestinationPlugin, PlatformPlugin, Plugin } from './plugin';
29
+ import { SegmentDestination } from './plugins/SegmentDestination';
30
+ import {
31
+ createGetter,
32
+ DeepLinkData,
33
+ Settable,
34
+ Storage,
35
+ Watchable,
36
+ } from './storage';
37
+ import { Timeline } from './timeline';
38
+ import {
39
+ DestinationFilters,
40
+ EventType,
41
+ SegmentAPISettings,
42
+ SegmentAPIConsentSettings,
43
+ EdgeFunctionSettings,
44
+ EnrichmentClosure,
45
+ } from './types';
46
+ import {
47
+ Config,
48
+ Context,
49
+ DeepPartial,
50
+ GroupTraits,
51
+ IntegrationSettings,
52
+ JsonMap,
53
+ LoggerType,
54
+ PluginType,
55
+ SegmentAPIIntegrations,
56
+ SegmentEvent,
57
+ UserInfoState,
58
+ UserTraits,
59
+ } from './types';
60
+ import {
61
+ allSettled,
62
+ getPluginsWithFlush,
63
+ getPluginsWithReset,
64
+ getURL,
65
+ } from './util';
66
+ import { getUUID } from './uuid';
67
+ import type { FlushPolicy } from './flushPolicies';
68
+ import {
69
+ checkResponseForErrors,
70
+ ErrorType,
71
+ SegmentError,
72
+ translateHTTPError,
73
+ } from './errors';
74
+ import { QueueFlushingPlugin } from './plugins/QueueFlushingPlugin';
75
+
76
+ type OnPluginAddedCallback = (plugin: Plugin) => void;
77
+
78
+ export class SegmentClient {
79
+ // the config parameters for the client - a merge of user provided and default options
80
+ private config: Config;
81
+
82
+ // Storage
83
+ private store: Storage;
84
+
85
+ // current app state
86
+ private appState: AppStateStatus | 'unknown' = 'unknown';
87
+
88
+ // subscription for propagating changes to appState
89
+ private appStateSubscription?: NativeEventSubscription;
90
+
91
+ // logger
92
+ public logger: LoggerType;
93
+
94
+ // whether the user has called cleanup
95
+ private destroyed = false;
96
+
97
+ private isAddingPlugins = false;
98
+
99
+ private timeline: Timeline;
100
+
101
+ private pluginsToAdd: Plugin[] = [];
102
+
103
+ private flushPolicyExecuter: FlushPolicyExecuter = new FlushPolicyExecuter(
104
+ [],
105
+ () => {
106
+ void this.flush();
107
+ }
108
+ );
109
+
110
+ private onPluginAddedObservers: OnPluginAddedCallback[] = [];
111
+
112
+ private readonly platformPlugins: PlatformPlugin[] = [];
113
+
114
+ // Watchables
115
+ /**
116
+ * Observable to know when the client is fully initialized and ready to send events to destination
117
+ */
118
+ readonly isReady = new Observable<boolean>(false);
119
+ /**
120
+ * Access or subscribe to client enabled
121
+ */
122
+ readonly enabled: Watchable<boolean> & Settable<boolean>;
123
+ /**
124
+ * Access or subscribe to client context
125
+ */
126
+ readonly context: Watchable<DeepPartial<Context> | undefined> &
127
+ Settable<DeepPartial<Context>>;
128
+
129
+ /**
130
+ * Access or subscribe to adTrackingEnabled (also accesible from context)
131
+ */
132
+ readonly adTrackingEnabled: Watchable<boolean>;
133
+
134
+ /**
135
+ * Access or subscribe to integration settings
136
+ */
137
+ readonly settings: Watchable<SegmentAPIIntegrations | undefined>;
138
+
139
+ /**
140
+ * Access or subscribe to integration settings
141
+ */
142
+ readonly consentSettings: Watchable<SegmentAPIConsentSettings | undefined>;
143
+
144
+ /**
145
+ * Access or subscribe to edge functions settings
146
+ */
147
+ readonly edgeFunctionSettings: Watchable<EdgeFunctionSettings | undefined>;
148
+
149
+ /**
150
+ * Access or subscribe to destination filter settings
151
+ */
152
+ readonly filters: Watchable<DestinationFilters | undefined>;
153
+
154
+ /**
155
+ * Access or subscribe to user info (anonymousId, userId, traits)
156
+ */
157
+ readonly userInfo: Watchable<UserInfoState> & Settable<UserInfoState>;
158
+
159
+ readonly deepLinkData: Watchable<DeepLinkData>;
160
+
161
+ // private telemetry?: Telemetry;
162
+
163
+ /**
164
+ * Returns the plugins currently loaded in the timeline
165
+ * @param ofType Type of plugins, defaults to all
166
+ * @returns List of Plugin objects
167
+ */
168
+ getPlugins(ofType?: PluginType): readonly Plugin[] {
169
+ const plugins = { ...this.timeline.plugins };
170
+ if (ofType !== undefined) {
171
+ return [...(plugins[ofType] ?? [])];
172
+ }
173
+ return [
174
+ ...this.getPlugins(PluginType.before),
175
+ ...this.getPlugins(PluginType.enrichment),
176
+ ...this.getPlugins(PluginType.utility),
177
+ ...this.getPlugins(PluginType.destination),
178
+ ...this.getPlugins(PluginType.after),
179
+ ];
180
+ }
181
+
182
+ /**
183
+ * Retrieves a copy of the current client configuration
184
+ */
185
+ getConfig() {
186
+ return { ...this.config };
187
+ }
188
+
189
+ constructor({
190
+ config,
191
+ logger,
192
+ store,
193
+ }: {
194
+ config: Config;
195
+ logger: LoggerType;
196
+ store: Storage;
197
+ }) {
198
+ this.logger = logger;
199
+ this.config = config;
200
+ this.store = store;
201
+ this.timeline = new Timeline();
202
+
203
+ // Initialize the watchables
204
+ this.context = {
205
+ get: this.store.context.get,
206
+ set: this.store.context.set,
207
+ onChange: this.store.context.onChange,
208
+ };
209
+
210
+ this.adTrackingEnabled = {
211
+ get: createGetter(
212
+ () => this.store.context.get()?.device?.adTrackingEnabled ?? false,
213
+ async () => {
214
+ const context = await this.store.context.get(true);
215
+ return context?.device?.adTrackingEnabled ?? false;
216
+ }
217
+ ),
218
+ onChange: (callback: (value: boolean) => void) =>
219
+ this.store.context.onChange((context?: DeepPartial<Context>) => {
220
+ callback(context?.device?.adTrackingEnabled ?? false);
221
+ }),
222
+ };
223
+
224
+ this.settings = {
225
+ get: this.store.settings.get,
226
+ onChange: this.store.settings.onChange,
227
+ };
228
+
229
+ this.consentSettings = {
230
+ get: this.store.consentSettings.get,
231
+ onChange: this.store.consentSettings.onChange,
232
+ };
233
+
234
+ this.edgeFunctionSettings = {
235
+ get: this.store.edgeFunctionSettings.get,
236
+ onChange: this.store.edgeFunctionSettings.onChange,
237
+ };
238
+
239
+ this.filters = {
240
+ get: this.store.filters.get,
241
+ onChange: this.store.filters.onChange,
242
+ };
243
+
244
+ this.userInfo = {
245
+ get: this.store.userInfo.get,
246
+ set: this.store.userInfo.set,
247
+ onChange: this.store.userInfo.onChange,
248
+ };
249
+
250
+ this.deepLinkData = {
251
+ get: this.store.deepLinkData.get,
252
+ onChange: this.store.deepLinkData.onChange,
253
+ };
254
+
255
+ this.enabled = {
256
+ get: this.store.enabled.get,
257
+ set: this.store.enabled.set,
258
+ onChange: this.store.enabled.onChange,
259
+ };
260
+
261
+ // add segment destination plugin unless
262
+ // asked not to via configuration.
263
+ if (this.config.autoAddSegmentDestination === true) {
264
+ const segmentDestination = new SegmentDestination();
265
+ this.add({ plugin: segmentDestination });
266
+ }
267
+
268
+ // Setup platform specific plugins
269
+ this.platformPlugins.forEach((plugin) => this.add({ plugin: plugin }));
270
+
271
+ // set up tracking for lifecycle events
272
+ this.setupLifecycleEvents();
273
+ }
274
+
275
+ // Watch for isReady so that we can handle any pending events
276
+ private async storageReady(): Promise<boolean> {
277
+ return new Promise((resolve) => {
278
+ this.store.isReady.onChange((value) => {
279
+ resolve(value);
280
+ });
281
+ });
282
+ }
283
+
284
+ /**
285
+ * Initializes the client plugins, settings and subscribers.
286
+ * Can only be called once.
287
+ */
288
+ async init() {
289
+ try {
290
+ if (this.isReady.value) {
291
+ this.logger.warn('SegmentClient already initialized');
292
+ return;
293
+ }
294
+
295
+ if ((await this.store.isReady.get(true)) === false) {
296
+ await this.storageReady();
297
+ }
298
+
299
+ // Get new settings from segment
300
+ // It's important to run this before checkInstalledVersion and trackDeeplinks to give time for destination plugins
301
+ // which make use of the settings object to initialize
302
+ await this.fetchSettings();
303
+
304
+ await allSettled([
305
+ // save the current installed version
306
+ this.checkInstalledVersion(),
307
+ // check if the app was opened from a deep link
308
+ this.trackDeepLinks(),
309
+ ]);
310
+ await this.onReady();
311
+ this.isReady.value = true;
312
+
313
+ // Process all pending events
314
+ await this.processPendingEvents();
315
+ // Trigger manual flush
316
+ this.flushPolicyExecuter.manualFlush();
317
+ } catch (error) {
318
+ this.reportInternalError(
319
+ new SegmentError(
320
+ ErrorType.InitializationError,
321
+ 'Client did not initialize correctly',
322
+ error
323
+ )
324
+ );
325
+ }
326
+ }
327
+
328
+ private generateFiltersMap(rules: Rule[]): DestinationFilters {
329
+ const map: DestinationFilters = {};
330
+
331
+ for (const r of rules) {
332
+ const key = r.destinationName ?? workspaceDestinationFilterKey;
333
+ map[key] = r;
334
+ }
335
+
336
+ return map;
337
+ }
338
+ private getEndpointForSettings(): string {
339
+ let settingsPrefix = '';
340
+ let settingsEndpoint = '';
341
+ const hasProxy = !!(this.config?.cdnProxy ?? '');
342
+ const useSegmentEndpoints = Boolean(this.config?.useSegmentEndpoints);
343
+
344
+ if (hasProxy) {
345
+ settingsPrefix = this.config.cdnProxy ?? '';
346
+ if (useSegmentEndpoints) {
347
+ const isCdnProxyEndsWithSlash = settingsPrefix.endsWith('/');
348
+ settingsEndpoint = isCdnProxyEndsWithSlash
349
+ ? `projects/${this.config.writeKey}/settings`
350
+ : `/projects/${this.config.writeKey}/settings`;
351
+ }
352
+ } else {
353
+ settingsPrefix = settingsCDN;
354
+ settingsEndpoint = `/${this.config.writeKey}/settings`;
355
+ }
356
+ try {
357
+ return getURL(settingsPrefix, settingsEndpoint);
358
+ } catch (error) {
359
+ console.error(
360
+ 'Error in getEndpointForSettings:',
361
+ `fallback to ${settingsCDN}/${this.config.writeKey}/settings`
362
+ );
363
+ return `${settingsCDN}/${this.config.writeKey}/settings`;
364
+ }
365
+ }
366
+
367
+ async fetchSettings() {
368
+ const settingsURL = this.getEndpointForSettings();
369
+ try {
370
+ const res = await fetch(settingsURL, {
371
+ headers: {
372
+ 'Cache-Control': 'no-cache',
373
+ },
374
+ });
375
+ checkResponseForErrors(res);
376
+
377
+ const resJson: SegmentAPISettings =
378
+ (await res.json()) as SegmentAPISettings;
379
+ const integrations = resJson.integrations;
380
+ const consentSettings = resJson.consentSettings;
381
+ const edgeFunctionSettings = resJson.edgeFunction;
382
+ const filters = this.generateFiltersMap(
383
+ resJson.middlewareSettings?.routingRules ?? []
384
+ );
385
+ this.logger.info('Received settings from Segment succesfully.');
386
+ await Promise.all([
387
+ this.store.settings.set(integrations),
388
+ this.store.consentSettings.set(consentSettings),
389
+ this.store.edgeFunctionSettings.set(edgeFunctionSettings),
390
+ this.store.filters.set(filters),
391
+ ]);
392
+ } catch (e) {
393
+ this.reportInternalError(translateHTTPError(e));
394
+
395
+ this.logger.warn(
396
+ `Could not receive settings from Segment. ${
397
+ this.config.defaultSettings
398
+ ? 'Will use the default settings.'
399
+ : 'Device mode destinations will be ignored unless you specify default settings in the client config.'
400
+ }`
401
+ );
402
+
403
+ if (this.config.defaultSettings) {
404
+ await this.store.settings.set(this.config.defaultSettings.integrations);
405
+ }
406
+ }
407
+ }
408
+
409
+ /**
410
+ * There is no garbage collection in JS, which means that any listeners, timeouts and subscriptions
411
+ * would run until the application closes
412
+ *
413
+ * This method exists in case the user for some reason needs to recreate the class instance during runtime.
414
+ * In this case, they should run client.cleanup() to destroy the listeners in the old client before creating a new one.
415
+ *
416
+ * There is a Stage 3 EMCAScript proposal to add a user-defined finalizer, which we could potentially switch to if
417
+ * it gets approved: https://github.com/tc39/proposal-weakrefs#finalizers
418
+ */
419
+ cleanup() {
420
+ this.flushPolicyExecuter.cleanup();
421
+ this.appStateSubscription?.remove();
422
+
423
+ this.destroyed = true;
424
+ }
425
+
426
+ private setupLifecycleEvents() {
427
+ this.appStateSubscription?.remove();
428
+
429
+ this.appStateSubscription = AppState.addEventListener(
430
+ 'change',
431
+ (nextAppState) => {
432
+ this.handleAppStateChange(nextAppState);
433
+ }
434
+ );
435
+ }
436
+
437
+ /**
438
+ Applies the supplied closure to the currently loaded set of plugins.
439
+ NOTE: This does not apply to plugins contained within DestinationPlugins.
440
+
441
+ - Parameter closure: A closure that takes an plugin to be operated on as a parameter.
442
+
443
+ */
444
+ apply(closure: (plugin: Plugin) => void) {
445
+ this.timeline.apply(closure);
446
+ }
447
+
448
+ /**
449
+ * Adds a new plugin to the currently loaded set.
450
+ * @param {{ plugin: Plugin, settings?: IntegrationSettings }} Plugin to be added. Settings are optional if you want to force a configuration instead of the Segment Cloud received one
451
+ */
452
+ add<P extends Plugin>({
453
+ plugin,
454
+ settings,
455
+ }: {
456
+ plugin: P;
457
+ settings?: P extends DestinationPlugin ? IntegrationSettings : never;
458
+ }) {
459
+ // plugins can either be added immediately or
460
+ // can be cached and added later during the next state update
461
+ // this is to avoid adding plugins before network requests made as part of setup have resolved
462
+ if (settings !== undefined && plugin.type === PluginType.destination) {
463
+ void this.store.settings.add(
464
+ (plugin as unknown as DestinationPlugin).key,
465
+ settings
466
+ );
467
+ }
468
+
469
+ if (!this.isReady.value) {
470
+ this.pluginsToAdd.push(plugin);
471
+ } else {
472
+ this.addPlugin(plugin);
473
+ }
474
+ }
475
+
476
+ private addPlugin(plugin: Plugin) {
477
+ plugin.configure(this);
478
+ this.timeline.add(plugin);
479
+ this.triggerOnPluginLoaded(plugin);
480
+ }
481
+
482
+ /**
483
+ Removes and unloads plugins with a matching name from the system.
484
+
485
+ - Parameter pluginName: An plugin name.
486
+ */
487
+ remove({ plugin }: { plugin: Plugin }) {
488
+ this.timeline.remove(plugin);
489
+ }
490
+
491
+ async process(incomingEvent: SegmentEvent, enrichment?: EnrichmentClosure) {
492
+ const event = this.applyRawEventData(incomingEvent);
493
+ event.enrichment = enrichment;
494
+ if (this.enabled.get() === false) {
495
+ return;
496
+ }
497
+ if (this.isReady.value) {
498
+ return this.startTimelineProcessing(event);
499
+ } else {
500
+ this.store.pendingEvents.add(event);
501
+ return event;
502
+ }
503
+ }
504
+
505
+ /**
506
+ * Starts timeline processing
507
+ * @param incomingEvent Segment Event
508
+ * @returns Segment Event
509
+ */
510
+ private async startTimelineProcessing(
511
+ incomingEvent: SegmentEvent
512
+ ): Promise<SegmentEvent | undefined> {
513
+ const event = await this.applyContextData(incomingEvent);
514
+ this.flushPolicyExecuter.notify(event);
515
+ return this.timeline.process(event);
516
+ }
517
+
518
+ private async trackDeepLinks() {
519
+ if (this.getConfig().trackDeepLinks === true) {
520
+ const deepLinkProperties = await this.store.deepLinkData.get(true);
521
+ this.trackDeepLinkEvent(deepLinkProperties);
522
+
523
+ this.store.deepLinkData.onChange((data) => {
524
+ this.trackDeepLinkEvent(data);
525
+ });
526
+ }
527
+ }
528
+
529
+ private trackDeepLinkEvent(deepLinkProperties: DeepLinkData) {
530
+ if (deepLinkProperties.url !== '') {
531
+ const event = createTrackEvent({
532
+ event: 'Deep Link Opened',
533
+ properties: {
534
+ ...deepLinkProperties,
535
+ },
536
+ });
537
+
538
+ void this.process(event);
539
+ this.logger.info('TRACK (Deep Link Opened) event saved', event);
540
+ }
541
+ }
542
+
543
+ /**
544
+ * Executes when everything in the client is ready for sending events
545
+ * @param isReady
546
+ */
547
+ private async onReady() {
548
+ // Add all plugins awaiting store
549
+ if (this.pluginsToAdd.length > 0 && !this.isAddingPlugins) {
550
+ this.isAddingPlugins = true;
551
+ try {
552
+ // start by adding the plugins
553
+ this.pluginsToAdd.forEach((plugin) => {
554
+ this.addPlugin(plugin);
555
+ });
556
+
557
+ // now that they're all added, clear the cache
558
+ // this prevents this block running for every update
559
+ this.pluginsToAdd = [];
560
+ } finally {
561
+ this.isAddingPlugins = false;
562
+ }
563
+ }
564
+
565
+ // Start flush policies
566
+ // This should be done before any pending events are added to the queue so that any policies that rely on events queued can trigger accordingly
567
+ this.setupFlushPolicies();
568
+ }
569
+ private async processPendingEvents() {
570
+ const pending = await this.store.pendingEvents.get(true);
571
+ for (const event of pending) {
572
+ await this.startTimelineProcessing(event);
573
+ await this.store.pendingEvents.remove(event);
574
+ }
575
+ }
576
+
577
+ async flush(): Promise<void> {
578
+ try {
579
+ if (this.destroyed) {
580
+ return;
581
+ }
582
+
583
+ this.flushPolicyExecuter.reset();
584
+
585
+ const promises: (void | Promise<void>)[] = [];
586
+ getPluginsWithFlush(this.timeline).forEach((plugin) => {
587
+ promises.push(plugin.flush());
588
+ });
589
+
590
+ const results = await allSettled(promises);
591
+ for (const r of results) {
592
+ if (r.status === 'rejected') {
593
+ this.reportInternalError(
594
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
595
+ new SegmentError(ErrorType.FlushError, `Flush failed: ${r.reason}`)
596
+ );
597
+ }
598
+ }
599
+ } catch (error) {
600
+ this.reportInternalError(
601
+ new SegmentError(ErrorType.FlushError, 'Flush failed', error)
602
+ );
603
+ }
604
+ }
605
+
606
+ async screen(
607
+ name: string,
608
+ options?: JsonMap,
609
+ enrichment?: EnrichmentClosure
610
+ ) {
611
+ const event = createScreenEvent({
612
+ name,
613
+ properties: options,
614
+ });
615
+
616
+ await this.process(event, enrichment);
617
+ this.logger.info('SCREEN event saved', event);
618
+ }
619
+
620
+ async track(
621
+ eventName: string,
622
+ options?: JsonMap,
623
+ enrichment?: EnrichmentClosure
624
+ ) {
625
+ const event = createTrackEvent({
626
+ event: eventName,
627
+ properties: options,
628
+ });
629
+
630
+ await this.process(event, enrichment);
631
+ this.logger.info('TRACK event saved', event);
632
+ }
633
+
634
+ async identify(
635
+ userId?: string,
636
+ userTraits?: UserTraits,
637
+ enrichment?: EnrichmentClosure
638
+ ) {
639
+ const event = createIdentifyEvent({
640
+ userId: userId,
641
+ userTraits: userTraits,
642
+ });
643
+
644
+ await this.process(event, enrichment);
645
+ this.logger.info('IDENTIFY event saved', event);
646
+ }
647
+
648
+ async group(
649
+ groupId: string,
650
+ groupTraits?: GroupTraits,
651
+ enrichment?: EnrichmentClosure
652
+ ) {
653
+ const event = createGroupEvent({
654
+ groupId,
655
+ groupTraits,
656
+ });
657
+
658
+ await this.process(event, enrichment);
659
+ this.logger.info('GROUP event saved', event);
660
+ }
661
+
662
+ async alias(newUserId: string, enrichment?: EnrichmentClosure) {
663
+ // We don't use a concurrency safe version of get here as we don't want to lock the values yet,
664
+ // we will update the values correctly when InjectUserInfo processes the change
665
+ const { anonymousId, userId: previousUserId } = this.store.userInfo.get();
666
+
667
+ const event = createAliasEvent({
668
+ anonymousId,
669
+ userId: previousUserId,
670
+ newUserId,
671
+ });
672
+
673
+ await this.process(event, enrichment);
674
+ this.logger.info('ALIAS event saved', event);
675
+ }
676
+
677
+ /**
678
+ * Called once when the client is first created
679
+ *
680
+ * Detect and save the the currently installed application version
681
+ * Send application lifecycle events if trackAppLifecycleEvents is enabled
682
+ *
683
+ * Exactly one of these events will be sent, depending on the current and previous version:s
684
+ * Application Installed - no information on the previous version, so it's a fresh install
685
+ * Application Updated - the previous detected version is different from the current version
686
+ * Application Opened - the previously detected version is same as the current version
687
+ */
688
+ private async checkInstalledVersion() {
689
+ const context = await getContext(undefined, this.config);
690
+
691
+ const previousContext = this.store.context.get();
692
+
693
+ // Only overwrite the previous context values to preserve any values that are added by enrichment plugins like IDFA
694
+ await this.store.context.set(deepmerge(previousContext ?? {}, context));
695
+
696
+ if (this.config.trackAppLifecycleEvents !== true) {
697
+ return;
698
+ }
699
+
700
+ if (previousContext?.app === undefined) {
701
+ const event = createTrackEvent({
702
+ event: 'Application Installed',
703
+ properties: {
704
+ version: context.app.version,
705
+ build: context.app.build,
706
+ },
707
+ });
708
+ void this.process(event);
709
+ this.logger.info('TRACK (Application Installed) event saved', event);
710
+ } else if (context.app.version !== previousContext.app.version) {
711
+ const event = createTrackEvent({
712
+ event: 'Application Updated',
713
+ properties: {
714
+ version: context.app.version,
715
+ build: context.app.build,
716
+ previous_version: previousContext.app.version,
717
+ previous_build: previousContext.app.build,
718
+ },
719
+ });
720
+ void this.process(event);
721
+ this.logger.info('TRACK (Application Updated) event saved', event);
722
+ }
723
+
724
+ const event = createTrackEvent({
725
+ event: 'Application Opened',
726
+ properties: {
727
+ from_background: false,
728
+ version: context.app.version,
729
+ build: context.app.build,
730
+ },
731
+ });
732
+ void this.process(event);
733
+ this.logger.info('TRACK (Application Opened) event saved', event);
734
+ }
735
+
736
+ /**
737
+ * AppState event listener. Called whenever the app state changes.
738
+ *
739
+ * Send application lifecycle events if trackAppLifecycleEvents is enabled.
740
+ *
741
+ * Application Opened - only when the app state changes from 'inactive' or 'background' to 'active'
742
+ * The initial event from 'unknown' to 'active' is handled on launch in checkInstalledVersion
743
+ * Application Backgrounded - when the app state changes from 'inactive' or 'background' to 'active
744
+ *
745
+ * @param nextAppState 'active', 'inactive', 'background' or 'unknown'
746
+ */
747
+ private handleAppStateChange(nextAppState: AppStateStatus) {
748
+ if (this.config.trackAppLifecycleEvents === true) {
749
+ if (
750
+ ['inactive', 'background'].includes(this.appState) &&
751
+ nextAppState === 'active'
752
+ ) {
753
+ const context = this.store.context.get();
754
+ const event = createTrackEvent({
755
+ event: 'Application Opened',
756
+ properties: {
757
+ from_background: true,
758
+ version: context?.app?.version,
759
+ build: context?.app?.build,
760
+ },
761
+ });
762
+ void this.process(event);
763
+ this.logger.info('TRACK (Application Opened) event saved', event);
764
+ } else if (
765
+ (this.appState === 'active' || this.appState === 'unknown') && // Check if appState is 'active' or 'unknown'
766
+ ['inactive', 'background'].includes(nextAppState)
767
+ ) {
768
+ // Check if next app state is 'inactive' or 'background'
769
+ const event = createTrackEvent({
770
+ event: 'Application Backgrounded',
771
+ });
772
+ void this.process(event);
773
+ this.logger.info('TRACK (Application Backgrounded) event saved', event);
774
+ }
775
+ }
776
+
777
+ this.appState = nextAppState;
778
+ }
779
+
780
+ async reset(resetAnonymousId = true) {
781
+ try {
782
+ const { anonymousId: currentId } = await this.store.userInfo.get(true);
783
+ const anonymousId = resetAnonymousId === true ? getUUID() : currentId;
784
+
785
+ await this.store.userInfo.set({
786
+ anonymousId,
787
+ userId: undefined,
788
+ traits: undefined,
789
+ });
790
+
791
+ await allSettled(
792
+ getPluginsWithReset(this.timeline).map((plugin) => plugin.reset())
793
+ );
794
+
795
+ this.logger.info('Client has been reset');
796
+ } catch (error) {
797
+ this.reportInternalError(
798
+ new SegmentError(ErrorType.ResetError, 'Error during reset', error)
799
+ );
800
+ }
801
+ }
802
+
803
+ /**
804
+ * Registers a callback for each plugin that gets added to the analytics client.
805
+ * @param callback Function to call
806
+ */
807
+ onPluginLoaded(callback: OnPluginAddedCallback) {
808
+ const i = this.onPluginAddedObservers.push(callback);
809
+
810
+ return () => {
811
+ this.onPluginAddedObservers.splice(i, 1);
812
+ };
813
+ }
814
+
815
+ private triggerOnPluginLoaded(plugin: Plugin) {
816
+ this.onPluginAddedObservers.map((f) => f?.(plugin));
817
+ }
818
+
819
+ /**
820
+ * Initializes the flush policies from config and subscribes to updates to
821
+ * trigger flush
822
+ */
823
+ private setupFlushPolicies() {
824
+ const flushPolicies = [];
825
+
826
+ // If there are zero policies or flushAt/flushInterval use the defaults:
827
+ if (this.config.flushPolicies !== undefined) {
828
+ flushPolicies.push(...this.config.flushPolicies);
829
+ } else {
830
+ if (
831
+ this.config.flushAt === undefined ||
832
+ (this.config.flushAt !== null && this.config.flushAt > 0)
833
+ ) {
834
+ flushPolicies.push(
835
+ new CountFlushPolicy(this.config.flushAt ?? defaultFlushAt)
836
+ );
837
+ }
838
+
839
+ if (
840
+ this.config.flushInterval === undefined ||
841
+ (this.config.flushInterval !== null && this.config.flushInterval > 0)
842
+ ) {
843
+ flushPolicies.push(
844
+ new TimerFlushPolicy(
845
+ (this.config.flushInterval ?? defaultFlushInterval) * 1000
846
+ )
847
+ );
848
+ }
849
+ }
850
+
851
+ for (const fp of flushPolicies) {
852
+ this.flushPolicyExecuter.add(fp);
853
+ }
854
+ }
855
+
856
+ /**
857
+ * Adds a FlushPolicy to the list
858
+ * @param policies policies to add
859
+ */
860
+ addFlushPolicy(...policies: FlushPolicy[]) {
861
+ for (const policy of policies) {
862
+ this.flushPolicyExecuter.add(policy);
863
+ }
864
+ }
865
+
866
+ /**
867
+ * Removes a FlushPolicy from the execution
868
+ *
869
+ * @param policies policies to remove
870
+ * @returns true if the value was removed, false if not found
871
+ */
872
+ removeFlushPolicy(...policies: FlushPolicy[]) {
873
+ for (const policy of policies) {
874
+ this.flushPolicyExecuter.remove(policy);
875
+ }
876
+ }
877
+
878
+ /**
879
+ * Returns the current enabled flush policies
880
+ */
881
+ getFlushPolicies() {
882
+ return this.flushPolicyExecuter.policies;
883
+ }
884
+
885
+ reportInternalError(error: SegmentError, fatal = false) {
886
+ if (fatal) {
887
+ this.logger.error('A critical error ocurred: ', error);
888
+ } else {
889
+ this.logger.warn('An internal error occurred: ', error);
890
+ }
891
+ this.config.errorHandler?.(error);
892
+ }
893
+
894
+ /**
895
+ * Sets the messageId and timestamp
896
+ * @param event Segment Event
897
+ * @returns event with data injected
898
+ */
899
+ private applyRawEventData = (event: SegmentEvent): SegmentEvent => {
900
+ return {
901
+ ...event,
902
+ messageId: getUUID(),
903
+ timestamp: new Date().toISOString(),
904
+ integrations: event.integrations ?? {},
905
+ } as SegmentEvent;
906
+ };
907
+
908
+ /**
909
+ * Injects context and userInfo data into the event
910
+ * This is handled outside of the timeline to prevent concurrency issues between plugins
911
+ * This is only added after the client is ready to let the client restore values from storage
912
+ * @param event Segment Event
913
+ * @returns event with data injected
914
+ */
915
+ private applyContextData = async (
916
+ event: SegmentEvent
917
+ ): Promise<SegmentEvent> => {
918
+ const userInfo = await this.processUserInfo(event);
919
+ const context = await this.context.get(true);
920
+
921
+ return {
922
+ ...event,
923
+ ...userInfo,
924
+ context: {
925
+ ...event.context,
926
+ ...context,
927
+ },
928
+ } as SegmentEvent;
929
+ };
930
+
931
+ /**
932
+ * Processes the userInfo to add to an event.
933
+ * For Identify and Alias: it saves the new userId and traits into the storage
934
+ * For all: set the userId and anonymousId from the current values
935
+ * @param event segment event
936
+ * @returns userInfo to inject to an event
937
+ */
938
+ private processUserInfo = async (
939
+ event: SegmentEvent
940
+ ): Promise<Partial<SegmentEvent>> => {
941
+ // Order here is IMPORTANT!
942
+ // Identify and Alias userInfo set operations have to come as soon as possible
943
+ // Do not block the set by doing a safe get first as it might cause a race condition
944
+ // within events procesing in the timeline asyncronously
945
+ if (event.type === EventType.IdentifyEvent) {
946
+ const userInfo = await this.userInfo.set((state) => ({
947
+ ...state,
948
+ userId: event.userId ?? state.userId,
949
+ traits: {
950
+ ...state.traits,
951
+ ...event.traits,
952
+ },
953
+ }));
954
+
955
+ return {
956
+ anonymousId: userInfo.anonymousId,
957
+ userId: event.userId ?? userInfo.userId,
958
+ traits: {
959
+ ...userInfo.traits,
960
+ ...event.traits,
961
+ },
962
+ };
963
+ } else if (event.type === EventType.AliasEvent) {
964
+ let previousUserId: string;
965
+
966
+ const userInfo = await this.userInfo.set((state) => {
967
+ previousUserId = state.userId ?? state.anonymousId;
968
+
969
+ return {
970
+ ...state,
971
+ userId: event.userId,
972
+ };
973
+ });
974
+
975
+ return {
976
+ anonymousId: userInfo.anonymousId,
977
+ userId: event.userId,
978
+ previousId: previousUserId!,
979
+ };
980
+ }
981
+
982
+ const userInfo = await this.userInfo.get(true);
983
+ return {
984
+ anonymousId: userInfo.anonymousId,
985
+ userId: userInfo.userId,
986
+ };
987
+ };
988
+ /* Method for clearing flush queue */
989
+ clear() {
990
+ const plugins = this.getPlugins();
991
+
992
+ plugins.forEach(async (plugin) => {
993
+ if (plugin instanceof SegmentDestination) {
994
+ const timelinePlugins = plugin.timeline?.plugins?.after ?? [];
995
+
996
+ for (const subPlugin of timelinePlugins) {
997
+ if (subPlugin instanceof QueueFlushingPlugin) {
998
+ await subPlugin.dequeueEvents();
999
+ }
1000
+ }
1001
+ }
1002
+ });
1003
+
1004
+ this.flushPolicyExecuter.reset();
1005
+ }
1006
+
1007
+ /**
1008
+ * Method to get count of events in flush queue.
1009
+ */
1010
+ async pendingEvents() {
1011
+ const plugins = this.getPlugins();
1012
+ let totalEventsCount = 0;
1013
+
1014
+ for (const plugin of plugins) {
1015
+ // We're looking inside SegmentDestination's `after` plugins
1016
+ if (plugin instanceof SegmentDestination) {
1017
+ const timelinePlugins = plugin.timeline?.plugins?.after ?? [];
1018
+
1019
+ for (const subPlugin of timelinePlugins) {
1020
+ if (subPlugin instanceof QueueFlushingPlugin) {
1021
+ const eventsCount = await subPlugin.pendingEvents();
1022
+ totalEventsCount += eventsCount;
1023
+ }
1024
+ }
1025
+ }
1026
+ }
1027
+
1028
+ return totalEventsCount;
1029
+ }
1030
+ }