Skip to content

Commit

Permalink
swap vercel analytics for posthog (#1719)
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity authored Oct 25, 2024
1 parent 4cc3fa1 commit 9270067
Show file tree
Hide file tree
Showing 7 changed files with 491 additions and 214 deletions.
2 changes: 1 addition & 1 deletion packages/ui/docs-bundle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"@fern-ui/fern-docs-utils": "workspace:*",
"@fern-ui/search-utils": "workspace:*",
"@fern-ui/ui": "workspace:*",
"@vercel/analytics": "^1.3.1",
"@vercel/kv": "^2.0.0",
"@workos-inc/node": "^6.1.0",
"algoliasearch": "^5.8.1",
Expand All @@ -57,6 +56,7 @@
"next": "npm:@fern-api/[email protected]",
"node-fetch": "2.7.0",
"postcss-import": "^16.0.1",
"posthog-node": "^4.2.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"sharp": "^0.33.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/docs-bundle/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { rewritePosthog } from "@/server/analytics/rewritePosthog";
import { extractNextDataPathname } from "@/server/extractNextDataPathname";
import { rewritePosthog } from "@/server/rewritePosthog";
import { removeTrailingSlash } from "next/dist/shared/lib/router/utils/remove-trailing-slash";
import { NextResponse, type NextMiddleware } from "next/server";
import { withMiddlewareAuth } from "./server/withMiddlewareAuth";
Expand Down
22 changes: 14 additions & 8 deletions packages/ui/docs-bundle/src/server/LoadDocsPerformanceTracker.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { FernNavigation } from "@fern-api/fdr-sdk";
import { TRACK_LOAD_DOCS_PERFORMANCE } from "@fern-ui/fern-docs-utils";
import { DocsPage } from "@fern-ui/ui";
import { track } from "@vercel/analytics/server";
import { GetServerSidePropsResult } from "next/types";
import { ComponentProps } from "react";
import { track } from "./analytics/posthog";
import { AuthPartner } from "./auth/getAuthState";
import type { LoadWithUrlResponse } from "./loadWithUrl";

Expand All @@ -21,7 +21,7 @@ export class LoadDocsPerformanceTracker {
}

private constructor(
private host: string,
private domain: string,
private slug: FernNavigation.Slug,
private auth: AuthPartner | undefined,
) {}
Expand All @@ -47,12 +47,18 @@ export class LoadDocsPerformanceTracker {
}

async track(): Promise<void> {
return track(TRACK_LOAD_DOCS_PERFORMANCE, {
host: this.host,
const properties = {
domain: this.domain,
slug: this.slug,
auth: this.auth ?? null,
loadDocsDurationMs: this.loadDocsDurationMs ?? null,
initialPropsDurationMs: this.initialPropsDurationMs ?? null,
});
auth: this.auth,
loadDocsDurationMs: this.loadDocsDurationMs,
initialPropsDurationMs: this.initialPropsDurationMs,
$current_url: `https://${this.domain}/${this.slug}`,
};

// eslint-disable-next-line no-console
console.log(TRACK_LOAD_DOCS_PERFORMANCE, properties);

await track(TRACK_LOAD_DOCS_PERFORMANCE, properties);
}
}
36 changes: 36 additions & 0 deletions packages/ui/docs-bundle/src/server/analytics/posthog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { PostHog } from "posthog-node";

function getPosthogKey(): string {
const key = process.env.NEXT_PUBLIC_POSTHOG_API_KEY;
if (key == null) {
throw new Error("NEXT_PUBLIC_POSTHOG_API_KEY is not set");
}
return key.trim();
}

function getPosthog(): PostHog {
return new PostHog(getPosthogKey(), {
host: "https://us.i.posthog.com",
});
}

export async function track(event: string, properties?: Record<string, unknown>): Promise<void> {
try {
const client = getPosthog();

client.capture({
event,
distinctId: "server-side-event",
properties: {
// anonymize this event because it's server-side https://posthog.com/docs/product-analytics/capture-events?tab=Backend
$process_person_profile: false,
...properties,
},
});

await client.shutdown();
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
}
}
2 changes: 1 addition & 1 deletion packages/ui/docs-bundle/src/server/getDocsPageProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export async function getDocsPageProps(
console.log("Converted docs into initial props");

/**
* Send performance data to Vercel Analytics.
* Send performance data to PostHog.
*/
await performance.track();

Expand Down
Loading

0 comments on commit 9270067

Please sign in to comment.