@customerio/cdp-analytics-browser 0.0.1

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 (516) hide show
  1. package/LICENSE.MD +22 -0
  2. package/README.md +21 -0
  3. package/dist/cjs/browser/browser-umd.js +20 -0
  4. package/dist/cjs/browser/browser-umd.js.map +1 -0
  5. package/dist/cjs/browser/index.js +310 -0
  6. package/dist/cjs/browser/index.js.map +1 -0
  7. package/dist/cjs/browser/standalone-analytics.js +60 -0
  8. package/dist/cjs/browser/standalone-analytics.js.map +1 -0
  9. package/dist/cjs/browser/standalone.js +83 -0
  10. package/dist/cjs/browser/standalone.js.map +1 -0
  11. package/dist/cjs/core/analytics/index.js +544 -0
  12. package/dist/cjs/core/analytics/index.js.map +1 -0
  13. package/dist/cjs/core/analytics/interfaces.js +3 -0
  14. package/dist/cjs/core/analytics/interfaces.js.map +1 -0
  15. package/dist/cjs/core/arguments-resolver/index.js +106 -0
  16. package/dist/cjs/core/arguments-resolver/index.js.map +1 -0
  17. package/dist/cjs/core/auto-track.js +108 -0
  18. package/dist/cjs/core/auto-track.js.map +1 -0
  19. package/dist/cjs/core/buffer/index.js +237 -0
  20. package/dist/cjs/core/buffer/index.js.map +1 -0
  21. package/dist/cjs/core/buffer/snippet.js +30 -0
  22. package/dist/cjs/core/buffer/snippet.js.map +1 -0
  23. package/dist/cjs/core/callback/index.js +7 -0
  24. package/dist/cjs/core/callback/index.js.map +1 -0
  25. package/dist/cjs/core/connection/index.js +16 -0
  26. package/dist/cjs/core/connection/index.js.map +1 -0
  27. package/dist/cjs/core/context/index.js +19 -0
  28. package/dist/cjs/core/context/index.js.map +1 -0
  29. package/dist/cjs/core/environment/index.js +12 -0
  30. package/dist/cjs/core/environment/index.js.map +1 -0
  31. package/dist/cjs/core/events/index.js +134 -0
  32. package/dist/cjs/core/events/index.js.map +1 -0
  33. package/dist/cjs/core/events/interfaces.js +3 -0
  34. package/dist/cjs/core/events/interfaces.js.map +1 -0
  35. package/dist/cjs/core/inspector/index.js +14 -0
  36. package/dist/cjs/core/inspector/index.js.map +1 -0
  37. package/dist/cjs/core/plugin/index.js +3 -0
  38. package/dist/cjs/core/plugin/index.js.map +1 -0
  39. package/dist/cjs/core/query-string/gracefulDecodeURIComponent.js +20 -0
  40. package/dist/cjs/core/query-string/gracefulDecodeURIComponent.js.map +1 -0
  41. package/dist/cjs/core/query-string/index.js +48 -0
  42. package/dist/cjs/core/query-string/index.js.map +1 -0
  43. package/dist/cjs/core/query-string/pickPrefix.js +20 -0
  44. package/dist/cjs/core/query-string/pickPrefix.js.map +1 -0
  45. package/dist/cjs/core/queue/event-queue.js +15 -0
  46. package/dist/cjs/core/queue/event-queue.js.map +1 -0
  47. package/dist/cjs/core/stats/index.js +23 -0
  48. package/dist/cjs/core/stats/index.js.map +1 -0
  49. package/dist/cjs/core/stats/remote-metrics.js +107 -0
  50. package/dist/cjs/core/stats/remote-metrics.js.map +1 -0
  51. package/dist/cjs/core/user/index.js +422 -0
  52. package/dist/cjs/core/user/index.js.map +1 -0
  53. package/dist/cjs/core/user/tld.js +64 -0
  54. package/dist/cjs/core/user/tld.js.map +1 -0
  55. package/dist/cjs/generated/version.js +6 -0
  56. package/dist/cjs/generated/version.js.map +1 -0
  57. package/dist/cjs/index.js +11 -0
  58. package/dist/cjs/index.js.map +1 -0
  59. package/dist/cjs/lib/bind-all.js +17 -0
  60. package/dist/cjs/lib/bind-all.js.map +1 -0
  61. package/dist/cjs/lib/browser-polyfill.js +22 -0
  62. package/dist/cjs/lib/browser-polyfill.js.map +1 -0
  63. package/dist/cjs/lib/create-deferred.js +21 -0
  64. package/dist/cjs/lib/create-deferred.js.map +1 -0
  65. package/dist/cjs/lib/csp-detection.js +33 -0
  66. package/dist/cjs/lib/csp-detection.js.map +1 -0
  67. package/dist/cjs/lib/embedded-write-key.js +22 -0
  68. package/dist/cjs/lib/embedded-write-key.js.map +1 -0
  69. package/dist/cjs/lib/fetch.js +19 -0
  70. package/dist/cjs/lib/fetch.js.map +1 -0
  71. package/dist/cjs/lib/get-global.js +21 -0
  72. package/dist/cjs/lib/get-global.js.map +1 -0
  73. package/dist/cjs/lib/get-process-env.js +15 -0
  74. package/dist/cjs/lib/get-process-env.js.map +1 -0
  75. package/dist/cjs/lib/group-by.js +28 -0
  76. package/dist/cjs/lib/group-by.js.map +1 -0
  77. package/dist/cjs/lib/is-plan-event-enabled.js +20 -0
  78. package/dist/cjs/lib/is-plan-event-enabled.js.map +1 -0
  79. package/dist/cjs/lib/is-thenable.js +15 -0
  80. package/dist/cjs/lib/is-thenable.js.map +1 -0
  81. package/dist/cjs/lib/load-script.js +56 -0
  82. package/dist/cjs/lib/load-script.js.map +1 -0
  83. package/dist/cjs/lib/merged-options.js +31 -0
  84. package/dist/cjs/lib/merged-options.js.map +1 -0
  85. package/dist/cjs/lib/on-page-leave.js +35 -0
  86. package/dist/cjs/lib/on-page-leave.js.map +1 -0
  87. package/dist/cjs/lib/p-while.js +25 -0
  88. package/dist/cjs/lib/p-while.js.map +1 -0
  89. package/dist/cjs/lib/parse-cdn.js +79 -0
  90. package/dist/cjs/lib/parse-cdn.js.map +1 -0
  91. package/dist/cjs/lib/priority-queue/backoff.js +10 -0
  92. package/dist/cjs/lib/priority-queue/backoff.js.map +1 -0
  93. package/dist/cjs/lib/priority-queue/index.js +7 -0
  94. package/dist/cjs/lib/priority-queue/index.js.map +1 -0
  95. package/dist/cjs/lib/priority-queue/persisted.js +108 -0
  96. package/dist/cjs/lib/priority-queue/persisted.js.map +1 -0
  97. package/dist/cjs/lib/sleep.js +10 -0
  98. package/dist/cjs/lib/sleep.js.map +1 -0
  99. package/dist/cjs/lib/to-facade.js +32 -0
  100. package/dist/cjs/lib/to-facade.js.map +1 -0
  101. package/dist/cjs/node/index.js +43 -0
  102. package/dist/cjs/node/index.js.map +1 -0
  103. package/dist/cjs/node/node.browser.js +13 -0
  104. package/dist/cjs/node/node.browser.js.map +1 -0
  105. package/dist/cjs/plugins/ajs-destination/index.js +345 -0
  106. package/dist/cjs/plugins/ajs-destination/index.js.map +1 -0
  107. package/dist/cjs/plugins/ajs-destination/loader.js +110 -0
  108. package/dist/cjs/plugins/ajs-destination/loader.js.map +1 -0
  109. package/dist/cjs/plugins/ajs-destination/types.js +3 -0
  110. package/dist/cjs/plugins/ajs-destination/types.js.map +1 -0
  111. package/dist/cjs/plugins/ajs-destination/utils.js +23 -0
  112. package/dist/cjs/plugins/ajs-destination/utils.js.map +1 -0
  113. package/dist/cjs/plugins/analytics-node/index.js +65 -0
  114. package/dist/cjs/plugins/analytics-node/index.js.map +1 -0
  115. package/dist/cjs/plugins/customerio/batched-dispatcher.js +102 -0
  116. package/dist/cjs/plugins/customerio/batched-dispatcher.js.map +1 -0
  117. package/dist/cjs/plugins/customerio/fetch-dispatcher.js +18 -0
  118. package/dist/cjs/plugins/customerio/fetch-dispatcher.js.map +1 -0
  119. package/dist/cjs/plugins/customerio/index.js +97 -0
  120. package/dist/cjs/plugins/customerio/index.js.map +1 -0
  121. package/dist/cjs/plugins/customerio/normalize.js +176 -0
  122. package/dist/cjs/plugins/customerio/normalize.js.map +1 -0
  123. package/dist/cjs/plugins/customerio/schedule-flush.js +76 -0
  124. package/dist/cjs/plugins/customerio/schedule-flush.js.map +1 -0
  125. package/dist/cjs/plugins/legacy-video-plugins/index.js +25 -0
  126. package/dist/cjs/plugins/legacy-video-plugins/index.js.map +1 -0
  127. package/dist/cjs/plugins/middleware/index.js +124 -0
  128. package/dist/cjs/plugins/middleware/index.js.map +1 -0
  129. package/dist/cjs/plugins/page-enrichment/index.js +98 -0
  130. package/dist/cjs/plugins/page-enrichment/index.js.map +1 -0
  131. package/dist/cjs/plugins/remote-loader/index.js +211 -0
  132. package/dist/cjs/plugins/remote-loader/index.js.map +1 -0
  133. package/dist/cjs/plugins/remote-middleware/index.js +70 -0
  134. package/dist/cjs/plugins/remote-middleware/index.js.map +1 -0
  135. package/dist/cjs/plugins/routing-middleware/index.js +26 -0
  136. package/dist/cjs/plugins/routing-middleware/index.js.map +1 -0
  137. package/dist/cjs/plugins/schema-filter/index.js +67 -0
  138. package/dist/cjs/plugins/schema-filter/index.js.map +1 -0
  139. package/dist/cjs/plugins/validation/index.js +70 -0
  140. package/dist/cjs/plugins/validation/index.js.map +1 -0
  141. package/dist/pkg/browser/browser-umd.js +17 -0
  142. package/dist/pkg/browser/browser-umd.js.map +1 -0
  143. package/dist/pkg/browser/index.js +306 -0
  144. package/dist/pkg/browser/index.js.map +1 -0
  145. package/dist/pkg/browser/standalone-analytics.js +56 -0
  146. package/dist/pkg/browser/standalone-analytics.js.map +1 -0
  147. package/dist/pkg/browser/standalone.js +81 -0
  148. package/dist/pkg/browser/standalone.js.map +1 -0
  149. package/dist/pkg/core/analytics/index.js +541 -0
  150. package/dist/pkg/core/analytics/index.js.map +1 -0
  151. package/dist/pkg/core/analytics/interfaces.js +2 -0
  152. package/dist/pkg/core/analytics/interfaces.js.map +1 -0
  153. package/dist/pkg/core/arguments-resolver/index.js +99 -0
  154. package/dist/pkg/core/arguments-resolver/index.js.map +1 -0
  155. package/dist/pkg/core/auto-track.js +103 -0
  156. package/dist/pkg/core/auto-track.js.map +1 -0
  157. package/dist/pkg/core/buffer/index.js +231 -0
  158. package/dist/pkg/core/buffer/index.js.map +1 -0
  159. package/dist/pkg/core/buffer/snippet.js +25 -0
  160. package/dist/pkg/core/buffer/snippet.js.map +1 -0
  161. package/dist/pkg/core/callback/index.js +2 -0
  162. package/dist/pkg/core/callback/index.js.map +1 -0
  163. package/dist/pkg/core/connection/index.js +11 -0
  164. package/dist/pkg/core/connection/index.js.map +1 -0
  165. package/dist/pkg/core/context/index.js +16 -0
  166. package/dist/pkg/core/context/index.js.map +1 -0
  167. package/dist/pkg/core/environment/index.js +7 -0
  168. package/dist/pkg/core/environment/index.js.map +1 -0
  169. package/dist/pkg/core/events/index.js +131 -0
  170. package/dist/pkg/core/events/index.js.map +1 -0
  171. package/dist/pkg/core/events/interfaces.js +2 -0
  172. package/dist/pkg/core/events/interfaces.js.map +1 -0
  173. package/dist/pkg/core/inspector/index.js +10 -0
  174. package/dist/pkg/core/inspector/index.js.map +1 -0
  175. package/dist/pkg/core/plugin/index.js +2 -0
  176. package/dist/pkg/core/plugin/index.js.map +1 -0
  177. package/dist/pkg/core/query-string/gracefulDecodeURIComponent.js +16 -0
  178. package/dist/pkg/core/query-string/gracefulDecodeURIComponent.js.map +1 -0
  179. package/dist/pkg/core/query-string/index.js +44 -0
  180. package/dist/pkg/core/query-string/index.js.map +1 -0
  181. package/dist/pkg/core/query-string/pickPrefix.js +16 -0
  182. package/dist/pkg/core/query-string/pickPrefix.js.map +1 -0
  183. package/dist/pkg/core/queue/event-queue.js +12 -0
  184. package/dist/pkg/core/queue/event-queue.js.map +1 -0
  185. package/dist/pkg/core/stats/index.js +20 -0
  186. package/dist/pkg/core/stats/index.js.map +1 -0
  187. package/dist/pkg/core/stats/remote-metrics.js +104 -0
  188. package/dist/pkg/core/stats/remote-metrics.js.map +1 -0
  189. package/dist/pkg/core/user/index.js +418 -0
  190. package/dist/pkg/core/user/index.js.map +1 -0
  191. package/dist/pkg/core/user/tld.js +59 -0
  192. package/dist/pkg/core/user/tld.js.map +1 -0
  193. package/dist/pkg/generated/version.js +3 -0
  194. package/dist/pkg/generated/version.js.map +1 -0
  195. package/dist/pkg/index.js +8 -0
  196. package/dist/pkg/index.js.map +1 -0
  197. package/dist/pkg/lib/bind-all.js +14 -0
  198. package/dist/pkg/lib/bind-all.js.map +1 -0
  199. package/dist/pkg/lib/browser-polyfill.js +18 -0
  200. package/dist/pkg/lib/browser-polyfill.js.map +1 -0
  201. package/dist/pkg/lib/create-deferred.js +17 -0
  202. package/dist/pkg/lib/create-deferred.js.map +1 -0
  203. package/dist/pkg/lib/csp-detection.js +29 -0
  204. package/dist/pkg/lib/csp-detection.js.map +1 -0
  205. package/dist/pkg/lib/embedded-write-key.js +18 -0
  206. package/dist/pkg/lib/embedded-write-key.js.map +1 -0
  207. package/dist/pkg/lib/fetch.js +14 -0
  208. package/dist/pkg/lib/fetch.js.map +1 -0
  209. package/dist/pkg/lib/get-global.js +17 -0
  210. package/dist/pkg/lib/get-global.js.map +1 -0
  211. package/dist/pkg/lib/get-process-env.js +11 -0
  212. package/dist/pkg/lib/get-process-env.js.map +1 -0
  213. package/dist/pkg/lib/group-by.js +24 -0
  214. package/dist/pkg/lib/group-by.js.map +1 -0
  215. package/dist/pkg/lib/is-plan-event-enabled.js +16 -0
  216. package/dist/pkg/lib/is-plan-event-enabled.js.map +1 -0
  217. package/dist/pkg/lib/is-thenable.js +11 -0
  218. package/dist/pkg/lib/is-thenable.js.map +1 -0
  219. package/dist/pkg/lib/load-script.js +51 -0
  220. package/dist/pkg/lib/load-script.js.map +1 -0
  221. package/dist/pkg/lib/merged-options.js +27 -0
  222. package/dist/pkg/lib/merged-options.js.map +1 -0
  223. package/dist/pkg/lib/on-page-leave.js +31 -0
  224. package/dist/pkg/lib/on-page-leave.js.map +1 -0
  225. package/dist/pkg/lib/p-while.js +21 -0
  226. package/dist/pkg/lib/p-while.js.map +1 -0
  227. package/dist/pkg/lib/parse-cdn.js +72 -0
  228. package/dist/pkg/lib/parse-cdn.js.map +1 -0
  229. package/dist/pkg/lib/priority-queue/backoff.js +6 -0
  230. package/dist/pkg/lib/priority-queue/backoff.js.map +1 -0
  231. package/dist/pkg/lib/priority-queue/index.js +3 -0
  232. package/dist/pkg/lib/priority-queue/index.js.map +1 -0
  233. package/dist/pkg/lib/priority-queue/persisted.js +105 -0
  234. package/dist/pkg/lib/priority-queue/persisted.js.map +1 -0
  235. package/dist/pkg/lib/sleep.js +6 -0
  236. package/dist/pkg/lib/sleep.js.map +1 -0
  237. package/dist/pkg/lib/to-facade.js +28 -0
  238. package/dist/pkg/lib/to-facade.js.map +1 -0
  239. package/dist/pkg/node/index.js +40 -0
  240. package/dist/pkg/node/index.js.map +1 -0
  241. package/dist/pkg/node/node.browser.js +10 -0
  242. package/dist/pkg/node/node.browser.js.map +1 -0
  243. package/dist/pkg/plugins/ajs-destination/index.js +341 -0
  244. package/dist/pkg/plugins/ajs-destination/index.js.map +1 -0
  245. package/dist/pkg/plugins/ajs-destination/loader.js +102 -0
  246. package/dist/pkg/plugins/ajs-destination/loader.js.map +1 -0
  247. package/dist/pkg/plugins/ajs-destination/types.js +2 -0
  248. package/dist/pkg/plugins/ajs-destination/types.js.map +1 -0
  249. package/dist/pkg/plugins/ajs-destination/utils.js +18 -0
  250. package/dist/pkg/plugins/ajs-destination/utils.js.map +1 -0
  251. package/dist/pkg/plugins/analytics-node/index.js +60 -0
  252. package/dist/pkg/plugins/analytics-node/index.js.map +1 -0
  253. package/dist/pkg/plugins/customerio/batched-dispatcher.js +99 -0
  254. package/dist/pkg/plugins/customerio/batched-dispatcher.js.map +1 -0
  255. package/dist/pkg/plugins/customerio/fetch-dispatcher.js +15 -0
  256. package/dist/pkg/plugins/customerio/fetch-dispatcher.js.map +1 -0
  257. package/dist/pkg/plugins/customerio/index.js +93 -0
  258. package/dist/pkg/plugins/customerio/index.js.map +1 -0
  259. package/dist/pkg/plugins/customerio/normalize.js +168 -0
  260. package/dist/pkg/plugins/customerio/normalize.js.map +1 -0
  261. package/dist/pkg/plugins/customerio/schedule-flush.js +72 -0
  262. package/dist/pkg/plugins/customerio/schedule-flush.js.map +1 -0
  263. package/dist/pkg/plugins/legacy-video-plugins/index.js +25 -0
  264. package/dist/pkg/plugins/legacy-video-plugins/index.js.map +1 -0
  265. package/dist/pkg/plugins/middleware/index.js +119 -0
  266. package/dist/pkg/plugins/middleware/index.js.map +1 -0
  267. package/dist/pkg/plugins/page-enrichment/index.js +93 -0
  268. package/dist/pkg/plugins/page-enrichment/index.js.map +1 -0
  269. package/dist/pkg/plugins/remote-loader/index.js +207 -0
  270. package/dist/pkg/plugins/remote-loader/index.js.map +1 -0
  271. package/dist/pkg/plugins/remote-middleware/index.js +66 -0
  272. package/dist/pkg/plugins/remote-middleware/index.js.map +1 -0
  273. package/dist/pkg/plugins/routing-middleware/index.js +21 -0
  274. package/dist/pkg/plugins/routing-middleware/index.js.map +1 -0
  275. package/dist/pkg/plugins/schema-filter/index.js +63 -0
  276. package/dist/pkg/plugins/schema-filter/index.js.map +1 -0
  277. package/dist/pkg/plugins/validation/index.js +63 -0
  278. package/dist/pkg/plugins/validation/index.js.map +1 -0
  279. package/dist/types/browser/browser-umd.d.ts +2 -0
  280. package/dist/types/browser/browser-umd.d.ts.map +1 -0
  281. package/dist/types/browser/index.d.ts +86 -0
  282. package/dist/types/browser/index.d.ts.map +1 -0
  283. package/dist/types/browser/standalone-analytics.d.ts +16 -0
  284. package/dist/types/browser/standalone-analytics.d.ts.map +1 -0
  285. package/dist/types/browser/standalone.d.ts +2 -0
  286. package/dist/types/browser/standalone.d.ts.map +1 -0
  287. package/dist/types/core/analytics/index.d.ts +116 -0
  288. package/dist/types/core/analytics/index.d.ts.map +1 -0
  289. package/dist/types/core/analytics/interfaces.d.ts +77 -0
  290. package/dist/types/core/analytics/interfaces.d.ts.map +1 -0
  291. package/dist/types/core/arguments-resolver/index.d.ts +34 -0
  292. package/dist/types/core/arguments-resolver/index.d.ts.map +1 -0
  293. package/dist/types/core/auto-track.d.ts +10 -0
  294. package/dist/types/core/auto-track.d.ts.map +1 -0
  295. package/dist/types/core/buffer/index.d.ts +86 -0
  296. package/dist/types/core/buffer/index.d.ts.map +1 -0
  297. package/dist/types/core/buffer/snippet.d.ts +10 -0
  298. package/dist/types/core/buffer/snippet.d.ts.map +1 -0
  299. package/dist/types/core/callback/index.d.ts +2 -0
  300. package/dist/types/core/callback/index.d.ts.map +1 -0
  301. package/dist/types/core/connection/index.d.ts +3 -0
  302. package/dist/types/core/connection/index.d.ts.map +1 -0
  303. package/dist/types/core/context/index.d.ts +9 -0
  304. package/dist/types/core/context/index.d.ts.map +1 -0
  305. package/dist/types/core/environment/index.d.ts +3 -0
  306. package/dist/types/core/environment/index.d.ts.map +1 -0
  307. package/dist/types/core/events/index.d.ts +21 -0
  308. package/dist/types/core/events/index.d.ts.map +1 -0
  309. package/dist/types/core/events/interfaces.d.ts +9 -0
  310. package/dist/types/core/events/interfaces.d.ts.map +1 -0
  311. package/dist/types/core/inspector/index.d.ts +3 -0
  312. package/dist/types/core/inspector/index.d.ts.map +1 -0
  313. package/dist/types/core/plugin/index.d.ts +11 -0
  314. package/dist/types/core/plugin/index.d.ts.map +1 -0
  315. package/dist/types/core/query-string/gracefulDecodeURIComponent.d.ts +9 -0
  316. package/dist/types/core/query-string/gracefulDecodeURIComponent.d.ts.map +1 -0
  317. package/dist/types/core/query-string/index.d.ts +7 -0
  318. package/dist/types/core/query-string/index.d.ts.map +1 -0
  319. package/dist/types/core/query-string/pickPrefix.d.ts +9 -0
  320. package/dist/types/core/query-string/pickPrefix.d.ts.map +1 -0
  321. package/dist/types/core/queue/event-queue.d.ts +8 -0
  322. package/dist/types/core/queue/event-queue.d.ts.map +1 -0
  323. package/dist/types/core/stats/index.d.ts +7 -0
  324. package/dist/types/core/stats/index.d.ts.map +1 -0
  325. package/dist/types/core/stats/remote-metrics.d.ts +29 -0
  326. package/dist/types/core/stats/remote-metrics.d.ts.map +1 -0
  327. package/dist/types/core/user/index.d.ts +128 -0
  328. package/dist/types/core/user/index.d.ts.map +1 -0
  329. package/dist/types/core/user/tld.d.ts +2 -0
  330. package/dist/types/core/user/tld.d.ts.map +1 -0
  331. package/dist/types/generated/version.d.ts +2 -0
  332. package/dist/types/generated/version.d.ts.map +1 -0
  333. package/dist/types/index.d.ts +9 -0
  334. package/dist/types/index.d.ts.map +1 -0
  335. package/dist/types/lib/bind-all.d.ts +4 -0
  336. package/dist/types/lib/bind-all.d.ts.map +1 -0
  337. package/dist/types/lib/browser-polyfill.d.ts +2 -0
  338. package/dist/types/lib/browser-polyfill.d.ts.map +1 -0
  339. package/dist/types/lib/create-deferred.d.ts +9 -0
  340. package/dist/types/lib/create-deferred.d.ts.map +1 -0
  341. package/dist/types/lib/csp-detection.d.ts +4 -0
  342. package/dist/types/lib/csp-detection.d.ts.map +1 -0
  343. package/dist/types/lib/embedded-write-key.d.ts +7 -0
  344. package/dist/types/lib/embedded-write-key.d.ts.map +1 -0
  345. package/dist/types/lib/fetch.d.ts +5 -0
  346. package/dist/types/lib/fetch.d.ts.map +1 -0
  347. package/dist/types/lib/get-global.d.ts +2 -0
  348. package/dist/types/lib/get-global.d.ts.map +1 -0
  349. package/dist/types/lib/get-process-env.d.ts +8 -0
  350. package/dist/types/lib/get-process-env.d.ts.map +1 -0
  351. package/dist/types/lib/group-by.d.ts +4 -0
  352. package/dist/types/lib/group-by.d.ts.map +1 -0
  353. package/dist/types/lib/is-plan-event-enabled.d.ts +9 -0
  354. package/dist/types/lib/is-plan-event-enabled.d.ts.map +1 -0
  355. package/dist/types/lib/is-thenable.d.ts +6 -0
  356. package/dist/types/lib/is-thenable.d.ts.map +1 -0
  357. package/dist/types/lib/load-script.d.ts +3 -0
  358. package/dist/types/lib/load-script.d.ts.map +1 -0
  359. package/dist/types/lib/merged-options.d.ts +13 -0
  360. package/dist/types/lib/merged-options.d.ts.map +1 -0
  361. package/dist/types/lib/on-page-leave.d.ts +11 -0
  362. package/dist/types/lib/on-page-leave.d.ts.map +1 -0
  363. package/dist/types/lib/p-while.d.ts +2 -0
  364. package/dist/types/lib/p-while.d.ts.map +1 -0
  365. package/dist/types/lib/parse-cdn.d.ts +10 -0
  366. package/dist/types/lib/parse-cdn.d.ts.map +1 -0
  367. package/dist/types/lib/priority-queue/backoff.d.ts +13 -0
  368. package/dist/types/lib/priority-queue/backoff.d.ts.map +1 -0
  369. package/dist/types/lib/priority-queue/index.d.ts +3 -0
  370. package/dist/types/lib/priority-queue/index.d.ts.map +1 -0
  371. package/dist/types/lib/priority-queue/persisted.d.ts +6 -0
  372. package/dist/types/lib/priority-queue/persisted.d.ts.map +1 -0
  373. package/dist/types/lib/sleep.d.ts +2 -0
  374. package/dist/types/lib/sleep.d.ts.map +1 -0
  375. package/dist/types/lib/to-facade.d.ts +7 -0
  376. package/dist/types/lib/to-facade.d.ts.map +1 -0
  377. package/dist/types/node/index.d.ts +8 -0
  378. package/dist/types/node/index.d.ts.map +1 -0
  379. package/dist/types/node/node.browser.d.ts +4 -0
  380. package/dist/types/node/node.browser.d.ts.map +1 -0
  381. package/dist/types/plugins/ajs-destination/index.d.ts +42 -0
  382. package/dist/types/plugins/ajs-destination/index.d.ts.map +1 -0
  383. package/dist/types/plugins/ajs-destination/loader.d.ts +12 -0
  384. package/dist/types/plugins/ajs-destination/loader.d.ts.map +1 -0
  385. package/dist/types/plugins/ajs-destination/types.d.ts +36 -0
  386. package/dist/types/plugins/ajs-destination/types.d.ts.map +1 -0
  387. package/dist/types/plugins/ajs-destination/utils.d.ts +5 -0
  388. package/dist/types/plugins/ajs-destination/utils.d.ts.map +1 -0
  389. package/dist/types/plugins/analytics-node/index.d.ts +12 -0
  390. package/dist/types/plugins/analytics-node/index.d.ts.map +1 -0
  391. package/dist/types/plugins/customerio/batched-dispatcher.d.ts +8 -0
  392. package/dist/types/plugins/customerio/batched-dispatcher.d.ts.map +1 -0
  393. package/dist/types/plugins/customerio/fetch-dispatcher.d.ts +8 -0
  394. package/dist/types/plugins/customerio/fetch-dispatcher.d.ts.map +1 -0
  395. package/dist/types/plugins/customerio/index.d.ts +26 -0
  396. package/dist/types/plugins/customerio/index.d.ts.map +1 -0
  397. package/dist/types/plugins/customerio/normalize.d.ts +12 -0
  398. package/dist/types/plugins/customerio/normalize.d.ts.map +1 -0
  399. package/dist/types/plugins/customerio/schedule-flush.d.ts +5 -0
  400. package/dist/types/plugins/customerio/schedule-flush.d.ts.map +1 -0
  401. package/dist/types/plugins/legacy-video-plugins/index.d.ts +3 -0
  402. package/dist/types/plugins/legacy-video-plugins/index.d.ts.map +1 -0
  403. package/dist/types/plugins/middleware/index.d.ts +18 -0
  404. package/dist/types/plugins/middleware/index.d.ts.map +1 -0
  405. package/dist/types/plugins/page-enrichment/index.d.ts +21 -0
  406. package/dist/types/plugins/page-enrichment/index.d.ts.map +1 -0
  407. package/dist/types/plugins/remote-loader/index.d.ts +43 -0
  408. package/dist/types/plugins/remote-loader/index.d.ts.map +1 -0
  409. package/dist/types/plugins/remote-middleware/index.d.ts +5 -0
  410. package/dist/types/plugins/remote-middleware/index.d.ts.map +1 -0
  411. package/dist/types/plugins/routing-middleware/index.d.ts +13 -0
  412. package/dist/types/plugins/routing-middleware/index.d.ts.map +1 -0
  413. package/dist/types/plugins/schema-filter/index.d.ts +5 -0
  414. package/dist/types/plugins/schema-filter/index.d.ts.map +1 -0
  415. package/dist/types/plugins/validation/index.d.ts +7 -0
  416. package/dist/types/plugins/validation/index.d.ts.map +1 -0
  417. package/dist/umd/799.bundle.f804d7b881cf284fbd56.js +1 -0
  418. package/dist/umd/799.bundle.f804d7b881cf284fbd56.js.gz +0 -0
  419. package/dist/umd/870.bundle.e0b7bfa4797bb87217c9.js +1 -0
  420. package/dist/umd/870.bundle.e0b7bfa4797bb87217c9.js.gz +0 -0
  421. package/dist/umd/ajs-destination.bundle.847bf422720e5616c9d5.js +1 -0
  422. package/dist/umd/ajs-destination.bundle.847bf422720e5616c9d5.js.gz +0 -0
  423. package/dist/umd/auto-track.bundle.758629f7a47544af2d44.js +1 -0
  424. package/dist/umd/auto-track.bundle.758629f7a47544af2d44.js.gz +0 -0
  425. package/dist/umd/index.js +1 -0
  426. package/dist/umd/index.js.gz +0 -0
  427. package/dist/umd/legacyVideos.bundle.d7ac356ab28fab36e111.js +1 -0
  428. package/dist/umd/legacyVideos.bundle.d7ac356ab28fab36e111.js.gz +0 -0
  429. package/dist/umd/queryString.bundle.913501e40ce64ef92884.js +1 -0
  430. package/dist/umd/queryString.bundle.913501e40ce64ef92884.js.gz +0 -0
  431. package/dist/umd/remoteMiddleware.bundle.3bc569811c4c2c93d90c.js +1 -0
  432. package/dist/umd/remoteMiddleware.bundle.3bc569811c4c2c93d90c.js.gz +0 -0
  433. package/dist/umd/schemaFilter.bundle.bb8180ae95843c8f138e.js +1 -0
  434. package/dist/umd/schemaFilter.bundle.bb8180ae95843c8f138e.js.gz +0 -0
  435. package/dist/umd/standalone.js +1 -0
  436. package/dist/umd/standalone.js.gz +0 -0
  437. package/dist/umd/tsub-middleware.bundle.af0f402084ddd2b605cc.js +1 -0
  438. package/dist/umd/tsub-middleware.bundle.af0f402084ddd2b605cc.js.gz +0 -0
  439. package/package.json +103 -0
  440. package/src/browser/browser-umd.ts +19 -0
  441. package/src/browser/index.ts +397 -0
  442. package/src/browser/standalone-analytics.ts +74 -0
  443. package/src/browser/standalone.ts +78 -0
  444. package/src/core/analytics/index.ts +567 -0
  445. package/src/core/analytics/interfaces.ts +100 -0
  446. package/src/core/arguments-resolver/index.ts +166 -0
  447. package/src/core/auto-track.ts +152 -0
  448. package/src/core/buffer/index.ts +289 -0
  449. package/src/core/buffer/snippet.ts +43 -0
  450. package/src/core/callback/index.ts +1 -0
  451. package/src/core/connection/index.ts +13 -0
  452. package/src/core/context/index.ts +21 -0
  453. package/src/core/environment/index.ts +7 -0
  454. package/src/core/events/index.ts +254 -0
  455. package/src/core/events/interfaces.ts +36 -0
  456. package/src/core/inspector/index.ts +14 -0
  457. package/src/core/plugin/index.ts +12 -0
  458. package/src/core/query-string/gracefulDecodeURIComponent.ts +16 -0
  459. package/src/core/query-string/index.ts +64 -0
  460. package/src/core/query-string/pickPrefix.ts +20 -0
  461. package/src/core/queue/event-queue.ts +11 -0
  462. package/src/core/stats/index.ts +15 -0
  463. package/src/core/stats/remote-metrics.ts +137 -0
  464. package/src/core/user/index.ts +534 -0
  465. package/src/core/user/tld.ts +65 -0
  466. package/src/generated/version.ts +2 -0
  467. package/src/index.ts +10 -0
  468. package/src/lib/bind-all.ts +19 -0
  469. package/src/lib/browser-polyfill.ts +23 -0
  470. package/src/lib/create-deferred.ts +16 -0
  471. package/src/lib/csp-detection.ts +26 -0
  472. package/src/lib/embedded-write-key.ts +24 -0
  473. package/src/lib/fetch.ts +10 -0
  474. package/src/lib/get-global.ts +16 -0
  475. package/src/lib/get-process-env.ts +11 -0
  476. package/src/lib/group-by.ts +30 -0
  477. package/src/lib/is-plan-event-enabled.ts +20 -0
  478. package/src/lib/is-thenable.ts +9 -0
  479. package/src/lib/load-script.ts +66 -0
  480. package/src/lib/merged-options.ts +46 -0
  481. package/src/lib/on-page-leave.ts +29 -0
  482. package/src/lib/p-while.ts +12 -0
  483. package/src/lib/parse-cdn.ts +85 -0
  484. package/src/lib/priority-queue/backoff.ts +24 -0
  485. package/src/lib/priority-queue/index.ts +6 -0
  486. package/src/lib/priority-queue/persisted.ts +127 -0
  487. package/src/lib/sleep.ts +4 -0
  488. package/src/lib/to-facade.ts +53 -0
  489. package/src/node/index.ts +36 -0
  490. package/src/node/node.browser.ts +7 -0
  491. package/src/plugins/ajs-destination/index.ts +388 -0
  492. package/src/plugins/ajs-destination/loader.ts +127 -0
  493. package/src/plugins/ajs-destination/types.ts +44 -0
  494. package/src/plugins/ajs-destination/utils.ts +34 -0
  495. package/src/plugins/analytics-node/index.ts +64 -0
  496. package/src/plugins/customerio/batched-dispatcher.ts +115 -0
  497. package/src/plugins/customerio/fetch-dispatcher.ts +24 -0
  498. package/src/plugins/customerio/index.ts +142 -0
  499. package/src/plugins/customerio/normalize.ts +223 -0
  500. package/src/plugins/customerio/schedule-flush.ts +58 -0
  501. package/src/plugins/legacy-video-plugins/index.ts +16 -0
  502. package/src/plugins/middleware/index.ts +131 -0
  503. package/src/plugins/page-enrichment/index.ts +127 -0
  504. package/src/plugins/remote-loader/index.ts +240 -0
  505. package/src/plugins/remote-middleware/index.ts +44 -0
  506. package/src/plugins/routing-middleware/index.ts +37 -0
  507. package/src/plugins/schema-filter/index.ts +90 -0
  508. package/src/plugins/validation/index.ts +80 -0
  509. package/src/test-helpers/browser-storage.ts +72 -0
  510. package/src/test-helpers/factories.ts +18 -0
  511. package/src/test-helpers/fetch-parse.ts +8 -0
  512. package/src/test-helpers/fixtures/cdn-settings.ts +299 -0
  513. package/src/test-helpers/fixtures/classic-destination.ts +25 -0
  514. package/src/test-helpers/fixtures/create-fetch-method.ts +24 -0
  515. package/src/test-helpers/test-writekeys.ts +5 -0
  516. package/src/test-helpers/type-assertions.ts +11 -0
