@traqio/web 0.3.2 → 0.4.1
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 +26 -0
- package/build/analytics.d.ts +73 -0
- package/build/index.cjs +524 -11
- package/build/index.cjs.map +4 -4
- package/build/index.d.ts +27 -0
- package/build/index.js +524 -11
- package/build/index.js.map +4 -4
- package/build/traqio.min.js +1 -1
- package/build/traqio.min.js.map +4 -4
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -101,6 +101,31 @@ because a browser and a signed-in user are not tied together yet.
|
|
|
101
101
|
Events are batched, retried, kept across page loads, and flushed when the tab is
|
|
102
102
|
hidden or the page is left.
|
|
103
103
|
|
|
104
|
+
## Product analytics
|
|
105
|
+
|
|
106
|
+
Page views, traffic sources, clicks, scroll depth, front-end errors and your own
|
|
107
|
+
analytics events — read in the console's **Analytics** section (and through the API /
|
|
108
|
+
MCP). Off by default:
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
Traqio.init({ sdkKey: "YOUR_WEB_KEY", analytics: true });
|
|
112
|
+
// or pick: analytics: { pageviews: true, clicks: true, scroll: true, errors: false }
|
|
113
|
+
|
|
114
|
+
Traqio.capture("search_performed", { results: 12 }); // an analytics event, for events and funnels
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
- **Page views** are recorded on load and on every client-side route change (the history
|
|
118
|
+
API), so single-page apps need nothing more. `Traqio.pageview()` records one by hand.
|
|
119
|
+
- **The source** of a visit — `utm_*`, `gclid`/`fbclid`/`ttclid`, the referring site — is
|
|
120
|
+
read once per session (30 minutes of inactivity end one).
|
|
121
|
+
- **Clicks** record a link's or a button's visible text and link, never what was typed.
|
|
122
|
+
Add `data-traqio-ignore` to an element to leave it out.
|
|
123
|
+
- URLs are stored **without their query string**: tokens and emails live there.
|
|
124
|
+
|
|
125
|
+
**`capture()` is not `track()`.** An analytics event is never billed, never counted as a
|
|
126
|
+
conversion and never sent to an ad network — it has its own route and its own table.
|
|
127
|
+
A sign-up or a purchase stays `track()` (or your backend's server SDK).
|
|
128
|
+
|
|
104
129
|
## Frameworks
|
|
105
130
|
|
|
106
131
|
- **React / Next.js** — call `init()` in a `useEffect` of a client component mounted
|
|
@@ -137,6 +162,7 @@ networks.
|
|
|
137
162
|
| `trackIdDays` | `30` | How long the trackID and the visit's source are kept |
|
|
138
163
|
| `cookies` | `true` | `false` = localStorage only, no sharing across subdomains |
|
|
139
164
|
| `debug` | detected | Debug traffic, kept out of every report (see below) |
|
|
165
|
+
| `analytics` | `false` | Product analytics: `true`, or `{ pageviews, clicks, scroll, errors }` |
|
|
140
166
|
|
|
141
167
|
**Debug traffic.** Omitted, `debug` is on for a page served from `localhost`, `127.0.0.1`, `*.localhost`, `*.local` or `file:`, and off everywhere else; `true` forces it (a staging site), `false` counts a local page. `traqio.isDebug` tells which mode is on. Debug traffic shows in the console's **Live events › Debug** view, but it is never counted in a report, attributed, sent to an ad network or a webhook, or billed.
|
|
142
168
|
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { KeyValueStore } from "@traqio/core";
|
|
2
|
+
export interface AnalyticsOptions {
|
|
3
|
+
/** A page view on load and on every client-side route change (history API). Default true. */
|
|
4
|
+
pageviews?: boolean;
|
|
5
|
+
/** Clicks on links and buttons: their text and link. Default true. */
|
|
6
|
+
clicks?: boolean;
|
|
7
|
+
/** The deepest of 25/50/75/100 % each page view reached, sent once per page view. Default true. */
|
|
8
|
+
scroll?: boolean;
|
|
9
|
+
/** Uncaught errors and unhandled promise rejections. Default true. */
|
|
10
|
+
errors?: boolean;
|
|
11
|
+
}
|
|
12
|
+
/** Where the SESSION landed from — copied on each of its events (the server stores it per row). */
|
|
13
|
+
export interface SessionSource {
|
|
14
|
+
utmSource?: string;
|
|
15
|
+
utmMedium?: string;
|
|
16
|
+
utmCampaign?: string;
|
|
17
|
+
utmContent?: string;
|
|
18
|
+
utmTerm?: string;
|
|
19
|
+
referrer?: string;
|
|
20
|
+
gclid?: string;
|
|
21
|
+
fbclid?: string;
|
|
22
|
+
ttclid?: string;
|
|
23
|
+
}
|
|
24
|
+
export interface AnalyticsDeps {
|
|
25
|
+
store: KeyValueStore & {
|
|
26
|
+
getSync(key: string): string | null;
|
|
27
|
+
setSync(key: string, value: string): void;
|
|
28
|
+
};
|
|
29
|
+
endpoint: string;
|
|
30
|
+
sdkKey: string;
|
|
31
|
+
anonymousId: () => string | null;
|
|
32
|
+
userId: () => string | undefined;
|
|
33
|
+
isDebug: () => boolean;
|
|
34
|
+
isForgotten: () => boolean;
|
|
35
|
+
options: AnalyticsOptions;
|
|
36
|
+
}
|
|
37
|
+
export declare const SESSION_KEY = "traq_session";
|
|
38
|
+
/** 30 minutes without activity end a session — the convention PostHog and GA share. */
|
|
39
|
+
export declare const SESSION_IDLE_MS: number;
|
|
40
|
+
/** The landing's source: the URL's utm_* and click ids, and the referrer when it is another site. */
|
|
41
|
+
export declare function landingSource(href: string, referrer: string): SessionSource | undefined;
|
|
42
|
+
export declare class AnalyticsTracker {
|
|
43
|
+
private readonly deps;
|
|
44
|
+
private queue;
|
|
45
|
+
private timer;
|
|
46
|
+
private lastPath;
|
|
47
|
+
private maxScroll;
|
|
48
|
+
private scrollUrl;
|
|
49
|
+
private errorsThisPage;
|
|
50
|
+
private stopped;
|
|
51
|
+
constructor(deps: AnalyticsDeps);
|
|
52
|
+
/** Wires the listeners the options ask for and records the first page view. */
|
|
53
|
+
start(): void;
|
|
54
|
+
/** A custom analytics event — never a conversion (that is `track()`). */
|
|
55
|
+
capture(name: string, props?: Record<string, unknown>): void;
|
|
56
|
+
/** Records a page view of the current URL. Called by itself on route changes; exposed for routers that need it. */
|
|
57
|
+
pageview(): void;
|
|
58
|
+
/** Sends what is queued. `leaving` = the page is going away: keepalive, so the request outlives it. */
|
|
59
|
+
flush(leaving: boolean): void;
|
|
60
|
+
/** Forget everything and stop: after `forgetMe()`, or on a server. */
|
|
61
|
+
stop(): void;
|
|
62
|
+
private requeue;
|
|
63
|
+
private session;
|
|
64
|
+
private enqueue;
|
|
65
|
+
private leave;
|
|
66
|
+
/** Single-page apps change the URL through the history API: wrap it, and listen for back/forward. */
|
|
67
|
+
private watchRoutes;
|
|
68
|
+
private onScroll;
|
|
69
|
+
private sendScroll;
|
|
70
|
+
private onClick;
|
|
71
|
+
private onError;
|
|
72
|
+
private onRejection;
|
|
73
|
+
}
|