@zoho_apptics/apptics-js-sdk 2.0.0-beta.2 → 2.0.0

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 (223) hide show
  1. package/CHANGELOG.md +47 -0
  2. package/README.md +229 -174
  3. package/dist/apptics-pn-sw.js +3 -0
  4. package/dist/apptics-pn-sw.js.map +1 -0
  5. package/dist/apptics.esm.min.js +2 -2
  6. package/dist/apptics.esm.min.js.map +1 -1
  7. package/dist/apptics.min.js +2 -2
  8. package/dist/apptics.min.js.map +1 -1
  9. package/dist/chunks/batch-Axq9sTzR.min.js +3 -0
  10. package/dist/chunks/batch-Axq9sTzR.min.js.map +1 -0
  11. package/dist/chunks/lifecycle-Cvg78jN_.min.js +3 -0
  12. package/dist/chunks/lifecycle-Cvg78jN_.min.js.map +1 -0
  13. package/dist/chunks/send-DBpesydB.min.js +3 -0
  14. package/dist/chunks/send-DBpesydB.min.js.map +1 -0
  15. package/dist/chunks/transport-B21X39p4.min.js +3 -0
  16. package/dist/chunks/transport-B21X39p4.min.js.map +1 -0
  17. package/dist/cjs/core/attempts.js +1 -1
  18. package/dist/cjs/core/batch.js +4 -3
  19. package/dist/cjs/core/consent.d.ts +9 -6
  20. package/dist/cjs/core/consent.js +63 -10
  21. package/dist/cjs/core/console-call.d.ts +23 -0
  22. package/dist/cjs/core/console-call.js +63 -0
  23. package/dist/cjs/core/constants.d.ts +44 -0
  24. package/dist/cjs/core/constants.js +55 -1
  25. package/dist/cjs/core/db.js +31 -7
  26. package/dist/cjs/core/debug.d.ts +6 -4
  27. package/dist/cjs/core/debug.js +36 -12
  28. package/dist/cjs/core/env.js +1 -1
  29. package/dist/cjs/core/framework-id.d.ts +1 -1
  30. package/dist/cjs/core/framework-id.js +3 -3
  31. package/dist/cjs/core/guard.js +19 -4
  32. package/dist/cjs/core/hooks.d.ts +25 -1
  33. package/dist/cjs/core/hooks.js +11 -1
  34. package/dist/cjs/core/locks.js +1 -1
  35. package/dist/cjs/core/logger.js +1 -1
  36. package/dist/cjs/core/shield.d.ts +31 -0
  37. package/dist/cjs/core/shield.js +129 -0
  38. package/dist/cjs/core/state.d.ts +5 -1
  39. package/dist/cjs/core/state.js +3 -1
  40. package/dist/cjs/core/storage.d.ts +7 -0
  41. package/dist/cjs/core/storage.js +28 -6
  42. package/dist/cjs/core/stylesheet.js +1 -1
  43. package/dist/cjs/core/transport.d.ts +15 -4
  44. package/dist/cjs/core/transport.js +63 -13
  45. package/dist/cjs/core/utils.js +1 -1
  46. package/dist/cjs/core/validate.js +6 -5
  47. package/dist/cjs/device/appversion.js +1 -1
  48. package/dist/cjs/device/index.js +63 -15
  49. package/dist/cjs/engagement/api-rules.js +1 -1
  50. package/dist/cjs/engagement/auto-api.js +4 -3
  51. package/dist/cjs/engagement/auto-screen.js +10 -6
  52. package/dist/cjs/engagement/defined-events.d.ts +2 -3
  53. package/dist/cjs/engagement/defined-events.js +10 -16
  54. package/dist/cjs/engagement/send.js +1 -1
  55. package/dist/cjs/engagement/session.js +77 -37
  56. package/dist/cjs/engagement/track.js +9 -6
  57. package/dist/cjs/errors/send.js +1 -1
  58. package/dist/cjs/errors/track.js +3 -2
  59. package/dist/cjs/feedback/controls.d.ts +30 -4
  60. package/dist/cjs/feedback/controls.js +237 -51
  61. package/dist/cjs/feedback/i18n/index.d.ts +31 -0
  62. package/dist/cjs/feedback/i18n/index.js +187 -0
  63. package/dist/cjs/feedback/i18n/strings.d.ts +9 -0
  64. package/dist/cjs/feedback/i18n/strings.js +8 -0
  65. package/dist/cjs/feedback/index.js +1 -1
  66. package/dist/cjs/feedback/logs.js +5 -4
  67. package/dist/cjs/feedback/quartz.js +5 -2
  68. package/dist/cjs/feedback/send.d.ts +4 -1
  69. package/dist/cjs/feedback/send.js +56 -15
  70. package/dist/cjs/feedback/styles.js +2 -2
  71. package/dist/cjs/feedback/template.d.ts +4 -2
  72. package/dist/cjs/feedback/template.js +67 -46
  73. package/dist/cjs/feedback/ui.d.ts +40 -4
  74. package/dist/cjs/feedback/ui.js +208 -89
  75. package/dist/cjs/heatmap/send.js +1 -1
  76. package/dist/cjs/heatmap/track.js +13 -9
  77. package/dist/cjs/index.js +2 -2
  78. package/dist/cjs/lifecycle.js +119 -72
  79. package/dist/cjs/pushnotification/display.d.ts +65 -0
  80. package/dist/cjs/pushnotification/display.js +112 -0
  81. package/dist/cjs/pushnotification/index.d.ts +20 -0
  82. package/dist/cjs/pushnotification/index.js +96 -0
  83. package/dist/cjs/pushnotification/kept-opens.d.ts +23 -0
  84. package/dist/cjs/pushnotification/kept-opens.js +64 -0
  85. package/dist/cjs/pushnotification/notify.d.ts +45 -0
  86. package/dist/cjs/pushnotification/notify.js +476 -0
  87. package/dist/cjs/pushnotification/payload.d.ts +65 -0
  88. package/dist/cjs/pushnotification/payload.js +279 -0
  89. package/dist/cjs/pushnotification/session.d.ts +46 -0
  90. package/dist/cjs/pushnotification/session.js +413 -0
  91. package/dist/cjs/pushnotification/stats.d.ts +45 -0
  92. package/dist/cjs/pushnotification/stats.js +244 -0
  93. package/dist/cjs/pushnotification/wms.d.ts +30 -0
  94. package/dist/cjs/pushnotification/wms.js +374 -0
  95. package/dist/cjs/remoteconfig/country.js +1 -1
  96. package/dist/cjs/remoteconfig/custom.js +4 -3
  97. package/dist/cjs/remoteconfig/evaluate.js +1 -1
  98. package/dist/cjs/remoteconfig/fetch.js +27 -8
  99. package/dist/cjs/remoteconfig/index.d.ts +1 -6
  100. package/dist/cjs/remoteconfig/index.js +4 -28
  101. package/dist/cjs/remoteconfig/store.js +1 -1
  102. package/dist/cjs/remoteconfig/tz-countries.js +1 -1
  103. package/dist/cjs/remotelog/callsite.js +1 -1
  104. package/dist/cjs/remotelog/gate.js +1 -1
  105. package/dist/cjs/remotelog/logger.js +1 -1
  106. package/dist/cjs/remotelog/send.js +5 -4
  107. package/dist/cjs/remotelogger/index.js +1 -1
  108. package/dist/cjs/scheduler.js +11 -5
  109. package/dist/cjs/sw/apptics-pn-sw.d.ts +83 -0
  110. package/dist/cjs/sw/timers.d.ts +44 -0
  111. package/dist/cjs/types.d.ts +31 -15
  112. package/dist/cjs/user/index.d.ts +7 -0
  113. package/dist/cjs/user/index.js +25 -7
  114. package/dist/esm/core/attempts.js +1 -1
  115. package/dist/esm/core/batch.js +4 -3
  116. package/dist/esm/core/consent.d.ts +9 -6
  117. package/dist/esm/core/consent.js +67 -16
  118. package/dist/esm/core/console-call.d.ts +23 -0
  119. package/dist/esm/core/console-call.js +60 -0
  120. package/dist/esm/core/constants.d.ts +44 -0
  121. package/dist/esm/core/constants.js +52 -2
  122. package/dist/esm/core/db.js +31 -7
  123. package/dist/esm/core/debug.d.ts +6 -4
  124. package/dist/esm/core/debug.js +37 -13
  125. package/dist/esm/core/env.js +1 -1
  126. package/dist/esm/core/framework-id.d.ts +1 -1
  127. package/dist/esm/core/framework-id.js +3 -3
  128. package/dist/esm/core/guard.js +19 -4
  129. package/dist/esm/core/hooks.d.ts +25 -1
  130. package/dist/esm/core/hooks.js +11 -1
  131. package/dist/esm/core/locks.js +1 -1
  132. package/dist/esm/core/logger.js +1 -1
  133. package/dist/esm/core/shield.d.ts +31 -0
  134. package/dist/esm/core/shield.js +119 -0
  135. package/dist/esm/core/state.d.ts +5 -1
  136. package/dist/esm/core/state.js +3 -1
  137. package/dist/esm/core/storage.d.ts +7 -0
  138. package/dist/esm/core/storage.js +28 -8
  139. package/dist/esm/core/stylesheet.js +1 -1
  140. package/dist/esm/core/transport.d.ts +15 -4
  141. package/dist/esm/core/transport.js +64 -16
  142. package/dist/esm/core/utils.js +1 -1
  143. package/dist/esm/core/validate.js +6 -5
  144. package/dist/esm/device/appversion.js +1 -1
  145. package/dist/esm/device/index.js +67 -19
  146. package/dist/esm/engagement/api-rules.js +1 -1
  147. package/dist/esm/engagement/auto-api.js +4 -3
  148. package/dist/esm/engagement/auto-screen.js +10 -6
  149. package/dist/esm/engagement/defined-events.d.ts +2 -3
  150. package/dist/esm/engagement/defined-events.js +10 -16
  151. package/dist/esm/engagement/send.js +1 -1
  152. package/dist/esm/engagement/session.js +77 -37
  153. package/dist/esm/engagement/track.js +9 -6
  154. package/dist/esm/errors/send.js +1 -1
  155. package/dist/esm/errors/track.js +3 -2
  156. package/dist/esm/feedback/controls.d.ts +30 -4
  157. package/dist/esm/feedback/controls.js +234 -52
  158. package/dist/esm/feedback/i18n/index.d.ts +31 -0
  159. package/dist/esm/feedback/i18n/index.js +178 -0
  160. package/dist/esm/feedback/i18n/strings.d.ts +9 -0
  161. package/dist/esm/feedback/i18n/strings.js +4 -0
  162. package/dist/esm/feedback/index.js +1 -1
  163. package/dist/esm/feedback/logs.js +5 -4
  164. package/dist/esm/feedback/quartz.js +5 -2
  165. package/dist/esm/feedback/send.d.ts +4 -1
  166. package/dist/esm/feedback/send.js +56 -16
  167. package/dist/esm/feedback/styles.js +2 -2
  168. package/dist/esm/feedback/template.d.ts +4 -2
  169. package/dist/esm/feedback/template.js +67 -46
  170. package/dist/esm/feedback/ui.d.ts +40 -4
  171. package/dist/esm/feedback/ui.js +210 -91
  172. package/dist/esm/heatmap/send.js +1 -1
  173. package/dist/esm/heatmap/track.js +13 -9
  174. package/dist/esm/index.js +2 -2
  175. package/dist/esm/lifecycle.js +120 -73
  176. package/dist/esm/pushnotification/display.d.ts +65 -0
  177. package/dist/esm/pushnotification/display.js +95 -0
  178. package/dist/esm/pushnotification/index.d.ts +20 -0
  179. package/dist/esm/pushnotification/index.js +91 -0
  180. package/dist/esm/pushnotification/kept-opens.d.ts +23 -0
  181. package/dist/esm/pushnotification/kept-opens.js +62 -0
  182. package/dist/esm/pushnotification/notify.d.ts +45 -0
  183. package/dist/esm/pushnotification/notify.js +469 -0
  184. package/dist/esm/pushnotification/payload.d.ts +65 -0
  185. package/dist/esm/pushnotification/payload.js +272 -0
  186. package/dist/esm/pushnotification/session.d.ts +46 -0
  187. package/dist/esm/pushnotification/session.js +402 -0
  188. package/dist/esm/pushnotification/stats.d.ts +45 -0
  189. package/dist/esm/pushnotification/stats.js +238 -0
  190. package/dist/esm/pushnotification/wms.d.ts +30 -0
  191. package/dist/esm/pushnotification/wms.js +368 -0
  192. package/dist/esm/remoteconfig/country.js +1 -1
  193. package/dist/esm/remoteconfig/custom.js +4 -3
  194. package/dist/esm/remoteconfig/evaluate.js +1 -1
  195. package/dist/esm/remoteconfig/fetch.js +27 -8
  196. package/dist/esm/remoteconfig/index.d.ts +1 -6
  197. package/dist/esm/remoteconfig/index.js +4 -28
  198. package/dist/esm/remoteconfig/store.js +1 -1
  199. package/dist/esm/remoteconfig/tz-countries.js +1 -1
  200. package/dist/esm/remotelog/callsite.js +1 -1
  201. package/dist/esm/remotelog/gate.js +1 -1
  202. package/dist/esm/remotelog/logger.js +1 -1
  203. package/dist/esm/remotelog/send.js +5 -4
  204. package/dist/esm/remotelogger/index.js +1 -1
  205. package/dist/esm/scheduler.js +11 -5
  206. package/dist/esm/sw/apptics-pn-sw.d.ts +83 -0
  207. package/dist/esm/sw/timers.d.ts +44 -0
  208. package/dist/esm/types.d.ts +31 -15
  209. package/dist/esm/user/index.d.ts +7 -0
  210. package/dist/esm/user/index.js +26 -9
  211. package/dist/feedback.esm.min.js +2 -2
  212. package/dist/feedback.esm.min.js.map +1 -1
  213. package/dist/pushnotification.esm.min.js +3 -0
  214. package/dist/pushnotification.esm.min.js.map +1 -0
  215. package/dist/remoteconfig.esm.min.js +2 -2
  216. package/dist/remoteconfig.esm.min.js.map +1 -1
  217. package/dist/remotelogger.esm.min.js +2 -2
  218. package/dist/remotelogger.esm.min.js.map +1 -1
  219. package/package.json +15 -2
  220. package/dist/chunks/lifecycle-C6m1W7jo.min.js +0 -3
  221. package/dist/chunks/lifecycle-C6m1W7jo.min.js.map +0 -1
  222. package/dist/chunks/send-CgUO82ne.min.js +0 -3
  223. package/dist/chunks/send-CgUO82ne.min.js.map +0 -1
