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