@zoho_apptics/apptics-js-sdk 1.0.2 → 2.0.0-beta.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 (243) hide show
  1. package/CHANGELOG.md +193 -0
  2. package/LICENSE +13 -0
  3. package/README.md +846 -11
  4. package/dist/apptics.esm.min.js +3 -0
  5. package/dist/apptics.esm.min.js.map +1 -0
  6. package/dist/apptics.min.js +3 -0
  7. package/dist/apptics.min.js.map +1 -0
  8. package/dist/chunks/lifecycle-VYr43KcB.min.js +3 -0
  9. package/dist/chunks/lifecycle-VYr43KcB.min.js.map +1 -0
  10. package/dist/cjs/auto-init.d.ts +8 -0
  11. package/dist/cjs/auto-init.js +32 -0
  12. package/dist/cjs/core/attempts.d.ts +28 -0
  13. package/dist/cjs/core/attempts.js +113 -0
  14. package/dist/cjs/core/batch.d.ts +32 -0
  15. package/dist/cjs/core/batch.js +116 -0
  16. package/dist/cjs/core/callsite.d.ts +34 -0
  17. package/dist/cjs/core/callsite.js +115 -0
  18. package/dist/cjs/core/consent.d.ts +27 -0
  19. package/dist/cjs/core/consent.js +132 -0
  20. package/dist/cjs/core/constants.d.ts +237 -0
  21. package/dist/cjs/core/constants.js +315 -0
  22. package/dist/cjs/core/db.d.ts +48 -0
  23. package/dist/cjs/core/db.js +467 -0
  24. package/dist/cjs/core/debug.d.ts +22 -0
  25. package/dist/cjs/core/debug.js +88 -0
  26. package/dist/cjs/core/env.d.ts +28 -0
  27. package/dist/cjs/core/env.js +267 -0
  28. package/dist/cjs/core/framework-id.d.ts +1 -0
  29. package/dist/cjs/core/framework-id.js +9 -0
  30. package/dist/cjs/core/guard.d.ts +11 -0
  31. package/dist/cjs/core/guard.js +86 -0
  32. package/dist/cjs/core/hooks.d.ts +39 -0
  33. package/dist/cjs/core/hooks.js +22 -0
  34. package/dist/cjs/core/locks.d.ts +9 -0
  35. package/dist/cjs/core/locks.js +22 -0
  36. package/dist/cjs/core/logger.d.ts +15 -0
  37. package/dist/cjs/core/logger.js +50 -0
  38. package/dist/cjs/core/state.d.ts +70 -0
  39. package/dist/cjs/core/state.js +78 -0
  40. package/dist/cjs/core/storage.d.ts +61 -0
  41. package/dist/cjs/core/storage.js +327 -0
  42. package/dist/cjs/core/stylesheet.d.ts +8 -0
  43. package/dist/cjs/core/stylesheet.js +29 -0
  44. package/dist/cjs/core/transport.d.ts +85 -0
  45. package/dist/cjs/core/transport.js +456 -0
  46. package/dist/cjs/core/utils.d.ts +26 -0
  47. package/dist/cjs/core/utils.js +175 -0
  48. package/dist/cjs/core/validate.d.ts +18 -0
  49. package/dist/cjs/core/validate.js +233 -0
  50. package/dist/cjs/device/appversion.d.ts +15 -0
  51. package/dist/cjs/device/appversion.js +170 -0
  52. package/dist/cjs/device/index.d.ts +34 -0
  53. package/dist/cjs/device/index.js +472 -0
  54. package/dist/cjs/engagement/api-rules.d.ts +49 -0
  55. package/dist/cjs/engagement/api-rules.js +206 -0
  56. package/dist/cjs/engagement/auto-api.d.ts +7 -0
  57. package/dist/cjs/engagement/auto-api.js +200 -0
  58. package/dist/cjs/engagement/auto-screen.d.ts +18 -0
  59. package/dist/cjs/engagement/auto-screen.js +305 -0
  60. package/dist/cjs/engagement/defined-events.d.ts +23 -0
  61. package/dist/cjs/engagement/defined-events.js +58 -0
  62. package/dist/cjs/engagement/send.d.ts +6 -0
  63. package/dist/cjs/engagement/send.js +104 -0
  64. package/dist/cjs/engagement/session.d.ts +51 -0
  65. package/dist/cjs/engagement/session.js +463 -0
  66. package/dist/cjs/engagement/track.d.ts +56 -0
  67. package/dist/cjs/engagement/track.js +375 -0
  68. package/dist/cjs/errors/send.d.ts +6 -0
  69. package/dist/cjs/errors/send.js +73 -0
  70. package/dist/cjs/errors/track.d.ts +4 -0
  71. package/dist/cjs/errors/track.js +167 -0
  72. package/dist/cjs/feedback/controls.d.ts +37 -0
  73. package/dist/cjs/feedback/controls.js +316 -0
  74. package/dist/cjs/feedback/index.d.ts +9 -0
  75. package/dist/cjs/feedback/index.js +56 -0
  76. package/dist/cjs/feedback/logs.d.ts +45 -0
  77. package/dist/cjs/feedback/logs.js +241 -0
  78. package/dist/cjs/feedback/quartz.d.ts +18 -0
  79. package/dist/cjs/feedback/quartz.js +105 -0
  80. package/dist/cjs/feedback/send.d.ts +28 -0
  81. package/dist/cjs/feedback/send.js +203 -0
  82. package/dist/cjs/feedback/styles.d.ts +10 -0
  83. package/dist/cjs/feedback/styles.js +8 -0
  84. package/dist/cjs/feedback/template.d.ts +2 -0
  85. package/dist/cjs/feedback/template.js +162 -0
  86. package/dist/cjs/feedback/ui.d.ts +63 -0
  87. package/dist/cjs/feedback/ui.js +709 -0
  88. package/dist/cjs/heatmap/send.d.ts +13 -0
  89. package/dist/cjs/heatmap/send.js +82 -0
  90. package/dist/cjs/heatmap/track.d.ts +4 -0
  91. package/dist/cjs/heatmap/track.js +448 -0
  92. package/dist/cjs/index.d.ts +22 -0
  93. package/dist/cjs/index.js +162 -0
  94. package/dist/cjs/lifecycle.d.ts +21 -0
  95. package/dist/cjs/lifecycle.js +734 -0
  96. package/dist/cjs/package.json +3 -0
  97. package/dist/cjs/remoteconfig/country.d.ts +4 -0
  98. package/dist/cjs/remoteconfig/country.js +73 -0
  99. package/dist/cjs/remoteconfig/custom.d.ts +10 -0
  100. package/dist/cjs/remoteconfig/custom.js +58 -0
  101. package/dist/cjs/remoteconfig/evaluate.d.ts +24 -0
  102. package/dist/cjs/remoteconfig/evaluate.js +184 -0
  103. package/dist/cjs/remoteconfig/fetch.d.ts +24 -0
  104. package/dist/cjs/remoteconfig/fetch.js +200 -0
  105. package/dist/cjs/remoteconfig/index.d.ts +15 -0
  106. package/dist/cjs/remoteconfig/index.js +116 -0
  107. package/dist/cjs/remoteconfig/store.d.ts +18 -0
  108. package/dist/cjs/remoteconfig/store.js +98 -0
  109. package/dist/cjs/remoteconfig/tz-countries.d.ts +8 -0
  110. package/dist/cjs/remoteconfig/tz-countries.js +13 -0
  111. package/dist/cjs/remotelog/gate.d.ts +2 -0
  112. package/dist/cjs/remotelog/gate.js +28 -0
  113. package/dist/cjs/remotelog/index.d.ts +8 -0
  114. package/dist/cjs/remotelog/logger.d.ts +22 -0
  115. package/dist/cjs/remotelog/logger.js +111 -0
  116. package/dist/cjs/remotelog/send.d.ts +13 -0
  117. package/dist/cjs/remotelog/send.js +153 -0
  118. package/dist/cjs/remotelogger/index.d.ts +12 -0
  119. package/dist/cjs/remotelogger/index.js +18 -0
  120. package/dist/cjs/scheduler.d.ts +8 -0
  121. package/dist/cjs/scheduler.js +78 -0
  122. package/dist/cjs/types.d.ts +385 -0
  123. package/dist/cjs/user/index.d.ts +33 -0
  124. package/dist/cjs/user/index.js +183 -0
  125. package/dist/esm/auto-init.d.ts +8 -0
  126. package/dist/esm/auto-init.js +29 -0
  127. package/dist/esm/core/attempts.d.ts +28 -0
  128. package/dist/esm/core/attempts.js +106 -0
  129. package/dist/esm/core/batch.d.ts +32 -0
  130. package/dist/esm/core/batch.js +109 -0
  131. package/dist/esm/core/callsite.d.ts +34 -0
  132. package/dist/esm/core/callsite.js +110 -0
  133. package/dist/esm/core/consent.d.ts +27 -0
  134. package/dist/esm/core/consent.js +122 -0
  135. package/dist/esm/core/constants.d.ts +237 -0
  136. package/dist/esm/core/constants.js +292 -0
  137. package/dist/esm/core/db.d.ts +48 -0
  138. package/dist/esm/core/db.js +453 -0
  139. package/dist/esm/core/debug.d.ts +22 -0
  140. package/dist/esm/core/debug.js +81 -0
  141. package/dist/esm/core/env.d.ts +28 -0
  142. package/dist/esm/core/env.js +251 -0
  143. package/dist/esm/core/framework-id.d.ts +1 -0
  144. package/dist/esm/core/framework-id.js +7 -0
  145. package/dist/esm/core/guard.d.ts +11 -0
  146. package/dist/esm/core/guard.js +83 -0
  147. package/dist/esm/core/hooks.d.ts +39 -0
  148. package/dist/esm/core/hooks.js +20 -0
  149. package/dist/esm/core/locks.d.ts +9 -0
  150. package/dist/esm/core/locks.js +20 -0
  151. package/dist/esm/core/logger.d.ts +15 -0
  152. package/dist/esm/core/logger.js +46 -0
  153. package/dist/esm/core/state.d.ts +70 -0
  154. package/dist/esm/core/state.js +73 -0
  155. package/dist/esm/core/storage.d.ts +61 -0
  156. package/dist/esm/core/storage.js +314 -0
  157. package/dist/esm/core/stylesheet.d.ts +8 -0
  158. package/dist/esm/core/stylesheet.js +27 -0
  159. package/dist/esm/core/transport.d.ts +85 -0
  160. package/dist/esm/core/transport.js +439 -0
  161. package/dist/esm/core/utils.d.ts +26 -0
  162. package/dist/esm/core/utils.js +161 -0
  163. package/dist/esm/core/validate.d.ts +18 -0
  164. package/dist/esm/core/validate.js +226 -0
  165. package/dist/esm/device/appversion.d.ts +15 -0
  166. package/dist/esm/device/appversion.js +164 -0
  167. package/dist/esm/device/index.d.ts +34 -0
  168. package/dist/esm/device/index.js +461 -0
  169. package/dist/esm/engagement/api-rules.d.ts +49 -0
  170. package/dist/esm/engagement/api-rules.js +199 -0
  171. package/dist/esm/engagement/auto-api.d.ts +7 -0
  172. package/dist/esm/engagement/auto-api.js +198 -0
  173. package/dist/esm/engagement/auto-screen.d.ts +18 -0
  174. package/dist/esm/engagement/auto-screen.js +301 -0
  175. package/dist/esm/engagement/defined-events.d.ts +23 -0
  176. package/dist/esm/engagement/defined-events.js +55 -0
  177. package/dist/esm/engagement/send.d.ts +6 -0
  178. package/dist/esm/engagement/send.js +101 -0
  179. package/dist/esm/engagement/session.d.ts +51 -0
  180. package/dist/esm/engagement/session.js +448 -0
  181. package/dist/esm/engagement/track.d.ts +56 -0
  182. package/dist/esm/engagement/track.js +365 -0
  183. package/dist/esm/errors/send.d.ts +6 -0
  184. package/dist/esm/errors/send.js +70 -0
  185. package/dist/esm/errors/track.d.ts +4 -0
  186. package/dist/esm/errors/track.js +163 -0
  187. package/dist/esm/feedback/controls.d.ts +37 -0
  188. package/dist/esm/feedback/controls.js +309 -0
  189. package/dist/esm/feedback/index.d.ts +9 -0
  190. package/dist/esm/feedback/index.js +51 -0
  191. package/dist/esm/feedback/logs.d.ts +45 -0
  192. package/dist/esm/feedback/logs.js +227 -0
  193. package/dist/esm/feedback/quartz.d.ts +18 -0
  194. package/dist/esm/feedback/quartz.js +99 -0
  195. package/dist/esm/feedback/send.d.ts +28 -0
  196. package/dist/esm/feedback/send.js +198 -0
  197. package/dist/esm/feedback/styles.d.ts +10 -0
  198. package/dist/esm/feedback/styles.js +4 -0
  199. package/dist/esm/feedback/template.d.ts +2 -0
  200. package/dist/esm/feedback/template.js +159 -0
  201. package/dist/esm/feedback/ui.d.ts +63 -0
  202. package/dist/esm/feedback/ui.js +703 -0
  203. package/dist/esm/heatmap/send.d.ts +13 -0
  204. package/dist/esm/heatmap/send.js +79 -0
  205. package/dist/esm/heatmap/track.d.ts +4 -0
  206. package/dist/esm/heatmap/track.js +445 -0
  207. package/dist/esm/index.d.ts +22 -0
  208. package/dist/esm/index.js +150 -0
  209. package/dist/esm/lifecycle.d.ts +21 -0
  210. package/dist/esm/lifecycle.js +729 -0
  211. package/dist/esm/package.json +3 -0
  212. package/dist/esm/remoteconfig/country.d.ts +4 -0
  213. package/dist/esm/remoteconfig/country.js +70 -0
  214. package/dist/esm/remoteconfig/custom.d.ts +10 -0
  215. package/dist/esm/remoteconfig/custom.js +54 -0
  216. package/dist/esm/remoteconfig/evaluate.d.ts +24 -0
  217. package/dist/esm/remoteconfig/evaluate.js +179 -0
  218. package/dist/esm/remoteconfig/fetch.d.ts +24 -0
  219. package/dist/esm/remoteconfig/fetch.js +196 -0
  220. package/dist/esm/remoteconfig/index.d.ts +15 -0
  221. package/dist/esm/remoteconfig/index.js +111 -0
  222. package/dist/esm/remoteconfig/store.d.ts +18 -0
  223. package/dist/esm/remoteconfig/store.js +91 -0
  224. package/dist/esm/remoteconfig/tz-countries.d.ts +8 -0
  225. package/dist/esm/remoteconfig/tz-countries.js +11 -0
  226. package/dist/esm/remotelog/gate.d.ts +2 -0
  227. package/dist/esm/remotelog/gate.js +26 -0
  228. package/dist/esm/remotelog/index.d.ts +8 -0
  229. package/dist/esm/remotelog/logger.d.ts +22 -0
  230. package/dist/esm/remotelog/logger.js +108 -0
  231. package/dist/esm/remotelog/send.d.ts +13 -0
  232. package/dist/esm/remotelog/send.js +149 -0
  233. package/dist/esm/remotelogger/index.d.ts +12 -0
  234. package/dist/esm/remotelogger/index.js +13 -0
  235. package/dist/esm/scheduler.d.ts +8 -0
  236. package/dist/esm/scheduler.js +74 -0
  237. package/dist/esm/types.d.ts +385 -0
  238. package/dist/esm/user/index.d.ts +33 -0
  239. package/dist/esm/user/index.js +176 -0
  240. package/dist/feedback.esm.min.js +3 -0
  241. package/dist/feedback.esm.min.js.map +1 -0
  242. package/package.json +122 -7
  243. package/src/appticssdk.js +0 -1
