Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[web] Include flags in custom events #140

Merged
merged 20 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 4 additions & 2 deletions packages/web/src/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ function inject(
*/
function track(
name: string,
properties?: Record<string, AllowedPropertyValues>
properties?: Record<string, AllowedPropertyValues>,
options?: { flags?: string[] }
): void {
if (!isBrowser()) {
const msg =
Expand All @@ -106,7 +107,7 @@ function track(
}

if (!properties) {
window.va?.('event', { name });
window.va?.('event', { name, options });
AndyBitz marked this conversation as resolved.
Show resolved Hide resolved
return;
}

Expand All @@ -118,6 +119,7 @@ function track(
window.va?.('event', {
name,
data: props,
options,
AndyBitz marked this conversation as resolved.
Show resolved Hide resolved
});
} catch (err) {
if (err instanceof Error && isDevelopment()) {
Expand Down
27 changes: 25 additions & 2 deletions packages/web/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,19 @@ interface ContextWithHeaders {

type Context = ContextWithRequest | ContextWithHeaders;

interface Options {
flags?: string[];
}

interface RequestContext {
get: () => {
headers: Record<string, string | undefined>;
url: string;
waitUntil?: (promise: Promise<unknown>) => void;
flags?: {
getValues: () => Record<string, unknown>;
reportValue: (key: string, value: unknown) => void;
};
};
}

Expand All @@ -33,7 +41,8 @@ const logPrefix = '[Vercel Web Analytics]';
export async function track(
eventName: string,
properties?: Record<string, AllowedPropertyValues>,
context?: Context
context?: Context,
options?: Options
tobiaslins marked this conversation as resolved.
Show resolved Hide resolved
): Promise<void> {
const ENDPOINT =
process.env.VERCEL_WEB_ANALYTICS_ENDPOINT || process.env.VERCEL_URL;
Expand Down Expand Up @@ -98,12 +107,26 @@ export async function track(

const url = new URL(origin);

let flagsToReport: Record<string, unknown> = {};
const allFlags = requestContext?.flags?.getValues();
AndyBitz marked this conversation as resolved.
Show resolved Hide resolved
if (options?.flags && allFlags) {
AndyBitz marked this conversation as resolved.
Show resolved Hide resolved
options.flags.forEach((key) => {
flagsToReport[key] = allFlags[key];
});
} else if (allFlags) {
// Default to all flags. The ingest endpoint will truncate them.
flagsToReport = allFlags;
AndyBitz marked this conversation as resolved.
Show resolved Hide resolved
}

const body = {
o: origin,
ts: new Date().getTime(),
r: '',
en: eventName,
ed: props,
f: {
p: flagsToReport,
},
};

const hasHeaders = Boolean(headers);
Expand Down Expand Up @@ -135,7 +158,7 @@ export async function track(
body: JSON.stringify(body),
method: 'POST',
})
// We want to always consume to body; some cloud providers track fetch concurrency
// We want to always consume the body; some cloud providers track fetch concurrency
feugy marked this conversation as resolved.
Show resolved Hide resolved
// and may not release the connection until the body is consumed.
.then((response) => response.text())
.catch((err: unknown) => {
Expand Down
Loading