@nextanalytica/tag 0.0.0-stage → 1.0.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 +153 -2
- package/dist/index.d.ts +98 -0
- package/dist/index.js +115 -0
- package/dist/next.d.ts +6 -0
- package/dist/next.js +7 -0
- package/dist/nuxt.d.ts +36 -0
- package/dist/nuxt.js +19 -0
- package/dist/react.d.ts +7 -0
- package/dist/react.js +14 -0
- package/dist/server.d.ts +30 -0
- package/dist/server.js +51 -0
- package/dist/sveltekit.d.ts +10 -0
- package/dist/sveltekit.js +11 -0
- package/dist/vue.d.ts +13 -0
- package/dist/vue.js +15 -0
- package/gatsby/gatsby-ssr.js +19 -0
- package/gatsby/index.js +1 -0
- package/gatsby/package.json +6 -0
- package/package.json +81 -3
- package/src/astro/Analytics.astro +11 -0
package/README.md
CHANGED
|
@@ -1,3 +1,154 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @nextanalytica/tag
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The [nextanalytica](https://nextanalytica.app) tag for JavaScript apps. One component per framework, and the same
|
|
4
|
+
external script underneath every one of them:
|
|
5
|
+
|
|
6
|
+
```html
|
|
7
|
+
<script async src="https://t.nextanalytica.app/t.js" data-project="na_…"></script>
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
The package doesn't bundle a tracker. It adds that line once and passes your calls to it, so your CSP needs only our
|
|
11
|
+
host, and the numbers are the same however the tag was installed. Page views on client-side navigation
|
|
12
|
+
(`pushState`, `replaceState`, `popstate`) are counted by the tag itself, with no router code.
|
|
13
|
+
|
|
14
|
+
Your project key (`na_…`) is in nextanalytica, under Tracking.
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
npm install @nextanalytica/tag
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Frameworks
|
|
21
|
+
|
|
22
|
+
**Next.js**: `app/layout.tsx` (App Router) or `pages/_app.tsx` (Pages Router)
|
|
23
|
+
|
|
24
|
+
```tsx
|
|
25
|
+
import { Analytics } from "@nextanalytica/tag/next";
|
|
26
|
+
|
|
27
|
+
export default function RootLayout({ children }) {
|
|
28
|
+
return (
|
|
29
|
+
<html lang="en">
|
|
30
|
+
<body>
|
|
31
|
+
{children}
|
|
32
|
+
<Analytics project="na_…" />
|
|
33
|
+
</body>
|
|
34
|
+
</html>
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
**React, Remix, React Router**: once at the app's root (`app/root.tsx` in Remix and React Router)
|
|
40
|
+
|
|
41
|
+
```tsx
|
|
42
|
+
import { Analytics } from "@nextanalytica/tag/react";
|
|
43
|
+
|
|
44
|
+
<Analytics project="na_…" />
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
**Vue**: `src/main.ts`
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import { nextanalytica } from "@nextanalytica/tag/vue";
|
|
51
|
+
|
|
52
|
+
createApp(App).use(nextanalytica, { project: "na_…" }).mount("#app");
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
**Nuxt**: `nuxt.config.ts`. The line goes in every page's server-rendered `<head>`.
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
export default defineNuxtConfig({
|
|
59
|
+
modules: ["@nextanalytica/tag/nuxt"],
|
|
60
|
+
nextanalytica: { project: "na_…" },
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
**SvelteKit**: `src/routes/+layout.svelte`
|
|
65
|
+
|
|
66
|
+
```svelte
|
|
67
|
+
<script>
|
|
68
|
+
import { inject } from "@nextanalytica/tag/sveltekit";
|
|
69
|
+
inject({ project: "na_…" });
|
|
70
|
+
</script>
|
|
71
|
+
|
|
72
|
+
<slot />
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
**Astro**: the base layout's `<head>`. It renders the line itself, so static pages need no client JavaScript.
|
|
76
|
+
|
|
77
|
+
```astro
|
|
78
|
+
---
|
|
79
|
+
import Analytics from "@nextanalytica/tag/astro";
|
|
80
|
+
---
|
|
81
|
+
<head><Analytics project="na_…" /></head>
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
**Gatsby**: `gatsby-config.js`
|
|
85
|
+
|
|
86
|
+
```js
|
|
87
|
+
module.exports = {
|
|
88
|
+
plugins: [{ resolve: "@nextanalytica/tag/gatsby", options: { project: "na_…" } }],
|
|
89
|
+
};
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
**Any other app**
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
import nextanalytica from "@nextanalytica/tag";
|
|
96
|
+
|
|
97
|
+
nextanalytica.init("na_…");
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Your own events, consent and people
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
import { track, order, consent, identify, group, setPersonProperties, reset } from "@nextanalytica/tag";
|
|
104
|
+
|
|
105
|
+
track("signup", { plan: "pro" });
|
|
106
|
+
order({ ref: "A-1001", value: 49, currency: "EUR" });
|
|
107
|
+
consent(true); // from your cookie banner; until a yes, no cookie and no id
|
|
108
|
+
identify("user_42"); // your own user id after sign-in, never an email
|
|
109
|
+
group("company", "acme", { plan: "pro" });
|
|
110
|
+
reset(); // on sign-out
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Calls made before the tag has loaded wait and go in order. On the server they do nothing.
|
|
114
|
+
|
|
115
|
+
## Events from your servers
|
|
116
|
+
|
|
117
|
+
What only your servers know (a payment confirmed, a signup approved) goes straight to nextanalytica with a server key:
|
|
118
|
+
in nextanalytica, Tracking, Keys, "Server key". It's shown once; keep it on your servers, never in a page.
|
|
119
|
+
|
|
120
|
+
```sh
|
|
121
|
+
curl -X POST https://t.nextanalytica.app/s \
|
|
122
|
+
-H "Authorization: Bearer nask_…" -H "Content-Type: application/json" \
|
|
123
|
+
-d '{"events":[{"type":"order","user_id":"u_42","value":49,"currency":"EUR","ref":"A-1001"}]}'
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Each event is `order` (with `value`, `currency`, `ref`) or `custom` (with `name` and up to 10 string `props`), and
|
|
127
|
+
`user_id` is your own user id, the same one you pass to `identify`. Up to 50 events a request. They belong to no visit:
|
|
128
|
+
your agent sees them as events, and as conversions under the channel "Server".
|
|
129
|
+
|
|
130
|
+
## Content Security Policy
|
|
131
|
+
|
|
132
|
+
The tag loads its scripts from `https://t.nextanalytica.app` and sends events back to the same host, so a site with a
|
|
133
|
+
CSP needs that host in two directives (each falls back to `default-src` when it isn't set):
|
|
134
|
+
|
|
135
|
+
```
|
|
136
|
+
script-src https://t.nextanalytica.app
|
|
137
|
+
connect-src https://t.nextanalytica.app
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
With `'strict-dynamic'` in `script-src`, host sources are ignored: give our `<script>` tag the page's nonce instead.
|
|
141
|
+
Both the header and a `<meta http-equiv="Content-Security-Policy">` count; every policy must allow it. In
|
|
142
|
+
nextanalytica, Tracking's Check now reads your site's policy and names the lines it is missing.
|
|
143
|
+
|
|
144
|
+
## Options
|
|
145
|
+
|
|
146
|
+
Every component and `init` take the same options:
|
|
147
|
+
|
|
148
|
+
| Option | What it does |
|
|
149
|
+
|---|---|
|
|
150
|
+
| `consent: "granted"` | Your banner already said yes: ids are kept from the first page view |
|
|
151
|
+
| `cookieless: true` | Count without asking for consent, and keep nothing on the device |
|
|
152
|
+
| `vitals: "all"` | Measure Web Vitals on every page view instead of a sample |
|
|
153
|
+
| `search: ["q"]` | The URL parameters that hold your site search's query |
|
|
154
|
+
| `api_host` | Where `t.js` is served from, if not `https://t.nextanalytica.app` |
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@nextanalytica/tag`: puts the nextanalytica tag on the page and passes calls to it (DATA-ARCHITECTURE §7.16, A30).
|
|
3
|
+
*
|
|
4
|
+
* It doesn't bundle a tracker. `init` adds the same external script every install ends with,
|
|
5
|
+
* `<script async src="https://t.nextanalytica.app/t.js" data-project="…">`, once per page, so the site's CSP needs only
|
|
6
|
+
* our host and the numbers match whichever way the tag was installed. The script counts page views on history changes
|
|
7
|
+
* itself, so client-side navigation needs no router code.
|
|
8
|
+
*
|
|
9
|
+
* Calls made before the script has loaded wait on `window.nextanalytica` and go to it when it arrives. On the server
|
|
10
|
+
* (no `document`) everything here does nothing.
|
|
11
|
+
*/
|
|
12
|
+
type Props = Record<string, unknown>;
|
|
13
|
+
/** The script's own global (packages/tracker `browser.ts`): what a site's banner and its own code call. */
|
|
14
|
+
export interface Tracker {
|
|
15
|
+
consent(on: boolean): void;
|
|
16
|
+
track(name: string, props?: Props, o?: {
|
|
17
|
+
value?: number;
|
|
18
|
+
currency?: string;
|
|
19
|
+
}): void;
|
|
20
|
+
order(o: {
|
|
21
|
+
ref?: string;
|
|
22
|
+
value?: number;
|
|
23
|
+
currency?: string;
|
|
24
|
+
}): void;
|
|
25
|
+
identify(userId: string, props?: Props): void;
|
|
26
|
+
group(type: string, key: string, props?: Props): void;
|
|
27
|
+
setPersonProperties(props: Props): void;
|
|
28
|
+
reset(): void;
|
|
29
|
+
}
|
|
30
|
+
export interface Options {
|
|
31
|
+
/** Where `t.js` is served from. Default `https://t.nextanalytica.app`. */
|
|
32
|
+
api_host?: string;
|
|
33
|
+
/** The visitor already agreed (your banner said yes before the tag loads): ids are kept from the first page view. */
|
|
34
|
+
consent?: "granted";
|
|
35
|
+
/** Counting without consent, keeping nothing across visits (no cookie, no id), is the default; `false` waits for consent instead. */
|
|
36
|
+
cookieless?: boolean;
|
|
37
|
+
/** Measure Web Vitals on every page view instead of a sample. */
|
|
38
|
+
vitals?: "all";
|
|
39
|
+
/** The URL parameters that hold your site search's query, for example ["q", "s"]. */
|
|
40
|
+
search?: string[];
|
|
41
|
+
/** Your other domains in this project, for example ["shop.io"]: a link to one of them carries the visitor, after consent. */
|
|
42
|
+
domains?: string[];
|
|
43
|
+
}
|
|
44
|
+
declare global {
|
|
45
|
+
interface Window {
|
|
46
|
+
nextanalytica?: Tracker & {
|
|
47
|
+
__q?: [keyof Tracker, unknown[]][];
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export declare const API_HOST = "https://t.nextanalytica.app";
|
|
52
|
+
/** The host `t.js` comes from: an https URL (http only on localhost), without a path. */
|
|
53
|
+
export declare function hostOf(apiHost: string | undefined): string;
|
|
54
|
+
/** The attributes of the script line, as the tag reads them. */
|
|
55
|
+
export declare function attributes(project: string, o?: Options): Record<string, string>;
|
|
56
|
+
/** The script line itself, for server-rendered `<head>`s (Astro, Gatsby, Nuxt). */
|
|
57
|
+
export declare function scriptTag(project: string, o?: Options): {
|
|
58
|
+
src: string;
|
|
59
|
+
async: true;
|
|
60
|
+
attributes: Record<string, string>;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Put the tag on this page. Call it once, as early as you can; a second call, or a page that already has the line
|
|
64
|
+
* (pasted, or rendered by a server component), changes nothing.
|
|
65
|
+
*/
|
|
66
|
+
export declare function init(project: string, o?: Options): void;
|
|
67
|
+
/** Your own event: `track("signup", { plan: "pro" })`, with an optional value. */
|
|
68
|
+
export declare const track: (name: string, props?: Props, o?: {
|
|
69
|
+
value?: number;
|
|
70
|
+
currency?: string;
|
|
71
|
+
}) => void;
|
|
72
|
+
/** A purchase: `order({ ref: "A-1001", value: 49, currency: "EUR" })`. */
|
|
73
|
+
export declare const order: (o: {
|
|
74
|
+
ref?: string;
|
|
75
|
+
value?: number;
|
|
76
|
+
currency?: string;
|
|
77
|
+
}) => void;
|
|
78
|
+
/** The visitor's answer from your cookie banner. Until a yes, no cookie and no id. */
|
|
79
|
+
export declare const consent: (on: boolean) => void;
|
|
80
|
+
/** Who this is, once they sign in (after consent): your own user id, never an email. */
|
|
81
|
+
export declare const identify: (userId: string, props?: Props) => void;
|
|
82
|
+
/** The company or team they belong to: `group("company", "acme", { plan: "pro" })`. */
|
|
83
|
+
export declare const group: (type: string, key: string, props?: Props) => void;
|
|
84
|
+
/** Properties of the signed-in person: `setPersonProperties({ plan: "pro" })`. */
|
|
85
|
+
export declare const setPersonProperties: (props: Props) => void;
|
|
86
|
+
/** On sign-out: forget the person on this device. */
|
|
87
|
+
export declare const reset: () => void;
|
|
88
|
+
declare const nextanalytica: {
|
|
89
|
+
init: typeof init;
|
|
90
|
+
track: typeof track;
|
|
91
|
+
order: typeof order;
|
|
92
|
+
consent: typeof consent;
|
|
93
|
+
identify: typeof identify;
|
|
94
|
+
group: typeof group;
|
|
95
|
+
setPersonProperties: typeof setPersonProperties;
|
|
96
|
+
reset: typeof reset;
|
|
97
|
+
};
|
|
98
|
+
export default nextanalytica;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@nextanalytica/tag`: puts the nextanalytica tag on the page and passes calls to it (DATA-ARCHITECTURE §7.16, A30).
|
|
3
|
+
*
|
|
4
|
+
* It doesn't bundle a tracker. `init` adds the same external script every install ends with,
|
|
5
|
+
* `<script async src="https://t.nextanalytica.app/t.js" data-project="…">`, once per page, so the site's CSP needs only
|
|
6
|
+
* our host and the numbers match whichever way the tag was installed. The script counts page views on history changes
|
|
7
|
+
* itself, so client-side navigation needs no router code.
|
|
8
|
+
*
|
|
9
|
+
* Calls made before the script has loaded wait on `window.nextanalytica` and go to it when it arrives. On the server
|
|
10
|
+
* (no `document`) everything here does nothing.
|
|
11
|
+
*/
|
|
12
|
+
export const API_HOST = "https://t.nextanalytica.app";
|
|
13
|
+
const METHODS = ["consent", "track", "order", "identify", "group", "setPersonProperties", "reset"];
|
|
14
|
+
/** The project key as Tracking shows it: `na_` and letters and digits. */
|
|
15
|
+
const KEY = /^[A-Za-z0-9_-]{3,100}$/;
|
|
16
|
+
const browser = () => typeof window !== "undefined" && typeof document !== "undefined";
|
|
17
|
+
/** The host `t.js` comes from: an https URL (http only on localhost), without a path. */
|
|
18
|
+
export function hostOf(apiHost) {
|
|
19
|
+
if (!apiHost)
|
|
20
|
+
return API_HOST;
|
|
21
|
+
const u = new URL(apiHost);
|
|
22
|
+
const local = u.hostname === "localhost" || u.hostname === "127.0.0.1";
|
|
23
|
+
if (u.protocol !== "https:" && !(local && u.protocol === "http:"))
|
|
24
|
+
throw new Error(`nextanalytica: api_host must be https, got ${apiHost}`);
|
|
25
|
+
return u.origin;
|
|
26
|
+
}
|
|
27
|
+
/** The attributes of the script line, as the tag reads them. */
|
|
28
|
+
export function attributes(project, o = {}) {
|
|
29
|
+
const a = { "data-project": project };
|
|
30
|
+
if (o.consent === "granted")
|
|
31
|
+
a["data-consent"] = "granted";
|
|
32
|
+
// cookieless counting is the default either way; true says so on the line, false waits for consent
|
|
33
|
+
if (o.cookieless !== undefined)
|
|
34
|
+
a["data-cookieless"] = o.cookieless ? "" : "off";
|
|
35
|
+
if (o.vitals === "all")
|
|
36
|
+
a["data-vitals"] = "all";
|
|
37
|
+
if (o.domains?.length)
|
|
38
|
+
a["data-domains"] = o.domains.join(",");
|
|
39
|
+
if (o.search?.length)
|
|
40
|
+
a["data-search"] = o.search.join(",");
|
|
41
|
+
return a;
|
|
42
|
+
}
|
|
43
|
+
/** The script line itself, for server-rendered `<head>`s (Astro, Gatsby, Nuxt). */
|
|
44
|
+
export function scriptTag(project, o = {}) {
|
|
45
|
+
if (!KEY.test(project))
|
|
46
|
+
throw new Error(`nextanalytica: "${project}" isn't a project key. Copy it from Tracking.`);
|
|
47
|
+
return { src: `${hostOf(o.api_host)}/t.js`, async: true, attributes: attributes(project, o) };
|
|
48
|
+
}
|
|
49
|
+
/** Until the script has loaded: a stand-in that keeps the calls, in order. */
|
|
50
|
+
function standIn() {
|
|
51
|
+
const w = window;
|
|
52
|
+
if (w.nextanalytica)
|
|
53
|
+
return w.nextanalytica;
|
|
54
|
+
const q = [];
|
|
55
|
+
const s = { __q: q };
|
|
56
|
+
for (const m of METHODS)
|
|
57
|
+
s[m] = (...args) => void q.push([m, args]);
|
|
58
|
+
w.nextanalytica = s;
|
|
59
|
+
return s;
|
|
60
|
+
}
|
|
61
|
+
/** The waiting calls go to the real tag, once it has replaced the stand-in. */
|
|
62
|
+
function drain(q) {
|
|
63
|
+
const real = window.nextanalytica;
|
|
64
|
+
if (!real || real.__q === q)
|
|
65
|
+
return;
|
|
66
|
+
for (const [m, args] of q.splice(0))
|
|
67
|
+
real[m](...args);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Put the tag on this page. Call it once, as early as you can; a second call, or a page that already has the line
|
|
71
|
+
* (pasted, or rendered by a server component), changes nothing.
|
|
72
|
+
*/
|
|
73
|
+
export function init(project, o = {}) {
|
|
74
|
+
if (!browser())
|
|
75
|
+
return;
|
|
76
|
+
const tag = scriptTag(project, o);
|
|
77
|
+
const q = standIn().__q;
|
|
78
|
+
const there = document.querySelector("script[data-project]");
|
|
79
|
+
if (there) {
|
|
80
|
+
if (q)
|
|
81
|
+
there.addEventListener("load", () => drain(q));
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const el = document.createElement("script");
|
|
85
|
+
el.src = tag.src;
|
|
86
|
+
el.async = true;
|
|
87
|
+
for (const [k, v] of Object.entries(tag.attributes))
|
|
88
|
+
el.setAttribute(k, v);
|
|
89
|
+
if (q)
|
|
90
|
+
el.addEventListener("load", () => drain(q));
|
|
91
|
+
(document.head ?? document.documentElement).appendChild(el);
|
|
92
|
+
}
|
|
93
|
+
/** One of the tag's calls: now if it has loaded, else when it does. On the server, nothing. */
|
|
94
|
+
function call(m, ...args) {
|
|
95
|
+
if (!browser())
|
|
96
|
+
return;
|
|
97
|
+
const t = window.nextanalytica ?? standIn();
|
|
98
|
+
t[m](...args);
|
|
99
|
+
}
|
|
100
|
+
/** Your own event: `track("signup", { plan: "pro" })`, with an optional value. */
|
|
101
|
+
export const track = (name, props, o) => call("track", name, props, o);
|
|
102
|
+
/** A purchase: `order({ ref: "A-1001", value: 49, currency: "EUR" })`. */
|
|
103
|
+
export const order = (o) => call("order", o);
|
|
104
|
+
/** The visitor's answer from your cookie banner. Until a yes, no cookie and no id. */
|
|
105
|
+
export const consent = (on) => call("consent", on);
|
|
106
|
+
/** Who this is, once they sign in (after consent): your own user id, never an email. */
|
|
107
|
+
export const identify = (userId, props) => call("identify", userId, props);
|
|
108
|
+
/** The company or team they belong to: `group("company", "acme", { plan: "pro" })`. */
|
|
109
|
+
export const group = (type, key, props) => call("group", type, key, props);
|
|
110
|
+
/** Properties of the signed-in person: `setPersonProperties({ plan: "pro" })`. */
|
|
111
|
+
export const setPersonProperties = (props) => call("setPersonProperties", props);
|
|
112
|
+
/** On sign-out: forget the person on this device. */
|
|
113
|
+
export const reset = () => call("reset");
|
|
114
|
+
const nextanalytica = { init, track, order, consent, identify, group, setPersonProperties, reset };
|
|
115
|
+
export default nextanalytica;
|
package/dist/next.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `<Analytics project="…" />` for Next.js: in `app/layout.tsx` inside `<body>` (App Router), or in `pages/_app.tsx`
|
|
3
|
+
* (Pages Router). A client component, so it can sit in a server layout; route changes are counted by the tag itself.
|
|
4
|
+
*/
|
|
5
|
+
export { Analytics, default, type AnalyticsProps } from "./react.js";
|
|
6
|
+
export { track, order, consent, identify, group, setPersonProperties, reset } from "./index.js";
|
package/dist/next.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
/**
|
|
3
|
+
* `<Analytics project="…" />` for Next.js: in `app/layout.tsx` inside `<body>` (App Router), or in `pages/_app.tsx`
|
|
4
|
+
* (Pages Router). A client component, so it can sit in a server layout; route changes are counted by the tag itself.
|
|
5
|
+
*/
|
|
6
|
+
export { Analytics, default } from "./react.js";
|
|
7
|
+
export { track, order, consent, identify, group, setPersonProperties, reset } from "./index.js";
|
package/dist/nuxt.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Nuxt module: `modules: ["@nextanalytica/tag/nuxt"]` and `nextanalytica: { project: "…" }` in `nuxt.config.ts`.
|
|
3
|
+
* It puts the script line in every page's server-rendered `<head>`, so the tag starts with the first byte, not after
|
|
4
|
+
* hydration. Written without `@nuxt/kit`: a module is a function Nuxt calls with its options and the Nuxt instance.
|
|
5
|
+
*/
|
|
6
|
+
import { type Options } from "./index.js";
|
|
7
|
+
export type ModuleOptions = Options & {
|
|
8
|
+
project?: string;
|
|
9
|
+
};
|
|
10
|
+
type NuxtLike = {
|
|
11
|
+
options: {
|
|
12
|
+
app: {
|
|
13
|
+
head: {
|
|
14
|
+
script?: unknown[];
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
nextanalytica?: ModuleOptions;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
declare function nextanalyticaModule(inline: ModuleOptions | undefined, nuxt: NuxtLike): void;
|
|
21
|
+
declare namespace nextanalyticaModule {
|
|
22
|
+
var getMeta: () => Promise<{
|
|
23
|
+
name: string;
|
|
24
|
+
configKey: string;
|
|
25
|
+
}>;
|
|
26
|
+
var getOptions: (inline: ModuleOptions | undefined, nuxt: NuxtLike) => Promise<{
|
|
27
|
+
api_host?: string;
|
|
28
|
+
consent?: "granted";
|
|
29
|
+
cookieless?: boolean;
|
|
30
|
+
vitals?: "all";
|
|
31
|
+
search?: string[];
|
|
32
|
+
domains?: string[];
|
|
33
|
+
project?: string;
|
|
34
|
+
}>;
|
|
35
|
+
}
|
|
36
|
+
export default nextanalyticaModule;
|
package/dist/nuxt.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Nuxt module: `modules: ["@nextanalytica/tag/nuxt"]` and `nextanalytica: { project: "…" }` in `nuxt.config.ts`.
|
|
3
|
+
* It puts the script line in every page's server-rendered `<head>`, so the tag starts with the first byte, not after
|
|
4
|
+
* hydration. Written without `@nuxt/kit`: a module is a function Nuxt calls with its options and the Nuxt instance.
|
|
5
|
+
*/
|
|
6
|
+
import { scriptTag } from "./index.js";
|
|
7
|
+
function nextanalyticaModule(inline, nuxt) {
|
|
8
|
+
const o = { ...nuxt.options.nextanalytica, ...inline };
|
|
9
|
+
if (!o.project)
|
|
10
|
+
throw new Error("nextanalytica: set nextanalytica: { project } in nuxt.config.ts, with your project key from Tracking.");
|
|
11
|
+
const { project, ...rest } = o;
|
|
12
|
+
const tag = scriptTag(project, rest);
|
|
13
|
+
const head = nuxt.options.app.head;
|
|
14
|
+
head.script = [...(head.script ?? []), { src: tag.src, async: true, ...tag.attributes }];
|
|
15
|
+
}
|
|
16
|
+
/** What Nuxt reads off a module: its name and the config key its options live under. */
|
|
17
|
+
nextanalyticaModule.getMeta = async () => ({ name: "@nextanalytica/tag/nuxt", configKey: "nextanalytica" });
|
|
18
|
+
nextanalyticaModule.getOptions = async (inline, nuxt) => ({ ...nuxt.options.nextanalytica, ...inline });
|
|
19
|
+
export default nextanalyticaModule;
|
package/dist/react.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type Options } from "./index.js";
|
|
2
|
+
export type AnalyticsProps = Options & {
|
|
3
|
+
project: string;
|
|
4
|
+
};
|
|
5
|
+
export declare function Analytics({ project, ...o }: AnalyticsProps): null;
|
|
6
|
+
export default Analytics;
|
|
7
|
+
export { track, order, consent, identify, group, setPersonProperties, reset } from "./index.js";
|
package/dist/react.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
/**
|
|
3
|
+
* `<Analytics project="…" />` for React, Remix and React Router: put it once at the app's root. It renders nothing; it
|
|
4
|
+
* adds the tag when the app first mounts in the browser. Client-side navigation is counted by the tag itself.
|
|
5
|
+
*/
|
|
6
|
+
import { useEffect } from "react";
|
|
7
|
+
import { init } from "./index.js";
|
|
8
|
+
export function Analytics({ project, ...o }) {
|
|
9
|
+
const opts = JSON.stringify(o);
|
|
10
|
+
useEffect(() => { init(project, JSON.parse(opts)); }, [project, opts]);
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
export default Analytics;
|
|
14
|
+
export { track, order, consent, identify, group, setPersonProperties, reset } from "./index.js";
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* For the site's own server: renews the visitor's first-party id, so Safari keeps it for its full 13 months.
|
|
3
|
+
*
|
|
4
|
+
* Safari keeps a cookie a script writes for 7 days only, so a visitor who comes back after two weeks would be someone
|
|
5
|
+
* new. A cookie the site's own server sets (from the site's own address) keeps its full time. t.js still makes the id,
|
|
6
|
+
* and only after consent; this renews what it made, on every page, and never makes one: without `na_id` (no consent
|
|
7
|
+
* yet, or a no) nothing is set. It marks the cookie with `na_s`, so t.js leaves it to the server.
|
|
8
|
+
*
|
|
9
|
+
* Next.js, `middleware.ts`:
|
|
10
|
+
*
|
|
11
|
+
* import { NextResponse, type NextRequest } from "next/server";
|
|
12
|
+
* import { renew } from "@nextanalytica/tag/server";
|
|
13
|
+
* export function middleware(req: NextRequest) { const res = NextResponse.next(); renew(req, res.headers); return res; }
|
|
14
|
+
*
|
|
15
|
+
* SvelteKit, `src/hooks.server.ts`:
|
|
16
|
+
*
|
|
17
|
+
* export const handle = async ({ event, resolve }) => { const res = await resolve(event); renew(event.request, res.headers); return res; };
|
|
18
|
+
*
|
|
19
|
+
* Any other server: `renewCookies(cookieHeader, host)` gives the `Set-Cookie` values to add to a page's response.
|
|
20
|
+
* Behind a CDN, renew on your own server, not on ours: a cookie set from another company's address keeps Safari's
|
|
21
|
+
* 7 days.
|
|
22
|
+
*/
|
|
23
|
+
export interface RenewOptions {
|
|
24
|
+
/** Add `Secure` (default true). Turn it off only for plain `http://` on your own machine. */
|
|
25
|
+
secure?: boolean;
|
|
26
|
+
}
|
|
27
|
+
/** The `Set-Cookie` values that renew the visitor's id, or none when there's nothing of ours to renew. */
|
|
28
|
+
export declare function renewCookies(cookie: string | null | undefined, host: string, o?: RenewOptions): string[];
|
|
29
|
+
/** Renew on a page's response: for a page only, not for an image, a script or an API call. */
|
|
30
|
+
export declare function renew(request: Request, headers: Headers, o?: RenewOptions): void;
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* For the site's own server: renews the visitor's first-party id, so Safari keeps it for its full 13 months.
|
|
3
|
+
*
|
|
4
|
+
* Safari keeps a cookie a script writes for 7 days only, so a visitor who comes back after two weeks would be someone
|
|
5
|
+
* new. A cookie the site's own server sets (from the site's own address) keeps its full time. t.js still makes the id,
|
|
6
|
+
* and only after consent; this renews what it made, on every page, and never makes one: without `na_id` (no consent
|
|
7
|
+
* yet, or a no) nothing is set. It marks the cookie with `na_s`, so t.js leaves it to the server.
|
|
8
|
+
*
|
|
9
|
+
* Next.js, `middleware.ts`:
|
|
10
|
+
*
|
|
11
|
+
* import { NextResponse, type NextRequest } from "next/server";
|
|
12
|
+
* import { renew } from "@nextanalytica/tag/server";
|
|
13
|
+
* export function middleware(req: NextRequest) { const res = NextResponse.next(); renew(req, res.headers); return res; }
|
|
14
|
+
*
|
|
15
|
+
* SvelteKit, `src/hooks.server.ts`:
|
|
16
|
+
*
|
|
17
|
+
* export const handle = async ({ event, resolve }) => { const res = await resolve(event); renew(event.request, res.headers); return res; };
|
|
18
|
+
*
|
|
19
|
+
* Any other server: `renewCookies(cookieHeader, host)` gives the `Set-Cookie` values to add to a page's response.
|
|
20
|
+
* Behind a CDN, renew on your own server, not on ours: a cookie set from another company's address keeps Safari's
|
|
21
|
+
* 7 days.
|
|
22
|
+
*/
|
|
23
|
+
/** 13 months, in seconds, as t.js keeps it. */
|
|
24
|
+
const KEEP = 34214400;
|
|
25
|
+
/** The `Set-Cookie` values that renew the visitor's id, or none when there's nothing of ours to renew. */
|
|
26
|
+
export function renewCookies(cookie, host, o = {}) {
|
|
27
|
+
const jar = new Map();
|
|
28
|
+
for (const part of (cookie ?? "").split(";")) {
|
|
29
|
+
const i = part.indexOf("=");
|
|
30
|
+
if (i > 0)
|
|
31
|
+
jar.set(part.slice(0, i).trim(), part.slice(i + 1).trim());
|
|
32
|
+
}
|
|
33
|
+
const id = jar.get("na_id"), d = jar.get("na_d")?.toLowerCase();
|
|
34
|
+
if (!id || !/^[\w-]{6,64}$/.test(id))
|
|
35
|
+
return [];
|
|
36
|
+
const h = host.toLowerCase().replace(/:\d+$/, "");
|
|
37
|
+
// the domain t.js found for the site, only if this host is under it (no copy on another domain, or on this host alone)
|
|
38
|
+
if (d && (!/^[a-z0-9.-]+$/.test(d) || (h !== d && !h.endsWith(`.${d}`))))
|
|
39
|
+
return [];
|
|
40
|
+
const tail = `; Max-Age=${KEEP}${d ? `; Domain=${d}` : ""}; Path=/; SameSite=Lax${o.secure === false ? "" : "; Secure"}`;
|
|
41
|
+
return [`na_id=${id}${tail}`, ...(d ? [`na_d=${d}${tail}`] : []), `na_s=1${tail}`];
|
|
42
|
+
}
|
|
43
|
+
/** Renew on a page's response: for a page only, not for an image, a script or an API call. */
|
|
44
|
+
export function renew(request, headers, o = {}) {
|
|
45
|
+
const dest = request.headers.get("sec-fetch-dest");
|
|
46
|
+
if (dest ? dest !== "document" : !(request.headers.get("accept") ?? "").includes("text/html"))
|
|
47
|
+
return;
|
|
48
|
+
const host = request.headers.get("host") ?? new URL(request.url).host;
|
|
49
|
+
for (const c of renewCookies(request.headers.get("cookie"), host, o))
|
|
50
|
+
headers.append("set-cookie", c);
|
|
51
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SvelteKit: `inject({ project: "…" })` in `src/routes/+layout.svelte`. On the server it does nothing; in the browser it
|
|
3
|
+
* adds the tag once, and the tag counts client-side navigation itself.
|
|
4
|
+
*/
|
|
5
|
+
import { type Options } from "./index.js";
|
|
6
|
+
export declare function inject(o: Options & {
|
|
7
|
+
project: string;
|
|
8
|
+
}): void;
|
|
9
|
+
export default inject;
|
|
10
|
+
export { track, order, consent, identify, group, setPersonProperties, reset } from "./index.js";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SvelteKit: `inject({ project: "…" })` in `src/routes/+layout.svelte`. On the server it does nothing; in the browser it
|
|
3
|
+
* adds the tag once, and the tag counts client-side navigation itself.
|
|
4
|
+
*/
|
|
5
|
+
import { init } from "./index.js";
|
|
6
|
+
export function inject(o) {
|
|
7
|
+
const { project, ...rest } = o;
|
|
8
|
+
init(project, rest);
|
|
9
|
+
}
|
|
10
|
+
export default inject;
|
|
11
|
+
export { track, order, consent, identify, group, setPersonProperties, reset } from "./index.js";
|
package/dist/vue.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Vue plugin: `createApp(App).use(nextanalytica, { project: "…" })` in `src/main.ts`. It adds the tag in the
|
|
3
|
+
* browser; Vue Router's navigation is counted by the tag itself.
|
|
4
|
+
*/
|
|
5
|
+
import { type Options } from "./index.js";
|
|
6
|
+
export type PluginOptions = Options & {
|
|
7
|
+
project: string;
|
|
8
|
+
};
|
|
9
|
+
export declare const nextanalytica: {
|
|
10
|
+
install(_app: unknown, o: PluginOptions): void;
|
|
11
|
+
};
|
|
12
|
+
export default nextanalytica;
|
|
13
|
+
export { track, order, consent, identify, group, setPersonProperties, reset } from "./index.js";
|
package/dist/vue.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Vue plugin: `createApp(App).use(nextanalytica, { project: "…" })` in `src/main.ts`. It adds the tag in the
|
|
3
|
+
* browser; Vue Router's navigation is counted by the tag itself.
|
|
4
|
+
*/
|
|
5
|
+
import { init } from "./index.js";
|
|
6
|
+
export const nextanalytica = {
|
|
7
|
+
install(_app, o) {
|
|
8
|
+
if (!o?.project)
|
|
9
|
+
throw new Error("nextanalytica: app.use(nextanalytica, { project }) needs your project key from Tracking.");
|
|
10
|
+
const { project, ...rest } = o;
|
|
11
|
+
init(project, rest);
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
export default nextanalytica;
|
|
15
|
+
export { track, order, consent, identify, group, setPersonProperties, reset } from "./index.js";
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Gatsby plugin: `plugins: [{ resolve: "@nextanalytica/tag/gatsby", options: { project: "…" } }]` in
|
|
3
|
+
* gatsby-config.js. Every page's server-rendered <head> gets the script line; Gatsby's client navigation is counted by
|
|
4
|
+
* the tag itself.
|
|
5
|
+
*/
|
|
6
|
+
const React = require("react");
|
|
7
|
+
|
|
8
|
+
const KEY = /^[A-Za-z0-9_-]{3,100}$/;
|
|
9
|
+
|
|
10
|
+
exports.onRenderBody = ({ setHeadComponents }, o = {}) => {
|
|
11
|
+
if (!o.project || !KEY.test(o.project)) throw new Error("nextanalytica: the Gatsby plugin needs options: { project } with your project key from Tracking.");
|
|
12
|
+
const host = o.api_host ? new URL(o.api_host).origin : "https://t.nextanalytica.app";
|
|
13
|
+
const a = { key: "nextanalytica", async: true, src: `${host}/t.js`, "data-project": o.project };
|
|
14
|
+
if (o.consent === "granted") a["data-consent"] = "granted";
|
|
15
|
+
if (o.cookieless) a["data-cookieless"] = "";
|
|
16
|
+
if (o.vitals === "all") a["data-vitals"] = "all";
|
|
17
|
+
if (o.search && o.search.length) a["data-search"] = o.search.join(",");
|
|
18
|
+
setHeadComponents([React.createElement("script", a)]);
|
|
19
|
+
};
|
package/gatsby/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
// Gatsby finds the plugin's files next to this one: gatsby-ssr.js puts the tag in every page's <head>.
|
package/package.json
CHANGED
|
@@ -1,6 +1,84 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nextanalytica/tag",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "The nextanalytica tag for JavaScript apps: one component per framework, the same t.js underneath.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"homepage": "https://nextanalytica.app",
|
|
9
|
+
"keywords": [
|
|
10
|
+
"analytics",
|
|
11
|
+
"nextanalytica",
|
|
12
|
+
"next",
|
|
13
|
+
"react",
|
|
14
|
+
"vue",
|
|
15
|
+
"nuxt",
|
|
16
|
+
"sveltekit",
|
|
17
|
+
"astro",
|
|
18
|
+
"gatsby"
|
|
19
|
+
],
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"src/astro",
|
|
23
|
+
"gatsby",
|
|
24
|
+
"README.md"
|
|
25
|
+
],
|
|
26
|
+
"main": "./dist/index.js",
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"import": "./dist/index.js"
|
|
32
|
+
},
|
|
33
|
+
"./react": {
|
|
34
|
+
"types": "./dist/react.d.ts",
|
|
35
|
+
"import": "./dist/react.js"
|
|
36
|
+
},
|
|
37
|
+
"./next": {
|
|
38
|
+
"types": "./dist/next.d.ts",
|
|
39
|
+
"import": "./dist/next.js"
|
|
40
|
+
},
|
|
41
|
+
"./vue": {
|
|
42
|
+
"types": "./dist/vue.d.ts",
|
|
43
|
+
"import": "./dist/vue.js"
|
|
44
|
+
},
|
|
45
|
+
"./nuxt": {
|
|
46
|
+
"types": "./dist/nuxt.d.ts",
|
|
47
|
+
"import": "./dist/nuxt.js"
|
|
48
|
+
},
|
|
49
|
+
"./sveltekit": {
|
|
50
|
+
"types": "./dist/sveltekit.d.ts",
|
|
51
|
+
"import": "./dist/sveltekit.js"
|
|
52
|
+
},
|
|
53
|
+
"./server": {
|
|
54
|
+
"types": "./dist/server.d.ts",
|
|
55
|
+
"import": "./dist/server.js"
|
|
56
|
+
},
|
|
57
|
+
"./astro": "./src/astro/Analytics.astro",
|
|
58
|
+
"./gatsby/package.json": "./gatsby/package.json",
|
|
59
|
+
"./gatsby/*": "./gatsby/*",
|
|
60
|
+
"./package.json": "./package.json"
|
|
61
|
+
},
|
|
62
|
+
"peerDependencies": {
|
|
63
|
+
"react": ">=18"
|
|
64
|
+
},
|
|
65
|
+
"peerDependenciesMeta": {
|
|
66
|
+
"react": {
|
|
67
|
+
"optional": true
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
"devDependencies": {
|
|
71
|
+
"@types/node": "^26.6.5",
|
|
72
|
+
"@types/react": "^19.2.7",
|
|
73
|
+
"@types/react-dom": "^19.2.3",
|
|
74
|
+
"react": "19.3.1",
|
|
75
|
+
"react-dom": "19.3.1",
|
|
76
|
+
"typescript": "^7.0.2",
|
|
77
|
+
"vitest": "^5.0.5"
|
|
78
|
+
},
|
|
79
|
+
"scripts": {
|
|
80
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
81
|
+
"typecheck": "tsc -p tsconfig.json",
|
|
82
|
+
"test": "vitest run --passWithNoTests"
|
|
83
|
+
}
|
|
6
84
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* `<Analytics project="…" />` for Astro: in the base layout's <head>. It renders the script line itself, so it works
|
|
4
|
+
* on static pages with no client JavaScript, and with View Transitions (the tag counts history changes).
|
|
5
|
+
*/
|
|
6
|
+
import { scriptTag, type Options } from "../../dist/index.js";
|
|
7
|
+
interface Props extends Options { project: string }
|
|
8
|
+
const { project, ...o } = Astro.props;
|
|
9
|
+
const tag = scriptTag(project, o);
|
|
10
|
+
---
|
|
11
|
+
<script is:inline async src={tag.src} {...tag.attributes}></script>
|