@zoho_apptics/apptics-js-sdk 1.0.2 → 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 -1
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Starts or stops automatic tracking. Safe to call repeatedly: installing twice would report each
3
+ * call twice, so the second call is a no-op.
4
+ */
5
+ export declare function enableApiTracking(enabled?: boolean): void;
6
+ /** Whether the wrappers are currently in place. */
7
+ export declare function isApiTrackingEnabled(): boolean;
@@ -0,0 +1,200 @@
1
+ /*! @zoho_apptics/apptics-js-sdk v2.0.0-beta | (c) Zoho Corporation Private Limited | see LICENSE */
2
+ 'use strict';
3
+
4
+ var state = require('../core/state.js');
5
+ var logger = require('../core/logger.js');
6
+ var utils = require('../core/utils.js');
7
+ var track = require('./track.js');
8
+ var apiRules = require('./api-rules.js');
9
+
10
+ /**
11
+ * Automatic API tracking: wraps XMLHttpRequest and fetch so calls the app makes are recorded
12
+ * without it calling startTrackApi() / endTrackApi() itself.
13
+ *
14
+ * Two rules shape everything here. The wrappers must be invisible — return values, `this`, thrown
15
+ * errors and argument forms all pass straight through, because a mistake in this file breaks the
16
+ * host app's networking rather than just losing a metric. And the SDK's own traffic must never be
17
+ * recorded: engagement/add reports API calls, so tracking it would have each batch produce the
18
+ * items for the next one.
19
+ *
20
+ * Off unless `track_api` is set. Hostnames and paths of every request an app makes is a wider
21
+ * grant than events and screens, so it is the app's decision, not a default.
22
+ */
23
+ var installed = false;
24
+ var originalOpen = null;
25
+ var originalSend = null;
26
+ var originalFetch = null;
27
+ /**
28
+ * Whether a request is the SDK's own, which is never reported: engagement/add carries API items, so
29
+ * recording it would have every batch produce the items for the next one, forever.
30
+ *
31
+ * Two checks, because one is not enough to bet a loop on. The host comparison is a prefix match, so
32
+ * it misses if the host is spelled differently in the request than in serviceUrl — a trailing
33
+ * slash is enough. The path check catches those, since every SDK call sits under the configured
34
+ * endpoint whatever host it goes to, including through a same-origin proxy.
35
+ */
36
+ function isOwnRequest(url, pathname) {
37
+ var own = [state._ZAP._defaultoptions.base_domain, state._ZAP._defaultoptions.serviceUrl];
38
+ for (var i = 0; i < own.length; i++) {
39
+ if (!utils.isEmpty(own[i]) && url.indexOf(own[i]) === 0) {
40
+ return true;
41
+ }
42
+ }
43
+ var endpoint = state.getConfig('endpoint');
44
+ return !utils.isEmpty(endpoint) && pathname.indexOf(endpoint) === 0;
45
+ }
46
+ /**
47
+ * Whether one URL is ours to report: an http(s) call, not the SDK's own, and one the
48
+ * configureApiTracking rules let through. Anything unparseable is skipped rather than guessed at.
49
+ */
50
+ function shouldTrack(rawUrl) {
51
+ if (utils.isEmpty(rawUrl)) {
52
+ return false;
53
+ }
54
+ var absolute;
55
+ try {
56
+ absolute = new URL(rawUrl, window.location.href);
57
+ }
58
+ catch (err) {
59
+ return false;
60
+ }
61
+ if (absolute.protocol !== 'https:' && absolute.protocol !== 'http:') { // no i18n
62
+ return false; // data:, blob:, chrome-extension: and friends
63
+ }
64
+ if (isOwnRequest(absolute.href, absolute.pathname)) {
65
+ return false;
66
+ }
67
+ // configureApiTracking's rules: checked here too so a call left out is not even timed. trackApi checks them again, for the
68
+ // calls an app reports itself
69
+ return apiRules.hostAllowed(absolute.hostname) && !apiRules.endpointIgnored(absolute.pathname);
70
+ }
71
+ /** One finished call. Never throws: a tracking failure must not surface in the app's call. */
72
+ function record(method, url, starttime, responsecode, responsemessage) {
73
+ try {
74
+ track.trackApi({
75
+ url: url,
76
+ method: method,
77
+ responsecode: responsecode,
78
+ responsemessage: responsemessage,
79
+ starttime: starttime,
80
+ endtime: utils.nowMs(),
81
+ });
82
+ }
83
+ catch (err) {
84
+ logger.Console.log('auto api tracking: ' + String(err)); // no i18n
85
+ }
86
+ }
87
+ /** The url of any of fetch's three first-argument forms. */
88
+ function fetchUrl(input) {
89
+ if (typeof input === 'string') { // no i18n
90
+ return input;
91
+ }
92
+ if (typeof Request !== 'undefined' && input instanceof Request) { // no i18n
93
+ return input.url;
94
+ }
95
+ return String(input);
96
+ }
97
+ function fetchMethod(input, init) {
98
+ if (init && init.method) {
99
+ return init.method;
100
+ }
101
+ if (typeof Request !== 'undefined' && input instanceof Request) { // no i18n
102
+ return input.method;
103
+ }
104
+ return 'GET'; // no i18n
105
+ }
106
+ function installXhr() {
107
+ if (typeof XMLHttpRequest !== 'function') { // no i18n
108
+ return;
109
+ }
110
+ originalOpen = XMLHttpRequest.prototype.open;
111
+ originalSend = XMLHttpRequest.prototype.send;
112
+ XMLHttpRequest.prototype.open = function (method, url) {
113
+ try {
114
+ var href = String(url);
115
+ if (shouldTrack(href)) {
116
+ // starttime is set in send(): open() may happen long before the call goes out
117
+ this._ap_api = { method: String(method || 'GET'), url: href, starttime: 0 }; // no i18n
118
+ }
119
+ else {
120
+ this._ap_api = undefined;
121
+ }
122
+ }
123
+ catch (err) {
124
+ this._ap_api = undefined;
125
+ }
126
+ return originalOpen.apply(this, arguments);
127
+ };
128
+ XMLHttpRequest.prototype.send = function () {
129
+ const tracked = this._ap_api;
130
+ if (tracked) {
131
+ tracked.starttime = utils.nowMs();
132
+ // loadend fires for success, error, abort and timeout alike, so one listener covers
133
+ // every ending; status 0 is how a network failure arrives here
134
+ this.addEventListener('loadend', function () {
135
+ var message = this.status === 0 ? 'Network Error' : (this.statusText || '-'); // no i18n
136
+ record(tracked.method, tracked.url, tracked.starttime, this.status, message);
137
+ });
138
+ }
139
+ return originalSend.apply(this, arguments);
140
+ };
141
+ }
142
+ function installFetch() {
143
+ if (typeof fetch !== 'function') { // no i18n
144
+ return;
145
+ }
146
+ originalFetch = fetch;
147
+ window.fetch = function (input, init) {
148
+ var url, method, starttime;
149
+ try {
150
+ url = fetchUrl(input);
151
+ method = fetchMethod(input, init);
152
+ starttime = utils.nowMs();
153
+ }
154
+ catch (err) {
155
+ return originalFetch(input, init);
156
+ }
157
+ if (!shouldTrack(url)) {
158
+ return originalFetch(input, init);
159
+ }
160
+ return originalFetch(input, init).then(function (response) {
161
+ record(method, url, starttime, response.status, response.statusText || '-'); // no i18n
162
+ return response;
163
+ }, function (err) {
164
+ // a rejected fetch never reached a status; the rejection is re-thrown untouched
165
+ record(method, url, starttime, 0, String(err && err.message ? err.message : err) || 'Network Error'); // no i18n
166
+ throw err;
167
+ });
168
+ };
169
+ }
170
+ /**
171
+ * Starts or stops automatic tracking. Safe to call repeatedly: installing twice would report each
172
+ * call twice, so the second call is a no-op.
173
+ */
174
+ function enableApiTracking(enabled) {
175
+ var on = typeof enabled === 'boolean' ? enabled : true; // no i18n
176
+ if (on === installed) {
177
+ return;
178
+ }
179
+ if (on) {
180
+ installXhr();
181
+ installFetch();
182
+ installed = true;
183
+ logger.Console.log('automatic api tracking: on'); // no i18n
184
+ return;
185
+ }
186
+ if (originalOpen) {
187
+ XMLHttpRequest.prototype.open = originalOpen;
188
+ }
189
+ if (originalSend) {
190
+ XMLHttpRequest.prototype.send = originalSend;
191
+ }
192
+ if (originalFetch) {
193
+ window.fetch = originalFetch;
194
+ }
195
+ originalOpen = originalSend = originalFetch = null;
196
+ installed = false;
197
+ logger.Console.log('automatic api tracking: off'); // no i18n
198
+ }
199
+
200
+ exports.enableApiTracking = enableApiTracking;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Starts or stops automatic screen tracking. Starting records the screen the page is on, unless
3
+ * the app has already called inScreen() for it. Safe to call repeatedly: wrapping history twice
4
+ * would check every navigation twice, so a second start does nothing. `wait` replaces SETTLE_MS,
5
+ * for tests.
6
+ */
7
+ export declare function enableScreenTracking(enabled?: boolean, wait?: number): void;
8
+ /**
9
+ * The name automatic tracking gives the page as it is now: the cleaned path. For records that
10
+ * need a screen when none has been
11
+ * tracked — heatmaps with `track_screen: false` and no inScreen() call, say. '' when the page
12
+ * has no name.
13
+ */
14
+ export declare function automaticScreenName(): string;
15
+ /** Whether history is being watched right now. */
16
+ export declare function isScreenTrackingEnabled(): boolean;
@@ -0,0 +1,241 @@
1
+ /*! @zoho_apptics/apptics-js-sdk v2.0.0-beta | (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
+ /** The most a screen name may be, and the characters it may hold (see validateScreenName). */
36
+ const NAME_MAX = 99;
37
+ const NAME_DISALLOWED = /[^\w\s.\-&/:]/g;
38
+ /** How long an automatic view waits for the app's own inScreen() of the same navigation. */
39
+ const SETTLE_MS = 300;
40
+ var installed = false;
41
+ /** The name last recorded, so a navigation that lands on the same screen is not a new view. */
42
+ var lastName = ''; // no i18n
43
+ var timer = null;
44
+ var settleMs = SETTLE_MS;
45
+ /** The automatic view waiting out SETTLE_MS, and its timer. */
46
+ var pendingName = ''; // no i18n
47
+ var settleTimer = null;
48
+ /** The app called inScreen() during the current navigation, so it is not recorded here. */
49
+ var claimed = false;
50
+ /** When the navigation waiting out SETTLE_MS happened: its view starts then, not when recorded. */
51
+ var pendingAt = 0;
52
+ /** The wrappers this module put on history, to tell them apart from one a library added later. */
53
+ var wrappedPush = null;
54
+ var wrappedReplace = null;
55
+ var originalPush = null;
56
+ var originalReplace = null;
57
+ /**
58
+ * The default name: the path, without the query string, which is where search terms, tokens and
59
+ * other values that are not a screen live. A hash route (`#/orders`, `#!/orders`) is the route,
60
+ * so it is joined onto the path; a plain `#anchor` is a place on the same screen and is left out.
61
+ */
62
+ function nameFromUrl(url) {
63
+ var path = url.pathname;
64
+ var hash = url.hash;
65
+ if (/^#!?\//.test(hash)) {
66
+ var route = hash.replace(/^#!?/, '').split('?')[0]; // no i18n
67
+ path = path.replace(/\/+$/, '') + route; // no i18n
68
+ }
69
+ return path;
70
+ }
71
+ /**
72
+ * A name inScreen will accept. A path holds characters a screen name may not — percent
73
+ * encoding, `+`, `@` — and a refused name is dropped, so they are replaced rather than risked.
74
+ */
75
+ function cleanName(raw) {
76
+ var name = raw;
77
+ try {
78
+ name = decodeURIComponent(raw);
79
+ }
80
+ catch (err) {
81
+ // a malformed escape: clean it as it stands
82
+ }
83
+ return name.replace(NAME_DISALLOWED, '_').trim().slice(0, NAME_MAX); // no i18n
84
+ }
85
+ /**
86
+ * The name for the page as it is now, from its url, or '' to record nothing. Never throws. An app
87
+ * that wants its own names calls inScreen() as the route changes, which replaces this view.
88
+ */
89
+ function currentName() {
90
+ try {
91
+ return cleanName(nameFromUrl(new URL(window.location.href)));
92
+ }
93
+ catch (err) {
94
+ return ''; // no i18n
95
+ }
96
+ }
97
+ function check() {
98
+ timer = null;
99
+ if (!installed) {
100
+ return;
101
+ }
102
+ var name = currentName();
103
+ if (name === '' || name === lastName) { // no i18n
104
+ return;
105
+ }
106
+ lastName = name;
107
+ if (claimed) {
108
+ return; // the app has already recorded this navigation under its own name
109
+ }
110
+ // the current screen now, the view once SETTLE_MS has passed with no inScreen() of the app's.
111
+ // The open view keeps its id, so the view that follows still closes it, at the navigation
112
+ state._ZAP._current_screen = Object.assign({}, state._ZAP._current_screen, { screen: name });
113
+ pendingName = name;
114
+ pendingAt = utils.nowMs();
115
+ settleTimer = setTimeout(settle, settleMs);
116
+ }
117
+ function settle() {
118
+ settleTimer = null;
119
+ var name = pendingName;
120
+ pendingName = ''; // no i18n
121
+ if (!installed || claimed || name === '') { // no i18n
122
+ return;
123
+ }
124
+ try {
125
+ track.recordScreen(name, undefined, undefined, pendingAt || undefined);
126
+ }
127
+ catch (err) {
128
+ logger.Console.log('automatic screen tracking: ' + String(err)); // no i18n
129
+ }
130
+ }
131
+ /** Drops an automatic view still waiting out SETTLE_MS. */
132
+ function cancelPending() {
133
+ if (settleTimer !== null) {
134
+ clearTimeout(settleTimer);
135
+ settleTimer = null;
136
+ }
137
+ pendingName = ''; // no i18n
138
+ }
139
+ /**
140
+ * Collects the history calls and events of one navigation into a single check. The first call of
141
+ * a navigation starts it afresh: a inScreen() from before it belonged to the previous screen,
142
+ * and a view still waiting is a page left too soon to count.
143
+ */
144
+ function navigated() {
145
+ if (timer === null) {
146
+ claimed = false;
147
+ cancelPending();
148
+ timer = setTimeout(check, 0);
149
+ }
150
+ }
151
+ /** The app recorded a screen itself; the automatic view of this navigation gives way to it. */
152
+ function screenTrackedByApp() {
153
+ claimed = true;
154
+ cancelPending();
155
+ }
156
+ function installHistory() {
157
+ var push = history.pushState;
158
+ var replace = history.replaceState;
159
+ originalPush = push;
160
+ originalReplace = replace;
161
+ wrappedPush = function () {
162
+ var result = push.apply(this, arguments);
163
+ navigated();
164
+ return result;
165
+ };
166
+ wrappedReplace = function () {
167
+ var result = replace.apply(this, arguments);
168
+ navigated();
169
+ return result;
170
+ };
171
+ history.pushState = wrappedPush;
172
+ history.replaceState = wrappedReplace;
173
+ }
174
+ /**
175
+ * Puts history back. Only a wrapper still on top is taken off: if a library wrapped history after
176
+ * us, removing ours would remove theirs too. Ours then stays inside theirs, passing calls through
177
+ * and recording nothing, since `installed` is false.
178
+ */
179
+ function restoreHistory() {
180
+ if (wrappedPush && history.pushState === wrappedPush && originalPush) {
181
+ history.pushState = originalPush;
182
+ }
183
+ if (wrappedReplace && history.replaceState === wrappedReplace && originalReplace) {
184
+ history.replaceState = originalReplace;
185
+ }
186
+ wrappedPush = wrappedReplace = originalPush = originalReplace = null;
187
+ }
188
+ /**
189
+ * Starts or stops automatic screen tracking. Starting records the screen the page is on, unless
190
+ * the app has already called inScreen() for it. Safe to call repeatedly: wrapping history twice
191
+ * would check every navigation twice, so a second start does nothing. `wait` replaces SETTLE_MS,
192
+ * for tests.
193
+ */
194
+ function enableScreenTracking(enabled, wait) {
195
+ var on = typeof enabled === 'boolean' ? enabled : true; // no i18n
196
+ if (on) {
197
+ settleMs = SETTLE_MS; // no i18n
198
+ }
199
+ if (on === installed) {
200
+ return;
201
+ }
202
+ if (on) {
203
+ lastName = ''; // no i18n
204
+ installHistory();
205
+ window.addEventListener('popstate', navigated); // no i18n
206
+ window.addEventListener('hashchange', navigated); // no i18n
207
+ installed = true;
208
+ logger.Console.log('automatic screen tracking: on'); // no i18n
209
+ // the page the SDK starts on. Not through navigated(): a inScreen() the app made during
210
+ // start-up was replayed just before this, and it is this screen's, so its claim stands
211
+ if (timer === null) {
212
+ timer = setTimeout(check, 0);
213
+ }
214
+ return;
215
+ }
216
+ restoreHistory();
217
+ window.removeEventListener('popstate', navigated); // no i18n
218
+ window.removeEventListener('hashchange', navigated); // no i18n
219
+ if (timer !== null) {
220
+ clearTimeout(timer);
221
+ timer = null;
222
+ }
223
+ cancelPending();
224
+ installed = false;
225
+ lastName = ''; // no i18n
226
+ claimed = false;
227
+ logger.Console.log('automatic screen tracking: off'); // no i18n
228
+ }
229
+ /**
230
+ * The name automatic tracking gives the page as it is now: the cleaned path. For records that
231
+ * need a screen when none has been
232
+ * tracked — heatmaps with `track_screen: false` and no inScreen() call, say. '' when the page
233
+ * has no name.
234
+ */
235
+ function automaticScreenName() {
236
+ return currentName();
237
+ }
238
+ hooks.hooks.screenTrackedByApp = screenTrackedByApp;
239
+
240
+ exports.automaticScreenName = automaticScreenName;
241
+ exports.enableScreenTracking = enableScreenTracking;
@@ -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 | (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,99 @@
1
+ /*! @zoho_apptics/apptics-js-sdk v2.0.0-beta | (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
+ });
62
+ if (eventObj.length > 0)
63
+ _payload.events = eventObj;
64
+ if (screenObj.length > 0)
65
+ _payload.screens = screenObj;
66
+ if (sessionObj.length > 0)
67
+ _payload.sessions = sessionObj;
68
+ if (apiObj.length > 0)
69
+ _payload.apis = apiObj;
70
+ if (utils.isEmptyObj(_payload)) {
71
+ return;
72
+ }
73
+ return batch.awaitUnlessClosing(requestToSendEngagements(_payload, forget).then(function () {
74
+ return storage.engagement_transactions.remove(sent);
75
+ }, function (err) {
76
+ // the server will never accept this batch. Keeping it would block every later
77
+ // batch behind it for good, so it is dropped and said out loud
78
+ if (transport.isPermanentRefusal(err)) {
79
+ logger.warnAlways('apptics: the server refused these engagement items, so they were dropped: ' + String(err && err.message)); // no i18n
80
+ return storage.engagement_transactions.remove(sent);
81
+ }
82
+ return Promise.reject(err);
83
+ }), forget, 'sendEngagementData'); // no i18n
84
+ });
85
+ }).catch(function (err) {
86
+ logger.logFailure(constants.AP_LOG_MODULE.API, 'sendEngagementData: ' + err); // no i18n
87
+ }).then(function () {
88
+ // something went wrong or we finished: allow this method to be recalled
89
+ state._ZAP._sending = false;
90
+ });
91
+ }
92
+ // Function to send engagement data to server
93
+ function requestToSendEngagements(_payload, forget) {
94
+ var label = 'requestToSendEngagements'; // no i18n
95
+ return batch.sendBatch('engagement', label, _payload, forget); // no i18n
96
+ }
97
+
98
+ exports.requestToSendEngagements = requestToSendEngagements;
99
+ exports.sendEngagementData = sendEngagementData;