Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/core/src/server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type {
NextlyticsResult,
RequestContext,
ServerEventContext,
UserContext,
} from "./types";
import { logConfigWarnings, validateConfig, withDefaults } from "./config-helpers";
import { createNextlyticsMiddleware } from "./middleware";
Expand Down Expand Up @@ -314,7 +315,7 @@ export function Nextlytics(userConfig: NextlyticsConfig): NextlyticsResult {
return {
sendEvent: async (
eventName: string,
opts?: { props?: Record<string, unknown> }
opts?: { props?: Record<string, unknown>; user?: UserContext }
): Promise<{ ok: boolean }> => {
// In the App Router no-arg path a missing pageRenderId means middleware
// never ran — keep that as a hard error. With an explicit `req` the
Expand All @@ -332,7 +333,9 @@ export function Nextlytics(userConfig: NextlyticsConfig): NextlyticsResult {
collectedAt: new Date().toISOString(),
anonymousUserId,
serverContext,
userContext,
// An explicit per-event user (e.g. an email recipient resolved from a
// link) overrides the request-derived one from callbacks.getUser.
userContext: opts?.user ?? userContext,
properties: { ...propsFromCallback, ...opts?.props },
};
// Await full delivery, not just dispatch kickoff. Unlike the middleware
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,13 @@ export type NextlyticsServerSide = {
/** Send custom event from a server component, server action, or API route */
sendEvent: (
eventName: string,
opts?: { props?: Record<string, unknown> }
opts?: {
props?: Record<string, unknown>;
/** Identify the event's user explicitly. Overrides callbacks.getUser for
* this event — e.g. an email recipient resolved from a link, where the
* request has no session to derive identity from. */
user?: UserContext;
}
) => Promise<{ ok: boolean }>;
};

Expand Down
Loading