@nextlytics/core 0.2.0 → 0.2.1-canary.55
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/dist/anonymous-user.js +26 -2
- package/dist/backends/clickhouse.js +32 -14
- package/dist/backends/ga.js +26 -2
- package/dist/backends/gtm.js +26 -2
- package/dist/backends/lib/db.js +33 -2
- package/dist/backends/logging.js +26 -2
- package/dist/backends/neon.js +41 -20
- package/dist/backends/postgrest.js +33 -8
- package/dist/backends/posthog.js +26 -2
- package/dist/backends/segment.js +26 -2
- package/dist/client.js +41 -16
- package/dist/config-helpers.js +28 -2
- package/dist/handlers.js +35 -11
- package/dist/headers.js +26 -2
- package/dist/index.js +35 -6
- package/dist/middleware.js +47 -26
- package/dist/pages-router.js +28 -4
- package/dist/plugins/vercel-geo.js +26 -2
- package/dist/server-component-context.js +29 -3
- package/dist/server.js +60 -35
- package/dist/template.js +27 -2
- package/dist/types.js +16 -0
- package/dist/uitils.js +30 -4
- package/package.json +28 -101
- package/dist/anonymous-user.cjs +0 -118
- package/dist/anonymous-user.d.mts +0 -22
- package/dist/backends/clickhouse.cjs +0 -110
- package/dist/backends/clickhouse.d.mts +0 -58
- package/dist/backends/ga.cjs +0 -207
- package/dist/backends/ga.d.mts +0 -21
- package/dist/backends/gtm.cjs +0 -155
- package/dist/backends/gtm.d.mts +0 -11
- package/dist/backends/lib/db.cjs +0 -150
- package/dist/backends/lib/db.d.mts +0 -121
- package/dist/backends/logging.cjs +0 -45
- package/dist/backends/logging.d.mts +0 -7
- package/dist/backends/neon.cjs +0 -84
- package/dist/backends/neon.d.mts +0 -11
- package/dist/backends/postgrest.cjs +0 -98
- package/dist/backends/postgrest.d.mts +0 -46
- package/dist/backends/posthog.cjs +0 -120
- package/dist/backends/posthog.d.mts +0 -13
- package/dist/backends/segment.cjs +0 -112
- package/dist/backends/segment.d.mts +0 -43
- package/dist/client.cjs +0 -171
- package/dist/client.d.mts +0 -29
- package/dist/config-helpers.cjs +0 -71
- package/dist/config-helpers.d.mts +0 -16
- package/dist/handlers.cjs +0 -123
- package/dist/handlers.d.mts +0 -9
- package/dist/headers.cjs +0 -41
- package/dist/headers.d.mts +0 -3
- package/dist/index.cjs +0 -41
- package/dist/index.d.mts +0 -9
- package/dist/middleware.cjs +0 -204
- package/dist/middleware.d.mts +0 -10
- package/dist/pages-router.cjs +0 -45
- package/dist/pages-router.d.mts +0 -45
- package/dist/plugins/vercel-geo.cjs +0 -60
- package/dist/plugins/vercel-geo.d.mts +0 -25
- package/dist/server-component-context.cjs +0 -95
- package/dist/server-component-context.d.mts +0 -30
- package/dist/server.cjs +0 -236
- package/dist/server.d.mts +0 -13
- package/dist/template.cjs +0 -108
- package/dist/template.d.mts +0 -27
- package/dist/types.cjs +0 -16
- package/dist/types.d.mts +0 -216
- package/dist/uitils.cjs +0 -94
- package/dist/uitils.d.mts +0 -22
package/dist/middleware.cjs
DELETED
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var middleware_exports = {};
|
|
20
|
-
__export(middleware_exports, {
|
|
21
|
-
createNextlyticsMiddleware: () => createNextlyticsMiddleware
|
|
22
|
-
});
|
|
23
|
-
module.exports = __toCommonJS(middleware_exports);
|
|
24
|
-
var import_server = require("next/server");
|
|
25
|
-
var import_server_component_context = require("./server-component-context");
|
|
26
|
-
var import_uitils = require("./uitils");
|
|
27
|
-
var import_anonymous_user = require("./anonymous-user");
|
|
28
|
-
function resolveBackends(config, ctx) {
|
|
29
|
-
const backends = config.backends || [];
|
|
30
|
-
return backends.map((backend) => typeof backend === "function" ? backend(ctx) : backend).filter((b) => b !== null);
|
|
31
|
-
}
|
|
32
|
-
function collectTemplates(backends) {
|
|
33
|
-
const templates = {};
|
|
34
|
-
for (const backend of backends) {
|
|
35
|
-
if (backend.getClientSideTemplates) {
|
|
36
|
-
Object.assign(templates, backend.getClientSideTemplates());
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
return templates;
|
|
40
|
-
}
|
|
41
|
-
function createRequestContext(request) {
|
|
42
|
-
return {
|
|
43
|
-
headers: request.headers,
|
|
44
|
-
cookies: request.cookies
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
function createNextlyticsMiddleware(config, dispatchEvent, updateEvent) {
|
|
48
|
-
const { eventEndpoint } = config;
|
|
49
|
-
return async (request) => {
|
|
50
|
-
const pathname = request.nextUrl.pathname;
|
|
51
|
-
const reqInfo = (0, import_uitils.getRequestInfo)(request);
|
|
52
|
-
if (pathname === eventEndpoint) {
|
|
53
|
-
if (request.method === "POST") {
|
|
54
|
-
return handleEventPost(request, config, dispatchEvent, updateEvent);
|
|
55
|
-
}
|
|
56
|
-
return Response.json({ error: "Method not allowed" }, { status: 405 });
|
|
57
|
-
}
|
|
58
|
-
if (reqInfo.isNextjsInternal || reqInfo.isPrefetch || reqInfo.isStaticFile) {
|
|
59
|
-
return import_server.NextResponse.next();
|
|
60
|
-
}
|
|
61
|
-
const pageRenderId = (0, import_uitils.generateId)();
|
|
62
|
-
const serverContext = (0, import_uitils.createServerContext)(request);
|
|
63
|
-
const response = import_server.NextResponse.next();
|
|
64
|
-
const ctx = createRequestContext(request);
|
|
65
|
-
const { anonId } = await (0, import_anonymous_user.resolveAnonymousUser)({ ctx, serverContext, config, response });
|
|
66
|
-
const backends = resolveBackends(config, ctx);
|
|
67
|
-
const templates = collectTemplates(backends);
|
|
68
|
-
let scripts = [];
|
|
69
|
-
if (config.pageViewMode !== "client-init") {
|
|
70
|
-
if (config.excludePaths?.(pathname)) {
|
|
71
|
-
(0, import_server_component_context.serializeServerComponentContext)(response, {
|
|
72
|
-
pageRenderId,
|
|
73
|
-
pathname: request.nextUrl.pathname,
|
|
74
|
-
search: request.nextUrl.search,
|
|
75
|
-
scripts,
|
|
76
|
-
templates
|
|
77
|
-
});
|
|
78
|
-
return response;
|
|
79
|
-
}
|
|
80
|
-
const isApiPath = config.isApiPath(pathname);
|
|
81
|
-
if (isApiPath && config.excludeApiCalls) {
|
|
82
|
-
(0, import_server_component_context.serializeServerComponentContext)(response, {
|
|
83
|
-
pageRenderId,
|
|
84
|
-
pathname: request.nextUrl.pathname,
|
|
85
|
-
search: request.nextUrl.search,
|
|
86
|
-
scripts,
|
|
87
|
-
templates
|
|
88
|
-
});
|
|
89
|
-
return response;
|
|
90
|
-
}
|
|
91
|
-
const userContext = await getUserContext(config, ctx);
|
|
92
|
-
const pageViewEvent = createPageViewEvent(
|
|
93
|
-
pageRenderId,
|
|
94
|
-
serverContext,
|
|
95
|
-
isApiPath,
|
|
96
|
-
userContext,
|
|
97
|
-
anonId
|
|
98
|
-
);
|
|
99
|
-
const { clientActions, completion } = dispatchEvent(pageViewEvent, ctx);
|
|
100
|
-
const actions = await clientActions;
|
|
101
|
-
scripts = actions.items.filter(
|
|
102
|
-
(i) => i.type === "script-template"
|
|
103
|
-
);
|
|
104
|
-
(0, import_server.after)(() => completion);
|
|
105
|
-
}
|
|
106
|
-
(0, import_server_component_context.serializeServerComponentContext)(response, {
|
|
107
|
-
pageRenderId,
|
|
108
|
-
pathname: request.nextUrl.pathname,
|
|
109
|
-
search: request.nextUrl.search,
|
|
110
|
-
scripts,
|
|
111
|
-
templates
|
|
112
|
-
});
|
|
113
|
-
return response;
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
function createPageViewEvent(pageRenderId, serverContext, isApiPath, userContext, anonymousUserId) {
|
|
117
|
-
const eventType = isApiPath ? "apiCall" : "pageView";
|
|
118
|
-
return {
|
|
119
|
-
collectedAt: serverContext.collectedAt.toISOString(),
|
|
120
|
-
eventId: pageRenderId,
|
|
121
|
-
type: eventType,
|
|
122
|
-
anonymousUserId,
|
|
123
|
-
serverContext,
|
|
124
|
-
userContext,
|
|
125
|
-
properties: {}
|
|
126
|
-
};
|
|
127
|
-
}
|
|
128
|
-
async function getUserContext(config, ctx) {
|
|
129
|
-
if (!config.callbacks.getUser) return void 0;
|
|
130
|
-
try {
|
|
131
|
-
return await config.callbacks.getUser(ctx) || void 0;
|
|
132
|
-
} catch {
|
|
133
|
-
return void 0;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
async function handleEventPost(request, config, dispatchEvent, updateEvent) {
|
|
137
|
-
const pageRenderId = request.headers.get(import_server_component_context.headers.pageRenderId);
|
|
138
|
-
if (!pageRenderId) {
|
|
139
|
-
return Response.json({ error: "Missing page render ID" }, { status: 400 });
|
|
140
|
-
}
|
|
141
|
-
let body;
|
|
142
|
-
try {
|
|
143
|
-
body = await request.json();
|
|
144
|
-
} catch {
|
|
145
|
-
return Response.json({ error: "Invalid JSON" }, { status: 400 });
|
|
146
|
-
}
|
|
147
|
-
const { type, payload } = body;
|
|
148
|
-
const ctx = createRequestContext(request);
|
|
149
|
-
const serverContext = (0, import_uitils.createServerContext)(request);
|
|
150
|
-
const userContext = await getUserContext(config, ctx);
|
|
151
|
-
const { anonId: anonymousUserId } = await (0, import_anonymous_user.resolveAnonymousUser)({ ctx, serverContext, config });
|
|
152
|
-
if (type === "client-init") {
|
|
153
|
-
const clientContext = payload;
|
|
154
|
-
if (clientContext?.path) {
|
|
155
|
-
serverContext.path = clientContext.path;
|
|
156
|
-
}
|
|
157
|
-
if (config.pageViewMode === "client-init") {
|
|
158
|
-
const event = {
|
|
159
|
-
eventId: pageRenderId,
|
|
160
|
-
type: "pageView",
|
|
161
|
-
collectedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
162
|
-
anonymousUserId,
|
|
163
|
-
serverContext,
|
|
164
|
-
clientContext,
|
|
165
|
-
userContext,
|
|
166
|
-
properties: {}
|
|
167
|
-
};
|
|
168
|
-
const { clientActions, completion } = dispatchEvent(event, ctx);
|
|
169
|
-
const actions = await clientActions;
|
|
170
|
-
(0, import_server.after)(() => completion);
|
|
171
|
-
const scripts = actions.items.filter((i) => i.type === "script-template");
|
|
172
|
-
return Response.json({ ok: true, scripts: scripts.length > 0 ? scripts : void 0 });
|
|
173
|
-
} else {
|
|
174
|
-
(0, import_server.after)(() => updateEvent(pageRenderId, { clientContext, userContext, anonymousUserId }, ctx));
|
|
175
|
-
return Response.json({ ok: true });
|
|
176
|
-
}
|
|
177
|
-
} else if (type === "client-event") {
|
|
178
|
-
const clientContext = payload.clientContext || void 0;
|
|
179
|
-
if (clientContext?.path) {
|
|
180
|
-
serverContext.path = clientContext.path;
|
|
181
|
-
}
|
|
182
|
-
const event = {
|
|
183
|
-
eventId: (0, import_uitils.generateId)(),
|
|
184
|
-
parentEventId: pageRenderId,
|
|
185
|
-
type: payload.name || type,
|
|
186
|
-
collectedAt: payload.collectedAt || (/* @__PURE__ */ new Date()).toISOString(),
|
|
187
|
-
anonymousUserId,
|
|
188
|
-
serverContext,
|
|
189
|
-
clientContext,
|
|
190
|
-
userContext,
|
|
191
|
-
properties: payload.props || {}
|
|
192
|
-
};
|
|
193
|
-
const { clientActions, completion } = dispatchEvent(event, ctx);
|
|
194
|
-
const actions = await clientActions;
|
|
195
|
-
(0, import_server.after)(() => completion);
|
|
196
|
-
const scripts = actions.items.filter((i) => i.type === "script-template");
|
|
197
|
-
return Response.json({ ok: true, scripts: scripts.length > 0 ? scripts : void 0 });
|
|
198
|
-
}
|
|
199
|
-
return Response.json({ ok: true });
|
|
200
|
-
}
|
|
201
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
202
|
-
0 && (module.exports = {
|
|
203
|
-
createNextlyticsMiddleware
|
|
204
|
-
});
|
package/dist/middleware.d.mts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { NextMiddleware } from 'next/server';
|
|
2
|
-
import { NextlyticsEvent, RequestContext, DispatchResult } from './types.mjs';
|
|
3
|
-
import { NextlyticsConfigWithDefaults } from './config-helpers.mjs';
|
|
4
|
-
import 'next/dist/server/web/spec-extension/cookies';
|
|
5
|
-
|
|
6
|
-
type DispatchEvent = (event: NextlyticsEvent, ctx: RequestContext) => DispatchResult;
|
|
7
|
-
type UpdateEvent = (eventId: string, patch: Partial<NextlyticsEvent>, ctx: RequestContext) => Promise<void>;
|
|
8
|
-
declare function createNextlyticsMiddleware(config: NextlyticsConfigWithDefaults, dispatchEvent: DispatchEvent, updateEvent: UpdateEvent): NextMiddleware;
|
|
9
|
-
|
|
10
|
-
export { createNextlyticsMiddleware };
|
package/dist/pages-router.cjs
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var pages_router_exports = {};
|
|
20
|
-
__export(pages_router_exports, {
|
|
21
|
-
getNextlyticsProps: () => getNextlyticsProps
|
|
22
|
-
});
|
|
23
|
-
module.exports = __toCommonJS(pages_router_exports);
|
|
24
|
-
var import_server_component_context = require("./server-component-context");
|
|
25
|
-
function getNextlyticsProps(ctx) {
|
|
26
|
-
const headersList = new Headers();
|
|
27
|
-
for (const [key, value] of Object.entries(ctx.req.headers)) {
|
|
28
|
-
if (value) {
|
|
29
|
-
headersList.set(key, Array.isArray(value) ? value[0] : value);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
const context = (0, import_server_component_context.restoreServerComponentContext)(headersList);
|
|
33
|
-
if (!context) {
|
|
34
|
-
return { requestId: "" };
|
|
35
|
-
}
|
|
36
|
-
return {
|
|
37
|
-
requestId: context.pageRenderId,
|
|
38
|
-
scripts: context.scripts,
|
|
39
|
-
templates: context.templates
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
43
|
-
0 && (module.exports = {
|
|
44
|
-
getNextlyticsProps
|
|
45
|
-
});
|
package/dist/pages-router.d.mts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { NextlyticsContext } from './client.mjs';
|
|
2
|
-
import 'react/jsx-runtime';
|
|
3
|
-
import 'react';
|
|
4
|
-
import './types.mjs';
|
|
5
|
-
import 'next/dist/server/web/spec-extension/cookies';
|
|
6
|
-
import 'next/server';
|
|
7
|
-
|
|
8
|
-
type ContextWithHeaders = {
|
|
9
|
-
req: {
|
|
10
|
-
headers: Record<string, string | string[] | undefined>;
|
|
11
|
-
};
|
|
12
|
-
};
|
|
13
|
-
/**
|
|
14
|
-
* Extract Nextlytics context from Pages Router context (getServerSideProps or getInitialProps).
|
|
15
|
-
* Use this in _app.tsx with getInitialProps to pass context to NextlyticsClient.
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* ```tsx
|
|
19
|
-
* // pages/_app.tsx
|
|
20
|
-
* import type { AppContext, AppProps } from 'next/app'
|
|
21
|
-
* import { NextlyticsClient, getNextlyticsProps, type NextlyticsContext } from '@nextlytics/core'
|
|
22
|
-
*
|
|
23
|
-
* type MyAppProps = AppProps & { nextlyticsCtx: NextlyticsContext }
|
|
24
|
-
*
|
|
25
|
-
* function MyApp({ Component, pageProps, nextlyticsCtx }: MyAppProps) {
|
|
26
|
-
* return (
|
|
27
|
-
* <NextlyticsClient ctx={nextlyticsCtx}>
|
|
28
|
-
* <Component {...pageProps} />
|
|
29
|
-
* </NextlyticsClient>
|
|
30
|
-
* )
|
|
31
|
-
* }
|
|
32
|
-
*
|
|
33
|
-
* MyApp.getInitialProps = async (appContext: AppContext) => {
|
|
34
|
-
* return {
|
|
35
|
-
* pageProps: appContext.Component.getInitialProps
|
|
36
|
-
* ? await appContext.Component.getInitialProps(appContext.ctx)
|
|
37
|
-
* : {},
|
|
38
|
-
* nextlyticsCtx: getNextlyticsProps(appContext.ctx),
|
|
39
|
-
* }
|
|
40
|
-
* }
|
|
41
|
-
* ```
|
|
42
|
-
*/
|
|
43
|
-
declare function getNextlyticsProps(ctx: ContextWithHeaders): NextlyticsContext;
|
|
44
|
-
|
|
45
|
-
export { getNextlyticsProps };
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var vercel_geo_exports = {};
|
|
20
|
-
__export(vercel_geo_exports, {
|
|
21
|
-
vercelGeoPlugin: () => vercelGeoPlugin
|
|
22
|
-
});
|
|
23
|
-
module.exports = __toCommonJS(vercel_geo_exports);
|
|
24
|
-
const VERCEL_GEO_HEADERS = {
|
|
25
|
-
"x-vercel-ip-country": "country",
|
|
26
|
-
"x-vercel-ip-country-region": "region",
|
|
27
|
-
"x-vercel-ip-city": "city",
|
|
28
|
-
"x-vercel-ip-latitude": "latitude",
|
|
29
|
-
"x-vercel-ip-longitude": "longitude",
|
|
30
|
-
"x-vercel-ip-timezone": "timezone"
|
|
31
|
-
};
|
|
32
|
-
function vercelGeoPlugin(options) {
|
|
33
|
-
const geoPropertyName = options?.geoPropertyName ?? "geo";
|
|
34
|
-
return {
|
|
35
|
-
async onDispatch(event) {
|
|
36
|
-
const headers = event.serverContext.requestHeaders;
|
|
37
|
-
const geo = {};
|
|
38
|
-
for (const [header, prop] of Object.entries(VERCEL_GEO_HEADERS)) {
|
|
39
|
-
const value = headers[header];
|
|
40
|
-
if (value) {
|
|
41
|
-
if (prop === "latitude" || prop === "longitude") {
|
|
42
|
-
const num = parseFloat(value);
|
|
43
|
-
if (!isNaN(num)) {
|
|
44
|
-
geo[prop] = num;
|
|
45
|
-
}
|
|
46
|
-
} else {
|
|
47
|
-
geo[prop] = value;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
if (Object.keys(geo).length > 0) {
|
|
52
|
-
event.properties[geoPropertyName] = geo;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
58
|
-
0 && (module.exports = {
|
|
59
|
-
vercelGeoPlugin
|
|
60
|
-
});
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { NextlyticsPlugin } from '../types.mjs';
|
|
2
|
-
import 'next/dist/server/web/spec-extension/cookies';
|
|
3
|
-
import 'next/server';
|
|
4
|
-
|
|
5
|
-
type VercelGeoPluginOptions = {
|
|
6
|
-
/** Property name to store geo data under. Default: "geo" */
|
|
7
|
-
geoPropertyName?: string;
|
|
8
|
-
};
|
|
9
|
-
/**
|
|
10
|
-
* Plugin that extracts Vercel geo headers and adds them to event properties.
|
|
11
|
-
* Only works when deployed to Vercel.
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```ts
|
|
15
|
-
* import { vercelGeoPlugin } from "@nextlytics/core/plugins/vercel-geo";
|
|
16
|
-
*
|
|
17
|
-
* export const { middleware, handlers } = Nextlytics({
|
|
18
|
-
* plugins: [vercelGeoPlugin()],
|
|
19
|
-
* // ...
|
|
20
|
-
* });
|
|
21
|
-
* ```
|
|
22
|
-
*/
|
|
23
|
-
declare function vercelGeoPlugin(options?: VercelGeoPluginOptions): NextlyticsPlugin;
|
|
24
|
-
|
|
25
|
-
export { type VercelGeoPluginOptions, vercelGeoPlugin };
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var server_component_context_exports = {};
|
|
20
|
-
__export(server_component_context_exports, {
|
|
21
|
-
headers: () => headerKeys,
|
|
22
|
-
restoreServerComponentContext: () => restoreServerComponentContext,
|
|
23
|
-
serializeServerComponentContext: () => serializeServerComponentContext
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(server_component_context_exports);
|
|
26
|
-
const HEADER_PREFIX = "x-nc-";
|
|
27
|
-
const headerKeys = {
|
|
28
|
-
pathname: `${HEADER_PREFIX}pathname`,
|
|
29
|
-
search: `${HEADER_PREFIX}search`,
|
|
30
|
-
pageRenderId: `${HEADER_PREFIX}page-render-id`,
|
|
31
|
-
scripts: `${HEADER_PREFIX}scripts`,
|
|
32
|
-
templates: `${HEADER_PREFIX}templates`
|
|
33
|
-
};
|
|
34
|
-
function serializeServerComponentContext(response, ctx) {
|
|
35
|
-
response.headers.set(headerKeys.pageRenderId, ctx.pageRenderId);
|
|
36
|
-
response.headers.set(headerKeys.pathname, ctx.pathname);
|
|
37
|
-
response.headers.set(headerKeys.search, ctx.search);
|
|
38
|
-
if (ctx.scripts.length > 0) {
|
|
39
|
-
const scriptParts = ctx.scripts.filter((item) => item.type === "script-template").map((s) => `${s.templateId}=${JSON.stringify(s.params)}`);
|
|
40
|
-
if (scriptParts.length > 0) {
|
|
41
|
-
response.headers.set(headerKeys.scripts, scriptParts.join(";"));
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
if (Object.keys(ctx.templates).length > 0) {
|
|
45
|
-
response.headers.set(headerKeys.templates, JSON.stringify(ctx.templates));
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
function parseScriptsHeader(header) {
|
|
49
|
-
const scripts = [];
|
|
50
|
-
for (const part of header.split(";")) {
|
|
51
|
-
const eqIdx = part.indexOf("=");
|
|
52
|
-
if (eqIdx === -1) continue;
|
|
53
|
-
const templateId = part.slice(0, eqIdx);
|
|
54
|
-
const paramsJson = part.slice(eqIdx + 1);
|
|
55
|
-
try {
|
|
56
|
-
const params = JSON.parse(paramsJson);
|
|
57
|
-
scripts.push({ type: "script-template", templateId, params });
|
|
58
|
-
} catch {
|
|
59
|
-
console.warn(`[Nextlytics] Failed to parse script params for ${templateId}`);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
return scripts;
|
|
63
|
-
}
|
|
64
|
-
function restoreServerComponentContext(headersList) {
|
|
65
|
-
const pageRenderId = headersList.get(headerKeys.pageRenderId);
|
|
66
|
-
if (!pageRenderId) {
|
|
67
|
-
return null;
|
|
68
|
-
}
|
|
69
|
-
const pathname = headersList.get(headerKeys.pathname) || "";
|
|
70
|
-
const search = headersList.get(headerKeys.search) || "";
|
|
71
|
-
const scriptsHeader = headersList.get(headerKeys.scripts);
|
|
72
|
-
const scripts = scriptsHeader ? parseScriptsHeader(scriptsHeader) : [];
|
|
73
|
-
let templates = {};
|
|
74
|
-
const templatesHeader = headersList.get(headerKeys.templates);
|
|
75
|
-
if (templatesHeader) {
|
|
76
|
-
try {
|
|
77
|
-
templates = JSON.parse(templatesHeader);
|
|
78
|
-
} catch {
|
|
79
|
-
console.warn("[Nextlytics] Failed to parse templates header");
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
return {
|
|
83
|
-
pageRenderId,
|
|
84
|
-
pathname,
|
|
85
|
-
search,
|
|
86
|
-
scripts,
|
|
87
|
-
templates
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
91
|
-
0 && (module.exports = {
|
|
92
|
-
headers,
|
|
93
|
-
restoreServerComponentContext,
|
|
94
|
-
serializeServerComponentContext
|
|
95
|
-
});
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { NextResponse } from 'next/server';
|
|
2
|
-
import { TemplatizedScriptInsertion, JavascriptTemplate } from './types.mjs';
|
|
3
|
-
import 'next/dist/server/web/spec-extension/cookies';
|
|
4
|
-
|
|
5
|
-
declare const headerKeys: {
|
|
6
|
-
readonly pathname: "x-nc-pathname";
|
|
7
|
-
readonly search: "x-nc-search";
|
|
8
|
-
readonly pageRenderId: "x-nc-page-render-id";
|
|
9
|
-
readonly scripts: "x-nc-scripts";
|
|
10
|
-
readonly templates: "x-nc-templates";
|
|
11
|
-
};
|
|
12
|
-
/** Context passed from middleware to server components via headers */
|
|
13
|
-
type ServerComponentContext = {
|
|
14
|
-
/** Unique page render ID (event ID) */
|
|
15
|
-
pageRenderId: string;
|
|
16
|
-
/** Request pathname */
|
|
17
|
-
pathname: string;
|
|
18
|
-
/** Query string */
|
|
19
|
-
search: string;
|
|
20
|
-
/** Script actions to execute on client */
|
|
21
|
-
scripts: TemplatizedScriptInsertion<unknown>[];
|
|
22
|
-
/** Template definitions for scripts */
|
|
23
|
-
templates: Record<string, JavascriptTemplate>;
|
|
24
|
-
};
|
|
25
|
-
/** Serialize context to response headers (called in middleware) */
|
|
26
|
-
declare function serializeServerComponentContext(response: NextResponse, ctx: ServerComponentContext): void;
|
|
27
|
-
/** Restore context from request headers (called in server components) */
|
|
28
|
-
declare function restoreServerComponentContext(headersList: Headers): ServerComponentContext | null;
|
|
29
|
-
|
|
30
|
-
export { type ServerComponentContext, headerKeys as headers, restoreServerComponentContext, serializeServerComponentContext };
|