@daybook/analytics 0.2.0 → 0.4.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 +66 -33
- package/LICENSE +21 -21
- package/README.md +536 -439
- package/dist/browser.js +99 -8
- package/dist/client.d.ts +32 -2
- package/dist/client.js +55 -12
- package/dist/context.d.ts +15 -0
- package/dist/context.js +85 -1
- package/dist/daybook.js +1 -1
- package/dist/index.d.ts +11 -1
- package/dist/index.js +10 -1
- package/dist/module.d.ts +17 -0
- package/dist/module.js +70 -0
- package/dist/storage.d.ts +11 -0
- package/dist/storage.js +40 -0
- package/dist/types.d.ts +23 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +85 -80
package/dist/module.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { DaybookAnalytics } from './client.js';
|
|
2
|
+
import type { AnalyticsOptions } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* What `Daybook.initialize` takes under `analytics`: every option of analytics but the ones the core
|
|
5
|
+
* holds. The key and the address are the core's, and consent is `trackingConsent`.
|
|
6
|
+
*/
|
|
7
|
+
export type AnalyticsModuleOptions = Omit<AnalyticsOptions, 'key' | 'optOutByDefault'>;
|
|
8
|
+
declare module '@daybook/core' {
|
|
9
|
+
interface Daybook {
|
|
10
|
+
/** Analytics' door: events, page views, the person's properties. From `@daybook/analytics`. */
|
|
11
|
+
readonly analytics: DaybookAnalytics;
|
|
12
|
+
}
|
|
13
|
+
interface DaybookOptions {
|
|
14
|
+
/** What only analytics needs: page views, where its state is kept, `beforeSend`. */
|
|
15
|
+
analytics?: AnalyticsModuleOptions;
|
|
16
|
+
}
|
|
17
|
+
}
|
package/dist/module.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { registerModule } from '@daybook/core';
|
|
2
|
+
import { DaybookAnalytics } from './client.js';
|
|
3
|
+
import { earlierState, storeFor } from './storage.js';
|
|
4
|
+
/**
|
|
5
|
+
* Analytics as a module of Daybook's core (PM-R-0110, PM-T-0971): importing `@daybook/analytics`
|
|
6
|
+
* registers it, `Daybook.initialize` starts it with the site's one key, and `daybook.analytics` is the
|
|
7
|
+
* same object `init({ key })` used to answer.
|
|
8
|
+
*/
|
|
9
|
+
class AnalyticsModule {
|
|
10
|
+
constructor() {
|
|
11
|
+
this.name = 'analytics';
|
|
12
|
+
/** First: it owns the anonymous id the other modules may send. */
|
|
13
|
+
this.order = 10;
|
|
14
|
+
this.client = null;
|
|
15
|
+
this.sending = false;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Before the family, analytics kept the visitor by itself, under its hub key's own name. A site
|
|
19
|
+
* that has that keeps the visitor's id and the answer they gave about being measured.
|
|
20
|
+
*/
|
|
21
|
+
legacy(options) {
|
|
22
|
+
if (typeof window === 'undefined' || typeof document === 'undefined')
|
|
23
|
+
return null;
|
|
24
|
+
const o = (options ?? {});
|
|
25
|
+
const earlier = earlierState(storeFor(o.persistence ?? 'localStorage', o.cookieDomain), '');
|
|
26
|
+
if (!earlier)
|
|
27
|
+
return null;
|
|
28
|
+
return {
|
|
29
|
+
visitorId: typeof earlier.anon === 'string' ? earlier.anon : undefined,
|
|
30
|
+
trackingConsent: earlier.consent === 'in' ? true : earlier.consent === 'out' ? false : undefined,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
start(context) {
|
|
34
|
+
if (context.key === '')
|
|
35
|
+
throw new Error('there is no key to send events with');
|
|
36
|
+
const o = (context.options.analytics ?? {});
|
|
37
|
+
const client = new DaybookAnalytics({ ...o, key: context.key, apiUrl: o.apiUrl ?? context.serverUrl, debug: o.debug ?? context.log.enabled }, {
|
|
38
|
+
consent: () => context.trackingConsent,
|
|
39
|
+
answered: () => context.trackingConsentAnswered,
|
|
40
|
+
answer: (granted) => context.setTrackingConsent(granted),
|
|
41
|
+
anonymousId: context.anonymousId,
|
|
42
|
+
onAnonymousId: (id) => { context.anonymousId = id; },
|
|
43
|
+
});
|
|
44
|
+
this.client = client;
|
|
45
|
+
this.sending = client.sending;
|
|
46
|
+
context.anonymousId = client.distinctId;
|
|
47
|
+
// The site may already know who is signed in when it starts.
|
|
48
|
+
if (context.user && client.userId !== context.user.id)
|
|
49
|
+
this.identify(context.user);
|
|
50
|
+
return client;
|
|
51
|
+
}
|
|
52
|
+
identify(user) {
|
|
53
|
+
const person = {};
|
|
54
|
+
if (user.email)
|
|
55
|
+
person.$email = user.email;
|
|
56
|
+
if (user.name)
|
|
57
|
+
person.$name = user.name;
|
|
58
|
+
this.client?.identify(user.id, Object.keys(person).length > 0 ? person : undefined);
|
|
59
|
+
}
|
|
60
|
+
reset() {
|
|
61
|
+
this.client?.reset();
|
|
62
|
+
}
|
|
63
|
+
setTrackingConsent() {
|
|
64
|
+
// The core has changed its answer already, so how it was before is remembered here.
|
|
65
|
+
const before = this.sending;
|
|
66
|
+
this.sending = this.client?.sending === true;
|
|
67
|
+
this.client?.consented(before);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
registerModule('analytics', () => new AnalyticsModule());
|
package/dist/storage.d.ts
CHANGED
|
@@ -4,6 +4,8 @@ export interface Store {
|
|
|
4
4
|
get(name: string): string | null;
|
|
5
5
|
set(name: string, value: string): void;
|
|
6
6
|
remove(name: string): void;
|
|
7
|
+
/** Every name kept here, so a state written under another key can be found (PM-T-0971). */
|
|
8
|
+
names(): string[];
|
|
7
9
|
}
|
|
8
10
|
/** What the SDK remembers about this browser between page loads. */
|
|
9
11
|
export interface State {
|
|
@@ -42,3 +44,12 @@ export declare function cookieStore(domain?: string): Store | null;
|
|
|
42
44
|
export declare function storeFor(persistence: Persistence, cookieDomain?: string): Store;
|
|
43
45
|
/** A stored state, or null when there is none or it is not one. */
|
|
44
46
|
export declare function readState(store: Store, name: string): Partial<State> | null;
|
|
47
|
+
/**
|
|
48
|
+
* The state this browser kept under another key of the same site, newest first by its session: what a
|
|
49
|
+
* site that started analytics with its hub key (`dak_…`) has when it moves to the site's one key
|
|
50
|
+
* (`dbk_…`). The stored name carries the key's first letters, so a new key is a new name, and without
|
|
51
|
+
* this the visitor would be somebody new (PM-T-0971).
|
|
52
|
+
*/
|
|
53
|
+
export declare function earlierState(store: Store, ownName: string): Partial<State> | null;
|
|
54
|
+
/** The name a key's state is kept under: two projects on one site keep apart by the key's first letters. */
|
|
55
|
+
export declare function stateNameOf(key: string): string;
|
package/dist/storage.js
CHANGED
|
@@ -4,6 +4,7 @@ export function memoryStore() {
|
|
|
4
4
|
get: (name) => values.get(name) ?? null,
|
|
5
5
|
set: (name, value) => void values.set(name, value),
|
|
6
6
|
remove: (name) => void values.delete(name),
|
|
7
|
+
names: () => [...values.keys()],
|
|
7
8
|
};
|
|
8
9
|
}
|
|
9
10
|
/**
|
|
@@ -43,6 +44,14 @@ export function webStore(kind) {
|
|
|
43
44
|
}
|
|
44
45
|
catch { /* blocked */ }
|
|
45
46
|
},
|
|
47
|
+
names: () => {
|
|
48
|
+
try {
|
|
49
|
+
return Array.from({ length: storage.length }, (_, i) => storage.key(i)).filter((n) => typeof n === 'string');
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
},
|
|
46
55
|
};
|
|
47
56
|
}
|
|
48
57
|
/**
|
|
@@ -77,6 +86,14 @@ export function cookieStore(domain) {
|
|
|
77
86
|
}
|
|
78
87
|
catch { /* blocked */ }
|
|
79
88
|
},
|
|
89
|
+
names: () => {
|
|
90
|
+
try {
|
|
91
|
+
return document.cookie.split('; ').map((c) => c.split('=')[0] ?? '').filter(Boolean);
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
return [];
|
|
95
|
+
}
|
|
96
|
+
},
|
|
80
97
|
};
|
|
81
98
|
}
|
|
82
99
|
/** The store a persistence choice gets, falling back to memory where the browser refuses it. */
|
|
@@ -100,3 +117,26 @@ export function readState(store, name) {
|
|
|
100
117
|
return null;
|
|
101
118
|
}
|
|
102
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* The state this browser kept under another key of the same site, newest first by its session: what a
|
|
122
|
+
* site that started analytics with its hub key (`dak_…`) has when it moves to the site's one key
|
|
123
|
+
* (`dbk_…`). The stored name carries the key's first letters, so a new key is a new name, and without
|
|
124
|
+
* this the visitor would be somebody new (PM-T-0971).
|
|
125
|
+
*/
|
|
126
|
+
export function earlierState(store, ownName) {
|
|
127
|
+
let found = null;
|
|
128
|
+
for (const name of store.names()) {
|
|
129
|
+
if (name === ownName || !/^dbk_[A-Za-z0-9]{1,8}$/.test(name))
|
|
130
|
+
continue;
|
|
131
|
+
const state = readState(store, name);
|
|
132
|
+
if (!state || (typeof state.anon !== 'string' && state.consent === undefined))
|
|
133
|
+
continue;
|
|
134
|
+
if (!found || (state.session?.last ?? 0) > (found.session?.last ?? 0))
|
|
135
|
+
found = state;
|
|
136
|
+
}
|
|
137
|
+
return found;
|
|
138
|
+
}
|
|
139
|
+
/** The name a key's state is kept under: two projects on one site keep apart by the key's first letters. */
|
|
140
|
+
export function stateNameOf(key) {
|
|
141
|
+
return `dbk_${key.trim().replace(/^dak_/, '').slice(0, 8)}`;
|
|
142
|
+
}
|
package/dist/types.d.ts
CHANGED
|
@@ -16,6 +16,25 @@ export interface EventContext {
|
|
|
16
16
|
device_model?: string;
|
|
17
17
|
browser?: string;
|
|
18
18
|
locale?: string;
|
|
19
|
+
/** Every language the person prefers, in their order, comma-separated: `pl-PL,en-US`. */
|
|
20
|
+
languages?: string;
|
|
21
|
+
/** The device's time zone setting, IANA: `Europe/Warsaw`. */
|
|
22
|
+
timezone?: string;
|
|
23
|
+
/** The calendar the person's dates are written in: `gregory`, `buddhist`, `islamic`… */
|
|
24
|
+
calendar?: string;
|
|
25
|
+
/** The digits their numbers are written with: `latn`, `arab`… */
|
|
26
|
+
numbering_system?: string;
|
|
27
|
+
/** A 12- or 24-hour clock: `h12`, `h23`. */
|
|
28
|
+
hour_cycle?: string;
|
|
29
|
+
/** The screen, in CSS pixels. */
|
|
30
|
+
screen_width?: number;
|
|
31
|
+
screen_height?: number;
|
|
32
|
+
/** Device pixels to a CSS pixel: 2 on most phones and Retina screens. */
|
|
33
|
+
pixel_ratio?: number;
|
|
34
|
+
/** `light` or `dark`, as the person prefers. */
|
|
35
|
+
color_scheme?: string;
|
|
36
|
+
/** The connection as the browser rates it, where it says: `4g`, `3g`, `2g`, `slow-2g`. */
|
|
37
|
+
network?: string;
|
|
19
38
|
}
|
|
20
39
|
/**
|
|
21
40
|
* One event exactly as it is sent to `/analytics/v1/batch`, which is also what `beforeSend` is given.
|
|
@@ -38,7 +57,10 @@ export interface AnalyticsEvent {
|
|
|
38
57
|
/** Where the SDK keeps the anonymous id, the session, registered properties and the consent choice. */
|
|
39
58
|
export type Persistence = 'localStorage' | 'cookie' | 'memory';
|
|
40
59
|
export interface AnalyticsOptions {
|
|
41
|
-
/**
|
|
60
|
+
/**
|
|
61
|
+
* The project's ingest key, `dak_…`, from Analytics → Settings, or the site's own key, `dbk_…`, from
|
|
62
|
+
* the project's settings. Either is public: it only says which project events are for.
|
|
63
|
+
*/
|
|
42
64
|
key: string;
|
|
43
65
|
/** Where Daybook's API is. Only for a self-hosted Daybook or a proxy of your own. */
|
|
44
66
|
apiUrl?: string;
|
package/dist/version.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* of the SDK its pages run. A constant rather than read from package.json at build time, because the
|
|
4
4
|
* npm build is plain tsc; test/version.test.js refuses a build where the two disagree.
|
|
5
5
|
*/
|
|
6
|
-
export declare const SDK_VERSION = "0.
|
|
6
|
+
export declare const SDK_VERSION = "0.4.0";
|
|
7
7
|
/**
|
|
8
8
|
* What the API records as every event's `source`, and one of the names it knows as a Daybook SDK
|
|
9
9
|
* (PM-T-0528): only those may send `$pageview` and `$pageleave`.
|
package/dist/version.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* of the SDK its pages run. A constant rather than read from package.json at build time, because the
|
|
4
4
|
* npm build is plain tsc; test/version.test.js refuses a build where the two disagree.
|
|
5
5
|
*/
|
|
6
|
-
export const SDK_VERSION = '0.
|
|
6
|
+
export const SDK_VERSION = '0.4.0';
|
|
7
7
|
/**
|
|
8
8
|
* What the API records as every event's `source`, and one of the names it knows as a Daybook SDK
|
|
9
9
|
* (PM-T-0528): only those may send `$pageview` and `$pageleave`.
|
package/package.json
CHANGED
|
@@ -1,80 +1,85 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@daybook/analytics",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Product analytics for the web: page views, sessions, attribution and your own events, sent to Daybook's Analytics hub. Plain JS, React and Vue.",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "dist/index.js",
|
|
7
|
-
"types": "dist/index.d.ts",
|
|
8
|
-
"unpkg": "dist/daybook.js",
|
|
9
|
-
"jsdelivr": "dist/daybook.js",
|
|
10
|
-
"files": [
|
|
11
|
-
"dist",
|
|
12
|
-
"README.md",
|
|
13
|
-
"CHANGELOG.md"
|
|
14
|
-
],
|
|
15
|
-
"scripts": {
|
|
16
|
-
"build": "tsc -p tsconfig.json && npm run bundle",
|
|
17
|
-
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
18
|
-
"test": "node --test test/*.test.js",
|
|
19
|
-
"bundle": "esbuild
|
|
20
|
-
"prepublishOnly": "npm run build && npm test",
|
|
21
|
-
"release": "node ../../devops/publish-sdk.mjs analytics"
|
|
22
|
-
},
|
|
23
|
-
"peerDependencies": {
|
|
24
|
-
"react": ">=17",
|
|
25
|
-
"vue": ">=3.3"
|
|
26
|
-
},
|
|
27
|
-
"peerDependenciesMeta": {
|
|
28
|
-
"react": {
|
|
29
|
-
"optional": true
|
|
30
|
-
},
|
|
31
|
-
"vue": {
|
|
32
|
-
"optional": true
|
|
33
|
-
}
|
|
34
|
-
},
|
|
35
|
-
"devDependencies": {
|
|
36
|
-
"@types/react": "^19.3.0",
|
|
37
|
-
"esbuild": "^0.28.2",
|
|
38
|
-
"react": "^19.3.1",
|
|
39
|
-
"react-dom": "^19.3.1",
|
|
40
|
-
"typescript": "^5.7.2",
|
|
41
|
-
"vue": "^3.5.13"
|
|
42
|
-
},
|
|
43
|
-
"allowScripts": {
|
|
44
|
-
"esbuild@0.28.2": true
|
|
45
|
-
},
|
|
46
|
-
"exports": {
|
|
47
|
-
".": {
|
|
48
|
-
"types": "./dist/index.d.ts",
|
|
49
|
-
"import": "./dist/index.js"
|
|
50
|
-
},
|
|
51
|
-
"./react": {
|
|
52
|
-
"types": "./dist/react.d.ts",
|
|
53
|
-
"import": "./dist/react.js"
|
|
54
|
-
},
|
|
55
|
-
"./vue": {
|
|
56
|
-
"types": "./dist/vue.d.ts",
|
|
57
|
-
"import": "./dist/vue.js"
|
|
58
|
-
},
|
|
59
|
-
"./browser": "./dist/daybook.js"
|
|
60
|
-
},
|
|
61
|
-
"sideEffects": [
|
|
62
|
-
"./dist/
|
|
63
|
-
"./dist/
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
"
|
|
74
|
-
"
|
|
75
|
-
"
|
|
76
|
-
"
|
|
77
|
-
"
|
|
78
|
-
"
|
|
79
|
-
|
|
80
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@daybook/analytics",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Product analytics for the web: page views, sessions, attribution and your own events, sent to Daybook's Analytics hub. Plain JS, React and Vue.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"unpkg": "dist/daybook.js",
|
|
9
|
+
"jsdelivr": "dist/daybook.js",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist",
|
|
12
|
+
"README.md",
|
|
13
|
+
"CHANGELOG.md"
|
|
14
|
+
],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsc -p ../core/tsconfig.json && tsc -p tsconfig.json && npm run bundle",
|
|
17
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
18
|
+
"test": "node --test test/*.test.js",
|
|
19
|
+
"bundle": "esbuild dist/browser.js --bundle --format=iife --minify --target=es2019 --outfile=dist/daybook.js",
|
|
20
|
+
"prepublishOnly": "npm run build && npm test",
|
|
21
|
+
"release": "node ../../devops/publish-sdk.mjs analytics"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"react": ">=17",
|
|
25
|
+
"vue": ">=3.3"
|
|
26
|
+
},
|
|
27
|
+
"peerDependenciesMeta": {
|
|
28
|
+
"react": {
|
|
29
|
+
"optional": true
|
|
30
|
+
},
|
|
31
|
+
"vue": {
|
|
32
|
+
"optional": true
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/react": "^19.3.0",
|
|
37
|
+
"esbuild": "^0.28.2",
|
|
38
|
+
"react": "^19.3.1",
|
|
39
|
+
"react-dom": "^19.3.1",
|
|
40
|
+
"typescript": "^5.7.2",
|
|
41
|
+
"vue": "^3.5.13"
|
|
42
|
+
},
|
|
43
|
+
"allowScripts": {
|
|
44
|
+
"esbuild@0.28.2": true
|
|
45
|
+
},
|
|
46
|
+
"exports": {
|
|
47
|
+
".": {
|
|
48
|
+
"types": "./dist/index.d.ts",
|
|
49
|
+
"import": "./dist/index.js"
|
|
50
|
+
},
|
|
51
|
+
"./react": {
|
|
52
|
+
"types": "./dist/react.d.ts",
|
|
53
|
+
"import": "./dist/react.js"
|
|
54
|
+
},
|
|
55
|
+
"./vue": {
|
|
56
|
+
"types": "./dist/vue.d.ts",
|
|
57
|
+
"import": "./dist/vue.js"
|
|
58
|
+
},
|
|
59
|
+
"./browser": "./dist/daybook.js"
|
|
60
|
+
},
|
|
61
|
+
"sideEffects": [
|
|
62
|
+
"./dist/index.js",
|
|
63
|
+
"./dist/module.js",
|
|
64
|
+
"./dist/browser.js",
|
|
65
|
+
"./dist/daybook.js"
|
|
66
|
+
],
|
|
67
|
+
"license": "MIT",
|
|
68
|
+
"publishConfig": {
|
|
69
|
+
"access": "public"
|
|
70
|
+
},
|
|
71
|
+
"homepage": "https://daybook.team",
|
|
72
|
+
"keywords": [
|
|
73
|
+
"analytics",
|
|
74
|
+
"product-analytics",
|
|
75
|
+
"events",
|
|
76
|
+
"pageviews",
|
|
77
|
+
"react",
|
|
78
|
+
"vue",
|
|
79
|
+
"nextjs",
|
|
80
|
+
"nuxt"
|
|
81
|
+
],
|
|
82
|
+
"dependencies": {
|
|
83
|
+
"@daybook/core": "^0.1.0"
|
|
84
|
+
}
|
|
85
|
+
}
|