@@ -0,0 +1,115 @@
1
+ import { CustomerioEvent } from '../../core/events'
2
+ import { fetch } from '../../lib/fetch'
3
+ import { onPageLeave } from '../../lib/on-page-leave'
4
+
5
+ export type BatchingDispatchConfig = {
6
+ size?: number
7
+ timeout?: number
8
+ }
9
+
10
+ const MAX_PAYLOAD_SIZE = 500
11
+
12
+ function kilobytes(buffer: unknown): number {
13
+ const size = encodeURI(JSON.stringify(buffer)).split(/%..|./).length - 1
14
+ return size / 1024
15
+ }
16
+
17
+ /**
18
+ * Checks if the payload is over or close to
19
+ * the maximum payload size allowed by tracking
20
+ * API.
21
+ */
22
+ function approachingTrackingAPILimit(buffer: unknown): boolean {
23
+ return kilobytes(buffer) >= MAX_PAYLOAD_SIZE - 50
24
+ }
25
+
26
+ function chunks(batch: object[]): Array<object[]> {
27
+ const result: object[][] = []
28
+ let index = 0
29
+
30
+ batch.forEach((item) => {
31
+ const size = kilobytes(result[index])
32
+ if (size >= 64) {
33
+ index++
34
+ }
35
+
36
+ if (result[index]) {
37
+ result[index].push(item)
38
+ } else {
39
+ result[index] = [item]
40
+ }
41
+ })
42
+
43
+ return result
44
+ }
45
+
46
+ export default function batch(
47
+ apiHost: string,
48
+ config?: BatchingDispatchConfig
49
+ ) {
50
+ let buffer: object[] = []
51
+ let pageUnloaded = false
52
+
53
+ const limit = config?.size ?? 10
54
+ const timeout = config?.timeout ?? 5000
55
+
56
+ function sendBatch(batch: object[]) {
57
+ if (batch.length === 0) {
58
+ return
59
+ }
60
+
61
+ const writeKey = (batch[0] as CustomerioEvent)?.writeKey
62
+
63
+ return fetch(`https://${apiHost}/b`, {
64
+ keepalive: pageUnloaded,
65
+ headers: {
66
+ 'Content-Type': 'text/plain',
67
+ },
68
+ method: 'post',
69
+ body: JSON.stringify({ batch, writeKey }),
70
+ })
71
+ }
72
+
73
+ async function flush(): Promise<unknown> {
74
+ if (buffer.length) {
75
+ const batch = buffer
76
+ buffer = []
77
+ return sendBatch(batch)
78
+ }
79
+ }
80
+
81
+ let schedule: NodeJS.Timeout | undefined
82
+
83
+ function scheduleFlush(): void {
84
+ if (schedule) {
85
+ return
86
+ }
87
+
88
+ schedule = setTimeout(() => {
89
+ schedule = undefined
90
+ flush().catch(console.error)
91
+ }, timeout)
92
+ }
93
+
94
+ onPageLeave(() => {
95
+ pageUnloaded = true
96
+
97
+ if (buffer.length) {
98
+ const reqs = chunks(buffer).map(sendBatch)
99
+ Promise.all(reqs).catch(console.error)
100
+ }
101
+ })
102
+
103
+ async function dispatch(_url: string, body: object): Promise<unknown> {
104
+ buffer.push(body)
105
+
106
+ const bufferOverflow =
107
+ buffer.length >= limit || approachingTrackingAPILimit(buffer)
108
+
109
+ return bufferOverflow || pageUnloaded ? flush() : scheduleFlush()
110
+ }
111
+
112
+ return {
113
+ dispatch,
114
+ }
115
+ }
@@ -0,0 +1,24 @@
1
+ import { fetch } from '../../lib/fetch'
2
+
3
+ export type Dispatcher = (url: string, body: object) => Promise<unknown>
4
+
5
+ export type StandardDispatcherConfig = {
6
+ keepalive?: boolean
7
+ }
8
+
9
+ export default function (config?: StandardDispatcherConfig): {
10
+ dispatch: Dispatcher
11
+ } {
12
+ function dispatch(url: string, body: object): Promise<unknown> {
13
+ return fetch(url, {
14
+ keepalive: config?.keepalive,
15
+ headers: { 'Content-Type': 'text/plain' },
16
+ method: 'post',
17
+ body: JSON.stringify(body),
18
+ })
19
+ }
20
+
21
+ return {
22
+ dispatch,
23
+ }
24
+ }
@@ -0,0 +1,142 @@
1
+ import { Facade } from '@segment/facade'
2
+ import { Analytics } from '../../core/analytics'
3
+ import { LegacySettings } from '../../browser'
4
+ import { isOffline } from '../../core/connection'
5
+ import { Context } from '../../core/context'
6
+ import { Plugin } from '../../core/plugin'
7
+ import { PriorityQueue } from '../../lib/priority-queue'
8
+ import { PersistedPriorityQueue } from '../../lib/priority-queue/persisted'
9
+ import { toFacade } from '../../lib/to-facade'
10
+ import batch, { BatchingDispatchConfig } from './batched-dispatcher'
11
+ import standard, { StandardDispatcherConfig } from './fetch-dispatcher'
12
+ import { normalize } from './normalize'
13
+ import { scheduleFlush } from './schedule-flush'
14
+
15
+ type DeliveryStrategy =
16
+ | {
17
+ strategy?: 'standard'
18
+ config?: StandardDispatcherConfig
19
+ }
20
+ | {
21
+ strategy?: 'batching'
22
+ config?: BatchingDispatchConfig
23
+ }
24
+
25
+ export type CustomerioSettings = {
26
+ apiKey: string
27
+ apiHost?: string
28
+ protocol?: 'http' | 'https'
29
+
30
+ addBundledMetadata?: boolean
31
+ unbundledIntegrations?: string[]
32
+ bundledConfigIds?: string[]
33
+ unbundledConfigIds?: string[]
34
+
35
+ maybeBundledConfigIds?: Record<string, string[]>
36
+
37
+ deliveryStrategy?: DeliveryStrategy
38
+ }
39
+
40
+ type JSON = ReturnType<Facade['json']>
41
+
42
+ function onAlias(analytics: Analytics, json: JSON): JSON {
43
+ const user = analytics.user()
44
+ json.previousId =
45
+ json.previousId ?? json.from ?? user.id() ?? user.anonymousId()
46
+ json.userId = json.userId ?? json.to
47
+ delete json.from
48
+ delete json.to
49
+ return json
50
+ }
51
+
52
+ export function customerio(
53
+ analytics: Analytics,
54
+ settings?: CustomerioSettings,
55
+ integrations?: LegacySettings['integrations']
56
+ ): Plugin {
57
+ // Attach `pagehide` before buffer is created so that inflight events are added
58
+ // to the buffer before the buffer persists events in its own `pagehide` handler.
59
+ window.addEventListener('pagehide', () => {
60
+ buffer.push(...Array.from(inflightEvents))
61
+ inflightEvents.clear()
62
+ })
63
+
64
+ const buffer = analytics.options.disableClientPersistence
65
+ ? new PriorityQueue<Context>(analytics.queue.queue.maxAttempts, [])
66
+ : new PersistedPriorityQueue(
67
+ analytics.queue.queue.maxAttempts,
68
+ `dest-Customer.io-Data-Pipelines`
69
+ )
70
+
71
+ const inflightEvents = new Set<Context>()
72
+ const flushing = false
73
+
74
+ const apiHost = settings?.apiHost ?? 'cdp.customer.io/v1'
75
+ const protocol = settings?.protocol ?? 'https'
76
+ const remote = `${protocol}://${apiHost}`
77
+
78
+ const deliveryStrategy = settings?.deliveryStrategy
79
+ const client =
80
+ deliveryStrategy?.strategy === 'batching'
81
+ ? batch(apiHost, deliveryStrategy.config)
82
+ : standard(deliveryStrategy?.config as StandardDispatcherConfig)
83
+
84
+ async function send(ctx: Context): Promise<Context> {
85
+ if (isOffline()) {
86
+ buffer.push(ctx)
87
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
88
+ scheduleFlush(flushing, buffer, customerio, scheduleFlush)
89
+ return ctx
90
+ }
91
+
92
+ inflightEvents.add(ctx)
93
+
94
+ const path = ctx.event.type.charAt(0)
95
+ let json = toFacade(ctx.event).json()
96
+
97
+ if (ctx.event.type === 'track') {
98
+ delete json.traits
99
+ }
100
+
101
+ if (ctx.event.type === 'alias') {
102
+ json = onAlias(analytics, json)
103
+ }
104
+
105
+ return client
106
+ .dispatch(
107
+ `${remote}/${path}`,
108
+ normalize(analytics, json, settings, integrations)
109
+ )
110
+ .then(() => ctx)
111
+ .catch(() => {
112
+ buffer.pushWithBackoff(ctx)
113
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
114
+ scheduleFlush(flushing, buffer, customerio, scheduleFlush)
115
+ return ctx
116
+ })
117
+ .finally(() => {
118
+ inflightEvents.delete(ctx)
119
+ })
120
+ }
121
+
122
+ const customerio: Plugin = {
123
+ name: 'Customer.io Data Pipelines',
124
+ type: 'after',
125
+ version: '0.1.0',
126
+ isLoaded: (): boolean => true,
127
+ load: (): Promise<void> => Promise.resolve(),
128
+ track: send,
129
+ identify: send,
130
+ page: send,
131
+ alias: send,
132
+ group: send,
133
+ }
134
+
135
+ // Buffer may already have items if they were previously stored in localStorage.
136
+ // Start flushing them immediately.
137
+ if (buffer.todo) {
138
+ scheduleFlush(flushing, buffer, customerio, scheduleFlush)
139
+ }
140
+
141
+ return customerio
142
+ }
@@ -0,0 +1,223 @@
1
+ import jar from 'js-cookie'
2
+ import { Analytics } from '../../core/analytics'
3
+ import { LegacySettings } from '../../browser'
4
+ import { CustomerioEvent } from '../../core/events'
5
+ import { gracefulDecodeURIComponent } from '../../core/query-string/gracefulDecodeURIComponent'
6
+ import { tld } from '../../core/user/tld'
7
+ import { CustomerioFacade } from '../../lib/to-facade'
8
+ import { CustomerioSettings } from './index'
9
+ import { version } from '../../generated/version'
10
+ import { getAvailableStorageOptions, UniversalStorage } from '../../core/user'
11
+
12
+ let cookieOptions: jar.CookieAttributes | undefined
13
+ function getCookieOptions(): jar.CookieAttributes {
14
+ if (cookieOptions) {
15
+ return cookieOptions
16
+ }
17
+
18
+ const domain = tld(window.location.href)
19
+ cookieOptions = {
20
+ expires: 31536000000, // 1 year
21
+ secure: false,
22
+ path: '/',
23
+ }
24
+ if (domain) {
25
+ cookieOptions.domain = domain
26
+ }
27
+
28
+ return cookieOptions
29
+ }
30
+
31
+ // Default value will be updated to 'web' in `bundle-umd.ts` for web build.
32
+ let _version: 'web' | 'npm' = 'npm'
33
+
34
+ export function setVersionType(version: typeof _version) {
35
+ _version = version
36
+ }
37
+
38
+ export function getVersionType(): typeof _version {
39
+ return _version
40
+ }
41
+
42
+ type Ad = { id: string; type: string }
43
+
44
+ export function ampId(): string | undefined {
45
+ const ampId = jar.get('_ga')
46
+ if (ampId && ampId.startsWith('amp')) {
47
+ return ampId
48
+ }
49
+ }
50
+
51
+ export function utm(query: string): Record<string, string> {
52
+ if (query.startsWith('?')) {
53
+ query = query.substring(1)
54
+ }
55
+ query = query.replace(/\?/g, '&')
56
+
57
+ return query.split('&').reduce((acc, str) => {
58
+ const [k, v = ''] = str.split('=')
59
+ if (k.includes('utm_') && k.length > 4) {
60
+ let utmParam = k.substr(4)
61
+ if (utmParam === 'campaign') {
62
+ utmParam = 'name'
63
+ }
64
+ acc[utmParam] = gracefulDecodeURIComponent(v)
65
+ }
66
+ return acc
67
+ }, {} as Record<string, string>)
68
+ }
69
+
70
+ function ads(query: string): Ad | undefined {
71
+ const queryIds: Record<string, string> = {
72
+ btid: 'dataxu',
73
+ urid: 'millennial-media',
74
+ }
75
+
76
+ if (query.startsWith('?')) {
77
+ query = query.substring(1)
78
+ }
79
+ query = query.replace(/\?/g, '&')
80
+ const parts = query.split('&')
81
+
82
+ for (const part of parts) {
83
+ const [k, v] = part.split('=')
84
+ if (queryIds[k]) {
85
+ return {
86
+ id: v,
87
+ type: queryIds[k],
88
+ }
89
+ }
90
+ }
91
+ }
92
+
93
+ function referrerId(
94
+ query: string,
95
+ ctx: CustomerioEvent['context'],
96
+ disablePersistance: boolean
97
+ ): void {
98
+ const storage = new UniversalStorage<{
99
+ 's:context.referrer': Ad
100
+ }>(
101
+ disablePersistance ? [] : ['cookie'],
102
+ getAvailableStorageOptions(getCookieOptions())
103
+ )
104
+
105
+ const stored = storage.get('s:context.referrer')
106
+ let ad: Ad | undefined | null = ads(query)
107
+
108
+ ad = ad ?? stored
109
+
110
+ if (!ad) {
111
+ return
112
+ }
113
+
114
+ if (ctx) {
115
+ ctx.referrer = { ...ctx.referrer, ...ad }
116
+ }
117
+
118
+ storage.set('s:context.referrer', ad)
119
+ }
120
+
121
+ export function normalize(
122
+ analytics: Analytics,
123
+ json: ReturnType<CustomerioFacade['json']>,
124
+ settings?: CustomerioSettings,
125
+ integrations?: LegacySettings['integrations']
126
+ ): object {
127
+ const user = analytics.user()
128
+ const query = window.location.search
129
+
130
+ json.context = json.context ?? json.options ?? {}
131
+ const ctx = json.context
132
+
133
+ delete json.options
134
+ json.writeKey = settings?.apiKey
135
+ ctx.userAgent = window.navigator.userAgent
136
+
137
+ // @ts-ignore
138
+ const locale = navigator.userLanguage || navigator.language
139
+
140
+ if (typeof ctx.locale === 'undefined' && typeof locale !== 'undefined') {
141
+ ctx.locale = locale
142
+ }
143
+
144
+ if (!ctx.library) {
145
+ const type = getVersionType()
146
+ if (type === 'web') {
147
+ ctx.library = {
148
+ name: 'analytics.js',
149
+ version: `next-${version}`,
150
+ }
151
+ } else {
152
+ ctx.library = {
153
+ name: 'analytics.js',
154
+ version: `npm:next-${version}`,
155
+ }
156
+ }
157
+ }
158
+
159
+ if (query && !ctx.campaign) {
160
+ ctx.campaign = utm(query)
161
+ }
162
+
163
+ referrerId(query, ctx, analytics.options.disableClientPersistence ?? false)
164
+
165
+ json.userId = json.userId || user.id()
166
+ json.anonymousId = json.anonymousId || user.anonymousId()
167
+
168
+ json.sentAt = new Date()
169
+
170
+ const failed = analytics.queue.failedInitializations || []
171
+ if (failed.length > 0) {
172
+ json._metadata = { failedInitializations: failed }
173
+ }
174
+
175
+ const bundled: string[] = []
176
+ const unbundled: string[] = []
177
+
178
+ for (const key in integrations) {
179
+ const integration = integrations[key]
180
+ if (key === 'Customer.io Data Pipelines') {
181
+ bundled.push(key)
182
+ }
183
+ if (integration.bundlingStatus === 'bundled') {
184
+ bundled.push(key)
185
+ }
186
+ if (integration.bundlingStatus === 'unbundled') {
187
+ unbundled.push(key)
188
+ }
189
+ }
190
+
191
+ // This will make sure that the disabled cloud mode destinations will be
192
+ // included in the unbundled list.
193
+ for (const settingsUnbundled of settings?.unbundledIntegrations || []) {
194
+ if (!unbundled.includes(settingsUnbundled)) {
195
+ unbundled.push(settingsUnbundled)
196
+ }
197
+ }
198
+
199
+ const configIds = settings?.maybeBundledConfigIds ?? {}
200
+ const bundledConfigIds: string[] = []
201
+
202
+ bundled.sort().forEach((name) => {
203
+ ; (configIds[name] ?? []).forEach((id) => {
204
+ bundledConfigIds.push(id)
205
+ })
206
+ })
207
+
208
+ if (settings?.addBundledMetadata !== false) {
209
+ json._metadata = {
210
+ ...json._metadata,
211
+ bundled: bundled.sort(),
212
+ unbundled: unbundled.sort(),
213
+ bundledIds: bundledConfigIds,
214
+ }
215
+ }
216
+
217
+ const amp = ampId()
218
+ if (amp) {
219
+ ctx.amp = { id: amp }
220
+ }
221
+
222
+ return json
223
+ }
@@ -0,0 +1,58 @@
1
+ import { isOffline } from '../../core/connection'
2
+ import { Context } from '../../core/context'
3
+ import { Plugin } from '../../core/plugin'
4
+ import { attempt } from '@customerio/cdp-analytics-core'
5
+ import { pWhile } from '../../lib/p-while'
6
+ import { PriorityQueue } from '../../lib/priority-queue'
7
+
8
+ async function flushQueue(
9
+ xt: Plugin,
10
+ queue: PriorityQueue<Context>
11
+ ): Promise<PriorityQueue<Context>> {
12
+ const failedQueue: Context[] = []
13
+ if (isOffline()) {
14
+ return queue
15
+ }
16
+
17
+ await pWhile(
18
+ () => queue.length > 0 && !isOffline(),
19
+ async () => {
20
+ const ctx = queue.pop()
21
+ if (!ctx) {
22
+ return
23
+ }
24
+
25
+ const result = await attempt(ctx, xt)
26
+ const success = result instanceof Context
27
+ if (!success) {
28
+ failedQueue.push(ctx)
29
+ }
30
+ }
31
+ )
32
+ // re-add failed tasks
33
+ failedQueue.map((failed) => queue.pushWithBackoff(failed))
34
+ return queue
35
+ }
36
+
37
+ export function scheduleFlush(
38
+ flushing: boolean,
39
+ buffer: PriorityQueue<Context>,
40
+ xt: Plugin,
41
+ scheduleFlush: Function
42
+ ): void {
43
+ if (flushing) {
44
+ return
45
+ }
46
+
47
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
48
+ setTimeout(async () => {
49
+ let isFlushing = true
50
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
51
+ const newBuffer = await flushQueue(xt, buffer)
52
+ isFlushing = false
53
+
54
+ if (buffer.todo > 0) {
55
+ scheduleFlush(isFlushing, newBuffer, xt, scheduleFlush)
56
+ }
57
+ }, Math.random() * 5000)
58
+ }
@@ -0,0 +1,16 @@
1
+ import { Analytics } from '../../core/analytics'
2
+
3
+ export async function loadLegacyVideoPlugins(
4
+ analytics: Analytics
5
+ ): Promise<void> {
6
+ const plugins = await import(
7
+ // @ts-expect-error
8
+ '@segment/analytics.js-video-plugins/dist/index.umd.js'
9
+ )
10
+
11
+ // This is super gross, but we need to support the `window.analytics.plugins` namespace
12
+ // that is linked in the docs in order to be backwards compatible with ajs-classic
13
+
14
+ // @ts-expect-error
15
+ analytics._plugins = plugins
16
+ }
@@ -0,0 +1,131 @@
1
+ import { Context, ContextCancelation } from '../../core/context'
2
+ import { CustomerioEvent } from '../../core/events'
3
+ import { Plugin } from '../../core/plugin'
4
+ import { CustomerioFacade, toFacade } from '../../lib/to-facade'
5
+
6
+ export interface MiddlewareParams {
7
+ payload: CustomerioFacade
8
+
9
+ integrations?: CustomerioEvent['integrations']
10
+ next: (payload: MiddlewareParams['payload'] | null) => void
11
+ }
12
+
13
+ export interface DestinationMiddlewareParams {
14
+ payload: CustomerioFacade
15
+ integration: string
16
+ next: (payload: MiddlewareParams['payload'] | null) => void
17
+ }
18
+
19
+ export type MiddlewareFunction = (
20
+ middleware: MiddlewareParams
21
+ ) => void | Promise<void>
22
+
23
+ export type DestinationMiddlewareFunction = (
24
+ middleware: DestinationMiddlewareParams
25
+ ) => void | Promise<void>
26
+
27
+ export async function applyDestinationMiddleware(
28
+ destination: string,
29
+ evt: CustomerioEvent,
30
+ middleware: DestinationMiddlewareFunction[]
31
+ ): Promise<CustomerioEvent | null> {
32
+ // Clone the event so mutations are localized to a single destination.
33
+ let modifiedEvent = toFacade(evt, {
34
+ clone: true,
35
+ traverse: false,
36
+ }).rawEvent() as CustomerioEvent
37
+ async function applyMiddleware(
38
+ event: CustomerioEvent,
39
+ fn: DestinationMiddlewareFunction
40
+ ): Promise<CustomerioEvent | null> {
41
+ let nextCalled = false
42
+ let returnedEvent: CustomerioEvent | null = null
43
+
44
+ await fn({
45
+ payload: toFacade(event, {
46
+ clone: true,
47
+ traverse: false,
48
+ }),
49
+ integration: destination,
50
+ next(evt) {
51
+ nextCalled = true
52
+
53
+ if (evt === null) {
54
+ returnedEvent = null
55
+ }
56
+
57
+ if (evt) {
58
+ returnedEvent = evt.obj
59
+ }
60
+ },
61
+ })
62
+
63
+ if (!nextCalled && returnedEvent !== null) {
64
+ returnedEvent = returnedEvent as CustomerioEvent
65
+ returnedEvent.integrations = {
66
+ ...event.integrations,
67
+ [destination]: false,
68
+ }
69
+ }
70
+
71
+ return returnedEvent
72
+ }
73
+
74
+ for (const md of middleware) {
75
+ const result = await applyMiddleware(modifiedEvent, md)
76
+ if (result === null) {
77
+ return null
78
+ }
79
+ modifiedEvent = result
80
+ }
81
+
82
+ return modifiedEvent
83
+ }
84
+
85
+ export function sourceMiddlewarePlugin(
86
+ fn: MiddlewareFunction,
87
+ integrations: CustomerioEvent['integrations']
88
+ ): Plugin {
89
+ async function apply(ctx: Context): Promise<Context> {
90
+ let nextCalled = false
91
+
92
+ await fn({
93
+ payload: toFacade(ctx.event, {
94
+ clone: true,
95
+ traverse: false,
96
+ }),
97
+ integrations: integrations ?? {},
98
+ next(evt) {
99
+ nextCalled = true
100
+ if (evt) {
101
+ ctx.event = evt.obj
102
+ }
103
+ },
104
+ })
105
+
106
+ if (!nextCalled) {
107
+ throw new ContextCancelation({
108
+ retry: false,
109
+ type: 'middleware_cancellation',
110
+ reason: 'Middleware `next` function skipped',
111
+ })
112
+ }
113
+
114
+ return ctx
115
+ }
116
+
117
+ return {
118
+ name: `Source Middleware ${fn.name}`,
119
+ type: 'before',
120
+ version: '0.1.0',
121
+
122
+ isLoaded: (): boolean => true,
123
+ load: (ctx): Promise<Context> => Promise.resolve(ctx),
124
+
125
+ track: apply,
126
+ page: apply,
127
+ identify: apply,
128
+ alias: apply,
129
+ group: apply,
130
+ }
131
+ }