@@ -0,0 +1,305 @@
1
+ /*! @zoho_apptics/apptics-js-sdk v2.0.0-beta.1 | (c) Zoho Corporation Private Limited | see LICENSE */
2
+ 'use strict';
3
+
4
+ var logger = require('../core/logger.js');
5
+ var hooks = require('../core/hooks.js');
6
+ var state = require('../core/state.js');
7
+ var utils = require('../core/utils.js');
8
+ var track = require('./track.js');
9
+
10
+ /**
11
+ * Automatic screen tracking: a screen view for the page the SDK starts on, and one for every
12
+ * navigation after, without the app calling inScreen() from its router.
13
+ *
14
+ * Single-page routers move with history.pushState and replaceState, which fire no event, so those
15
+ * two are wrapped; back and forward arrive as popstate, and hash routers as hashchange. As with
16
+ * automatic API tracking, the wrappers must be invisible — arguments, `this` and the return value
17
+ * pass straight through — because a mistake here breaks the app's navigation, not just a metric.
18
+ *
19
+ * A router seldom makes one history call per move: it pushes the route, then replaces it to store
20
+ * its own state. Calls are therefore collected into one check a tick later, and a name equal to
21
+ * the last one recorded is not a new view. That also keeps a replaceState that only stores state
22
+ * from counting as a screen.
23
+ *
24
+ * An app may still name some screens itself. A inScreen() call during a navigation replaces
25
+ * the automatic view of it, so the navigation counts once, under the app's name. Router hooks
26
+ * often run after the navigation has been seen — React's effects, a Vue afterEach — so the
27
+ * automatic view waits SETTLE_MS before it is recorded, and a manual call in that time wins. The
28
+ * new path is the current screen from the moment the navigation is seen, so events fired while
29
+ * the page mounts are attributed to it, not to the page that was left. A page left again within
30
+ * the wait, such as a redirect, is not counted.
31
+ *
32
+ * On by default: most apps never call inScreen() themselves, and a screen-less session says
33
+ * little. An app that wants only its own calls sets `track_screen: false`.
34
+ */
35
+ /** A path as screen_routes compares it: no query or fragment, no trailing slash but on the root. */
36
+ function routePath(path) {
37
+ var bare = String(path || '').split(/[?#]/)[0] || '/'; // no i18n
38
+ return bare.length > 1 ? bare.replace(/\/+$/, '') : bare; // no i18n
39
+ }
40
+ /** The most a screen name may be, and the characters it may hold (see validateScreenName). */
41
+ const NAME_MAX = 99;
42
+ const NAME_DISALLOWED = /[^\w\s.\-&/:*]/g;
43
+ /** How long an automatic view waits for the app's own inScreen() of the same navigation. */
44
+ const SETTLE_MS = 300;
45
+ var installed = false;
46
+ /** The name last recorded, so a navigation that lands on the same screen is not a new view. */
47
+ var lastName = ''; // no i18n
48
+ var timer = null;
49
+ var settleMs = SETTLE_MS;
50
+ /** The automatic view waiting out SETTLE_MS, and its timer. */
51
+ var pendingName = ''; // no i18n
52
+ var settleTimer = null;
53
+ /** The app called inScreen() during the current navigation, so it is not recorded here. */
54
+ var claimed = false;
55
+ /** When the navigation waiting out SETTLE_MS happened: its view starts then, not when recorded. */
56
+ var pendingAt = 0;
57
+ /** The wrappers this module put on history, to tell them apart from one a library added later. */
58
+ var wrappedPush = null;
59
+ var wrappedReplace = null;
60
+ var originalPush = null;
61
+ var originalReplace = null;
62
+ /**
63
+ * The default name: the path, without the query string, which is where search terms, tokens and
64
+ * other values that are not a screen live. A hash route (`#/orders`, `#!/orders`) is the route,
65
+ * so it is joined onto the path; a plain `#anchor` is a place on the same screen and is left out.
66
+ */
67
+ function nameFromUrl(url) {
68
+ var path = url.pathname;
69
+ var hash = url.hash;
70
+ if (/^#!?\//.test(hash)) {
71
+ var route = hash.replace(/^#!?/, '').split('?')[0]; // no i18n
72
+ path = path.replace(/\/+$/, '') + route; // no i18n
73
+ }
74
+ return path;
75
+ }
76
+ /**
77
+ * A name inScreen will accept. A path holds characters a screen name may not — percent
78
+ * encoding, `+`, `@` — and a refused name is dropped, so they are replaced rather than risked.
79
+ */
80
+ function cleanName(raw) {
81
+ var name = raw;
82
+ try {
83
+ name = decodeURIComponent(raw);
84
+ }
85
+ catch (err) {
86
+ // a malformed escape: clean it as it stands
87
+ }
88
+ return name.replace(NAME_DISALLOWED, '_').trim().slice(0, NAME_MAX); // no i18n
89
+ }
90
+ /** Whether a pattern segment is a wildcard: `{id}`, `:id` or `*`. */
91
+ function isWildcard(segment) {
92
+ return segment === '*' || /^\{[^/]*\}$/.test(segment) || /^:[^/]+$/.test(segment); // no i18n
93
+ }
94
+ /** Whether a route pattern matches a path: every segment, `{id}`, `:id` and `*` matching any one. */
95
+ function patternMatches(pattern, path) {
96
+ var segments = routePath(pattern).split('/'); // no i18n
97
+ var actual = path.split('/'); // no i18n
98
+ if (segments.length !== actual.length) {
99
+ return false;
100
+ }
101
+ for (var j = 0; j < segments.length; j++) {
102
+ var seg = segments[j];
103
+ if (!isWildcard(seg) && seg !== actual[j]) {
104
+ return false;
105
+ }
106
+ }
107
+ return true;
108
+ }
109
+ /**
110
+ * The name screen_routes gives a path, or the path itself when no entry matches. Tried in order:
111
+ * a route pattern records the path with its variable segments as `*` (`/users/{id}/orders`
112
+ * claims `/users/1/orders`, not `/users/1/orders/2`); an exact path or a regular expression
113
+ * records the name the entry gives. Matched on the path without its query.
114
+ */
115
+ function applyRoutes(path) {
116
+ var routes = state._ZAP._config.screen_routes;
117
+ if (!routes || routes.length === 0) {
118
+ return path;
119
+ }
120
+ var bare = routePath(path);
121
+ for (var i = 0; i < routes.length; i++) {
122
+ var route = routes[i];
123
+ if (route.kind === 'equals' && route.path === bare) { // no i18n
124
+ return route.name;
125
+ }
126
+ if (route.kind === 'regex') { // no i18n
127
+ route.regex.lastIndex = 0; // a /g regular expression keeps a position between tests
128
+ if (route.regex.test(bare)) {
129
+ return route.name;
130
+ }
131
+ }
132
+ if (route.kind === 'pattern' && patternMatches(route.pattern, bare)) { // no i18n
133
+ return routePath(route.pattern).split('/').map((s) => (isWildcard(s) ? '*' : s)).join('/'); // no i18n
134
+ }
135
+ }
136
+ return path;
137
+ }
138
+ /**
139
+ * The name for the page as it is now, from its url, or '' to record nothing. Never throws. An app
140
+ * that wants its own names calls inScreen() as the route changes, which replaces this view.
141
+ */
142
+ function currentName() {
143
+ try {
144
+ return cleanName(applyRoutes(nameFromUrl(new URL(window.location.href))));
145
+ }
146
+ catch (err) {
147
+ return ''; // no i18n
148
+ }
149
+ }
150
+ function check() {
151
+ timer = null;
152
+ if (!installed) {
153
+ return;
154
+ }
155
+ var name = currentName();
156
+ if (name === '' || name === lastName) { // no i18n
157
+ return;
158
+ }
159
+ lastName = name;
160
+ if (claimed) {
161
+ return; // the app has already recorded this navigation under its own name
162
+ }
163
+ // the current screen now, the view once SETTLE_MS has passed with no inScreen() of the app's.
164
+ // The open view keeps its id, so the view that follows still closes it, at the navigation
165
+ state._ZAP._current_screen = Object.assign({}, state._ZAP._current_screen, { screen: name });
166
+ pendingName = name;
167
+ pendingAt = utils.nowMs();
168
+ settleTimer = setTimeout(settle, settleMs);
169
+ }
170
+ function settle() {
171
+ settleTimer = null;
172
+ var name = pendingName;
173
+ pendingName = ''; // no i18n
174
+ if (!installed || claimed || name === '') { // no i18n
175
+ return;
176
+ }
177
+ try {
178
+ track.recordScreen(name, undefined, undefined, pendingAt || undefined);
179
+ }
180
+ catch (err) {
181
+ logger.Console.log('automatic screen tracking: ' + String(err)); // no i18n
182
+ }
183
+ }
184
+ /** Drops an automatic view still waiting out SETTLE_MS. */
185
+ function cancelPending() {
186
+ if (settleTimer !== null) {
187
+ clearTimeout(settleTimer);
188
+ settleTimer = null;
189
+ }
190
+ pendingName = ''; // no i18n
191
+ }
192
+ /**
193
+ * Collects the history calls and events of one navigation into a single check. The first call of
194
+ * a navigation starts it afresh: a inScreen() from before it belonged to the previous screen,
195
+ * and a view still waiting is a page left too soon to count.
196
+ *
197
+ * A history call that leaves the screen where it is, is not a navigation. Next.js and other
198
+ * routers replaceState the same url right after a route change, to store their state or the
199
+ * scroll position. Treated as one, it cancelled the view still waiting to settle, and the check
200
+ * that followed found the url already seen and recorded nothing: the page went untracked and the
201
+ * view before it was never ended.
202
+ */
203
+ function navigated() {
204
+ var name = currentName();
205
+ if (name !== '' && (name === pendingName || (pendingName === '' && name === lastName))) { // no i18n
206
+ return;
207
+ }
208
+ if (timer === null) {
209
+ claimed = false;
210
+ cancelPending();
211
+ timer = setTimeout(check, 0);
212
+ }
213
+ }
214
+ /** The app recorded a screen itself; the automatic view of this navigation gives way to it. */
215
+ function screenTrackedByApp() {
216
+ claimed = true;
217
+ cancelPending();
218
+ }
219
+ function installHistory() {
220
+ var push = history.pushState;
221
+ var replace = history.replaceState;
222
+ originalPush = push;
223
+ originalReplace = replace;
224
+ wrappedPush = function () {
225
+ var result = push.apply(this, arguments);
226
+ navigated();
227
+ return result;
228
+ };
229
+ wrappedReplace = function () {
230
+ var result = replace.apply(this, arguments);
231
+ navigated();
232
+ return result;
233
+ };
234
+ history.pushState = wrappedPush;
235
+ history.replaceState = wrappedReplace;
236
+ }
237
+ /**
238
+ * Puts history back. Only a wrapper still on top is taken off: if a library wrapped history after
239
+ * us, removing ours would remove theirs too. Ours then stays inside theirs, passing calls through
240
+ * and recording nothing, since `installed` is false.
241
+ */
242
+ function restoreHistory() {
243
+ if (wrappedPush && history.pushState === wrappedPush && originalPush) {
244
+ history.pushState = originalPush;
245
+ }
246
+ if (wrappedReplace && history.replaceState === wrappedReplace && originalReplace) {
247
+ history.replaceState = originalReplace;
248
+ }
249
+ wrappedPush = wrappedReplace = originalPush = originalReplace = null;
250
+ }
251
+ /**
252
+ * Starts or stops automatic screen tracking. Starting records the screen the page is on, unless
253
+ * the app has already called inScreen() for it. Safe to call repeatedly: wrapping history twice
254
+ * would check every navigation twice, so a second start does nothing. `wait` replaces SETTLE_MS,
255
+ * for tests.
256
+ */
257
+ function enableScreenTracking(enabled, wait) {
258
+ var on = typeof enabled === 'boolean' ? enabled : true; // no i18n
259
+ if (on) {
260
+ settleMs = SETTLE_MS; // no i18n
261
+ }
262
+ if (on === installed) {
263
+ return;
264
+ }
265
+ if (on) {
266
+ lastName = ''; // no i18n
267
+ installHistory();
268
+ window.addEventListener('popstate', navigated); // no i18n
269
+ window.addEventListener('hashchange', navigated); // no i18n
270
+ installed = true;
271
+ logger.Console.log('automatic screen tracking: on'); // no i18n
272
+ // the page the SDK starts on. Not through navigated(): a inScreen() the app made during
273
+ // start-up was replayed just before this, and it is this screen's, so its claim stands
274
+ if (timer === null) {
275
+ timer = setTimeout(check, 0);
276
+ }
277
+ return;
278
+ }
279
+ restoreHistory();
280
+ window.removeEventListener('popstate', navigated); // no i18n
281
+ window.removeEventListener('hashchange', navigated); // no i18n
282
+ if (timer !== null) {
283
+ clearTimeout(timer);
284
+ timer = null;
285
+ }
286
+ cancelPending();
287
+ installed = false;
288
+ lastName = ''; // no i18n
289
+ claimed = false;
290
+ logger.Console.log('automatic screen tracking: off'); // no i18n
291
+ }
292
+ /**
293
+ * The name automatic tracking gives the page as it is now: the cleaned path. For records that
294
+ * need a screen when none has been
295
+ * tracked — heatmaps with `track_screen: false` and no inScreen() call, say. '' when the page
296
+ * has no name.
297
+ */
298
+ function automaticScreenName() {
299
+ return currentName();
300
+ }
301
+ hooks.hooks.screenTrackedByApp = screenTrackedByApp;
302
+
303
+ exports.automaticScreenName = automaticScreenName;
304
+ exports.enableScreenTracking = enableScreenTracking;
305
+ exports.routePath = routePath;
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Apptics' defined events, as Android's DefinedEvents: ready-made names in reserved `AP_` groups,
3
+ * logged with trackEvent like any other event and never tracked automatically.
4
+ *
5
+ * trackEvent(DefinedEvents.AP_USER_LOGIN);
6
+ * trackEvent(DefinedEvents.AP_SEARCH, { query_length: 12 });
7
+ *
8
+ * They are the only events allowed the `ap_` prefix: an app's own event, group or property may
9
+ * not start with it. The list is Android's, so the console reads them the same from every platform;
10
+ * some (battery, OS update, uninstall) have no meaning in a browser and are simply never sent.
11
+ */
12
+ /** One defined event: the name and the group it is sent under. */
13
+ export interface DefinedEvent {
14
+ readonly event: string;
15
+ readonly group: string;
16
+ }
17
+ /** Every defined event, by name: `DefinedEvents.AP_USER_LOGIN` and so on. */
18
+ export declare const DefinedEvents: Readonly<Record<string, DefinedEvent>>;
19
+ /**
20
+ * The defined event `value` is, or undefined. Only the entries of DefinedEvents count, compared by
21
+ * identity, so an object that merely looks like one cannot borrow the reserved prefix.
22
+ */
23
+ export declare function asDefinedEvent(value: unknown): DefinedEvent | undefined;
@@ -0,0 +1,58 @@
1
+ /*! @zoho_apptics/apptics-js-sdk v2.0.0-beta.1 | (c) Zoho Corporation Private Limited | see LICENSE */
2
+ 'use strict';
3
+
4
+ /**
5
+ * Apptics' defined events, as Android's DefinedEvents: ready-made names in reserved `AP_` groups,
6
+ * logged with trackEvent like any other event and never tracked automatically.
7
+ *
8
+ * trackEvent(DefinedEvents.AP_USER_LOGIN);
9
+ * trackEvent(DefinedEvents.AP_SEARCH, { query_length: 12 });
10
+ *
11
+ * They are the only events allowed the `ap_` prefix: an app's own event, group or property may
12
+ * not start with it. The list is Android's, so the console reads them the same from every platform;
13
+ * some (battery, OS update, uninstall) have no meaning in a browser and are simply never sent.
14
+ */
15
+ const GROUPS = {
16
+ AP_APP_LIFE_CYCLE: [
17
+ 'AP_APP_INSTALL', 'AP_APP_UNINSTALL', 'AP_APP_UPDATE', 'AP_APP_CRACKED', 'AP_APP_OPEN', 'AP_APP_CLEAR_DATA', // no i18n
18
+ 'AP_APP_FOREGROUND', 'AP_APP_BACKGROUND', 'AP_APP_OUT_OF_MEMORY', 'AP_APP_FIRST_OPEN', 'AP_APP_LAUNCHING', // no i18n
19
+ 'AP_APP_RESIGN_ACTIVE', 'AP_APP_WILL_CONNECT', // no i18n
20
+ ],
21
+ AP_USER_LIFE_CYCLE: ['AP_USER_SIGNUP', 'AP_USER_LOGIN', 'AP_USER_LOGOUT'], // no i18n
22
+ AP_APPLICATION: [
23
+ 'AP_DEEP_LINK_OPEN', 'AP_DEEP_LINK_UPDATE', 'AP_DEEP_LINK_FIRST_OPEN', 'AP_IN_APP_PURCHASE', // no i18n
24
+ 'AP_NOTIFICATION_RECEIVE', 'AP_NOTIFICATION_OPEN', 'AP_NOTIFICATION_DISMISS', 'AP_NOTIFICATION_FOREGROUND', // no i18n
25
+ 'AP_SEARCH', 'AP_BATTERY_LOW', 'AP_BATTERY_FULL', 'AP_LOW_POWER_MODE_ON', 'AP_LOW_POWER_MODE_OFF', // no i18n
26
+ ],
27
+ AP_OTHERS: [
28
+ 'AP_NETWORK_REACHABILITY_CHANGE', 'AP_NETWORK_BANDWIDTH_CHANGE', 'AP_SWITCH_THEME_LIGHT', // no i18n
29
+ 'AP_SWITCH_THEME_DARK', 'AP_SWITCH_THEME_CUSTOM', 'AP_SWITCH_ORIENTATION_LANDSCAPE', // no i18n
30
+ 'AP_SWITCH_ORIENTATION_PORTRAIT', // no i18n
31
+ ],
32
+ AP_OS: ['AP_OS_UPDATE', 'AP_OS_UNSUPPORTED'], // no i18n
33
+ };
34
+ function build() {
35
+ var out = {};
36
+ Object.keys(GROUPS).forEach(function (group) {
37
+ GROUPS[group].forEach(function (event) {
38
+ out[event] = Object.freeze({ event: event, group: group });
39
+ });
40
+ });
41
+ return Object.freeze(out);
42
+ }
43
+ /** Every defined event, by name: `DefinedEvents.AP_USER_LOGIN` and so on. */
44
+ const DefinedEvents = build();
45
+ /**
46
+ * The defined event `value` is, or undefined. Only the entries of DefinedEvents count, compared by
47
+ * identity, so an object that merely looks like one cannot borrow the reserved prefix.
48
+ */
49
+ function asDefinedEvent(value) {
50
+ if (!value || typeof value !== 'object') { // no i18n
51
+ return undefined;
52
+ }
53
+ var named = value.event;
54
+ return typeof named === 'string' && DefinedEvents[named] === value ? value : undefined; // no i18n
55
+ }
56
+
57
+ exports.DefinedEvents = DefinedEvents;
58
+ exports.asDefinedEvent = asDefinedEvent;
@@ -0,0 +1,6 @@
1
+ import type { Dict, ServerResult } from '../types.js';
2
+ /**
3
+ * Sends pending engagements to the Apptics API.
4
+ */
5
+ export declare function sendEngagementData(forget?: boolean): Promise<void>;
6
+ export declare function requestToSendEngagements(_payload: Dict, forget?: boolean): Promise<ServerResult>;
@@ -0,0 +1,104 @@
1
+ /*! @zoho_apptics/apptics-js-sdk v2.0.0-beta.1 | (c) Zoho Corporation Private Limited | see LICENSE */
2
+ 'use strict';
3
+
4
+ var constants = require('../core/constants.js');
5
+ var state = require('../core/state.js');
6
+ var logger = require('../core/logger.js');
7
+ var utils = require('../core/utils.js');
8
+ var storage = require('../core/storage.js');
9
+ var env = require('../core/env.js');
10
+ var consent = require('../core/consent.js');
11
+ var batch = require('../core/batch.js');
12
+ var transport = require('../core/transport.js');
13
+
14
+ /**
15
+ * Sends pending engagements to the Apptics API.
16
+ */
17
+ function sendEngagementData(forget) {
18
+ // engagementtracking off (getupdates): queued items stay until tracking is back on or they expire
19
+ if (env.is_apptics_disabled() || !consent.shouldTrackOrSendData() || !state._ZAP._engagementTracking) {
20
+ return Promise.resolve();
21
+ }
22
+ if (navigator.onLine == false) {
23
+ logger.Console.warn('sendEngagementData: No Network'); // no i18n
24
+ return Promise.resolve();
25
+ }
26
+ // if we're busy sending, retry in .5s
27
+ if (state._ZAP._sending) {
28
+ batch.defer(sendEngagementData);
29
+ return Promise.resolve();
30
+ }
31
+ state._ZAP._sending = true;
32
+ batch.clearDefer();
33
+ return batch.withSendLock('engagements', function () {
34
+ return batch.freshItems(storage.engagement_transactions, function (item) { return item.endtime != 0; }, function (item) { return item.starttime; }).then(function (engagement_items) {
35
+ if (engagement_items.length == 0) {
36
+ return;
37
+ }
38
+ let sessionObj = [], screenObj = [], eventObj = [], apiObj = [];
39
+ var _payload = {};
40
+ var sent = [];
41
+ utils.cloneObject(engagement_items).forEach(function (item) {
42
+ const itemWithId = { ...item };
43
+ if (item.type == constants.AP_CONSTANTS.EVENT) {
44
+ eventObj.push(item);
45
+ }
46
+ else if (item.type == constants.AP_CONSTANTS.SCREEN) {
47
+ screenObj.push(item);
48
+ }
49
+ else if (item.type == constants.AP_CONSTANTS.SESSION) {
50
+ sessionObj.push(item);
51
+ }
52
+ else if (item.type == constants.AP_CONSTANTS.API) {
53
+ apiObj.push(item);
54
+ }
55
+ else {
56
+ return;
57
+ }
58
+ sent.push(itemWithId);
59
+ delete item.id;
60
+ delete item.type;
61
+ // which tabs share a session, which tab shows a screen and when it was last seen: the
62
+ // SDK's own bookkeeping, not part of the payload
63
+ delete item.tabs;
64
+ delete item.tab;
65
+ delete item.lastseen;
66
+ });
67
+ if (eventObj.length > 0)
68
+ _payload.events = eventObj;
69
+ if (screenObj.length > 0)
70
+ _payload.screens = screenObj;
71
+ if (sessionObj.length > 0)
72
+ _payload.sessions = sessionObj;
73
+ if (apiObj.length > 0)
74
+ _payload.apis = apiObj;
75
+ if (utils.isEmptyObj(_payload)) {
76
+ return;
77
+ }
78
+ return batch.awaitUnlessClosing(requestToSendEngagements(_payload, forget).then(function () {
79
+ return storage.engagement_transactions.remove(sent);
80
+ }, function (err) {
81
+ // the server will never accept this batch. Keeping it would block every later
82
+ // batch behind it for good, so it is dropped and said out loud
83
+ if (transport.isPermanentRefusal(err)) {
84
+ logger.warnAlways('apptics: the server refused these engagement items, so they were dropped: ' + String(err && err.message)); // no i18n
85
+ return storage.engagement_transactions.remove(sent);
86
+ }
87
+ return Promise.reject(err);
88
+ }), forget, 'sendEngagementData'); // no i18n
89
+ });
90
+ }).catch(function (err) {
91
+ logger.logFailure(constants.AP_LOG_MODULE.API, 'sendEngagementData: ' + err); // no i18n
92
+ }).then(function () {
93
+ // something went wrong or we finished: allow this method to be recalled
94
+ state._ZAP._sending = false;
95
+ });
96
+ }
97
+ // Function to send engagement data to server
98
+ function requestToSendEngagements(_payload, forget) {
99
+ var label = 'requestToSendEngagements'; // no i18n
100
+ return batch.sendBatch('engagement', label, _payload, forget); // no i18n
101
+ }
102
+
103
+ exports.requestToSendEngagements = requestToSendEngagements;
104
+ exports.sendEngagementData = sendEngagementData;
@@ -0,0 +1,51 @@
1
+ import type { Dict, QueueItem } from '../types.js';
2
+ export declare function currentScreen(): Promise<QueueItem | undefined>;
3
+ /**
4
+ * Ends one screen view, by id: it gets its end time and, with it, a duration, and becomes sendable
5
+ * (an open screen, endtime 0, waits like an open session does). A view already ended keeps its end.
6
+ */
7
+ export declare function closeScreen(id: string, time: number): Promise<void>;
8
+ /**
9
+ * Ends this tab's current screen view: the page is hidden or closing. By id, so a screen another
10
+ * tab has open is never closed from here.
11
+ */
12
+ export declare function updateScreenWithEndtime(): Promise<void>;
13
+ /**
14
+ * Track the session.
15
+ * @param sessionData A set of properties belonging to the session.
16
+ */
17
+ export declare function trackSession(sessionData: Dict): Promise<void>;
18
+ /**
19
+ * This tab's session while it is open, otherwise the newest open session in storage: the one the
20
+ * site's other tabs are on, which this tab joins rather than starting a second session beside it.
21
+ */
22
+ export declare function currentSession(): Promise<QueueItem | undefined>;
23
+ export declare function tabId(): string;
24
+ /** Takes this tab's lock, held until the page is gone. Once per page. */
25
+ export declare function holdTabLock(): void;
26
+ /** Ends the open screen views of tabs that are gone. Resolves right away without Web Locks. */
27
+ export declare function closeScreensOfGoneTabs(): Promise<void>;
28
+ /**
29
+ * Writes the waiting session, if there is one. Called by the timer and by anything that tracks an
30
+ * item, so an event in the first seconds takes its session with it.
31
+ */
32
+ export declare function commitPendingSession(): Promise<void>;
33
+ /** Forgets a session that never earned its place, on a tab closing inside the minimum. */
34
+ export declare function dropPendingSession(): boolean;
35
+ /** Whether a session is waiting to be written. Used by tests and by the page-closing path. */
36
+ export declare function hasPendingSession(): boolean;
37
+ export declare function startNewSession(time: number): Promise<void>;
38
+ /**
39
+ * This tab is closing, or the page is being replaced. The session ends here when this is the last
40
+ * tab using it, so it can be sent; while another tab of the site is still on it, this tab only
41
+ * leaves it, and the last tab to close ends it. Nothing happens when it is already closed.
42
+ */
43
+ export declare function endCurrentSession(): Promise<void>;
44
+ /**
45
+ * Closes sessions left open by a tab that was killed before it could close its own: anything
46
+ * idle for longer than inactiveduration. A live tab in another window keeps its session,
47
+ * because that one's lastactivetime is recent.
48
+ */
49
+ export declare function closeAbandonedSessions(): Promise<void>;
50
+ export declare function checkAndUpdateSession(): Promise<void>;
51
+ export declare function updateLastActiveTimeOfSession(): Promise<void>;