@kk-forge/artel-collector 0.7.0 → 0.8.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/README.md +37 -1
- package/dist/infra/exports.d.ts +1 -1
- package/dist/infra/exports.js +1 -1
- package/dist/infra/web.d.ts +11 -2
- package/dist/infra/web.js +38 -9
- package/dist/shared/collector.js +4 -1
- package/dist/shared/page.d.ts +11 -1
- package/dist/shared/page.js +28 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -223,7 +223,8 @@ An extension imports `infra/chrome` only in its entrypoints, the scripts that st
|
|
|
223
223
|
- `newLocalStorageAdapter()` returns a `storage` over `localStorage`. Every script of the origin can read it, the queued events with their `id`, the last id and the install id included, and no other storage of the origin hides them from its scripts. A site that runs untrusted scripts uses an anonymous channel.
|
|
224
224
|
- `newWebLock()` returns a `lock` over the Web Lock named `artel`, shared by the tabs and workers of one origin. Without Web Locks, operations run unguarded.
|
|
225
225
|
- `pageFields()` gives the page's address without its query string and fragment, and `document.referrer`. `newWebCollector`'s default `getPage` takes it, and the collector shapes both as "Options" describes under `getPage`.
|
|
226
|
-
- `trackPageViews(collector)` sends `page_view` for the page now and for each later navigation: `history.pushState` and `replaceState`, `popstate`, and the Navigation API's `currententrychange` where the browser has it.
|
|
226
|
+
- `trackPageViews(collector)` sends `page_view` for the page now and for each later navigation: `history.pushState` and `replaceState`, `popstate`, `hashchange`, and the Navigation API's `currententrychange` where the browser has it. By the HTML standard a change of the hash, from a link or from setting `location.hash`, fires `popstate` and then `hashchange`. The tracker listens to `hashchange` as well, for a browser that skips the `popstate`. A navigation sends a `page_view` only when it leads to another page, the page the collector would send, as `getPage` gives it. So a navigation that several of these events report sends one. With the default `getPage` the page is the path, so a change of the query or the hash alone sends none. A router that keeps its routes in the hash passes a `getPage` that reads the route from the hash, as below, and gets a `page_view` for each route. Where the collector reads no page, as on another platform than `web`, or `getPage` gives none, the page is `location.pathname`. The trackers of one collector and its calls of `trackPageView` share the last page, so two trackers send one `page_view` per navigation, and a navigation back to the page a call left sends one. They count a page whether or not the collector records its `page_view`, so a page loaded in the `opted_out` state sends a `page_view` at the first navigation to another page after the user leaves that state. A tracker sends the page it starts on even when another tracker of the collector counted it, and the collector drops it if its last recorded `page_view` sent that page. To read the page, the tracker calls `getPage` at each of these events, and `trackPageView` calls it once more than the `page_view` itself does. Leaving `opted_out` sends none by itself. `page_view` is a system event, which the intake counts without the product's allowlist. It returns the function that stops it. Once every tracker stopped, in any order, `history` is as they found it: a method the page inherited from `History.prototype` is inherited again. A product that wraps a method after the tracker keeps its wrapper, and the stopped tracker's wrapper under it stays and only passes calls on.
|
|
227
|
+
- `trackPageView(collector)` sends `page_view` for the page `getPage` gives now, unless the last recorded `page_view` sent that page, whether a navigation or a call sent it. A router that changes the screen without changing the address calls it after each change, with a `getPage` that reads the screen. So does a product that decides itself when a view counts. It works with or without `trackPageViews`. It resolves once the collector queued or dropped the event, and never rejects.
|
|
227
228
|
|
|
228
229
|
`newWebCollector` needs `localStorage`, which a Web Worker lacks, so a worker passes its own `storage`. Its default `getPage` sends no page where there is no `document` or `location`.
|
|
229
230
|
|
|
@@ -236,6 +237,41 @@ const analytics = newWebCollector({ key, version, endpoint, mode: "anonymous" })
|
|
|
236
237
|
trackPageViews(analytics);
|
|
237
238
|
```
|
|
238
239
|
|
|
240
|
+
A web app that keeps its routes in the hash, such as `#/ideas/123`, sends its route pattern as the page. The pattern has `:id` in place of the record's number and leaves out the query, so a move between records of one route sends no `page_view`. A hash can carry a secret, as an OAuth redirect's `#access_token=…` does, so this `getPage` sends only the routes in its list and `/other` for any other hash:
|
|
241
|
+
|
|
242
|
+
```ts
|
|
243
|
+
const routes = new Set(["/", "/ideas", "/ideas/:id", "/projects", "/projects/:id"]);
|
|
244
|
+
|
|
245
|
+
function routeOf(hash: string): string {
|
|
246
|
+
const route = `/${hash.replace(/^#\/?/, "").split("?")[0] ?? ""}`.replaceAll(/\/\d+/g, "/:id");
|
|
247
|
+
return routes.has(route) ? route : "/other";
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const analytics = newWebCollector({
|
|
251
|
+
key,
|
|
252
|
+
version,
|
|
253
|
+
endpoint,
|
|
254
|
+
mode: "anonymous",
|
|
255
|
+
getPage: () => ({ page: routeOf(location.hash), referrer: document.referrer }),
|
|
256
|
+
});
|
|
257
|
+
trackPageViews(analytics);
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
A web app that changes its screen without changing the address reads the screen in `getPage` and calls `trackPageView` after each change:
|
|
261
|
+
|
|
262
|
+
```ts
|
|
263
|
+
import { newWebCollector, trackPageView } from "@kk-forge/artel-collector/infra";
|
|
264
|
+
|
|
265
|
+
const analytics = newWebCollector({
|
|
266
|
+
key,
|
|
267
|
+
version,
|
|
268
|
+
endpoint,
|
|
269
|
+
mode: "anonymous",
|
|
270
|
+
getPage: () => ({ page: `/${router.screen}`, referrer: document.referrer }),
|
|
271
|
+
});
|
|
272
|
+
router.afterEach(() => trackPageView(analytics));
|
|
273
|
+
```
|
|
274
|
+
|
|
239
275
|
### Chrome extension
|
|
240
276
|
|
|
241
277
|
`@kk-forge/artel-collector/infra/chrome` exports the code for a Chrome extension. The extension's service worker and its pages each make a collector with `newExtensionCollector`. They share `chrome.storage.local` and Web Locks, so they keep one queue and one state.
|
package/dist/infra/exports.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { newCollector, newHttpTransport } from "./systemCollector.js";
|
|
2
2
|
export { systemClock } from "./system.js";
|
|
3
|
-
export { newLocalStorageAdapter, newWebCollector, newWebLock, pageFields, trackPageViews, type WebCollectorOptions, } from "./web.js";
|
|
3
|
+
export { newLocalStorageAdapter, newWebCollector, newWebLock, pageFields, trackPageView, trackPageViews, type WebCollectorOptions, } from "./web.js";
|
|
4
4
|
export { newDesktopCollector, type DesktopCollectorOptions } from "./desktop.js";
|
package/dist/infra/exports.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { newCollector, newHttpTransport } from "./systemCollector.js";
|
|
2
2
|
export { systemClock } from "./system.js";
|
|
3
|
-
export { newLocalStorageAdapter, newWebCollector, newWebLock, pageFields, trackPageViews, } from "./web.js";
|
|
3
|
+
export { newLocalStorageAdapter, newWebCollector, newWebLock, pageFields, trackPageView, trackPageViews, } from "./web.js";
|
|
4
4
|
export { newDesktopCollector } from "./desktop.js";
|
package/dist/infra/web.d.ts
CHANGED
|
@@ -41,7 +41,16 @@ export declare function newWebLock(): Lock;
|
|
|
41
41
|
*/
|
|
42
42
|
export declare function pageFields(): PageFields;
|
|
43
43
|
/**
|
|
44
|
-
* Sends `page_view` for the current page and after each navigation
|
|
45
|
-
*
|
|
44
|
+
* Sends `page_view` for the current page and after each navigation to another page, and returns the function that
|
|
45
|
+
* stops it. The page is the one the collector would send, as its `getPage` gives it: with the default `getPage` the
|
|
46
|
+
* path, so a change of the query or the hash alone sends none, and with a `getPage` that reads a route from the hash, the
|
|
47
|
+
* route. Where the collector reads no page, as on another platform than web, or `getPage` gives none, the page is
|
|
48
|
+
* `location.pathname`. Once every tracker stops, `history` is as they found it.
|
|
46
49
|
*/
|
|
47
50
|
export declare function trackPageViews(collector: Collector): () => void;
|
|
51
|
+
/**
|
|
52
|
+
* Sends `page_view` for the page `getPage` gives now, unless the last recorded `page_view` sent that page. A router
|
|
53
|
+
* that changes the screen without changing the address calls it after each change, with a `getPage` that reads the
|
|
54
|
+
* screen. It resolves once the collector has queued or dropped the event, and never rejects.
|
|
55
|
+
*/
|
|
56
|
+
export declare function trackPageView(collector: Collector): Promise<void>;
|
package/dist/infra/web.js
CHANGED
|
@@ -2,6 +2,7 @@ import { detectBrowser } from "../shared/browser.js";
|
|
|
2
2
|
import { newCollector } from "./systemCollector.js";
|
|
3
3
|
import { Mode, } from "../shared/collectorTypes.js";
|
|
4
4
|
import { PAGE_VIEW, Platform } from "../shared/envelope.js";
|
|
5
|
+
import { currentPage } from "../shared/page.js";
|
|
5
6
|
export function newWebCollector(options) {
|
|
6
7
|
const defaults = {
|
|
7
8
|
platform: Platform.Web,
|
|
@@ -77,32 +78,60 @@ function pageFieldsWherePageIs() {
|
|
|
77
78
|
: pageFields();
|
|
78
79
|
}
|
|
79
80
|
/**
|
|
80
|
-
* Sends `page_view` for the current page and after each navigation
|
|
81
|
-
*
|
|
81
|
+
* Sends `page_view` for the current page and after each navigation to another page, and returns the function that
|
|
82
|
+
* stops it. The page is the one the collector would send, as its `getPage` gives it: with the default `getPage` the
|
|
83
|
+
* path, so a change of the query or the hash alone sends none, and with a `getPage` that reads a route from the hash, the
|
|
84
|
+
* route. Where the collector reads no page, as on another platform than web, or `getPage` gives none, the page is
|
|
85
|
+
* `location.pathname`. Once every tracker stops, `history` is as they found it.
|
|
82
86
|
*/
|
|
83
87
|
export function trackPageViews(collector) {
|
|
84
|
-
let lastPath;
|
|
85
88
|
const onNavigation = () => {
|
|
86
|
-
//
|
|
87
|
-
|
|
88
|
-
|
|
89
|
+
// One navigation can reach the tracker up to three times: as a `pushState` and a `currententrychange`, or as a
|
|
90
|
+
// `popstate`, a `hashchange` and a `currententrychange`. All of them find the same page.
|
|
91
|
+
if (pageNow(collector) !== trackedPages.get(collector)) {
|
|
92
|
+
void trackPageView(collector);
|
|
89
93
|
}
|
|
90
|
-
lastPath = location.pathname;
|
|
91
|
-
void collector.track(PAGE_VIEW);
|
|
92
94
|
};
|
|
93
95
|
const unwrapPushState = callAfter("pushState", onNavigation);
|
|
94
96
|
const unwrapReplaceState = callAfter("replaceState", onNavigation);
|
|
97
|
+
// By the HTML standard, a browser reports a change of the hash as a `popstate` and then a `hashchange`. The tracker
|
|
98
|
+
// listens to `hashchange` as well, so a change still counts in a browser that skips the `popstate`.
|
|
95
99
|
window.addEventListener("popstate", onNavigation);
|
|
100
|
+
window.addEventListener("hashchange", onNavigation);
|
|
96
101
|
const navigation = navigationApi();
|
|
97
102
|
navigation?.addEventListener("currententrychange", onNavigation);
|
|
98
|
-
|
|
103
|
+
// The page it starts on counts even when an earlier tracker of the collector counted it, as one stopped and started
|
|
104
|
+
// again after the user left `opted_out`. The collector drops it if its last recorded `page_view` sent that page.
|
|
105
|
+
void trackPageView(collector);
|
|
99
106
|
return () => {
|
|
100
107
|
unwrapPushState();
|
|
101
108
|
unwrapReplaceState();
|
|
102
109
|
window.removeEventListener("popstate", onNavigation);
|
|
110
|
+
window.removeEventListener("hashchange", onNavigation);
|
|
103
111
|
navigation?.removeEventListener("currententrychange", onNavigation);
|
|
104
112
|
};
|
|
105
113
|
}
|
|
114
|
+
/**
|
|
115
|
+
* Sends `page_view` for the page `getPage` gives now, unless the last recorded `page_view` sent that page. A router
|
|
116
|
+
* that changes the screen without changing the address calls it after each change, with a `getPage` that reads the
|
|
117
|
+
* screen. It resolves once the collector has queued or dropped the event, and never rejects.
|
|
118
|
+
*/
|
|
119
|
+
export function trackPageView(collector) {
|
|
120
|
+
trackedPages.set(collector, pageNow(collector));
|
|
121
|
+
return collector.track(PAGE_VIEW);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* The page of the last `page_view` each collector was asked for, whether or not it recorded one. The trackers of a
|
|
125
|
+
* collector and its calls of `trackPageView` share it, so a navigation back to a page after a call counts.
|
|
126
|
+
*/
|
|
127
|
+
const trackedPages = new WeakMap();
|
|
128
|
+
/**
|
|
129
|
+
* Returns the page a `page_view` of `collector` would send now, or `location.pathname` where it reads none, or
|
|
130
|
+
* undefined where there is no `location`.
|
|
131
|
+
*/
|
|
132
|
+
function pageNow(collector) {
|
|
133
|
+
return (currentPage(collector) ?? (typeof location === "undefined" ? undefined : location.pathname));
|
|
134
|
+
}
|
|
106
135
|
/** The wrappers `callAfter` installed, by the wrapper function. */
|
|
107
136
|
const wrappings = new WeakMap();
|
|
108
137
|
/**
|
package/dist/shared/collector.js
CHANGED
|
@@ -8,7 +8,7 @@ import { acceptedEvent } from "./eventCheck.js";
|
|
|
8
8
|
import { newInstallState } from "./installState.js";
|
|
9
9
|
import { exclusive } from "./lock.js";
|
|
10
10
|
import { newOutbox } from "./outbox.js";
|
|
11
|
-
import { newPageReader } from "./page.js";
|
|
11
|
+
import { newPageReader, notePageReader } from "./page.js";
|
|
12
12
|
import { carriesPage, channelTakes, fieldsForState, isAnalyticsState, } from "./stateFields.js";
|
|
13
13
|
import { newThrottle } from "./throttle.js";
|
|
14
14
|
import { transportsOf } from "./transport.js";
|
|
@@ -375,6 +375,9 @@ export function newCollectorOn({ host, options, onNewDay, }) {
|
|
|
375
375
|
};
|
|
376
376
|
}, undefined),
|
|
377
377
|
};
|
|
378
|
+
if (pages !== undefined) {
|
|
379
|
+
notePageReader(collector, pages);
|
|
380
|
+
}
|
|
378
381
|
void sendWhatIsLeft();
|
|
379
382
|
return collector;
|
|
380
383
|
}
|
package/dist/shared/page.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { PageFields } from "./collectorTypes.js";
|
|
1
|
+
import type { Collector, PageFields } from "./collectorTypes.js";
|
|
2
2
|
/** What `getPage` gave when a method was called, shaped as the collector sends it. */
|
|
3
3
|
export interface PageRead {
|
|
4
4
|
name: string;
|
|
@@ -17,7 +17,17 @@ interface PageView {
|
|
|
17
17
|
export interface PageReader {
|
|
18
18
|
read(name: string): PageRead;
|
|
19
19
|
view(read: PageRead): PageView;
|
|
20
|
+
/** Returns the page a `page_view` would send now, or undefined for none. It reports nothing: the event reports it. */
|
|
21
|
+
pageNow(): string | undefined;
|
|
20
22
|
}
|
|
23
|
+
/** Notes `reader` as the page reader of `collector`, for `currentPage`. */
|
|
24
|
+
export declare function notePageReader(collector: Collector, reader: PageReader): void;
|
|
25
|
+
/**
|
|
26
|
+
* Returns the page a `page_view` of `collector` would send now, as its `getPage` gives it. It returns undefined for a
|
|
27
|
+
* collector that reads no page, as one of another platform than web or a product's own `Collector`, and where `getPage`
|
|
28
|
+
* gives no page it can read.
|
|
29
|
+
*/
|
|
30
|
+
export declare function currentPage(collector: Collector): string | undefined;
|
|
21
31
|
/**
|
|
22
32
|
* Returns the reader of an event's page fields from `getPage`. It sends the referrer only with the first recorded
|
|
23
33
|
* `page_view`, as the referrer belongs to the page load. It sends nothing for a `page_view` that repeats the last page.
|
package/dist/shared/page.js
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
import { PAGE_VIEW } from "./envelope.js";
|
|
2
2
|
import { pagePath, referrerHost } from "./pagePath.js";
|
|
3
|
+
/** The page reader of each collector that reads pages, for `currentPage`. */
|
|
4
|
+
const readers = new WeakMap();
|
|
5
|
+
/** Notes `reader` as the page reader of `collector`, for `currentPage`. */
|
|
6
|
+
export function notePageReader(collector, reader) {
|
|
7
|
+
readers.set(collector, reader);
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Returns the page a `page_view` of `collector` would send now, as its `getPage` gives it. It returns undefined for a
|
|
11
|
+
* collector that reads no page, as one of another platform than web or a product's own `Collector`, and where `getPage`
|
|
12
|
+
* gives no page it can read.
|
|
13
|
+
*/
|
|
14
|
+
export function currentPage(collector) {
|
|
15
|
+
return readers.get(collector)?.pageNow();
|
|
16
|
+
}
|
|
3
17
|
/**
|
|
4
18
|
* Returns the reader of an event's page fields from `getPage`. It sends the referrer only with the first recorded
|
|
5
19
|
* `page_view`, as the referrer belongs to the page load. It sends nothing for a `page_view` that repeats the last page.
|
|
@@ -8,15 +22,15 @@ import { pagePath, referrerHost } from "./pagePath.js";
|
|
|
8
22
|
export function newPageReader({ getPage, reportError, }) {
|
|
9
23
|
let isPageViewRecorded = false;
|
|
10
24
|
let lastViewedPage;
|
|
11
|
-
function fieldsOf(name, mayCarryReferrer) {
|
|
25
|
+
function fieldsOf({ name, mayCarryReferrer, report, }) {
|
|
12
26
|
// A host outside TypeScript can return any value, or fields whose getters throw.
|
|
13
27
|
try {
|
|
14
28
|
const fields = getPage?.(name);
|
|
15
|
-
const page = stringField("page", fields?.page,
|
|
29
|
+
const page = stringField("page", fields?.page, report);
|
|
16
30
|
const referrer = mayCarryReferrer
|
|
17
|
-
? stringField("referrer", fields?.referrer,
|
|
31
|
+
? stringField("referrer", fields?.referrer, report)
|
|
18
32
|
: undefined;
|
|
19
|
-
const path = page === undefined ? undefined : checkedPath(page,
|
|
33
|
+
const path = page === undefined ? undefined : checkedPath(page, report);
|
|
20
34
|
const host = referrer === undefined ? undefined : referrerHost(referrer);
|
|
21
35
|
const isPageHost = page !== undefined && host === referrerHost(page);
|
|
22
36
|
return {
|
|
@@ -25,12 +39,20 @@ export function newPageReader({ getPage, reportError, }) {
|
|
|
25
39
|
};
|
|
26
40
|
}
|
|
27
41
|
catch (error) {
|
|
28
|
-
|
|
42
|
+
report(error);
|
|
29
43
|
return undefined;
|
|
30
44
|
}
|
|
31
45
|
}
|
|
32
46
|
return {
|
|
33
|
-
|
|
47
|
+
pageNow: () => fieldsOf({ name: PAGE_VIEW, mayCarryReferrer: false, report: () => undefined })?.page,
|
|
48
|
+
read: (name) => ({
|
|
49
|
+
name,
|
|
50
|
+
fields: fieldsOf({
|
|
51
|
+
name,
|
|
52
|
+
mayCarryReferrer: name === PAGE_VIEW && !isPageViewRecorded,
|
|
53
|
+
report: reportError,
|
|
54
|
+
}),
|
|
55
|
+
}),
|
|
34
56
|
view: ({ name, fields }) => {
|
|
35
57
|
if (name !== PAGE_VIEW) {
|
|
36
58
|
return { fields, isRepeatedView: false, recorded: () => undefined };
|