@inglorious/web 6.12.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 +5 -1
- package/src/analytics/index.js +99 -0
- package/types/analytics.d.ts +47 -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"
|
|
@@ -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,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
|