@zoho_apptics/apptics-js-sdk 1.0.1 → 2.0.0-beta

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 (226) hide show
  1. package/CHANGELOG.md +87 -0
  2. package/LICENSE +13 -0
  3. package/README.md +747 -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/batch-BU_mTpm1.min.js +3 -0
  9. package/dist/chunks/batch-BU_mTpm1.min.js.map +1 -0
  10. package/dist/chunks/logs-Bea5zUW_.min.js +3 -0
  11. package/dist/chunks/logs-Bea5zUW_.min.js.map +1 -0
  12. package/dist/cjs/auto-init.d.ts +8 -0
  13. package/dist/cjs/auto-init.js +32 -0
  14. package/dist/cjs/core/attempts.d.ts +28 -0
  15. package/dist/cjs/core/attempts.js +113 -0
  16. package/dist/cjs/core/batch.d.ts +32 -0
  17. package/dist/cjs/core/batch.js +115 -0
  18. package/dist/cjs/core/callsite.d.ts +28 -0
  19. package/dist/cjs/core/callsite.js +93 -0
  20. package/dist/cjs/core/consent.d.ts +23 -0
  21. package/dist/cjs/core/consent.js +130 -0
  22. package/dist/cjs/core/constants.d.ts +212 -0
  23. package/dist/cjs/core/constants.js +299 -0
  24. package/dist/cjs/core/db.d.ts +40 -0
  25. package/dist/cjs/core/db.js +334 -0
  26. package/dist/cjs/core/debug.d.ts +22 -0
  27. package/dist/cjs/core/debug.js +88 -0
  28. package/dist/cjs/core/env.d.ts +28 -0
  29. package/dist/cjs/core/env.js +267 -0
  30. package/dist/cjs/core/framework-id.d.ts +1 -0
  31. package/dist/cjs/core/framework-id.js +9 -0
  32. package/dist/cjs/core/guard.d.ts +2 -0
  33. package/dist/cjs/core/guard.js +72 -0
  34. package/dist/cjs/core/hooks.d.ts +41 -0
  35. package/dist/cjs/core/hooks.js +23 -0
  36. package/dist/cjs/core/logger.d.ts +16 -0
  37. package/dist/cjs/core/logger.js +86 -0
  38. package/dist/cjs/core/state.d.ts +69 -0
  39. package/dist/cjs/core/state.js +78 -0
  40. package/dist/cjs/core/storage.d.ts +46 -0
  41. package/dist/cjs/core/storage.js +229 -0
  42. package/dist/cjs/core/transport.d.ts +83 -0
  43. package/dist/cjs/core/transport.js +441 -0
  44. package/dist/cjs/core/utils.d.ts +26 -0
  45. package/dist/cjs/core/utils.js +175 -0
  46. package/dist/cjs/core/validate.d.ts +18 -0
  47. package/dist/cjs/core/validate.js +233 -0
  48. package/dist/cjs/device/appversion.d.ts +20 -0
  49. package/dist/cjs/device/appversion.js +158 -0
  50. package/dist/cjs/device/index.d.ts +23 -0
  51. package/dist/cjs/device/index.js +372 -0
  52. package/dist/cjs/engagement/api-rules.d.ts +84 -0
  53. package/dist/cjs/engagement/api-rules.js +219 -0
  54. package/dist/cjs/engagement/auto-api.d.ts +7 -0
  55. package/dist/cjs/engagement/auto-api.js +200 -0
  56. package/dist/cjs/engagement/auto-screen.d.ts +16 -0
  57. package/dist/cjs/engagement/auto-screen.js +241 -0
  58. package/dist/cjs/engagement/defined-events.d.ts +23 -0
  59. package/dist/cjs/engagement/defined-events.js +58 -0
  60. package/dist/cjs/engagement/send.d.ts +6 -0
  61. package/dist/cjs/engagement/send.js +99 -0
  62. package/dist/cjs/engagement/session.d.ts +42 -0
  63. package/dist/cjs/engagement/session.js +327 -0
  64. package/dist/cjs/engagement/track.d.ts +56 -0
  65. package/dist/cjs/engagement/track.js +350 -0
  66. package/dist/cjs/errors/send.d.ts +6 -0
  67. package/dist/cjs/errors/send.js +73 -0
  68. package/dist/cjs/errors/track.d.ts +4 -0
  69. package/dist/cjs/errors/track.js +127 -0
  70. package/dist/cjs/feedback/controls.d.ts +31 -0
  71. package/dist/cjs/feedback/controls.js +286 -0
  72. package/dist/cjs/feedback/index.d.ts +8 -0
  73. package/dist/cjs/feedback/index.js +37 -0
  74. package/dist/cjs/feedback/logs.d.ts +40 -0
  75. package/dist/cjs/feedback/logs.js +202 -0
  76. package/dist/cjs/feedback/send.d.ts +25 -0
  77. package/dist/cjs/feedback/send.js +178 -0
  78. package/dist/cjs/feedback/template.d.ts +2 -0
  79. package/dist/cjs/feedback/template.js +162 -0
  80. package/dist/cjs/feedback/ui.d.ts +55 -0
  81. package/dist/cjs/feedback/ui.js +631 -0
  82. package/dist/cjs/heatmap/index.d.ts +16 -0
  83. package/dist/cjs/heatmap/index.js +22 -0
  84. package/dist/cjs/heatmap/send.d.ts +13 -0
  85. package/dist/cjs/heatmap/send.js +82 -0
  86. package/dist/cjs/heatmap/track.d.ts +4 -0
  87. package/dist/cjs/heatmap/track.js +377 -0
  88. package/dist/cjs/index.d.ts +68 -0
  89. package/dist/cjs/index.js +150 -0
  90. package/dist/cjs/lifecycle.d.ts +14 -0
  91. package/dist/cjs/lifecycle.js +538 -0
  92. package/dist/cjs/package.json +3 -0
  93. package/dist/cjs/remoteconfig/custom.d.ts +10 -0
  94. package/dist/cjs/remoteconfig/custom.js +78 -0
  95. package/dist/cjs/remoteconfig/evaluate.d.ts +24 -0
  96. package/dist/cjs/remoteconfig/evaluate.js +171 -0
  97. package/dist/cjs/remoteconfig/fetch.d.ts +24 -0
  98. package/dist/cjs/remoteconfig/fetch.js +214 -0
  99. package/dist/cjs/remoteconfig/index.d.ts +13 -0
  100. package/dist/cjs/remoteconfig/index.js +90 -0
  101. package/dist/cjs/remoteconfig/store.d.ts +18 -0
  102. package/dist/cjs/remoteconfig/store.js +98 -0
  103. package/dist/cjs/remotelog/gate.d.ts +11 -0
  104. package/dist/cjs/remotelog/gate.js +54 -0
  105. package/dist/cjs/remotelog/index.d.ts +25 -0
  106. package/dist/cjs/remotelog/index.js +55 -0
  107. package/dist/cjs/remotelog/logger.d.ts +28 -0
  108. package/dist/cjs/remotelog/logger.js +139 -0
  109. package/dist/cjs/remotelog/send.d.ts +13 -0
  110. package/dist/cjs/remotelog/send.js +153 -0
  111. package/dist/cjs/scheduler.d.ts +8 -0
  112. package/dist/cjs/scheduler.js +78 -0
  113. package/dist/cjs/types.d.ts +312 -0
  114. package/dist/cjs/user/index.d.ts +33 -0
  115. package/dist/cjs/user/index.js +178 -0
  116. package/dist/esm/auto-init.d.ts +8 -0
  117. package/dist/esm/auto-init.js +29 -0
  118. package/dist/esm/core/attempts.d.ts +28 -0
  119. package/dist/esm/core/attempts.js +106 -0
  120. package/dist/esm/core/batch.d.ts +32 -0
  121. package/dist/esm/core/batch.js +108 -0
  122. package/dist/esm/core/callsite.d.ts +28 -0
  123. package/dist/esm/core/callsite.js +89 -0
  124. package/dist/esm/core/consent.d.ts +23 -0
  125. package/dist/esm/core/consent.js +120 -0
  126. package/dist/esm/core/constants.d.ts +212 -0
  127. package/dist/esm/core/constants.js +277 -0
  128. package/dist/esm/core/db.d.ts +40 -0
  129. package/dist/esm/core/db.js +324 -0
  130. package/dist/esm/core/debug.d.ts +22 -0
  131. package/dist/esm/core/debug.js +81 -0
  132. package/dist/esm/core/env.d.ts +28 -0
  133. package/dist/esm/core/env.js +251 -0
  134. package/dist/esm/core/framework-id.d.ts +1 -0
  135. package/dist/esm/core/framework-id.js +7 -0
  136. package/dist/esm/core/guard.d.ts +2 -0
  137. package/dist/esm/core/guard.js +70 -0
  138. package/dist/esm/core/hooks.d.ts +41 -0
  139. package/dist/esm/core/hooks.js +21 -0
  140. package/dist/esm/core/logger.d.ts +16 -0
  141. package/dist/esm/core/logger.js +82 -0
  142. package/dist/esm/core/state.d.ts +69 -0
  143. package/dist/esm/core/state.js +73 -0
  144. package/dist/esm/core/storage.d.ts +46 -0
  145. package/dist/esm/core/storage.js +219 -0
  146. package/dist/esm/core/transport.d.ts +83 -0
  147. package/dist/esm/core/transport.js +425 -0
  148. package/dist/esm/core/utils.d.ts +26 -0
  149. package/dist/esm/core/utils.js +161 -0
  150. package/dist/esm/core/validate.d.ts +18 -0
  151. package/dist/esm/core/validate.js +226 -0
  152. package/dist/esm/device/appversion.d.ts +20 -0
  153. package/dist/esm/device/appversion.js +153 -0
  154. package/dist/esm/device/index.d.ts +23 -0
  155. package/dist/esm/device/index.js +362 -0
  156. package/dist/esm/engagement/api-rules.d.ts +84 -0
  157. package/dist/esm/engagement/api-rules.js +211 -0
  158. package/dist/esm/engagement/auto-api.d.ts +7 -0
  159. package/dist/esm/engagement/auto-api.js +198 -0
  160. package/dist/esm/engagement/auto-screen.d.ts +16 -0
  161. package/dist/esm/engagement/auto-screen.js +238 -0
  162. package/dist/esm/engagement/defined-events.d.ts +23 -0
  163. package/dist/esm/engagement/defined-events.js +55 -0
  164. package/dist/esm/engagement/send.d.ts +6 -0
  165. package/dist/esm/engagement/send.js +96 -0
  166. package/dist/esm/engagement/session.d.ts +42 -0
  167. package/dist/esm/engagement/session.js +315 -0
  168. package/dist/esm/engagement/track.d.ts +56 -0
  169. package/dist/esm/engagement/track.js +340 -0
  170. package/dist/esm/errors/send.d.ts +6 -0
  171. package/dist/esm/errors/send.js +70 -0
  172. package/dist/esm/errors/track.d.ts +4 -0
  173. package/dist/esm/errors/track.js +123 -0
  174. package/dist/esm/feedback/controls.d.ts +31 -0
  175. package/dist/esm/feedback/controls.js +280 -0
  176. package/dist/esm/feedback/index.d.ts +8 -0
  177. package/dist/esm/feedback/index.js +30 -0
  178. package/dist/esm/feedback/logs.d.ts +40 -0
  179. package/dist/esm/feedback/logs.js +188 -0
  180. package/dist/esm/feedback/send.d.ts +25 -0
  181. package/dist/esm/feedback/send.js +175 -0
  182. package/dist/esm/feedback/template.d.ts +2 -0
  183. package/dist/esm/feedback/template.js +159 -0
  184. package/dist/esm/feedback/ui.d.ts +55 -0
  185. package/dist/esm/feedback/ui.js +626 -0
  186. package/dist/esm/heatmap/index.d.ts +16 -0
  187. package/dist/esm/heatmap/index.js +17 -0
  188. package/dist/esm/heatmap/send.d.ts +13 -0
  189. package/dist/esm/heatmap/send.js +79 -0
  190. package/dist/esm/heatmap/track.d.ts +4 -0
  191. package/dist/esm/heatmap/track.js +374 -0
  192. package/dist/esm/index.d.ts +68 -0
  193. package/dist/esm/index.js +122 -0
  194. package/dist/esm/lifecycle.d.ts +14 -0
  195. package/dist/esm/lifecycle.js +534 -0
  196. package/dist/esm/package.json +3 -0
  197. package/dist/esm/remoteconfig/custom.d.ts +10 -0
  198. package/dist/esm/remoteconfig/custom.js +74 -0
  199. package/dist/esm/remoteconfig/evaluate.d.ts +24 -0
  200. package/dist/esm/remoteconfig/evaluate.js +166 -0
  201. package/dist/esm/remoteconfig/fetch.d.ts +24 -0
  202. package/dist/esm/remoteconfig/fetch.js +210 -0
  203. package/dist/esm/remoteconfig/index.d.ts +13 -0
  204. package/dist/esm/remoteconfig/index.js +85 -0
  205. package/dist/esm/remoteconfig/store.d.ts +18 -0
  206. package/dist/esm/remoteconfig/store.js +91 -0
  207. package/dist/esm/remotelog/gate.d.ts +11 -0
  208. package/dist/esm/remotelog/gate.js +48 -0
  209. package/dist/esm/remotelog/index.d.ts +25 -0
  210. package/dist/esm/remotelog/index.js +51 -0
  211. package/dist/esm/remotelog/logger.d.ts +28 -0
  212. package/dist/esm/remotelog/logger.js +136 -0
  213. package/dist/esm/remotelog/send.d.ts +13 -0
  214. package/dist/esm/remotelog/send.js +149 -0
  215. package/dist/esm/scheduler.d.ts +8 -0
  216. package/dist/esm/scheduler.js +74 -0
  217. package/dist/esm/types.d.ts +312 -0
  218. package/dist/esm/user/index.d.ts +33 -0
  219. package/dist/esm/user/index.js +170 -0
  220. package/dist/feedback.esm.min.js +3 -0
  221. package/dist/feedback.esm.min.js.map +1 -0
  222. package/dist/heatmap.esm.min.js +3 -0
  223. package/dist/heatmap.esm.min.js.map +1 -0
  224. package/dist/style.css +1 -0
  225. package/package.json +124 -7
  226. package/src/appticssdk.js +0 -3817
