@deltabreen/analytics-next 0.0.0-stage → 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 DeltaBreen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,47 @@
1
- # Temporary Holding Version
1
+ # @deltabreen/analytics-next
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Analytics by DeltaBreen for Next.js, App Router or Pages Router. It loads the same per-site script as the `<script>`
4
+ tag, so pageviews (navigations included), heatmaps, errors, Web Vitals, experiments, recordings and surveys all work
5
+ the same. On a server it does nothing.
6
+
7
+ ```bash
8
+ npm install @deltabreen/analytics-next
9
+ ```
10
+
11
+ ```jsx
12
+ // app/layout.tsx (or pages/_app.tsx)
13
+ import { Analytics } from '@deltabreen/analytics-next';
14
+
15
+ export default function RootLayout({ children }) {
16
+ return (<html><body>{children}<Analytics siteKey="ans_YOUR_KEY" origin="/stats" /></body></html>);
17
+ }
18
+ ```
19
+
20
+ ```jsx
21
+ 'use client';
22
+ import { useVariant, track } from '@deltabreen/analytics-next';
23
+
24
+ export function Hero() {
25
+ const v = useVariant('hero-copy'); // null until the tracker has loaded, or for people outside the test
26
+ return <h1 onClick={() => track('Hero clicked')}>{v === 'b' ? 'Ship faster' : 'Build better'}</h1>;
27
+ }
28
+ ```
29
+
30
+ ## Through your own domain
31
+
32
+ Content blockers block well-known analytics addresses, so serve the tracker through your site. One route:
33
+
34
+ ```js
35
+ // app/stats/[...path]/route.ts (a Pages Router app can hold this app/ folder beside pages/)
36
+ export { GET, POST } from '@deltabreen/analytics-next/proxy';
37
+ ```
38
+
39
+ It sends `/stats/…` to the collector with your visitor's address (wherever Next.js runs), only the headers the
40
+ collector reads (never your site's cookies), and nothing but the tracker's own paths. Then switch on Settings → Site →
41
+ Behind my own proxy, so the collector reads that address. For a collector other than ours:
42
+ `export const { GET, POST } = analyticsProxy({ collector: 'https://…' })`.
43
+
44
+ `<Analytics />` also takes `consent="tcf"` to follow a consent banner on the IAB's framework. Both hooks start at the
45
+ original so a server render and the first client render agree, then update once the tracker has loaded.
46
+
47
+ Docs: https://analytics.deltabreen.eu/docs/install
package/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from '@deltabreen/analytics-react';
package/index.js ADDED
@@ -0,0 +1,5 @@
1
+ 'use client';
2
+ // Next.js helpers: <Analytics siteKey="ans_…" /> in the root layout (App Router) or _app (Pages Router), the variant
3
+ // and flag hooks for client components, and the core's functions. Navigations between pages are counted by the
4
+ // tracker itself; the route that serves it through the site's own domain is @deltabreen/analytics-next/proxy.
5
+ export { Analytics, consent, flag, identify, init, notFound, optOut, pageview, ready, track, useFlag, useVariant, variant } from '@deltabreen/analytics-react';
package/package.json CHANGED
@@ -1,6 +1,52 @@
1
1
  {
2
2
  "name": "@deltabreen/analytics-next",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Analytics by DeltaBreen for Next.js: an <Analytics /> component, hooks for experiment variants and feature flags, and a route that serves the tracker through your own domain.",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./index.d.ts",
9
+ "default": "./index.js"
10
+ },
11
+ "./proxy": {
12
+ "types": "./proxy.d.ts",
13
+ "default": "./proxy.js"
14
+ },
15
+ "./package.json": "./package.json"
16
+ },
17
+ "files": [
18
+ "index.js",
19
+ "index.d.ts",
20
+ "proxy.js",
21
+ "proxy.d.ts",
22
+ "README.md",
23
+ "LICENSE"
24
+ ],
25
+ "sideEffects": false,
26
+ "dependencies": {
27
+ "@deltabreen/analytics-react": "^0.1.0"
28
+ },
29
+ "peerDependencies": {
30
+ "next": ">=13.4",
31
+ "react": ">=17"
32
+ },
33
+ "engines": {
34
+ "node": ">=18"
35
+ },
36
+ "license": "MIT",
37
+ "scripts": {
38
+ "test": "node --test test/*.test.mjs"
39
+ },
40
+ "keywords": [
41
+ "analytics",
42
+ "cookieless",
43
+ "heatmaps",
44
+ "ab-testing",
45
+ "feature-flags",
46
+ "deltabreen",
47
+ "next",
48
+ "nextjs",
49
+ "react"
50
+ ],
51
+ "homepage": "https://analytics.deltabreen.eu/docs/install"
52
+ }
package/proxy.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ export interface ProxyOptions {
2
+ /** The collector's address, as on Sites → your site → Install; ours unless you are told otherwise. */
3
+ collector?: string;
4
+ }
5
+ /** A route handler for app/<path>/[...path]/route.ts. */
6
+ export type ProxyHandler = (request: Request, context: { params: Promise<Record<string, string | string[]>> | Record<string, string | string[]> }) => Promise<Response>;
7
+ /** GET and POST for the route, for a collector other than ours. */
8
+ export function analyticsProxy(options?: ProxyOptions): { GET: ProxyHandler; POST: ProxyHandler };
9
+ /** GET and POST for the route, to our collector. */
10
+ export const GET: ProxyHandler;
11
+ export const POST: ProxyHandler;
package/proxy.js ADDED
@@ -0,0 +1,54 @@
1
+ // For Next.js: the tracker and its data through the site's own domain (docs/proxy), as an App Router route at
2
+ // app/stats/[...path]/route.ts holding `export { GET, POST } from '@deltabreen/analytics-next/proxy'`. Unlike a rewrite
3
+ // it passes on the visitor's address on every host (next start alone included), sends the collector only the
4
+ // headers it reads (Global Privacy Control among them) and never the site's cookies, and opens only the collector's
5
+ // own paths.
6
+
7
+ const COLLECTOR = 'https://dbreencdn.eu';
8
+ const PATHS = /^(s\/ans_[A-Za-z0-9_-]{8,40}\.js|m\/[a-z]+\.js|e|r|snap|sv)$/;
9
+ const ASKED = ['origin', 'sec-gpc', 'sec-ch-ua', 'sec-ch-ua-mobile', 'sec-ch-ua-platform', 'content-type', 'if-none-match', 'x-forwarded-for'];
10
+ // fetch writes these itself when they are missing (a script sends none), which would make it look like a browser:
11
+ // they go empty instead, and the browser's Sec-Fetch-Mode, which fetch always replaces, goes as X-Dba-Fetch-Mode.
12
+ const ALWAYS = { 'user-agent': 'user-agent', 'accept-language': 'accept-language', 'x-dba-fetch-mode': 'sec-fetch-mode' };
13
+ const ANSWERED = ['content-type', 'cache-control', 'etag', 'last-modified', 'retry-after'];
14
+
15
+ /** GET and POST for the route, sending on to the collector given (the one on Sites → your site → Install). */
16
+ export function analyticsProxy({ collector = COLLECTOR } = {}) {
17
+ const base = String(collector).replace(/\/+$/, '');
18
+ if (!/^https?:\/\/[^/?#]+$/.test(base)) throw new Error('collector must be an address such as https://dbreencdn.eu');
19
+ const handle = async (req, ctx) => {
20
+ const params = await ctx?.params;
21
+ const parts = Object.values(params ?? {}).find(Array.isArray) ?? [];
22
+ const path = parts.join('/');
23
+ const script = path.endsWith('.js');
24
+ if (!PATHS.test(path) || script !== (req.method === 'GET' || req.method === 'HEAD')) return new Response(null, { status: 404 });
25
+ const headers = new Headers();
26
+ for (const h of ASKED) {
27
+ const v = req.headers.get(h);
28
+ if (v !== null) headers.set(h, v);
29
+ }
30
+ for (const [h, from] of Object.entries(ALWAYS)) headers.set(h, req.headers.get(from) ?? '');
31
+ let res;
32
+ try {
33
+ res = await fetch(`${base}/${path}${new URL(req.url).search}`, {
34
+ method: req.method,
35
+ headers,
36
+ body: script ? undefined : req.body,
37
+ duplex: 'half',
38
+ cache: 'no-store',
39
+ redirect: 'manual',
40
+ });
41
+ } catch {
42
+ return new Response(null, { status: 502 });
43
+ }
44
+ const out = new Headers();
45
+ for (const h of ANSWERED) {
46
+ const v = res.headers.get(h);
47
+ if (v !== null) out.set(h, v);
48
+ }
49
+ return new Response(req.method === 'HEAD' ? null : res.body, { status: res.status, headers: out });
50
+ };
51
+ return { GET: handle, POST: handle };
52
+ }
53
+
54
+ export const { GET, POST } = analyticsProxy();