@inglorious/web 6.11.0 → 6.13.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/package.json +10 -2
- package/src/analytics/index.js +99 -0
- package/src/push/index.js +142 -0
- package/types/analytics.d.ts +47 -0
- package/types/push.d.ts +54 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/web",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.13.0",
|
|
4
4
|
"description": "A new web framework that leverages the power of the Inglorious Store combined with the performance and simplicity of lit-html.",
|
|
5
5
|
"author": "Matteo Antony Mistretta <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
|
|
6
6
|
"contributors": [
|
|
@@ -33,6 +33,10 @@
|
|
|
33
33
|
"types": "./types/index.d.ts",
|
|
34
34
|
"import": "./src/index.js"
|
|
35
35
|
},
|
|
36
|
+
"./analytics": {
|
|
37
|
+
"types": "./types/analytics.d.ts",
|
|
38
|
+
"import": "./src/analytics/index.js"
|
|
39
|
+
},
|
|
36
40
|
"./decorators/with-debounce": {
|
|
37
41
|
"types": "./types/decorators/with-debounce.d.ts",
|
|
38
42
|
"import": "./src/decorators/with-debounce/index.js"
|
|
@@ -66,6 +70,10 @@
|
|
|
66
70
|
"types": "./types/pwa.d.ts",
|
|
67
71
|
"import": "./src/pwa/index.js"
|
|
68
72
|
},
|
|
73
|
+
"./push": {
|
|
74
|
+
"types": "./types/push.d.ts",
|
|
75
|
+
"import": "./src/push/index.js"
|
|
76
|
+
},
|
|
69
77
|
"./router": {
|
|
70
78
|
"types": "./types/router.d.ts",
|
|
71
79
|
"import": "./src/router/index.js"
|
|
@@ -127,7 +135,7 @@
|
|
|
127
135
|
"vitest": "^5.0.0"
|
|
128
136
|
},
|
|
129
137
|
"engines": {
|
|
130
|
-
"node": ">=
|
|
138
|
+
"node": ">= 26"
|
|
131
139
|
},
|
|
132
140
|
"scripts": {
|
|
133
141
|
"format": "prettier --write .",
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
const DEFAULT_CONSENT_KEY = "inglorious-analytics-consent"
|
|
2
|
+
const GRANTED = "granted"
|
|
3
|
+
const DENIED = "denied"
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The type that measures how an app is used, and only once the user said
|
|
7
|
+
* yes. Nothing reaches the adapter before then: in a browser, the first call
|
|
8
|
+
* to an analytics library is what sets its cookies and sends its first hit.
|
|
9
|
+
*
|
|
10
|
+
* Its entity's `consent` is `"granted"`, `"denied"` or `null` (not asked
|
|
11
|
+
* yet), remembered on the device. While `isAsking`, the app renders its own
|
|
12
|
+
* consent prompt, whose answer is `analyticsConsent` with `true` or `false`.
|
|
13
|
+
* `analyticsAsk` brings the prompt back, for a settings page where the
|
|
14
|
+
* choice can be changed as easily as it was made.
|
|
15
|
+
*
|
|
16
|
+
* Once granted, every `routeChange` from the router is a page view, and the
|
|
17
|
+
* app's own events go through `analyticsTrack` with `{ name, params }`.
|
|
18
|
+
*
|
|
19
|
+
* Everything vendor-specific is the `adapter`'s:
|
|
20
|
+
*
|
|
21
|
+
* - `setConsent(isGranted)`: starts or stops measuring.
|
|
22
|
+
* - `pageView({ route, path })`: one page or screen. An adapter whose
|
|
23
|
+
* library already counts history changes as page views can do nothing.
|
|
24
|
+
* - `event(name, params)`: one of the app's own events.
|
|
25
|
+
*
|
|
26
|
+
* @param {Object} config
|
|
27
|
+
* @param {Object} config.adapter
|
|
28
|
+
* @param {string} [config.consentKey] - The localStorage key of the choice.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* const types = { Analytics: [createAnalytics({ adapter }), renderers] }
|
|
32
|
+
* const entities = { analytics: { type: "Analytics" } }
|
|
33
|
+
* // later, anywhere
|
|
34
|
+
* api.notify("#analytics:analyticsTrack", { name: "sign_up" })
|
|
35
|
+
*/
|
|
36
|
+
export function createAnalytics({ adapter, consentKey = DEFAULT_CONSENT_KEY }) {
|
|
37
|
+
function readConsent() {
|
|
38
|
+
try {
|
|
39
|
+
const stored = window.localStorage.getItem(consentKey)
|
|
40
|
+
return stored === GRANTED || stored === DENIED ? stored : null
|
|
41
|
+
} catch {
|
|
42
|
+
return null
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function writeConsent(consent) {
|
|
47
|
+
try {
|
|
48
|
+
window.localStorage.setItem(consentKey, consent)
|
|
49
|
+
} catch {
|
|
50
|
+
// Without storage the prompt comes back next visit, nothing worse.
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
create(entity) {
|
|
56
|
+
entity.consent ??= readConsent()
|
|
57
|
+
entity.isAsking ??= entity.consent === null
|
|
58
|
+
if (entity.consent === GRANTED) call(() => adapter.setConsent(true))
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
routeChange(entity, { route, path } = {}) {
|
|
62
|
+
if (entity.consent !== GRANTED) return
|
|
63
|
+
call(() => adapter.pageView({ route, path }))
|
|
64
|
+
},
|
|
65
|
+
|
|
66
|
+
analyticsConsent(entity, isGranted, api) {
|
|
67
|
+
const consent = isGranted ? GRANTED : DENIED
|
|
68
|
+
writeConsent(consent)
|
|
69
|
+
entity.consent = consent
|
|
70
|
+
entity.isAsking = false
|
|
71
|
+
call(() => adapter.setConsent(isGranted))
|
|
72
|
+
if (!isGranted) return
|
|
73
|
+
|
|
74
|
+
// The page the answer came from is the first one measured.
|
|
75
|
+
const router = api.getEntity("router")
|
|
76
|
+
if (router?.route) {
|
|
77
|
+
call(() => adapter.pageView({ route: router.route, path: router.path }))
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
|
|
81
|
+
analyticsAsk(entity) {
|
|
82
|
+
entity.isAsking = true
|
|
83
|
+
},
|
|
84
|
+
|
|
85
|
+
analyticsTrack(entity, { name, params } = {}) {
|
|
86
|
+
if (entity.consent !== GRANTED || !name) return
|
|
87
|
+
call(() => adapter.event(name, params))
|
|
88
|
+
},
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Measuring is a courtesy: a failure, sync or async, never reaches the app.
|
|
93
|
+
function call(fn) {
|
|
94
|
+
try {
|
|
95
|
+
void Promise.resolve(fn()).catch(() => {})
|
|
96
|
+
} catch {
|
|
97
|
+
// Nothing to do: the next call tries again.
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { handleAsync } from "@inglorious/store/async"
|
|
2
|
+
|
|
3
|
+
const DEFAULT_DECLINE_KEY = "inglorious-push-declined"
|
|
4
|
+
|
|
5
|
+
// The adapters whose taps are already wired: an entity created again (a hot
|
|
6
|
+
// update, a second store) must not open each push twice.
|
|
7
|
+
const tappedAdapters = new WeakSet()
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The type that asks a device for push notifications and keeps it registered.
|
|
11
|
+
*
|
|
12
|
+
* A device shows its permission dialog only so many times, and a refusal
|
|
13
|
+
* there may be for good. So the type asks in the app's own words first: an
|
|
14
|
+
* entity with `isOpen` the app renders as a prompt, whose yes brings up the
|
|
15
|
+
* system's dialog (`pushAsk`) and whose no (`pushDecline`) is remembered on
|
|
16
|
+
* the device, so it never asks again. Allowed later from the system's
|
|
17
|
+
* settings, pushes still register.
|
|
18
|
+
*
|
|
19
|
+
* The app tells it when there is someone to register for: `pushStart` (at
|
|
20
|
+
* sign-in) checks the permission, registers the device if it is granted, and
|
|
21
|
+
* otherwise opens the prompt; `pushStop` (at sign-out) closes it. Map them
|
|
22
|
+
* onto the app's own events, such as `sessionStart: type.pushStart`.
|
|
23
|
+
*
|
|
24
|
+
* Everything vendor-specific is the `adapter`'s:
|
|
25
|
+
*
|
|
26
|
+
* - `isSupported()`: whether this device can receive pushes at all.
|
|
27
|
+
* - `permission()`: `"granted"`, `"denied"` or `"prompt"`.
|
|
28
|
+
* - `request()`: brings up the system's dialog, resolving to the same. In a
|
|
29
|
+
* browser it must call `Notification.requestPermission()` before it awaits
|
|
30
|
+
* anything, since the dialog needs the tap that asked.
|
|
31
|
+
* - `register()`: gets the device's token and stores it for the signed-in
|
|
32
|
+
* user. Unregistering is the app's to do at sign-out, while the user can
|
|
33
|
+
* still delete their own token.
|
|
34
|
+
* - `onTap(listener)`, optional: calls `listener(url)` when a push is tapped,
|
|
35
|
+
* where the system shows them itself (native apps). The type navigates
|
|
36
|
+
* there with `onTap(url, api)`, by default `#router:navigate`.
|
|
37
|
+
*
|
|
38
|
+
* @param {Object} config
|
|
39
|
+
* @param {Object} config.adapter
|
|
40
|
+
* @param {string} [config.declineKey] - The localStorage key of a decline.
|
|
41
|
+
* @param {(url: string, api: Object) => void} [config.onTap]
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* const prompt = createPushPrompt({ adapter: fcm })
|
|
45
|
+
* const types = {
|
|
46
|
+
* PushPrompt: {
|
|
47
|
+
* ...prompt,
|
|
48
|
+
* ...renderers,
|
|
49
|
+
* sessionStart: prompt.pushStart,
|
|
50
|
+
* sessionEnd: prompt.pushStop,
|
|
51
|
+
* },
|
|
52
|
+
* }
|
|
53
|
+
*/
|
|
54
|
+
export function createPushPrompt({
|
|
55
|
+
adapter,
|
|
56
|
+
declineKey = DEFAULT_DECLINE_KEY,
|
|
57
|
+
onTap = navigate,
|
|
58
|
+
}) {
|
|
59
|
+
function isDeclined() {
|
|
60
|
+
try {
|
|
61
|
+
return window.localStorage.getItem(declineKey) !== null
|
|
62
|
+
} catch {
|
|
63
|
+
return false
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
create(entity, _, api) {
|
|
69
|
+
entity.isOpen ??= false
|
|
70
|
+
entity.isActive ??= false
|
|
71
|
+
if (adapter.onTap && !tappedAdapters.has(adapter)) {
|
|
72
|
+
tappedAdapters.add(adapter)
|
|
73
|
+
adapter.onTap((url) => onTap(url, api))
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
|
|
77
|
+
pushStart(entity, _, api) {
|
|
78
|
+
entity.isActive = true
|
|
79
|
+
api.notify(`#${entity.id}:pushCheck`)
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
pushStop(entity) {
|
|
83
|
+
entity.isActive = false
|
|
84
|
+
entity.isOpen = false
|
|
85
|
+
},
|
|
86
|
+
|
|
87
|
+
// Asking the device is async, so a stop can land before the answer: the
|
|
88
|
+
// prompt only opens while started.
|
|
89
|
+
...handleAsync("pushCheck", {
|
|
90
|
+
async run() {
|
|
91
|
+
if (!(await adapter.isSupported())) return null
|
|
92
|
+
return adapter.permission()
|
|
93
|
+
},
|
|
94
|
+
success(entity, permission, api) {
|
|
95
|
+
if (!entity.isActive) return
|
|
96
|
+
if (permission === "granted") {
|
|
97
|
+
api.notify(`#${entity.id}:pushRegister`)
|
|
98
|
+
return
|
|
99
|
+
}
|
|
100
|
+
entity.isOpen = permission === "prompt" && !isDeclined()
|
|
101
|
+
},
|
|
102
|
+
error() {},
|
|
103
|
+
}),
|
|
104
|
+
|
|
105
|
+
// The prompt goes down as the system's dialog comes up. Dismissing the
|
|
106
|
+
// dialog leaves the permission undecided, so the next start asks again.
|
|
107
|
+
...handleAsync("pushAsk", {
|
|
108
|
+
start(entity) {
|
|
109
|
+
entity.isOpen = false
|
|
110
|
+
},
|
|
111
|
+
run() {
|
|
112
|
+
return adapter.request()
|
|
113
|
+
},
|
|
114
|
+
success(entity, permission, api) {
|
|
115
|
+
if (permission === "granted") api.notify(`#${entity.id}:pushRegister`)
|
|
116
|
+
},
|
|
117
|
+
error() {},
|
|
118
|
+
}),
|
|
119
|
+
|
|
120
|
+
pushDecline(entity) {
|
|
121
|
+
try {
|
|
122
|
+
window.localStorage.setItem(declineKey, "true")
|
|
123
|
+
} catch {
|
|
124
|
+
// Without storage the prompt comes back next start, nothing worse.
|
|
125
|
+
}
|
|
126
|
+
entity.isOpen = false
|
|
127
|
+
},
|
|
128
|
+
|
|
129
|
+
// Nothing on screen depends on it: a device that fails to register now
|
|
130
|
+
// tries again at the next start.
|
|
131
|
+
...handleAsync("pushRegister", {
|
|
132
|
+
run() {
|
|
133
|
+
return adapter.register()
|
|
134
|
+
},
|
|
135
|
+
error() {},
|
|
136
|
+
}),
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function navigate(url, api) {
|
|
141
|
+
api.notify("#router:navigate", url)
|
|
142
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { Api } from "./mount"
|
|
2
|
+
|
|
3
|
+
/** Everything vendor-specific about measuring an app. */
|
|
4
|
+
export interface AnalyticsAdapter {
|
|
5
|
+
/** Starts or stops measuring. Not called before the user answered. */
|
|
6
|
+
setConsent(isGranted: boolean): void | Promise<void>
|
|
7
|
+
/** One page or screen, on every route change once granted. */
|
|
8
|
+
pageView(page: { route?: string; path?: string }): void | Promise<void>
|
|
9
|
+
/** One of the app's own events. */
|
|
10
|
+
event(name: string, params?: Record<string, unknown>): void | Promise<void>
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface AnalyticsConfig {
|
|
14
|
+
adapter: AnalyticsAdapter
|
|
15
|
+
/** The localStorage key of the choice. Defaults to `"inglorious-analytics-consent"`. */
|
|
16
|
+
consentKey?: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface AnalyticsEntity {
|
|
20
|
+
id?: string
|
|
21
|
+
type: string
|
|
22
|
+
/** `null` until the user answered. */
|
|
23
|
+
consent: "granted" | "denied" | null
|
|
24
|
+
/** The app's own consent prompt is showing. */
|
|
25
|
+
isAsking: boolean
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type AnalyticsType = {
|
|
29
|
+
create(entity: AnalyticsEntity): void
|
|
30
|
+
/** A page view, from the router's broadcast, once granted. */
|
|
31
|
+
routeChange(
|
|
32
|
+
entity: AnalyticsEntity,
|
|
33
|
+
payload?: { route?: string; path?: string },
|
|
34
|
+
): void
|
|
35
|
+
/** The consent prompt's answer. */
|
|
36
|
+
analyticsConsent(entity: AnalyticsEntity, isGranted: boolean, api: Api): void
|
|
37
|
+
/** Brings the consent prompt back, to change the choice. */
|
|
38
|
+
analyticsAsk(entity: AnalyticsEntity): void
|
|
39
|
+
/** One of the app's own events, once granted. */
|
|
40
|
+
analyticsTrack(
|
|
41
|
+
entity: AnalyticsEntity,
|
|
42
|
+
payload: { name: string; params?: Record<string, unknown> },
|
|
43
|
+
): void
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The type that measures how an app is used, once the user said yes. */
|
|
47
|
+
export declare function createAnalytics(config: AnalyticsConfig): AnalyticsType
|
package/types/push.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Api } from "./mount"
|
|
2
|
+
|
|
3
|
+
export type PushPermission = "granted" | "denied" | "prompt"
|
|
4
|
+
|
|
5
|
+
/** Everything vendor-specific about pushes: the device, and where its token goes. */
|
|
6
|
+
export interface PushAdapter {
|
|
7
|
+
/** Whether this device can receive pushes at all. */
|
|
8
|
+
isSupported(): Promise<boolean>
|
|
9
|
+
permission(): Promise<PushPermission>
|
|
10
|
+
/**
|
|
11
|
+
* Brings up the system's dialog. In a browser it must call
|
|
12
|
+
* `Notification.requestPermission()` before it awaits anything.
|
|
13
|
+
*/
|
|
14
|
+
request(): Promise<PushPermission>
|
|
15
|
+
/** Gets the device's token and stores it for the signed-in user. */
|
|
16
|
+
register(): Promise<void>
|
|
17
|
+
/** Calls `listener(url)` when a push the system showed is tapped. */
|
|
18
|
+
onTap?(listener: (url: string) => void): void
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface PushPromptConfig {
|
|
22
|
+
adapter: PushAdapter
|
|
23
|
+
/** The localStorage key of a decline. Defaults to `"inglorious-push-declined"`. */
|
|
24
|
+
declineKey?: string
|
|
25
|
+
/** Opens a tapped push's `url`. Defaults to `#router:navigate`. */
|
|
26
|
+
onTap?: (url: string, api: Api) => void
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface PushPromptEntity {
|
|
30
|
+
id?: string
|
|
31
|
+
type: string
|
|
32
|
+
/** The app's own prompt is showing. */
|
|
33
|
+
isOpen: boolean
|
|
34
|
+
/** Between `pushStart` and `pushStop`: someone to register for. */
|
|
35
|
+
isActive: boolean
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export type PushPromptType = {
|
|
39
|
+
create(entity: PushPromptEntity, payload?: unknown, api?: Api): void
|
|
40
|
+
/** Checks the permission: registers if granted, else opens the prompt. */
|
|
41
|
+
pushStart(entity: PushPromptEntity, payload?: unknown, api?: Api): void
|
|
42
|
+
/** Closes the prompt. */
|
|
43
|
+
pushStop(entity: PushPromptEntity): void
|
|
44
|
+
/** The prompt's yes: brings up the system's dialog. */
|
|
45
|
+
pushAsk(entity: PushPromptEntity, payload?: unknown, api?: Api): void
|
|
46
|
+
/** The prompt's no, remembered on this device. */
|
|
47
|
+
pushDecline(entity: PushPromptEntity): void
|
|
48
|
+
[handler: string]: unknown
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The type that asks a device for pushes and keeps it registered. */
|
|
52
|
+
export declare function createPushPrompt(
|
|
53
|
+
config: PushPromptConfig,
|
|
54
|
+
): PushPromptType
|