@@ -0,0 +1,413 @@
1
+ /*! @zoho_apptics/apptics-js-sdk v2.0.0 | (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 hooks = require('../core/hooks.js');
8
+ var utils = require('../core/utils.js');
9
+ var storage = require('../core/storage.js');
10
+ var consent = require('../core/consent.js');
11
+ var transport = require('../core/transport.js');
12
+ var stats = require('./stats.js');
13
+
14
+ /**
15
+ * This browser's registration for push messages: pushnotification/device answers with the WMS
16
+ * guest session (prdId, sessionID, token) that wmsliteapi.js connects with.
17
+ *
18
+ * It registers on its own when getsdkconfig has push on (module 16 and its wms block), and again
19
+ * by the other id when the visitor switches between anonymous and identified tracking. While
20
+ * anonymous it goes by the anonymousid, and waits for device/add to give it one. No tracking
21
+ * drops the session without unregistering: the visitor turned tracking off, not push.
22
+ * unregisterPush() is the visitor opting out of push, which holds across visits until a manual
23
+ * registerPush(true).
24
+ *
25
+ * The session does not expire, so it is saved and reused: a reload, another tab or tracking coming
26
+ * back connects with the saved one instead of registering again. It is reused only for the id it
27
+ * was registered by, the same app (apid, mapid) and the same WMS (wms_js_url); anything else
28
+ * registers anew. When WMS refuses its token the session is forgotten and registered again, at
29
+ * most AUTH_RETRY_MAX times until WMS connects, the app asks, or the device or WMS changes.
30
+ */
31
+ const LABEL = 'AppticsPushNotification'; // no i18n
32
+ /** Set when the visitor unregistered, so start-up does not register them again. */
33
+ const OPTED_OUT_KEY = 'push_unregistered'; // no i18n
34
+ /** The saved session, with what it was registered for. */
35
+ const SAVED_KEY = 'push_session'; // no i18n
36
+ /** How many registers WMS refusing the token has caused since it last connected. */
37
+ const AUTH_RETRIES_KEY = 'push_auth_retries'; // no i18n
38
+ /** The most registers WMS refusing the token may cause before it connects again. */
39
+ const AUTH_RETRY_MAX = 5;
40
+ /**
41
+ * How long a reused saved session has to connect (serverup) before it is dropped for a fresh one.
42
+ * WMS may have ended it already: the last page's unload unregisters its WMS session, and then
43
+ * register still answers but the connection never comes up. Tests shorten it.
44
+ */
45
+ const timing = { savedConnectMs: 15000 };
46
+ /** The wait for the reused saved session to connect, while one runs. */
47
+ var savedWait = null;
48
+ function stopSavedWait() {
49
+ if (savedWait !== null) {
50
+ clearTimeout(savedWait);
51
+ savedWait = null;
52
+ }
53
+ }
54
+ /** How long a manual register waits for a getsdkconfig answer before giving up. */
55
+ const CONFIG_WAIT_MS = 30000;
56
+ var session = null;
57
+ var inFlight = null;
58
+ /** Manual registers waiting for a getsdkconfig answer. */
59
+ var configWaiters = [];
60
+ /** Calls made before init(), waiting for its start-up to settle. */
61
+ var startWaiters = [];
62
+ /** Bumped by reset(), so a register in flight across it is dropped. */
63
+ var generation = 0;
64
+ /**
65
+ * Whether start-up has finished. The saved getsdkconfig answer is restored at its very start,
66
+ * before the device info is loaded, and a register then would go out without the deviceid.
67
+ */
68
+ var startedUp = false;
69
+ var listener = function () { };
70
+ /**
71
+ * Who hears about a new session (connect) and a dropped one (null: disconnect). `manual` is true
72
+ * when the app asked, which is what may retry a connection that failed to load.
73
+ */
74
+ function setSessionListener(fn) {
75
+ listener = fn;
76
+ }
77
+ function currentSession() {
78
+ return session;
79
+ }
80
+ function optedOut() {
81
+ return storage.user_defaults.item(OPTED_OUT_KEY) === true;
82
+ }
83
+ /** What a session registered now would be for: the id it goes by, the app and the WMS. */
84
+ function registeringFor(byAnonymousId) {
85
+ var device = (state._ZAP._device_info || {});
86
+ var id = device[byAnonymousId ? 'anonymousid' : 'deviceid']; // no i18n
87
+ return {
88
+ id: utils.isEmpty(id) ? '' : String(id), // no i18n
89
+ apid: transport.getHeaderParams('apid'), // no i18n
90
+ mapid: transport.getHeaderParams('mapid'), // no i18n
91
+ wms: state._ZAP._wmsConfig ? state._ZAP._wmsConfig.wms_js_url : '', // no i18n
92
+ };
93
+ }
94
+ function save(next) {
95
+ var target = registeringFor(next.byAnonymousId);
96
+ if (target.id === '') { // no i18n
97
+ return;
98
+ }
99
+ storage.user_defaults.add(SAVED_KEY, Object.assign({}, next, target));
100
+ }
101
+ function forgetSaved() {
102
+ if (storage.user_defaults.item(SAVED_KEY) !== undefined) {
103
+ storage.user_defaults.add(SAVED_KEY, undefined);
104
+ }
105
+ }
106
+ /** The saved session, when it is for the id the current tracking status goes by, this app and this WMS. */
107
+ function savedSession() {
108
+ var saved = storage.user_defaults.item(SAVED_KEY);
109
+ if (!saved || typeof saved !== 'object') { // no i18n
110
+ return null;
111
+ }
112
+ var byAnonymousId = transport.sendsByAnonymousId('pushnotification'); // no i18n
113
+ var target = registeringFor(byAnonymousId);
114
+ if (saved.byAnonymousId !== byAnonymousId || target.id === '' || saved.id !== target.id // no i18n
115
+ || saved.apid !== target.apid || saved.mapid !== target.mapid || saved.wms !== target.wms) {
116
+ return null;
117
+ }
118
+ return sessionFrom(saved, byAnonymousId);
119
+ }
120
+ function authRetries() {
121
+ var n = storage.user_defaults.item(AUTH_RETRIES_KEY);
122
+ return typeof n === 'number' && n > 0 ? n : 0; // no i18n
123
+ }
124
+ function setAuthRetries(n) {
125
+ if (authRetries() !== n) {
126
+ storage.user_defaults.add(AUTH_RETRIES_KEY, n > 0 ? n : undefined);
127
+ }
128
+ }
129
+ /** Whether WMS refusing the token has used every register it may cause. */
130
+ function retriesUsed() {
131
+ return authRetries() >= AUTH_RETRY_MAX;
132
+ }
133
+ /** Whether the console has push on for this app, and the visitor's status lets anything go. */
134
+ function allowed() {
135
+ return state._ZAP._pushEnabled && consent.opt_in_status() !== constants.AP_TRACKING_STATUS.NO_TRACKING;
136
+ }
137
+ function setSession(next, manual) {
138
+ session = next;
139
+ try {
140
+ listener(next, manual === true);
141
+ }
142
+ catch (err) {
143
+ logger.Console.warn(LABEL + ': the session listener threw', err); // no i18n
144
+ }
145
+ }
146
+ function sessionFrom(data, byAnonymousId) {
147
+ if (!data || typeof data !== 'object') { // no i18n
148
+ return null;
149
+ }
150
+ var d = data;
151
+ if (utils.isEmpty(d.prdId) || utils.isEmpty(d.sessionID) || utils.isEmpty(d.token)) {
152
+ return null;
153
+ }
154
+ return {
155
+ prdId: String(d.prdId),
156
+ sessionID: String(d.sessionID),
157
+ token: String(d.token),
158
+ displayName: utils.isEmpty(d.displayName) ? '' : String(d.displayName), // no i18n
159
+ byAnonymousId: byAnonymousId,
160
+ };
161
+ }
162
+ /** pushnotification/device with `status`, by the id the current tracking status goes by. */
163
+ function send(status) {
164
+ var blocked = transport.rejectIfOffline(LABEL) || transport.rejectIfNoDeviceIds(LABEL, 'pushnotification'); // no i18n
165
+ if (blocked) {
166
+ // the ids are on their way (pushDeviceIdsChanged registers then), or the browser is offline
167
+ return blocked.then(null, function (err) {
168
+ logger.Console.log(String(err));
169
+ return null;
170
+ });
171
+ }
172
+ return transport.postToApptics('pushnotification', '', transport.apiHeaders(), LABEL, { status: status }).then(function (result) {
173
+ return utils.isSuccess(result) ? (result.data || {}) : null;
174
+ }, function (err) {
175
+ logger.Console.log(LABEL + ': ' + status + ' failed: ' + String(err)); // no i18n
176
+ return null;
177
+ });
178
+ }
179
+ /** Registers now; concurrent calls share the one request. Resolves with the session, or null. */
180
+ function registerNow(manual) {
181
+ if (inFlight !== null) {
182
+ return inFlight;
183
+ }
184
+ var byAnonymousId = transport.sendsByAnonymousId('pushnotification'); // no i18n
185
+ var round = generation;
186
+ var request = send('register').then(function (data) {
187
+ if (round !== generation) {
188
+ // reset() ran while this was in flight: the session is the forgotten device's
189
+ return null;
190
+ }
191
+ inFlight = null;
192
+ var next = sessionFrom(data, byAnonymousId);
193
+ if (next === null) {
194
+ return null;
195
+ }
196
+ // unregistered, or the status changed, while this was in flight: not this session
197
+ if (optedOut() || !allowed() || transport.sendsByAnonymousId('pushnotification') !== byAnonymousId) { // no i18n
198
+ return null;
199
+ }
200
+ logger.Console.log(LABEL + ': registered, session ' + next.sessionID); // no i18n
201
+ save(next);
202
+ setSession(next, manual);
203
+ return next;
204
+ });
205
+ inFlight = request;
206
+ return request;
207
+ }
208
+ /** Forgets the session, saved one included, so the next register asks for a new one. */
209
+ function dropSession() {
210
+ stopSavedWait();
211
+ forgetSaved();
212
+ if (session !== null) {
213
+ setSession(null);
214
+ }
215
+ }
216
+ /**
217
+ * WMS refused the session's token: forgets it and registers again, while fewer than
218
+ * AUTH_RETRY_MAX registers were caused this way since WMS last connected.
219
+ */
220
+ function sessionRefused() {
221
+ dropSession();
222
+ if (optedOut() || !allowed()) {
223
+ return;
224
+ }
225
+ if (retriesUsed()) {
226
+ logger.Console.log(LABEL + ': WMS refused the session ' + AUTH_RETRY_MAX + ' times; not registering again until setEnabled(true)'); // no i18n
227
+ return;
228
+ }
229
+ setAuthRetries(authRetries() + 1);
230
+ logger.Console.log(LABEL + ': WMS refused the session, registering again (' + authRetries() + ' of ' + AUTH_RETRY_MAX + ')'); // no i18n
231
+ registerNow();
232
+ }
233
+ /** WMS connected: the token was taken, so refusals count again from 0. */
234
+ function sessionConnected() {
235
+ stopSavedWait();
236
+ setAuthRetries(0);
237
+ }
238
+ /** Connects with the saved session when it is a current one. Resolves with it, or null. */
239
+ function useSaved(manual) {
240
+ var saved = savedSession();
241
+ if (saved === null) {
242
+ return null;
243
+ }
244
+ logger.Console.log(LABEL + ': reusing the saved session ' + saved.sessionID); // no i18n
245
+ setSession(saved, manual);
246
+ // once: a fresh session that does not connect either is WMS's to retry, not ours
247
+ stopSavedWait();
248
+ var round = generation;
249
+ savedWait = setTimeout(function () {
250
+ savedWait = null;
251
+ if (round !== generation || session !== saved) {
252
+ return;
253
+ }
254
+ logger.Console.log(LABEL + ': WMS did not connect with the saved session; registering a fresh one'); // no i18n
255
+ dropSession();
256
+ if (!optedOut() && allowed()) {
257
+ registerNow(manual);
258
+ }
259
+ }, timing.savedConnectMs);
260
+ return saved;
261
+ }
262
+ /** Whether a session exists for the id the current tracking status goes by. */
263
+ function hasCurrentSession() {
264
+ return session !== null && session.byAnonymousId === transport.sendsByAnonymousId('pushnotification'); // no i18n
265
+ }
266
+ /** Registers on its own when everything allows it and no current session exists. */
267
+ function autoRegister() {
268
+ if (!startedUp || !allowed() || optedOut() || hasCurrentSession()) {
269
+ return;
270
+ }
271
+ if (useSaved() !== null || retriesUsed()) {
272
+ return;
273
+ }
274
+ registerNow();
275
+ }
276
+ /** Resolves once a getsdkconfig answer is applied, or after CONFIG_WAIT_MS. */
277
+ function configAnswered() {
278
+ if (state._ZAP._nextConfigCheck > 0) {
279
+ return Promise.resolve();
280
+ }
281
+ return new Promise(function (resolve) {
282
+ var done = false;
283
+ var finish = function () {
284
+ if (!done) {
285
+ done = true;
286
+ resolve();
287
+ }
288
+ };
289
+ configWaiters.push(finish);
290
+ setTimeout(finish, CONFIG_WAIT_MS);
291
+ hooks.hooks.askServerSettings();
292
+ });
293
+ }
294
+ /**
295
+ * Registers this browser for push. `manual` is the app asking (AppticsPushNotification.setEnabled(true)):
296
+ * it clears an earlier unregister, and waits for getsdkconfig when nothing has answered yet.
297
+ * Resolves with the session, or null when the console has push off or registering failed.
298
+ */
299
+ function registerPush(manual) {
300
+ if (optedOut()) {
301
+ storage.user_defaults.add(OPTED_OUT_KEY, false);
302
+ }
303
+ {
304
+ // the app asking starts the count of WMS refusals again
305
+ setAuthRetries(0);
306
+ }
307
+ return configAnswered().then(function () {
308
+ if (!allowed()) {
309
+ return null;
310
+ }
311
+ if (hasCurrentSession()) {
312
+ // the app asking again reconnects a session whose connection failed to load
313
+ {
314
+ setSession(session, true);
315
+ }
316
+ return session;
317
+ }
318
+ return useSaved(manual) || registerNow(manual);
319
+ });
320
+ }
321
+ /** Unregisters this browser, and keeps it unregistered across visits. Resolves true on success. */
322
+ function unregisterPush() {
323
+ storage.user_defaults.add(OPTED_OUT_KEY, true);
324
+ forgetSaved();
325
+ if (session !== null) {
326
+ setSession(null);
327
+ }
328
+ return send('unregister').then(function (data) {
329
+ return data !== null;
330
+ });
331
+ }
332
+ function trackingChanged() {
333
+ if (consent.opt_in_status() === constants.AP_TRACKING_STATUS.NO_TRACKING) {
334
+ if (session !== null) {
335
+ setSession(null);
336
+ }
337
+ return;
338
+ }
339
+ if (session !== null && !hasCurrentSession()) {
340
+ // switched between anonymous and identified: the session belongs to the other id
341
+ setSession(null);
342
+ }
343
+ autoRegister();
344
+ }
345
+ function configChanged() {
346
+ var waiters = configWaiters;
347
+ configWaiters = [];
348
+ waiters.forEach(function (fn) { fn(); });
349
+ if (!state._ZAP._pushEnabled && session !== null) {
350
+ setSession(null);
351
+ return;
352
+ }
353
+ autoRegister();
354
+ }
355
+ /**
356
+ * Resolves true once the SDK is running, false when its start-up failed: right away when it has
357
+ * started, on init()'s start-up when that is under way, and on the next init() when none was made.
358
+ */
359
+ function whenStarted() {
360
+ if (state._ZAP._ready) {
361
+ return Promise.resolve(true);
362
+ }
363
+ if (state._ZAP._readyPromise) {
364
+ return state._ZAP._readyPromise.then(function (running) { return running === true; }, function () { return false; });
365
+ }
366
+ return new Promise(function (resolve) {
367
+ startWaiters.push(resolve);
368
+ });
369
+ }
370
+ function initCalled() {
371
+ var waiters = startWaiters;
372
+ startWaiters = [];
373
+ waiters.forEach(function (fn) {
374
+ whenStarted().then(fn);
375
+ });
376
+ }
377
+ function started() {
378
+ startedUp = true;
379
+ // opens the service worker kept while no tab of the site was open
380
+ stats.drainWorkerOpens();
381
+ autoRegister();
382
+ }
383
+ function reset() {
384
+ generation++;
385
+ stopSavedWait();
386
+ startedUp = false;
387
+ inFlight = null;
388
+ configWaiters = [];
389
+ forgetSaved();
390
+ setAuthRetries(0);
391
+ // the connection goes with the forgotten device
392
+ if (session !== null) {
393
+ setSession(null);
394
+ }
395
+ }
396
+ hooks.hooks.sdkConfigModuleIds = function () { return [constants.SDK_MODULE.PUSH_NOTIFICATION]; };
397
+ hooks.hooks.pushConfigChanged = configChanged;
398
+ hooks.hooks.pushTrackingChanged = trackingChanged;
399
+ hooks.hooks.pushStarted = started;
400
+ hooks.hooks.pushInitCalled = initCalled;
401
+ hooks.hooks.resetPush = reset;
402
+ hooks.hooks.pushDeviceIdsChanged = autoRegister;
403
+
404
+ exports.AUTH_RETRY_MAX = AUTH_RETRY_MAX;
405
+ exports.currentSession = currentSession;
406
+ exports.dropSession = dropSession;
407
+ exports.registerPush = registerPush;
408
+ exports.sessionConnected = sessionConnected;
409
+ exports.sessionRefused = sessionRefused;
410
+ exports.setSessionListener = setSessionListener;
411
+ exports.timing = timing;
412
+ exports.unregisterPush = unregisterPush;
413
+ exports.whenStarted = whenStarted;
@@ -0,0 +1,45 @@
1
+ export declare const STATUS: Readonly<{
2
+ readonly RECEIVED: 4;
3
+ readonly OPENED: 5;
4
+ readonly CONVERSION: 6;
5
+ }>;
6
+ type Status = 4 | 5 | 6;
7
+ export interface PushStat {
8
+ notificationid: number;
9
+ timestamp: number;
10
+ status: Status;
11
+ notificationnumber?: number;
12
+ actionid?: string;
13
+ }
14
+ /** What a stat is about: the message's appticsPnID and its run number. */
15
+ export interface StatTarget {
16
+ id: string;
17
+ number: number | null;
18
+ /**
19
+ * The run's send time (the envelope's timestamp), for a message without a run number: a
20
+ * recurring push repeats its appticsPnID, so each run is counted by this. Never sent.
21
+ */
22
+ run?: string;
23
+ }
24
+ /**
25
+ * Records a stat for the message `target`: once on this browser, and not at all while tracking
26
+ * is off or the message has no appticsPnID. `timestamp` is when it happened (now by default).
27
+ */
28
+ export declare function recordStat(target: StatTarget, status: Status, actionid?: string, timestamp?: number): void;
29
+ /** An opened message with a conversion event: it waits for that event for 7 days. */
30
+ export declare function awaitConversion(target: StatTarget, event: {
31
+ name: string;
32
+ group: string;
33
+ }): void;
34
+ /**
35
+ * The opens the service worker kept while no tab of the site was open: each becomes an opened
36
+ * stat with when it happened, and starts its conversion wait. Push start-up calls it.
37
+ */
38
+ export declare function drainWorkerOpens(): Promise<void>;
39
+ /**
40
+ * Sends a batch of the stored stats, and removes it once the server takes it, or when it refuses
41
+ * it for good (a malformed batch would otherwise block every later one). Nothing goes while the
42
+ * SDK is disabled or tracking is off. `forget`: the page is closing, so it goes by keepalive.
43
+ */
44
+ export declare function sendPushStats(forget?: boolean): Promise<void>;
45
+ export {};