Skip to content

Commit 440ebcd

Browse files
authored
feat: add posthog to track what examples are visited (#90)
1 parent be603f7 commit 440ebcd

File tree

6 files changed

+82
-11
lines changed

6 files changed

+82
-11
lines changed

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ ENV YARN_CACHE_FOLDER=/tmp/yarn_cache
99
# Environment variables for build time.
1010
ARG VITE_GOOGLE_ANALYTICS_MEASUREMENT_ID=""
1111

12+
ARG VITE_POSTHOG_KEY=""
13+
ARG VITE_POSTHOG_HOST=https://us.i.posthog.com
1214
ARG VITE_DISCORD_CHANNEL_ID=""
1315
ARG VITE_DISCORD_INVITE_URL="https://authzed.com/discord"
1416
ARG VITE_DISCORD_SERVER_ID=""

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"@material-ui/icons": "^4.11.3",
1818
"@material-ui/lab": "^4.0.0-alpha.61",
1919
"@monaco-editor/react": "^4.3.1",
20+
"@posthog/react": "^1.5.2",
2021
"@radix-ui/react-alert-dialog": "^1.1.11",
2122
"@radix-ui/react-select": "^2.2.2",
2223
"@radix-ui/react-slot": "^1.2.0",
@@ -41,6 +42,7 @@
4142
"monaco-editor": "~0.40.0",
4243
"next-themes": "^0.4.6",
4344
"parsimmon": "^1.18.1",
45+
"posthog-js": "^1.304.0",
4446
"react": "^18.3.1",
4547
"react-cookie": "^4.1.1",
4648
"react-dom": "^18.3.1",

src/App.tsx

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import {
1919
} from "@tanstack/react-router";
2020
import { TanStackRouterDevtools } from "@tanstack/react-router-devtools";
2121
import { Toaster } from "./components/ui/sonner";
22+
import posthog from "posthog-js";
23+
import { PostHogProvider } from "@posthog/react";
2224

2325
const rootRoute = createRootRoute({
2426
component: () => (
@@ -52,24 +54,36 @@ const routeTree = rootRoute.addChildren([
5254
embeddedRoute,
5355
]);
5456
const router = createRouter({ routeTree });
57+
58+
const config = AppConfig();
59+
if (config.posthog.apiKey && config.posthog.host) {
60+
posthog.init(config.posthog.apiKey, {
61+
api_host: config.posthog.host,
62+
person_profiles: "identified_only",
63+
defaults: "2025-11-30", // Enables automatic SPA pageview tracking via history API
64+
});
65+
}
66+
5567
function App() {
5668
// Register GA hook.
57-
useGoogleAnalytics(AppConfig().ga.measurementId);
69+
useGoogleAnalytics(config.ga.measurementId);
5870

5971
const isEmbeddedPlayground = window.location.pathname.indexOf("/e/") >= 0;
6072
return (
6173
<>
6274
<Toaster />
63-
<ThemeProvider>
64-
<PlaygroundUIThemed
65-
{...PLAYGROUND_UI_COLORS}
66-
forceDarkMode={isEmbeddedPlayground}
67-
>
68-
<ConfirmDialogProvider>
69-
<RouterProvider router={router} />
70-
</ConfirmDialogProvider>
71-
</PlaygroundUIThemed>
72-
</ThemeProvider>
75+
<PostHogProvider client={posthog}>
76+
<ThemeProvider>
77+
<PlaygroundUIThemed
78+
{...PLAYGROUND_UI_COLORS}
79+
forceDarkMode={isEmbeddedPlayground}
80+
>
81+
<ConfirmDialogProvider>
82+
<RouterProvider router={router} />
83+
</ConfirmDialogProvider>
84+
</PlaygroundUIThemed>
85+
</ThemeProvider>
86+
</PostHogProvider>
7387
</>
7488
);
7589
}

src/components/ExamplesDropdown.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
} from "@/components/ui/alert-dialog";
2020

2121
import { useEffect, useState } from "react";
22+
import { usePostHog } from "@posthog/react";
2223

2324
export function ExamplesDropdown({
2425
disabled,
@@ -30,6 +31,7 @@ export function ExamplesDropdown({
3031
const [examples, setExamples] = useState<Example[]>();
3132
const [promptOpen, setPromptOpen] = useState(false);
3233
const [selectedExample, setSelectedExample] = useState<Example>();
34+
const posthog = usePostHog();
3335

3436
useEffect(() => {
3537
const fetchExamples = async () => {
@@ -83,6 +85,10 @@ export function ExamplesDropdown({
8385
<AlertDialogAction
8486
onClick={() => {
8587
if (selectedExample) {
88+
posthog.capture("playground_schema_selected", {
89+
schema_id: selectedExample.id,
90+
schema_title: selectedExample.title,
91+
});
8692
loadExample(selectedExample);
8793
}
8894
}}

src/services/configservice.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ export default function AppConfig() {
1010
serverId: import.meta.env.VITE_DISCORD_SERVER_ID,
1111
inviteUrl: import.meta.env.VITE_DISCORD_INVITE_URL,
1212
},
13+
posthog: {
14+
apiKey: import.meta.env.VITE_POSTHOG_KEY,
15+
host: import.meta.env.VITE_POSTHOG_HOST,
16+
},
1317
};
1418

1519
if (

yarn.lock

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,6 +1930,18 @@
19301930
resolved "https://registry.yarnpkg.com/@pkgjs/parseargs/-/parseargs-0.11.0.tgz#a77ea742fab25775145434eb1d2328cf5013ac33"
19311931
integrity sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg==
19321932

1933+
"@posthog/core@1.7.1":
1934+
version "1.7.1"
1935+
resolved "https://registry.yarnpkg.com/@posthog/core/-/core-1.7.1.tgz#ec45a3b13acbe4fbf453625c83bcd38da21beeb7"
1936+
integrity sha512-kjK0eFMIpKo9GXIbts8VtAknsoZ18oZorANdtuTj1CbgS28t4ZVq//HAWhnxEuXRTrtkd+SUJ6Ux3j2Af8NCuA==
1937+
dependencies:
1938+
cross-spawn "^7.0.6"
1939+
1940+
"@posthog/react@^1.5.2":
1941+
version "1.5.2"
1942+
resolved "https://registry.yarnpkg.com/@posthog/react/-/react-1.5.2.tgz#846a649e7f256a6515497ec0db097292f236e3a5"
1943+
integrity sha512-KHdXbV1yba7Y2l8BVmwXlySWxqKVLNQ5ZiVvWOf7r3Eo7GIFxCM4CaNK/z83kKWn8KTskmKy7AGF6Hl6INWK3g==
1944+
19331945
"@radix-ui/number@1.1.1":
19341946
version "1.1.1"
19351947
resolved "https://registry.yarnpkg.com/@radix-ui/number/-/number-1.1.1.tgz#7b2c9225fbf1b126539551f5985769d0048d9090"
@@ -4251,6 +4263,11 @@ cookie@^0.4.0:
42514263
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.1.tgz#afd713fe26ebd21ba95ceb61f9a8116e50a537d1"
42524264
integrity sha512-ZwrFkGJxUR3EIoXtO+yVE69Eb7KlixbaeAWfBQB9vVsNn/o+Yw69gBWSSDK825hQNdN+wF8zELf3dFNl/kxkUA==
42534265

4266+
core-js@^3.38.1:
4267+
version "3.47.0"
4268+
resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.47.0.tgz#436ef07650e191afeb84c24481b298bd60eb4a17"
4269+
integrity sha512-c3Q2VVkGAUyupsjRnaNX6u8Dq2vAdzm9iuPj5FW0fRxzlxgq9Q39MDq10IvmQSpLgHQNyQzQmOo6bgGHmH3NNg==
4270+
42544271
core-util-is@1.0.2:
42554272
version "1.0.2"
42564273
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
@@ -5261,6 +5278,11 @@ fd-slicer@~1.1.0:
52615278
dependencies:
52625279
pend "~1.2.0"
52635280

5281+
fflate@^0.4.8:
5282+
version "0.4.8"
5283+
resolved "https://registry.yarnpkg.com/fflate/-/fflate-0.4.8.tgz#f90b82aefbd8ac174213abb338bd7ef848f0f5ae"
5284+
integrity sha512-FJqqoDBR00Mdj9ppamLa/Y7vxm+PRmNWA67N846RvsoYVMKB4q3y/de5PA7gUmRMYK/8CMz2GDZQmCRN1wBcWA==
5285+
52645286
figures@^3.2.0:
52655287
version "3.2.0"
52665288
resolved "https://registry.yarnpkg.com/figures/-/figures-3.2.0.tgz#625c18bd293c604dc4a8ddb2febf0c88341746af"
@@ -6882,6 +6904,22 @@ postcss@^8.4.43, postcss@^8.4.49:
68826904
picocolors "^1.1.1"
68836905
source-map-js "^1.2.1"
68846906

6907+
posthog-js@^1.304.0:
6908+
version "1.304.0"
6909+
resolved "https://registry.yarnpkg.com/posthog-js/-/posthog-js-1.304.0.tgz#230b89e12da5bdcefd2c8a89c70fd3ac9e8f5ba1"
6910+
integrity sha512-revqoppmJ5y1Oa9iRUb3P8w1htfxZdrSAe+elSNMxvl7wxY62qWN7Q0kE5Sk81o1qLHa6drPhVKa/dppWOfSUw==
6911+
dependencies:
6912+
"@posthog/core" "1.7.1"
6913+
core-js "^3.38.1"
6914+
fflate "^0.4.8"
6915+
preact "^10.19.3"
6916+
web-vitals "^4.2.4"
6917+
6918+
preact@^10.19.3:
6919+
version "10.28.0"
6920+
resolved "https://registry.yarnpkg.com/preact/-/preact-10.28.0.tgz#a851300df42842797046545e4172a4128d158755"
6921+
integrity sha512-rytDAoiXr3+t6OIP3WGlDd0ouCUG1iCWzkcY3++Nreuoi17y6T5i/zRhe6uYfoVcxq6YU+sBtJouuRDsq8vvqA==
6922+
68856923
prelude-ls@^1.2.1:
68866924
version "1.2.1"
68876925
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396"
@@ -8453,6 +8491,11 @@ w3c-keyname@^2.2.0:
84538491
resolved "https://registry.yarnpkg.com/w3c-keyname/-/w3c-keyname-2.2.8.tgz#7b17c8c6883d4e8b86ac8aba79d39e880f8869c5"
84548492
integrity sha512-dpojBhNsCNN7T82Tm7k26A6G9ML3NkhDsnw9n/eoxSRlVBB4CEtIQ/KTCLI2Fwf3ataSXRhYFkQi3SlnFwPvPQ==
84558493

8494+
web-vitals@^4.2.4:
8495+
version "4.2.4"
8496+
resolved "https://registry.yarnpkg.com/web-vitals/-/web-vitals-4.2.4.tgz#1d20bc8590a37769bd0902b289550936069184b7"
8497+
integrity sha512-r4DIlprAGwJ7YM11VZp4R884m0Vmgr6EAKe3P+kO0PPj3Unqyvv59rczf6UiGcb9Z8QxZVcqKNwv/g0WNdWwsw==
8498+
84568499
webidl-conversions@^3.0.0:
84578500
version "3.0.1"
84588501
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871"

0 commit comments

Comments
 (0)