Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Add GA analytics with posthog ID [WIP]
Tomas Valenta52f6b58819b1
diff --git a/components/Layout.tsx b/components/Layout.tsxindex e8ace16f6..28ebb1428 100644--- a/components/Layout.tsx+++ b/components/Layout.tsx@@ -13,10 +13,13 @@ import {Menu,List,} from 'lucide-react'+import * as gtag from '../utils/gtag'import DashboardDesktopSidebar from 'components/Sidebar/DashboardDesktopSidebar'import DashboardMobileSidebar from 'components/Sidebar/DashboardMobileSidebar'import Feedback from 'components/Feedback'+import Head from 'next/head'+import Script from 'next/script'const inter = Inter({subsets: ['latin'],@@ -29,6 +32,19 @@ function Layout({ children }: PropsWithChildren) {const posthog = usePostHog()const router = useRouter()const [isSidebarOpen, setIsSidebarOpen] = useState(false)+ const [distinctID, setDistinctID] = useState<string>()++ useEffect(() => {+ if (!distinctID) return++ const handleRouteChange = (url: string) => {+ gtag.pageview(url, distinctID)+ }+ router.events.on('routeChangeComplete', handleRouteChange)+ return () => {+ router.events.off('routeChangeComplete', handleRouteChange)+ }+ }, [router.events, distinctID])async function signOut() {await supabaseClient.auth.signOut()@@ -36,6 +52,10 @@ function Layout({ children }: PropsWithChildren) {router.push('/sign')}+ useEffect(function handleDistinctID() {+ setDistinctID(posthog?.get_distinct_id())+ }, [posthog])+useEffect(function identifyUser() {if (user) {posthog?.identify(user.id, {@@ -73,6 +93,29 @@ function Layout({ children }: PropsWithChildren) {return (<>+ <Head>+ {distinctID &&+ <script+ dangerouslySetInnerHTML={{+ __html: `+ window.dataLayer = window.dataLayer || [];+ function gtag(){dataLayer.push(arguments);}+ gtag('js', new Date());++ gtag('config', '${gtag.GA_TRACKING_ID}', {+ page_path: window.location.pathname,+ user_id: '${distinctID}',+ });+ `,+ }}+ />}+ </Head>++ {/* Global Site Tag (gtag.js) - Google Analytics */}+ <Script+ strategy="afterInteractive"+ src={`https://www.googletagmanager.com/gtag/js?id=${gtag.GA_TRACKING_ID}`}+ /><style jsx global>{`:root {diff --git a/tsconfig.json b/tsconfig.jsonindex 104f52e4f..c176259e9 100644--- a/tsconfig.json+++ b/tsconfig.json@@ -28,7 +28,7 @@"include": ["next-env.d.ts","**/*.ts",- "**/*.tsx",+ "**/*.tsx"],"exclude": ["supabase",diff --git a/utils/gtag.js b/utils/gtag.jsnew file mode 100644index 000000000..aa24d1b67--- /dev/null+++ b/utils/gtag.js@@ -0,0 +1,20 @@+export const GA_TRACKING_ID = process.env.NEXT_PUBLIC_GA_ID++// https://developers.google.com/analytics/devguides/collection/gtagjs/pages+export const pageview = (url, user_id) => {+ window.gtag('config', GA_TRACKING_ID, {+ page_path: url,+ user_id: user_id,+ })+ console.log('tracked pageview', url, user_id)+}++// https://developers.google.com/analytics/devguides/collection/gtagjs/events+export const event = ({ action, category, label, value, user_id }) => {+ window.gtag('event', action, {+ event_category: category,+ event_label: label,+ value: value,+ user_id: user_id,+ })+}