Skip to content

Commit 339d2c1

Browse files
committed
docs: align integration snippets with current core API
1 parent 94b71c3 commit 339d2c1

12 files changed

Lines changed: 86 additions & 54 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import { segmentBackend } from "@nextlytics/core/backends/segment";
4848
// Optional: import your auth library to track authenticated users
4949
import { auth } from "./lib/auth"; // next-auth
5050

51-
export const { middleware, handlers, analytics } = Nextlytics({
51+
export const { middleware, analytics } = Nextlytics({
5252
backends: [
5353
segmentBackend({
5454
writeKey: process.env.SEGMENT_WRITE_KEY!,

packages/core/src/plugins/vercel-geo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export type VercelGeoPluginOptions = {
2626
* ```ts
2727
* import { vercelGeoPlugin } from "@nextlytics/core/plugins/vercel-geo";
2828
*
29-
* export const { middleware, handlers } = Nextlytics({
29+
* export const { middleware } = Nextlytics({
3030
* plugins: [vercelGeoPlugin()],
3131
* // ...
3232
* });

packages/website/src/copy/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function configSnippet(opts: ConfigSnippetOptions): string {
4747
"// Optional: import your auth library to track authenticated users",
4848
authImport,
4949
"",
50-
"export const { middleware, handlers, analytics } = Nextlytics({",
50+
"export const { middleware, analytics } = Nextlytics({",
5151
" backends: [",
5252
opts.backendConfig,
5353
" ],",

packages/website/src/copy/integrations/clickhouse/documentation.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import { Nextlytics } from "@nextlytics/core/server";
55
import { clickhouseBackend } from "@nextlytics/core/backends/clickhouse";
66

7-
export const { middleware, handlers, analytics } = Nextlytics({
7+
export const { middleware, analytics } = Nextlytics({
88
backends: [
99
clickhouseBackend({
1010
// Required. ClickHouse HTTP API URL.
@@ -70,11 +70,11 @@ On first use, if the table doesn't exist, Nextlytics prints the required SQL. Th
7070

7171
- **ReplacingMergeTree** - enables deduplication for updates
7272
- **PARTITION BY month** - optimal partition size for analytics
73-
- **ORDER BY event_id** - enables efficient updates via insert
73+
- **ORDER BY (timestamp, event_id)** - enables efficient time-range queries and updates
7474

7575
```sql
7676
CREATE TABLE IF NOT EXISTS default.analytics (event_id String, timestamp DateTime64(3))
77-
ENGINE = ReplacingMergeTree() PARTITION BY toYYYYMM(timestamp) ORDER BY event_id;
77+
ENGINE = ReplacingMergeTree() PARTITION BY toYYYYMM(timestamp) ORDER BY (timestamp, event_id);
7878

7979
ALTER TABLE default.analytics ADD COLUMN IF NOT EXISTS parent_event_id Nullable(String);
8080
ALTER TABLE default.analytics ADD COLUMN IF NOT EXISTS type LowCardinality(String);

packages/website/src/copy/integrations/debug-logging/documentation.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import { Nextlytics } from "@nextlytics/core/server";
55
import { loggingBackend } from "@nextlytics/core/backends/logging";
66

7-
export const { middleware, handlers, analytics } = Nextlytics({
7+
export const { middleware, analytics } = Nextlytics({
88
backends: [loggingBackend()],
99
});
1010
```
@@ -62,7 +62,7 @@ import { Nextlytics } from "@nextlytics/core/server";
6262
import { loggingBackend } from "@nextlytics/core/backends/logging";
6363
import { posthogBackend } from "@nextlytics/core/backends/posthog";
6464

65-
export const { middleware, handlers, analytics } = Nextlytics({
65+
export const { middleware, analytics } = Nextlytics({
6666
backends: [
6767
// Always log in development
6868
...(process.env.NODE_ENV === "development" ? [loggingBackend()] : []),
@@ -89,7 +89,7 @@ Use in tests to verify events are being tracked correctly:
8989

9090
```typescript
9191
// In your test setup
92-
const { middleware, handlers, analytics } = Nextlytics({
92+
const { middleware, analytics } = Nextlytics({
9393
backends: [loggingBackend()],
9494
});
9595

packages/website/src/copy/integrations/google-analytics/documentation.mdx

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
import { Nextlytics } from "@nextlytics/core/server";
55
import { googleAnalyticsBackend } from "@nextlytics/core/backends/ga";
66

7-
export const { middleware, handlers, analytics } = Nextlytics({
7+
export const { middleware, analytics } = Nextlytics({
88
backends: [
99
googleAnalyticsBackend({
1010
// Required. Your GA4 Measurement ID.
1111
// Find it: GA4 Admin -> Data Streams -> Your Stream -> Measurement ID
1212
measurementId: "G-XXXXXXXXXX",
1313

14-
// Required for server-side events (everything except pageView).
14+
// Optional but recommended for Measurement Protocol delivery.
15+
// Required for server-origin custom events, and for client-origin custom
16+
// events when preferClientSideForClientEvents is false.
1517
// Find it: GA4 Admin -> Data Streams -> Measurement Protocol API secrets
1618
apiSecret: "your-api-secret",
1719

@@ -23,6 +25,11 @@ export const { middleware, handlers, analytics } = Nextlytics({
2325
// - "gaCookie" (default): Use _ga cookie set by gtag.js, fall back to anonymousUserId
2426
// - "anonymousUserId": Always use Nextlytics anonymousUserId
2527
clientIdSource: "gaCookie",
28+
29+
// Optional. For client-origin custom events:
30+
// - true (default): send via gtag in browser
31+
// - false: send via Measurement Protocol (requires apiSecret)
32+
preferClientSideForClientEvents: true,
2633
}),
2734
],
2835
});
@@ -34,7 +41,8 @@ Google Analytics requires page views to come from the browser. It needs to run J
3441
browser metadata and measure engagement. Because of this limitation, Nextlytics uses a **hybrid approach**:
3542

3643
- **Page views** are sent client-side via a JavaScript snippet
37-
- **All other events** are sent server-side via the Measurement Protocol
44+
- **Server-origin custom events** are sent via Measurement Protocol when `apiSecret` is configured
45+
- **Client-origin custom events** are sent client-side by default
3846

3947
### Page View Events
4048

@@ -46,9 +54,15 @@ The snippet maps to GA4's `page_view` event.
4654

4755
### Other Events
4856

49-
All other events (purchases, form submissions, custom events) are sent server-side via the
50-
[Measurement Protocol](https://developers.google.com/analytics/devguides/collection/protocol/ga4).
51-
This means 100% delivery rate - no ad blockers can interfere.
57+
Custom events (purchases, form submissions, API/server events) can be delivered in two modes:
58+
59+
- Server-origin events use
60+
[Measurement Protocol](https://developers.google.com/analytics/devguides/collection/protocol/ga4)
61+
when `apiSecret` is configured.
62+
- Client-origin events use `gtag('event', ...)` by default.
63+
64+
To force Measurement Protocol for client-origin events too, set
65+
`preferClientSideForClientEvents: false` and provide `apiSecret`.
5266

5367
```typescript
5468
const { analytics } = await import("./nextlytics");
@@ -75,7 +89,7 @@ is identified, Nextlytics sends only `user_id` to GA4. For privacy, email, name,
7589
Custom traits are sent as user properties.
7690

7791
```typescript
78-
export const { middleware, handlers, analytics } = Nextlytics({
92+
export const { middleware, analytics } = Nextlytics({
7993
callbacks: {
8094
getUser: async (ctx) => {
8195
const session = await getSession(ctx);
@@ -101,7 +115,7 @@ export const { middleware, handlers, analytics } = Nextlytics({
101115
(gtag.js hasn't run). Nextlytics falls back to `anonymousUserId` for that request. Subsequent requests use
102116
the real `_ga` cookie.
103117
- **Page views require JavaScript.** This is a GA limitation, not Nextlytics.
104-
- **Real-time reports may be delayed.** Server-side events can take a few minutes to appear in GA4.
118+
- **Real-time reports may be delayed.** Measurement Protocol events can take a few minutes to appear in GA4.
105119

106120
## Debugging
107121

packages/website/src/copy/integrations/google-tag-manager/documentation.mdx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import { Nextlytics } from "@nextlytics/core/server";
55
import { googleTagManagerBackend } from "@nextlytics/core/backends/gtm";
66

7-
export const { middleware, handlers, analytics } = Nextlytics({
7+
export const { middleware, analytics } = Nextlytics({
88
backends: [
99
googleTagManagerBackend({
1010
// Required. Your GTM Container ID.
@@ -37,10 +37,12 @@ dataLayer.push({
3737
eventId: "nl_abc123",
3838
page_path: "/products",
3939
page_title: "Products",
40-
page_location: "https://example.com/products",
40+
page_location: "example.com/products",
4141
});
4242
```
4343

44+
`page_location` is built from `serverContext.host + serverContext.path`.
45+
4446
### Custom Events
4547

4648
Custom events are converted to snake_case and pushed to `dataLayer`:
@@ -66,7 +68,7 @@ dataLayer.push({
6668
When a user is identified, their data is pushed to `dataLayer`:
6769

6870
```typescript
69-
export const { middleware, handlers, analytics } = Nextlytics({
71+
export const { middleware, analytics } = Nextlytics({
7072
callbacks: {
7173
getUser: async (ctx) => {
7274
const session = await getSession(ctx);

packages/website/src/copy/integrations/neon/documentation.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import { Nextlytics } from "@nextlytics/core/server";
55
import { neonBackend } from "@nextlytics/core/backends/neon";
66

7-
export const { middleware, handlers, analytics } = Nextlytics({
7+
export const { middleware, analytics } = Nextlytics({
88
backends: [
99
neonBackend({
1010
// Required. Your Neon database URL.

packages/website/src/copy/integrations/posthog/documentation.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import { Nextlytics } from "@nextlytics/core/server";
55
import { posthogBackend } from "@nextlytics/core/backends/posthog";
66

7-
export const { middleware, handlers, analytics } = Nextlytics({
7+
export const { middleware, analytics } = Nextlytics({
88
backends: [
99
posthogBackend({
1010
// Required. Your PostHog project API key.
@@ -90,7 +90,7 @@ When a user is identified via `callbacks.getUser`, their `userId` becomes the `d
9090
traits are sent via PostHog's `$set` mechanism:
9191

9292
```typescript
93-
export const { middleware, handlers, analytics } = Nextlytics({
93+
export const { middleware, analytics } = Nextlytics({
9494
callbacks: {
9595
getUser: async (ctx) => {
9696
const session = await getSession(ctx);

packages/website/src/copy/integrations/segment/documentation.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import { Nextlytics } from "@nextlytics/core/server";
55
import { segmentBackend } from "@nextlytics/core/backends/segment";
66

7-
export const { middleware, handlers, analytics } = Nextlytics({
7+
export const { middleware, analytics } = Nextlytics({
88
backends: [
99
segmentBackend({
1010
// Required. Your Segment write key.
@@ -41,7 +41,7 @@ All events are sent server-side via Segment's HTTP Tracking API. This means:
4141

4242
### Page Views
4343

44-
Page views are sent via `/v1/page`:
44+
Page views are sent in a batch to `/v1/batch` with `type: "page"`:
4545

4646
```typescript
4747
// Nextlytics
@@ -62,7 +62,7 @@ await api.sendEvent("pageView");
6262

6363
### Custom Events
6464

65-
All other events are sent via `/v1/track`:
65+
All other events are sent in a batch to `/v1/batch` with `type: "track"`:
6666

6767
```typescript
6868
await api.sendEvent("purchase", {
@@ -113,7 +113,7 @@ Segment's context object is populated with available data:
113113
Users are identified via `anonymousId` and `userId`:
114114

115115
```typescript
116-
export const { middleware, handlers, analytics } = Nextlytics({
116+
export const { middleware, analytics } = Nextlytics({
117117
callbacks: {
118118
getUser: async (ctx) => {
119119
const session = await getSession(ctx);

0 commit comments

Comments
 (0)