-
Notifications
You must be signed in to change notification settings - Fork 905
feat(docs): DR-7849 DR-7850 PostHog tracking + UTM injection #7697
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
Open
ArthurGamby
wants to merge
3
commits into
main
Choose a base branch
from
dr-7850-remark-console-utm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,37 @@ | ||
| 'use client'; | ||
| import { RootProvider } from 'fumadocs-ui/provider/next'; | ||
| import { NextProvider } from 'fumadocs-core/framework/next'; | ||
| import { KapaProvider } from '@kapaai/react-sdk'; | ||
| import CustomSearchDialog from '@/components/search'; | ||
| import type { ReactNode } from 'react'; | ||
| import { source } from '@/lib/source'; | ||
| import { TreeContextProvider } from 'fumadocs-ui/contexts/tree'; | ||
| "use client"; | ||
| import { RootProvider } from "fumadocs-ui/provider/next"; | ||
| import { NextProvider } from "fumadocs-core/framework/next"; | ||
| import { KapaProvider } from "@kapaai/react-sdk"; | ||
| import CustomSearchDialog from "@/components/search"; | ||
| import type { ReactNode } from "react"; | ||
| import { source } from "@/lib/source"; | ||
| import { TreeContextProvider } from "fumadocs-ui/contexts/tree"; | ||
| import { TrackingProvider } from "@/components/tracking-provider"; | ||
|
|
||
| const KAPA_INTEGRATION_ID = '1b51bb03-43cc-4ef4-95f1-93288a91b560'; | ||
| const KAPA_INTEGRATION_ID = "1b51bb03-43cc-4ef4-95f1-93288a91b560"; | ||
|
|
||
| export function Provider({ children }: { children: ReactNode }) { | ||
| return ( | ||
| <NextProvider> | ||
| <KapaProvider | ||
| integrationId={KAPA_INTEGRATION_ID} | ||
| callbacks={{ | ||
| askAI: { | ||
| onQuerySubmit: (data: { question: string }) => { | ||
| console.log('Question asked:', data.question); | ||
| }, | ||
| <KapaProvider | ||
| integrationId={KAPA_INTEGRATION_ID} | ||
| callbacks={{ | ||
| askAI: { | ||
| onQuerySubmit: (data: { question: string }) => { | ||
| console.log("Question asked:", data.question); | ||
| }, | ||
| }, | ||
| }} | ||
| > | ||
| <RootProvider | ||
| search={{ | ||
| SearchDialog: CustomSearchDialog, | ||
| }} | ||
| > | ||
| <RootProvider | ||
| search={{ | ||
| SearchDialog: CustomSearchDialog, | ||
| }} | ||
| > | ||
| {children} | ||
| </RootProvider> | ||
| </KapaProvider> | ||
| <TrackingProvider /> | ||
| {children} | ||
| </RootProvider> | ||
| </KapaProvider> | ||
| </NextProvider> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| "use client"; | ||
| import { useEffect } from "react"; | ||
| import { usePathname } from "next/navigation"; | ||
| import posthog from "posthog-js"; | ||
| import { getContentArea, getContentSubtype, isPpgOrCompute } from "@/lib/tracking"; | ||
|
|
||
| export function TrackingProvider() { | ||
| const pathname = usePathname(); | ||
|
|
||
| useEffect(() => { | ||
| const area = getContentArea(pathname); | ||
| posthog.register({ | ||
| content_area: area, | ||
| is_ppg_or_compute: isPpgOrCompute(area), | ||
| content_subtype: getContentSubtype(pathname), | ||
| }); | ||
| }, [pathname]); | ||
|
|
||
| useEffect(() => { | ||
| function handleClick(e: MouseEvent) { | ||
| const anchor = (e.target as HTMLElement).closest<HTMLAnchorElement>( | ||
| 'a[href*="console.prisma.io"]', | ||
| ); | ||
| if (!anchor) return; | ||
|
|
||
| const url = anchor.href; | ||
| posthog.capture("docs:console_link_click", { | ||
| page_path: pathname, | ||
| destination_url: url, | ||
| has_utm: url.includes("utm_source"), | ||
| }); | ||
| } | ||
|
|
||
| document.addEventListener("click", handleClick); | ||
| return () => document.removeEventListener("click", handleClick); | ||
| }, [pathname]); | ||
|
|
||
| return null; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import type { Root } from "mdast"; | ||
| import type { Plugin } from "unified"; | ||
| import { visit } from "unist-util-visit"; | ||
|
|
||
| const CONSOLE_HOST = "console.prisma.io"; | ||
|
|
||
| const remarkConsoleUtm: Plugin<[], Root> = () => (tree, vfile) => { | ||
| const filePath = vfile.path ?? ""; | ||
| const isV6 = filePath.includes("content/docs.v6/"); | ||
| const utmSource = isV6 ? "docs-v6" : "docs"; | ||
|
|
||
| const sectionMatch = filePath.match(/content\/docs(?:\.v6)?\/([^/]+)/); | ||
| const section = sectionMatch?.[1] ?? "unknown"; | ||
|
|
||
| visit(tree, "link", (node) => { | ||
| let url: URL; | ||
| try { | ||
| url = new URL(node.url); | ||
| } catch { | ||
| return; | ||
| } | ||
|
|
||
| if (url.hostname !== CONSOLE_HOST) return; | ||
| if (url.searchParams.has("utm_source")) return; | ||
|
|
||
| url.searchParams.set("utm_source", utmSource); | ||
| url.searchParams.set("utm_medium", "content"); | ||
| url.searchParams.set("utm_content", section); | ||
| node.url = url.toString(); | ||
| }); | ||
| }; | ||
|
|
||
| export default remarkConsoleUtm; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| type ContentArea = "ppg" | "orm" | "accelerate" | "console" | "guides" | "ai" | "other"; | ||
|
|
||
| const PREFIX_MAP: [string, ContentArea][] = [ | ||
| ["/postgres", "ppg"], | ||
| ["/prisma-postgres", "ppg"], | ||
| ["/orm", "orm"], | ||
| ["/prisma-orm", "orm"], | ||
| ["/accelerate", "accelerate"], | ||
| ["/console", "console"], | ||
| ["/guides", "guides"], | ||
| ["/ai", "ai"], | ||
| ]; | ||
|
|
||
| const PPG_AREAS: Set<ContentArea> = new Set(["ppg"]); | ||
|
|
||
| export function getContentArea(pathname: string): ContentArea { | ||
| const stripped = pathname.replace(/^\/docs\/v\d+/, ""); | ||
| for (const [prefix, area] of PREFIX_MAP) { | ||
| if (stripped.startsWith(prefix)) return area; | ||
| } | ||
| return "other"; | ||
| } | ||
|
|
||
| export function isPpgOrCompute(area: ContentArea): boolean { | ||
| return PPG_AREAS.has(area); | ||
| } | ||
|
|
||
| export function getContentSubtype(pathname: string): string | null { | ||
| if (pathname.includes("/quickstart")) return "quickstart"; | ||
| if (pathname.includes("/getting-started")) return "getting-started"; | ||
| return null; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function name
isPpgOrComputemay be misleading.The function name suggests it checks for both "ppg" and "compute" areas, but
PPG_AREASonly contains"ppg", and"compute"isn't in theContentAreatype.If "compute" was intentionally removed or consolidated into "ppg", consider renaming to
isPpg()for clarity. If "compute" should be tracked separately, it may need to be added.♻️ Suggested rename if compute is not needed
Note: You'd also need to update the call site in
tracking-provider.tsxand the PostHog property nameis_ppg_or_compute.🤖 Prompt for AI Agents