@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,448 @@
1
+ /*! @zoho_apptics/apptics-js-sdk v2.0.0-beta.1 | (c) Zoho Corporation Private Limited | see LICENSE */
2
+ import { AP_CONSTANTS, AP_LOG_MODULE } from '../core/constants.js';
3
+ import { _ZAP, getConfig } from '../core/state.js';
4
+ import { logFailure, Console } from '../core/logger.js';
5
+ import { isEmpty, getNewUUID, nowMs } from '../core/utils.js';
6
+ import { engagement_transactions, getKeyFromDeviceInfo, setDeviceInfoKey } from '../core/storage.js';
7
+ import { is_apptics_disabled, getNetworkStatus, getOrientation, getConnectionType, getRam } from '../core/env.js';
8
+ import { canTrackEngagement } from '../core/consent.js';
9
+ import { withLock } from '../core/locks.js';
10
+
11
+ /** The most recently started item of `type` that is still open (endtime 0). */
12
+ function latestOpen(type, items) {
13
+ return items.filter(function (item) {
14
+ return item.type == type && item.endtime === 0;
15
+ }).sort(function (a, b) {
16
+ return b.starttime - a.starttime;
17
+ })[0];
18
+ }
19
+ /**
20
+ * Ends one screen view, by id: it gets its end time and, with it, a duration, and becomes sendable
21
+ * (an open screen, endtime 0, waits like an open session does). A view already ended keeps its end.
22
+ */
23
+ function closeScreen(id, time) {
24
+ if (isEmpty(id)) {
25
+ return Promise.resolve();
26
+ }
27
+ if (_ZAP._current_screen && _ZAP._current_screen.id === id && !_ZAP._current_screen.endtime) {
28
+ _ZAP._current_screen = Object.assign({}, _ZAP._current_screen, { endtime: time });
29
+ }
30
+ return engagement_transactions.all().then(function (items) {
31
+ var open = items.filter(function (item) {
32
+ return item.type == AP_CONSTANTS.SCREEN && item.id === id && item.endtime === 0;
33
+ })[0];
34
+ if (open) {
35
+ return engagement_transactions.update(Object.assign({}, open, { endtime: Math.max(time, open.starttime) }));
36
+ }
37
+ });
38
+ }
39
+ /**
40
+ * Ends this tab's current screen view: the page is hidden or closing. By id, so a screen another
41
+ * tab has open is never closed from here.
42
+ */
43
+ function updateScreenWithEndtime() {
44
+ var current = _ZAP._current_screen;
45
+ return current && current.id ? closeScreen(current.id, nowMs()) : Promise.resolve();
46
+ }
47
+ /** Ends the screen views a session left open, at `time`: the session itself has ended. */
48
+ function closeScreensOf(session, time) {
49
+ return engagement_transactions.all().then(function (items) {
50
+ var open = items.filter(function (item) {
51
+ return item.type == AP_CONSTANTS.SCREEN && item.endtime === 0 && item.sessionstarttime === session.starttime;
52
+ });
53
+ return open.reduce(function (done, item) {
54
+ return done.then(function () {
55
+ return engagement_transactions.update(Object.assign({}, item, { endtime: Math.max(time, item.starttime) }));
56
+ });
57
+ }, Promise.resolve());
58
+ });
59
+ }
60
+ /**
61
+ * Track the session.
62
+ * @param sessionData A set of properties belonging to the session.
63
+ */
64
+ function trackSession(sessionData) {
65
+ if (!canTrackEngagement('trackSession')) { // no i18n
66
+ return Promise.resolve();
67
+ }
68
+ if (sessionData && typeof sessionData !== 'object') { // no i18n
69
+ logFailure(AP_LOG_MODULE.SESSION, 'apptics.trackSession: sessionData param must be an object'); // no i18n
70
+ return Promise.resolve();
71
+ }
72
+ var written = engagement_transactions.add(sessionData);
73
+ Console.log('apptics.trackSession(' + JSON.stringify(sessionData || {}) + ')'); // no i18n
74
+ Console.dir(sessionData);
75
+ return written;
76
+ }
77
+ /**
78
+ * This tab's session while it is open, otherwise the newest open session in storage: the one the
79
+ * site's other tabs are on, which this tab joins rather than starting a second session beside it.
80
+ */
81
+ function currentSession() {
82
+ var mine = tabSessionId();
83
+ return engagement_transactions.all().then(function (items) {
84
+ if (mine) {
85
+ var ours = items.filter(function (item) {
86
+ return item.type == AP_CONSTANTS.SESSION && item.id === mine && item.endtime === 0;
87
+ })[0];
88
+ if (ours) {
89
+ return ours;
90
+ }
91
+ }
92
+ return latestOpen(AP_CONSTANTS.SESSION, items);
93
+ });
94
+ }
95
+ /**
96
+ * This page's own id, among the tabs sharing a session. New on every page load, so a reload or a
97
+ * duplicated tab counts as a tab of its own rather than as the one it came from. Made on first use,
98
+ * not when the module loads: getNewUUID reads `screen`, which a server-side render doesn't have.
99
+ */
100
+ var tabIdValue = ''; // no i18n
101
+ function tabId() {
102
+ if (tabIdValue === '') {
103
+ tabIdValue = getNewUUID();
104
+ }
105
+ return tabIdValue;
106
+ }
107
+ /**
108
+ * The tabs of this site using a session. All of them share one session, which ends when the last
109
+ * of them closes (see endCurrentSession). Kept on the stored session, never sent: see
110
+ * sendEngagementData. A session stored by an older SDK has none, and counts as this tab's alone.
111
+ */
112
+ function tabsOf(session) {
113
+ return Array.isArray(session.tabs) ? session.tabs.slice() : [];
114
+ }
115
+ function withThisTab(session) {
116
+ var tabs = tabsOf(session);
117
+ if (tabs.indexOf(tabId()) === -1) {
118
+ tabs.push(tabId());
119
+ }
120
+ return tabs;
121
+ }
122
+ /* ---------------- screen views left open by tabs that are gone ----------------
123
+ * A screen view is ended by the tab that opened it: by the next screen, or as the page hides or
124
+ * closes. A tab that reloads, closes or crashes before that write lands leaves its view open, and
125
+ * nothing ended it until the whole session did, which with tabs sharing one session can be hours.
126
+ *
127
+ * So each tab holds a Web Lock, `apptics-tab-<its id>`, for as long as it is open: the browser
128
+ * lets go of it the moment the tab is gone, however it went. A tab that starts, or comes back into
129
+ * view, ends every open view whose tab no longer holds its lock, at the time that tab was last seen
130
+ * active. Without the Web Locks API, views are left as before and end with their session. */
131
+ const TAB_LOCK_PREFIX = 'apptics-tab-'; // no i18n
132
+ /** A view this young may belong to a tab whose lock is not granted yet: left alone. */
133
+ const YOUNG_VIEW_MS = 5000;
134
+ function tabLocks() {
135
+ var locks = typeof navigator !== 'undefined' ? navigator.locks : undefined; // no i18n
136
+ return locks && typeof locks.request === 'function' && typeof locks.query === 'function' ? locks : undefined; // no i18n
137
+ }
138
+ var holdingTabLock = false;
139
+ /** Takes this tab's lock, held until the page is gone. Once per page. */
140
+ function holdTabLock() {
141
+ var locks = tabLocks();
142
+ if (!locks || holdingTabLock) {
143
+ return;
144
+ }
145
+ holdingTabLock = true;
146
+ locks.request(TAB_LOCK_PREFIX + tabId(), function () {
147
+ return new Promise(function () { });
148
+ }).catch(function () {
149
+ holdingTabLock = false;
150
+ });
151
+ }
152
+ /** Ends the open screen views of tabs that are gone. Resolves right away without Web Locks. */
153
+ function closeScreensOfGoneTabs() {
154
+ var locks = tabLocks();
155
+ if (!locks) {
156
+ return Promise.resolve();
157
+ }
158
+ var mine = tabId();
159
+ return locks.query().then(function (state) {
160
+ var alive = {};
161
+ (state.held || []).concat(state.pending || []).forEach(function (lock) {
162
+ if (lock.name && lock.name.indexOf(TAB_LOCK_PREFIX) === 0) {
163
+ alive[lock.name.slice(TAB_LOCK_PREFIX.length)] = true;
164
+ }
165
+ });
166
+ var now = nowMs();
167
+ return engagement_transactions.all().then(function (items) {
168
+ var gone = items.filter(function (item) {
169
+ return item.type == AP_CONSTANTS.SCREEN && item.endtime === 0 && typeof item.tab === 'string' // no i18n
170
+ && item.tab !== mine && !alive[item.tab] && now - item.starttime > YOUNG_VIEW_MS;
171
+ });
172
+ if (gone.length) {
173
+ Console.log('closeScreensOfGoneTabs: ending ' + gone.length + ' screen view(s) left open by a tab that is gone'); // no i18n
174
+ }
175
+ return gone.reduce(function (done, item) {
176
+ // when that tab was last seen with the view open, not now: the time since is not time spent
177
+ var end = Math.max(item.starttime, Number(item.lastseen) || item.starttime);
178
+ return done.then(function () {
179
+ return engagement_transactions.update(Object.assign({}, item, { endtime: end }));
180
+ });
181
+ }, Promise.resolve());
182
+ });
183
+ }).catch(function (err) {
184
+ Console.log('closeScreensOfGoneTabs: ' + err); // no i18n
185
+ });
186
+ }
187
+ /** Notes on this tab's open view that the tab is still here, the end a gone tab's view is given. */
188
+ function markScreenSeen(time) {
189
+ var current = _ZAP._current_screen;
190
+ if (!current || !current.id || current.endtime) {
191
+ return Promise.resolve();
192
+ }
193
+ return engagement_transactions.all().then(function (items) {
194
+ var open = items.filter(function (item) {
195
+ return item.type == AP_CONSTANTS.SCREEN && item.id === current.id && item.endtime === 0;
196
+ })[0];
197
+ return open ? engagement_transactions.update(Object.assign({}, open, { lastseen: time })) : undefined;
198
+ });
199
+ }
200
+ /**
201
+ * Every change to the session runs under a lock shared by the site's tabs: each one reads the
202
+ * stored session, changes it and writes it back, and two tabs doing that at once would each write
203
+ * over the other's change.
204
+ */
205
+ function withSessionLock(run) {
206
+ return withLock('session', run); // no i18n
207
+ }
208
+ /** The id of this tab's session. sessionStorage is per tab and survives a reload. */
209
+ const TAB_KEY = 'apptics-session'; // no i18n
210
+ function tabSessionId() {
211
+ try {
212
+ return sessionStorage.getItem(TAB_KEY) || ''; // no i18n
213
+ }
214
+ catch (err) {
215
+ return ''; // no sessionStorage (private mode, sandboxed frame): fall back to the store
216
+ }
217
+ }
218
+ function rememberTabSession(id) {
219
+ try {
220
+ sessionStorage.setItem(TAB_KEY, id);
221
+ }
222
+ catch (err) {
223
+ // the session still works, it just can't be recognised after a reload
224
+ }
225
+ }
226
+ /** True until the first session on this device has been recorded: device_info's hadsession. */
227
+ function isFirstEverSession() {
228
+ return getKeyFromDeviceInfo(HAD_SESSION_KEY) !== true;
229
+ }
230
+ const HAD_SESSION_KEY = 'hadsession'; // no i18n
231
+ /**
232
+ * A session that has begun but is not stored yet.
233
+ *
234
+ * A visit shorter than `min_session_duration` and with nothing tracked in it is not a session worth
235
+ * reporting — a bounce, a preview, a crawler that runs scripts. So the session is held here for
236
+ * those first seconds and written only once it has earned it: when the timer fires, or the moment
237
+ * anything is tracked that needs a session to belong to, whichever comes first.
238
+ *
239
+ * `_ZAP._current_session` is set as soon as the session begins even though this is pending, so
240
+ * anything tracked in those seconds still reads the right `sessionstarttime`. Committing does not
241
+ * change `starttime`: it stays the moment the session began, so the wait is never counted as a gap.
242
+ */
243
+ var pendingSession = null;
244
+ var pendingTimer = null;
245
+ function clearPendingTimer() {
246
+ if (pendingTimer !== null) {
247
+ clearTimeout(pendingTimer);
248
+ pendingTimer = null;
249
+ }
250
+ }
251
+ /**
252
+ * Writes the waiting session, if there is one. Called by the timer and by anything that tracks an
253
+ * item, so an event in the first seconds takes its session with it.
254
+ */
255
+ function commitPendingSession() {
256
+ if (!pendingSession) {
257
+ return Promise.resolve();
258
+ }
259
+ var session = pendingSession;
260
+ pendingSession = null;
261
+ clearPendingTimer();
262
+ // the flag is set here rather than at the start, so a dropped session does not consume it and
263
+ // leave the next real session reporting firstsession false
264
+ if (session.firstsession) {
265
+ setDeviceInfoKey(HAD_SESSION_KEY, true);
266
+ }
267
+ return trackSession(session);
268
+ }
269
+ /** Forgets a session that never earned its place, on a tab closing inside the minimum. */
270
+ function dropPendingSession() {
271
+ if (!pendingSession) {
272
+ return false;
273
+ }
274
+ Console.log('session: dropped, shorter than the minimum with nothing tracked in it'); // no i18n
275
+ pendingSession = null;
276
+ clearPendingTimer();
277
+ return true;
278
+ }
279
+ function startNewSession(time) {
280
+ _ZAP._current_session = {
281
+ id: getNewUUID(),
282
+ type: AP_CONSTANTS.SESSION,
283
+ starttime: time,
284
+ endtime: 0,
285
+ edge: getConnectionType(),
286
+ orientation: getOrientation(),
287
+ firstsession: isFirstEverSession(),
288
+ networkstatus: getNetworkStatus(),
289
+ lastactivetime: time,
290
+ tabs: [tabId()],
291
+ };
292
+ // ram is optional in sessionWebSDK but checked against ram_rom ('4 GB'), which an empty
293
+ // string cannot match. navigator.deviceMemory is Chromium-only, so Firefox and Safari have
294
+ // nothing to put here - and a key that fails its regex does not lose the session, it loses
295
+ // the whole batch the session was sent in, permanently
296
+ var ram = getRam();
297
+ if (!isEmpty(ram)) {
298
+ _ZAP._current_session.ram = ram;
299
+ }
300
+ rememberTabSession(_ZAP._current_session.id);
301
+ var minimum = Number(getConfig('min_session_duration'));
302
+ if (!(minimum > 0)) {
303
+ return commitNow(_ZAP._current_session);
304
+ }
305
+ // held, not stored: see pendingSession above
306
+ pendingSession = _ZAP._current_session;
307
+ clearPendingTimer();
308
+ pendingTimer = setTimeout(function () {
309
+ pendingTimer = null;
310
+ commitPendingSession();
311
+ }, minimum);
312
+ return Promise.resolve();
313
+ }
314
+ /** The un-deferred path, for a minimum of 0. */
315
+ function commitNow(session) {
316
+ if (session.firstsession) {
317
+ setDeviceInfoKey(HAD_SESSION_KEY, true);
318
+ }
319
+ return trackSession(session);
320
+ }
321
+ /**
322
+ * This tab is closing, or the page is being replaced. The session ends here when this is the last
323
+ * tab using it, so it can be sent; while another tab of the site is still on it, this tab only
324
+ * leaves it, and the last tab to close ends it. Nothing happens when it is already closed.
325
+ */
326
+ function endCurrentSession() {
327
+ var time = nowMs();
328
+ // nothing was tracked and the minimum was never reached: this visit is not reported at all
329
+ if (dropPendingSession()) {
330
+ return Promise.resolve();
331
+ }
332
+ return withSessionLock(function () {
333
+ return closeOrLeaveSession(time);
334
+ });
335
+ }
336
+ function closeOrLeaveSession(time) {
337
+ return currentSession().then(function (_currentSession) {
338
+ if (!_currentSession || _currentSession.endtime !== 0) {
339
+ return;
340
+ }
341
+ var others = tabsOf(_currentSession).filter(function (tab) { return tab !== tabId(); });
342
+ if (others.length > 0) {
343
+ var left = Object.assign({}, _currentSession, { tabs: others, lastactivetime: time });
344
+ _ZAP._current_session = left;
345
+ Console.log('endCurrentSession: ' + others.length + ' other tab(s) still on this session, so it stays open'); // no i18n
346
+ return engagement_transactions.update(left);
347
+ }
348
+ var closed = Object.assign({}, _currentSession, { endtime: time, lastactivetime: time, tabs: [] });
349
+ _ZAP._current_session = closed;
350
+ Console.log('endCurrentSession: closing the session opened at ' + closed.starttime); // no i18n
351
+ return engagement_transactions.update(closed).then(function () {
352
+ return closeScreensOf(closed, time);
353
+ });
354
+ });
355
+ }
356
+ /**
357
+ * Closes sessions left open by a tab that was killed before it could close its own: anything
358
+ * idle for longer than inactiveduration. A live tab in another window keeps its session,
359
+ * because that one's lastactivetime is recent.
360
+ */
361
+ function closeAbandonedSessions() {
362
+ var cutoff = nowMs() - _ZAP.inactiveduration * 60 * 1000;
363
+ var mine = tabSessionId();
364
+ return engagement_transactions.all().then(function (items) {
365
+ var abandoned = items.filter(function (item) {
366
+ return item.type == AP_CONSTANTS.SESSION && item.endtime === 0 &&
367
+ item.id !== mine && item.lastactivetime < cutoff;
368
+ });
369
+ if (!abandoned.length) {
370
+ return Promise.resolve();
371
+ }
372
+ Console.log('closeAbandonedSessions: closing ' + abandoned.length + ' session(s) left open by a closed tab'); // no i18n
373
+ return abandoned.reduce(function (done, item) {
374
+ return done.then(function () {
375
+ return engagement_transactions.update(Object.assign({}, item, { endtime: item.lastactivetime }));
376
+ }).then(function () {
377
+ // the screen the closed tab was on ends with its session
378
+ return closeScreensOf(item, item.lastactivetime);
379
+ });
380
+ }, Promise.resolve());
381
+ });
382
+ }
383
+ function checkAndUpdateSession() {
384
+ if (!_ZAP._token) {
385
+ logFailure(AP_LOG_MODULE.SESSION, 'checkAndUpdateSession: You must call Apptics.init() first'); // no i18n
386
+ return Promise.resolve();
387
+ }
388
+ if (is_apptics_disabled()) {
389
+ return Promise.resolve();
390
+ }
391
+ var time = nowMs();
392
+ return withSessionLock(function () {
393
+ return settleSession(time);
394
+ });
395
+ }
396
+ function settleSession(time) {
397
+ // a tab that was killed before it could close its session: close it so it gets sent; and the
398
+ // screen views left open by tabs that are gone, so they end now rather than with the session
399
+ return closeAbandonedSessions().then(closeScreensOfGoneTabs).then(currentSession).then(function (_currentSession) {
400
+ // a closed session belongs to a tab that has gone: this tab starts its own
401
+ if (!_currentSession || _currentSession.endtime !== 0) {
402
+ return startNewSession(time);
403
+ }
404
+ _ZAP._current_session = _currentSession;
405
+ // Check if the user is inactive for the past 30 minutes
406
+ if ((time - _currentSession.lastactivetime) > (_ZAP.inactiveduration * 1000 * 60)) {
407
+ Console.log('checkAndUpdateSession: User is inactive for the past ' + _ZAP.inactiveduration + ' minutes so starting a new session'); // no i18n
408
+ // ends when the user was last active, so the idle gap isn't counted as session time
409
+ var _newSession = Object.assign({}, _currentSession, { endtime: _currentSession.lastactivetime });
410
+ Console.log('checkAndUpdateSession: (' + JSON.stringify(_newSession) + ')'); // no i18n
411
+ Console.dir(_newSession);
412
+ return engagement_transactions.update(_newSession).then(function () {
413
+ // the screen left open over the idle gap ends with the session, when it was last used
414
+ return closeScreensOf(_newSession, _newSession.endtime);
415
+ }).then(function () {
416
+ return startNewSession(time);
417
+ });
418
+ }
419
+ Console.log('checkAndUpdateSession: User is back before ' + _ZAP.inactiveduration + ' minutes so updating the lastactivitytime - ' + time); // no i18n
420
+ return touchSession();
421
+ });
422
+ }
423
+ function updateLastActiveTimeOfSession() {
424
+ return withSessionLock(touchSession);
425
+ }
426
+ /** Marks the session active now, with this tab among its tabs. Callers hold the session lock. */
427
+ function touchSession() {
428
+ var time = nowMs();
429
+ // a pending session is not in the store yet, so it is updated in place instead
430
+ if (pendingSession) {
431
+ pendingSession.lastactivetime = time;
432
+ _ZAP._current_session = pendingSession;
433
+ return Promise.resolve();
434
+ }
435
+ return currentSession().then(function (_currentSession) {
436
+ if (!_currentSession) {
437
+ return;
438
+ }
439
+ Console.log('updateLastActiveTimeOfSession: Updating the lastactivitytime - ' + time); // no i18n
440
+ var _newSession = Object.assign({}, _currentSession, { lastactivetime: time, tabs: withThisTab(_currentSession) });
441
+ _ZAP._current_session = _newSession;
442
+ return engagement_transactions.update(_newSession);
443
+ }).then(function () {
444
+ return markScreenSeen(time);
445
+ });
446
+ }
447
+
448
+ export { checkAndUpdateSession, closeAbandonedSessions, closeScreen, closeScreensOfGoneTabs, commitPendingSession, currentSession, dropPendingSession, endCurrentSession, holdTabLock, startNewSession, tabId, trackSession, updateLastActiveTimeOfSession, updateScreenWithEndtime };
@@ -0,0 +1,56 @@
1
+ import type { Dict, Properties } from '../types.js';
2
+ import { type DefinedEvent } from './defined-events.js';
3
+ /**
4
+ * Records an event: `trackEvent(name, group?, properties?)`, or a defined event with
5
+ * `trackEvent(DefinedEvents.AP_USER_LOGIN, properties?)`, which brings its own group.
6
+ *
7
+ * As on Android, an invalid name or group is not logged. A group left out is `default`, so the
8
+ * one-argument form keeps working; a group given but invalid is a mistake worth being told about,
9
+ * not one to file quietly under `default`.
10
+ */
11
+ export declare function trackEvent(eventName: string | DefinedEvent, groupName?: string | Properties, jsonObj?: Properties): void;
12
+ /** The app's own screen view. Automatic screen tracking calls recordScreen() instead. */
13
+ export declare function inScreen(screenName: string, jsonObj?: Properties): string;
14
+ /**
15
+ * The app's own screen view with a known id and start. The public inScreen takes the id and the
16
+ * time when the app calls it, so the id it hands back is the view's even when start-up is still
17
+ * running and this runs a moment later.
18
+ */
19
+ export declare function inScreenAt(screenName: string, jsonObj: Properties | undefined, id: string, startedAt: number): void;
20
+ /** Ends the screen view inScreen() began, as Android's AppticsScreenTracker.outScreen(id). */
21
+ export declare function outScreenAt(id: string, endedAt: number): void;
22
+ /**
23
+ * Queues a screen view and makes it the current screen.
24
+ *
25
+ * The view is stored open (endtime 0) and closed when the next screen starts, when the page is
26
+ * hidden or closed, when its session ends, or by outScreen() — which is what gives it a duration.
27
+ * An open view is not sent until it is closed, as an open session is not.
28
+ */
29
+ export declare function recordScreen(screenName: string, jsonObj?: Properties, id?: string, startedAt?: number, implicit?: boolean): void;
30
+ /**
31
+ * The screen a heatmap record belongs to, making sure engagement/add has a view of it: the console
32
+ * places a record by its screen, and a screen that was never sent as a view leaves it nowhere.
33
+ *
34
+ * A screen the app or automatic screen tracking named is used as it is — that one already has its
35
+ * view. Otherwise (automatic tracking off and no inScreen() call) the page is named the way
36
+ * automatic tracking would name it, and a view is recorded for it: once per screen, a new one when
37
+ * the path changes, and given way as soon as anything else names the screen.
38
+ */
39
+ export declare function screenForRecord(): string;
40
+ /**
41
+ * Back in view after being hidden: the hide ended the screen view, so the screen the user is on
42
+ * starts a new one, as an Android activity coming back to the foreground does.
43
+ */
44
+ export declare function resumeScreen(): void;
45
+ export declare function trackApi(jsonObj?: Dict): void;
46
+ /**
47
+ * Begins timing one API call, as Android's AppticsApiTracker.startTrackApi(url, method): returns
48
+ * the id to hand to endTrackApi once the response is in. Records nothing by itself.
49
+ */
50
+ export declare function startTrackApi(url: string, method?: string): string;
51
+ /**
52
+ * Finishes a call startTrackApi began and records it, timed from start to `endedAt`. The public
53
+ * wrapper takes `endedAt` when the app calls it, so a call ended while start-up is still running
54
+ * keeps its real duration even though it is recorded a moment later.
55
+ */
56
+ export declare function endTrackApiAt(id: string, responseCode: number, responseMessage: string | undefined, endedAt: number): void;