Skip to content

Commit 304dfd1

Browse files
wdawsoncursoragent
andauthored
Posthog 404 tracking (#652)
* Track 404s in PostHog Co-authored-by: wils <wils@arcade-ai.com> * Fix lint for 404 tracking * update PostHog * Guard referrer URL parsing Co-authored-by: wils <wils@arcade-ai.com> (#655) Guard referrer URL parsing Co-authored-by: Cursor Agent <cursoragent@cursor.com> * fix: reduce cognitive complexity in not-found.tsx useEffect Extract referrer domain logic into separate getReferrerInfo() helper function to reduce cognitive complexity from 16 to below the max threshold of 15. --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com>
1 parent b3cd0ce commit 304dfd1

20 files changed

Lines changed: 365 additions & 140 deletions

app/_components/analytics.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use client";
22
import { cn } from "@arcadeai/design-system/lib/utils";
33
import Link from "next/link";
4-
import { usePostHog } from "posthog-js/react";
4+
import posthog from "posthog-js";
55
import { getDashboardUrl } from "./dashboard-link";
66

77
export type LinkClickedProps = {
@@ -15,10 +15,8 @@ export const SignupLink = ({
1515
children,
1616
className,
1717
}: LinkClickedProps) => {
18-
const posthog = usePostHog();
19-
2018
const trackSignupClick = (source: string) => {
21-
posthog?.capture("Signup clicked", {
19+
posthog.capture("Signup clicked", {
2220
link_location: source,
2321
});
2422
};

app/_components/dynamic-survey/hooks/use-survey.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"use client";
2-
import type { Survey } from "posthog-js";
3-
import { usePostHog } from "posthog-js/react";
2+
import posthog, { type Survey } from "posthog-js";
43
import { type FormEvent, useRef, useState } from "react";
54

65
import {
@@ -20,7 +19,6 @@ export const useSurvey = ({ surveyData, onComplete }: UseSurveyProps) => {
2019
const [responses, setResponses] = useState<Record<number, unknown>>({});
2120
const [questionPath, setQuestionPath] = useState<number[]>([0]);
2221
const submitted = useRef(false);
23-
const posthog = usePostHog();
2422

2523
const handleSubmitQuestion = (e: FormEvent<HTMLFormElement>) => {
2624
e.preventDefault();
@@ -58,7 +56,7 @@ export const useSurvey = ({ surveyData, onComplete }: UseSurveyProps) => {
5856
if (nextIndex === "end") {
5957
const data = createSurveyEventProperties(updatedResponses, surveyData.id);
6058
if (!submitted.current) {
61-
posthog?.capture("survey sent", data);
59+
posthog.capture("survey sent", data);
6260
submitted.current = true;
6361
}
6462
onComplete(data);

app/_components/dynamic-survey/index.tsx

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"use client";
2-
import type { Survey } from "posthog-js";
3-
import { usePostHog } from "posthog-js/react";
2+
import posthog, { type Survey } from "posthog-js";
43
import { useEffect, useRef } from "react";
54
import { useSurvey } from "./hooks/use-survey";
65
import { Navigation } from "./navigation";
@@ -19,7 +18,6 @@ export default function DynamicSurvey({
1918
onComplete,
2019
onBack,
2120
}: DynamicSurveyProps) {
22-
const posthog = usePostHog();
2321
const surveyShown = useRef(false);
2422

2523
const {
@@ -33,30 +31,30 @@ export default function DynamicSurvey({
3331

3432
// Capture the survey shown event when the survey is loaded
3533
useEffect(() => {
36-
if (!(surveyData && posthog) || surveyData.questions.length === 0) {
34+
if (!surveyData || surveyData.questions.length === 0) {
3735
return;
3836
}
3937
if (!surveyShown.current) {
40-
posthog?.capture("survey shown", { $survey_id: surveyData.id });
38+
posthog.capture("survey shown", { $survey_id: surveyData.id });
4139
surveyShown.current = true;
4240
}
43-
}, [surveyData, posthog]);
41+
}, [surveyData]);
4442

4543
// Capture the survey dismissed event when the user navigates away from the survey
4644
useEffect(() => {
47-
if (!(surveyData && posthog)) {
45+
if (!surveyData) {
4846
return;
4947
}
5048

5149
const handleBeforeUnload = () => {
5250
if (currentQuestionIndex < surveyData.questions.length - 1) {
53-
posthog?.capture("survey dismissed", { $survey_id: surveyData.id });
51+
posthog.capture("survey dismissed", { $survey_id: surveyData.id });
5452
}
5553
};
5654

5755
window.addEventListener("beforeunload", handleBeforeUnload);
5856
return () => window.removeEventListener("beforeunload", handleBeforeUnload);
59-
}, [surveyData, posthog, currentQuestionIndex]);
57+
}, [surveyData, currentQuestionIndex]);
6058

6159
// Handle back button click
6260
const handleBackButton = () => {

app/_components/early-access-registry-survey.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
"use client";
22

3-
import type { Survey } from "posthog-js";
4-
import { usePostHog } from "posthog-js/react";
3+
import posthog, { type Survey } from "posthog-js";
54
import { useEffect, useState } from "react";
65
import DynamicSurvey from "./dynamic-survey";
76

87
const SURVEY_ID = "019683f6-4fe2-0000-d182-6ef8f3982fc3";
98

109
export const EarlyAccessRegistrySurvey = () => {
11-
const posthog = usePostHog();
1210
const [surveyData, setSurveyData] = useState<Survey | null>(null);
1311
const [completed, setCompleted] = useState(false);
1412

1513
useEffect(() => {
16-
if (!posthog) {
17-
return;
18-
}
1914
posthog.onFeatureFlags(() => {
2015
posthog.getSurveys((surveys) => {
2116
const survey = surveys.find((s) => s.id === SURVEY_ID);
@@ -24,7 +19,7 @@ export const EarlyAccessRegistrySurvey = () => {
2419
}
2520
}, true);
2621
});
27-
}, [posthog]);
22+
}, []);
2823

2924
const handleSurveyComplete = () => {
3025
setCompleted(true);

app/_components/integration-card.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { Card, CardContent } from "@arcadeai/design-system";
33
import { cn } from "@arcadeai/design-system/lib/utils";
44
import type { LucideIcon } from "lucide-react";
5-
import { usePostHog } from "posthog-js/react";
5+
import posthog from "posthog-js";
66

77
type IntegrationCardProps = {
88
id: string;
@@ -21,10 +21,8 @@ export function IntegrationCard({
2121
isActive = false,
2222
onClick,
2323
}: IntegrationCardProps) {
24-
const posthog = usePostHog();
25-
2624
const handleClick = () => {
27-
posthog?.capture("integration_card_clicked", {
25+
posthog.capture("Integration card clicked", {
2826
integration_id: id,
2927
integration_title: title,
3028
is_active: isActive,

app/_components/posthog.tsx

Lines changed: 0 additions & 51 deletions
This file was deleted.

app/_components/quick-start-card.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from "@arcadeai/design-system";
88
import { motion } from "motion/react";
99
import Link from "next/link";
10-
import { usePostHog } from "posthog-js/react";
10+
import posthog from "posthog-js";
1111

1212
type QuickStartCardProps = {
1313
icon: React.ElementType;
@@ -26,10 +26,8 @@ export function QuickStartCard({
2626
onClick,
2727
code,
2828
}: QuickStartCardProps) {
29-
const posthog = usePostHog();
30-
3129
const handleCardClick = () => {
32-
posthog?.capture("quickstart_card_clicked", {
30+
posthog.capture("Quickstart card clicked", {
3331
card_title: title,
3432
card_href: href || null,
3533
has_custom_onclick: !!onClick,

app/_components/sample-app-card.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use client";
22
import { Card, CardContent } from "@arcadeai/design-system";
33
import Link from "next/link";
4-
import { usePostHog } from "posthog-js/react";
4+
import posthog from "posthog-js";
55

66
type SampleAppCardProps = {
77
title: string;
@@ -21,10 +21,8 @@ export function SampleAppCard({
2121
tags = [],
2222
date,
2323
}: SampleAppCardProps) {
24-
const posthog = usePostHog();
25-
2624
const handleClick = () => {
27-
posthog?.capture("sample_app_clicked", {
25+
posthog.capture("Sample app clicked", {
2826
app_title: title,
2927
app_href: href,
3028
tags,

app/_components/scope-picker.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client";
22

3-
import { usePostHog } from "posthog-js/react";
3+
import posthog from "posthog-js";
44
import { useState } from "react";
55

66
type Tool = {
@@ -14,14 +14,13 @@ type ScopePickerProps = {
1414

1515
export default function ScopePicker({ tools }: ScopePickerProps) {
1616
const [selectedTools, setSelectedTools] = useState<Set<string>>(new Set());
17-
const posthog = usePostHog();
1817

1918
const trackScopeCalculatorUsed = (
2019
action: string,
2120
toolName: string | undefined,
2221
newSelectedCount: number
2322
) => {
24-
posthog?.capture("scope_calculator_used", {
23+
posthog.capture("Scope calculator used", {
2524
action,
2625
tool_name: toolName || null,
2726
selected_tools_count: newSelectedCount,

app/_components/tabbed-code-block.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use client";
22
import { Button } from "@arcadeai/design-system";
33
import { ChevronDown } from "lucide-react";
4-
import { usePostHog } from "posthog-js/react";
4+
import posthog from "posthog-js";
55
import React, { useMemo, useState } from "react";
66
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
77
import {
@@ -28,7 +28,6 @@ const CodeTabSwitcher = ({ tabs }: CodeTabSwitcherProps) => {
2828
const [activeTab, setActiveTab] = useState(0);
2929
const [selectedLanguage, setSelectedLanguage] = useState("Python");
3030
const [isDarkMode, setIsDarkMode] = useState(false);
31-
const posthog = usePostHog();
3231

3332
// Effect to monitor theme changes
3433
React.useEffect(() => {
@@ -108,7 +107,7 @@ const CodeTabSwitcher = ({ tabs }: CodeTabSwitcherProps) => {
108107

109108
const handleExpandExample = () => {
110109
setIsExpanded(true);
111-
posthog?.capture("code_example_expanded", {
110+
posthog.capture("Code example expanded", {
112111
tab_count: tabs.length,
113112
initial_language: selectedLanguage,
114113
});

0 commit comments

Comments
 (0)