@@ -0,0 +1,315 @@
1
+ /*! @zoho_apptics/apptics-js-sdk v2.0.0-beta | (c) Zoho Corporation Private Limited | see LICENSE */
2
+ import { AP_LOG_MODULE, AP_CONSTANTS } from '../core/constants.js';
3
+ import { _ZAP, getConfig } from '../core/state.js';
4
+ import { logFailure, Console } from '../core/logger.js';
5
+ import { nowMs, isEmpty, getNewUUID } from '../core/utils.js';
6
+ import { engagement_transactions, user_defaults } 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
+
10
+ /** The most recently started item of `type` that is still open (endtime 0). */
11
+ function latestOpen(type, items) {
12
+ return items.filter(function (item) {
13
+ return item.type == type && item.endtime === 0;
14
+ }).sort(function (a, b) {
15
+ return b.starttime - a.starttime;
16
+ })[0];
17
+ }
18
+ /**
19
+ * Ends one screen view, by id: it gets its end time and, with it, a duration, and becomes sendable
20
+ * (an open screen, endtime 0, waits like an open session does). A view already ended keeps its end.
21
+ */
22
+ function closeScreen(id, time) {
23
+ if (isEmpty(id)) {
24
+ return Promise.resolve();
25
+ }
26
+ if (_ZAP._current_screen && _ZAP._current_screen.id === id && !_ZAP._current_screen.endtime) {
27
+ _ZAP._current_screen = Object.assign({}, _ZAP._current_screen, { endtime: time });
28
+ }
29
+ return engagement_transactions.all().then(function (items) {
30
+ var open = items.filter(function (item) {
31
+ return item.type == AP_CONSTANTS.SCREEN && item.id === id && item.endtime === 0;
32
+ })[0];
33
+ if (open) {
34
+ return engagement_transactions.update(Object.assign({}, open, { endtime: Math.max(time, open.starttime) }));
35
+ }
36
+ });
37
+ }
38
+ /**
39
+ * Ends this tab's current screen view: the page is hidden or closing. By id, so a screen another
40
+ * tab has open is never closed from here.
41
+ */
42
+ function updateScreenWithEndtime() {
43
+ var current = _ZAP._current_screen;
44
+ return current && current.id ? closeScreen(current.id, nowMs()) : Promise.resolve();
45
+ }
46
+ /** Ends the screen views a session left open, at `time`: the session itself has ended. */
47
+ function closeScreensOf(session, time) {
48
+ return engagement_transactions.all().then(function (items) {
49
+ var open = items.filter(function (item) {
50
+ return item.type == AP_CONSTANTS.SCREEN && item.endtime === 0 && item.sessionstarttime === session.starttime;
51
+ });
52
+ return open.reduce(function (done, item) {
53
+ return done.then(function () {
54
+ return engagement_transactions.update(Object.assign({}, item, { endtime: Math.max(time, item.starttime) }));
55
+ });
56
+ }, Promise.resolve());
57
+ });
58
+ }
59
+ /** `track_session: false`, Android's automaticSessionsTrackingStatus: no sessions are recorded. */
60
+ function sessionsOff() {
61
+ return getConfig('track_session') === false; // no i18n
62
+ }
63
+ /**
64
+ * Track the session.
65
+ * @param sessionData A set of properties belonging to the session.
66
+ */
67
+ function trackSession(sessionData) {
68
+ if (!canTrackEngagement('trackSession')) { // no i18n
69
+ return Promise.resolve();
70
+ }
71
+ if (sessionData && typeof sessionData !== 'object') { // no i18n
72
+ logFailure(AP_LOG_MODULE.SESSION, 'apptics.trackSession: sessionData param must be an object'); // no i18n
73
+ return Promise.resolve();
74
+ }
75
+ var written = engagement_transactions.add(sessionData);
76
+ Console.log('apptics.trackSession(' + JSON.stringify(sessionData || {}) + ')'); // no i18n
77
+ Console.dir(sessionData);
78
+ return written;
79
+ }
80
+ /** This tab's session when it has one, otherwise the newest open session in storage. */
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;
87
+ })[0];
88
+ if (ours) {
89
+ return ours;
90
+ }
91
+ }
92
+ return latestOpen(AP_CONSTANTS.SESSION, items);
93
+ });
94
+ }
95
+ /** The id of this tab's session. sessionStorage is per tab and survives a reload. */
96
+ const TAB_KEY = 'apptics-session'; // no i18n
97
+ function tabSessionId() {
98
+ try {
99
+ return sessionStorage.getItem(TAB_KEY) || ''; // no i18n
100
+ }
101
+ catch (err) {
102
+ return ''; // no sessionStorage (private mode, sandboxed frame): fall back to the store
103
+ }
104
+ }
105
+ function rememberTabSession(id) {
106
+ try {
107
+ sessionStorage.setItem(TAB_KEY, id);
108
+ }
109
+ catch (err) {
110
+ // the session still works, it just can't be recognised after a reload
111
+ }
112
+ }
113
+ /** True until the first session on this device has been recorded. */
114
+ function isFirstEverSession() {
115
+ return isEmpty(user_defaults.item(HAD_SESSION_KEY));
116
+ }
117
+ const HAD_SESSION_KEY = 'hadsession'; // no i18n
118
+ /**
119
+ * A session that has begun but is not stored yet.
120
+ *
121
+ * A visit shorter than `min_session_duration` and with nothing tracked in it is not a session worth
122
+ * reporting — a bounce, a preview, a crawler that runs scripts. So the session is held here for
123
+ * those first seconds and written only once it has earned it: when the timer fires, or the moment
124
+ * anything is tracked that needs a session to belong to, whichever comes first.
125
+ *
126
+ * `_ZAP._current_session` is set as soon as the session begins even though this is pending, so
127
+ * anything tracked in those seconds still reads the right `sessionstarttime`. Committing does not
128
+ * change `starttime`: it stays the moment the session began, so the wait is never counted as a gap.
129
+ */
130
+ var pendingSession = null;
131
+ var pendingTimer = null;
132
+ function clearPendingTimer() {
133
+ if (pendingTimer !== null) {
134
+ clearTimeout(pendingTimer);
135
+ pendingTimer = null;
136
+ }
137
+ }
138
+ /**
139
+ * Writes the waiting session, if there is one. Called by the timer and by anything that tracks an
140
+ * item, so an event in the first seconds takes its session with it.
141
+ */
142
+ function commitPendingSession() {
143
+ if (!pendingSession) {
144
+ return Promise.resolve();
145
+ }
146
+ var session = pendingSession;
147
+ pendingSession = null;
148
+ clearPendingTimer();
149
+ // the flag is set here rather than at the start, so a dropped session does not consume it and
150
+ // leave the next real session reporting firstsession false
151
+ if (session.firstsession) {
152
+ user_defaults.add(HAD_SESSION_KEY, true);
153
+ }
154
+ return trackSession(session);
155
+ }
156
+ /** Forgets a session that never earned its place, on a tab closing inside the minimum. */
157
+ function dropPendingSession() {
158
+ if (!pendingSession) {
159
+ return false;
160
+ }
161
+ Console.log('session: dropped, shorter than the minimum with nothing tracked in it'); // no i18n
162
+ pendingSession = null;
163
+ clearPendingTimer();
164
+ return true;
165
+ }
166
+ function startNewSession(time) {
167
+ _ZAP._current_session = {
168
+ id: getNewUUID(),
169
+ type: AP_CONSTANTS.SESSION,
170
+ starttime: time,
171
+ endtime: 0,
172
+ edge: getConnectionType(),
173
+ orientation: getOrientation(),
174
+ firstsession: isFirstEverSession(),
175
+ networkstatus: getNetworkStatus(),
176
+ lastactivetime: time,
177
+ };
178
+ // ram is optional in sessionWebSDK but checked against ram_rom ('4 GB'), which an empty
179
+ // string cannot match. navigator.deviceMemory is Chromium-only, so Firefox and Safari have
180
+ // nothing to put here - and a key that fails its regex does not lose the session, it loses
181
+ // the whole batch the session was sent in, permanently
182
+ var ram = getRam();
183
+ if (!isEmpty(ram)) {
184
+ _ZAP._current_session.ram = ram;
185
+ }
186
+ rememberTabSession(_ZAP._current_session.id);
187
+ var minimum = Number(getConfig('min_session_duration'));
188
+ if (!(minimum > 0)) {
189
+ return commitNow(_ZAP._current_session);
190
+ }
191
+ // held, not stored: see pendingSession above
192
+ pendingSession = _ZAP._current_session;
193
+ clearPendingTimer();
194
+ pendingTimer = setTimeout(function () {
195
+ pendingTimer = null;
196
+ commitPendingSession();
197
+ }, minimum);
198
+ return Promise.resolve();
199
+ }
200
+ /** The un-deferred path, for a minimum of 0. */
201
+ function commitNow(session) {
202
+ if (session.firstsession) {
203
+ user_defaults.add(HAD_SESSION_KEY, true);
204
+ }
205
+ return trackSession(session);
206
+ }
207
+ /**
208
+ * Closes this tab's session, so it can be sent: the tab is closing or the page is being replaced.
209
+ * Nothing happens when the session is already closed.
210
+ */
211
+ function endCurrentSession() {
212
+ var time = nowMs();
213
+ // nothing was tracked and the minimum was never reached: this visit is not reported at all
214
+ if (dropPendingSession()) {
215
+ return Promise.resolve();
216
+ }
217
+ return currentSession().then(function (_currentSession) {
218
+ if (!_currentSession || _currentSession.endtime !== 0) {
219
+ return;
220
+ }
221
+ var closed = Object.assign({}, _currentSession, { endtime: time, lastactivetime: time });
222
+ _ZAP._current_session = closed;
223
+ Console.log('endCurrentSession: closing the session opened at ' + closed.starttime); // no i18n
224
+ return engagement_transactions.update(closed).then(function () {
225
+ return closeScreensOf(closed, time);
226
+ });
227
+ });
228
+ }
229
+ /**
230
+ * Closes sessions left open by a tab that was killed before it could close its own: anything
231
+ * idle for longer than inactiveduration. A live tab in another window keeps its session,
232
+ * because that one's lastactivetime is recent.
233
+ */
234
+ function closeAbandonedSessions() {
235
+ var cutoff = nowMs() - _ZAP.inactiveduration * 60 * 1000;
236
+ var mine = tabSessionId();
237
+ return engagement_transactions.all().then(function (items) {
238
+ var abandoned = items.filter(function (item) {
239
+ return item.type == AP_CONSTANTS.SESSION && item.endtime === 0 &&
240
+ item.id !== mine && item.lastactivetime < cutoff;
241
+ });
242
+ if (!abandoned.length) {
243
+ return Promise.resolve();
244
+ }
245
+ Console.log('closeAbandonedSessions: closing ' + abandoned.length + ' session(s) left open by a closed tab'); // no i18n
246
+ return abandoned.reduce(function (done, item) {
247
+ return done.then(function () {
248
+ return engagement_transactions.update(Object.assign({}, item, { endtime: item.lastactivetime }));
249
+ }).then(function () {
250
+ // the screen the closed tab was on ends with its session
251
+ return closeScreensOf(item, item.lastactivetime);
252
+ });
253
+ }, Promise.resolve());
254
+ });
255
+ }
256
+ function checkAndUpdateSession() {
257
+ if (!_ZAP._token) {
258
+ logFailure(AP_LOG_MODULE.SESSION, 'checkAndUpdateSession: You must call apptics.init() first'); // no i18n
259
+ return Promise.resolve();
260
+ }
261
+ if (is_apptics_disabled()) {
262
+ return Promise.resolve();
263
+ }
264
+ if (sessionsOff()) {
265
+ return Promise.resolve();
266
+ }
267
+ var time = nowMs();
268
+ // a tab that was killed before it could close its session: close it so it gets sent
269
+ return closeAbandonedSessions().then(currentSession).then(function (_currentSession) {
270
+ // a closed session belongs to a tab that has gone: this tab starts its own
271
+ if (!_currentSession || _currentSession.endtime !== 0) {
272
+ return startNewSession(time);
273
+ }
274
+ _ZAP._current_session = _currentSession;
275
+ // Check if the user is inactive for the past 30 minutes
276
+ if ((time - _currentSession.lastactivetime) > (_ZAP.inactiveduration * 1000 * 60)) {
277
+ Console.log('checkAndUpdateSession: User is inactive for the past ' + _ZAP.inactiveduration + ' minutes so starting a new session'); // no i18n
278
+ // ends when the user was last active, so the idle gap isn't counted as session time
279
+ var _newSession = Object.assign({}, _currentSession, { endtime: _currentSession.lastactivetime });
280
+ Console.log('checkAndUpdateSession: (' + JSON.stringify(_newSession) + ')'); // no i18n
281
+ Console.dir(_newSession);
282
+ return engagement_transactions.update(_newSession).then(function () {
283
+ // the screen left open over the idle gap ends with the session, when it was last used
284
+ return closeScreensOf(_newSession, _newSession.endtime);
285
+ }).then(function () {
286
+ return startNewSession(time);
287
+ });
288
+ }
289
+ Console.log('checkAndUpdateSession: User is back before ' + _ZAP.inactiveduration + ' minutes so updating the lastactivitytime - ' + time); // no i18n
290
+ return updateLastActiveTimeOfSession();
291
+ });
292
+ }
293
+ function updateLastActiveTimeOfSession() {
294
+ if (sessionsOff()) {
295
+ return Promise.resolve();
296
+ }
297
+ var time = nowMs();
298
+ // a pending session is not in the store yet, so it is updated in place instead
299
+ if (pendingSession) {
300
+ pendingSession.lastactivetime = time;
301
+ _ZAP._current_session = pendingSession;
302
+ return Promise.resolve();
303
+ }
304
+ return currentSession().then(function (_currentSession) {
305
+ if (!_currentSession) {
306
+ return;
307
+ }
308
+ Console.log('updateLastActiveTimeOfSession: Updating the lastactivitytime - ' + time); // no i18n
309
+ var _newSession = Object.assign({}, _currentSession, { lastactivetime: time });
310
+ _ZAP._current_session = _newSession;
311
+ return engagement_transactions.update(_newSession);
312
+ });
313
+ }
314
+
315
+ export { checkAndUpdateSession, closeAbandonedSessions, closeScreen, commitPendingSession, currentSession, dropPendingSession, endCurrentSession, startNewSession, 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;