Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Add analytics [WIP]

Tomas Valenta82af54e7adf5
diff --git a/components/AgentOverview.tsx b/components/AgentOverview.tsx
index 2542d091c..d0d8c7f55 100644
--- a/components/AgentOverview.tsx
+++ b/components/AgentOverview.tsx
@@ -6,8 +6,11 @@ import { Dialog, Transition } from '@headlessui/react'
import { ChevronRightIcon } from '@heroicons/react/20/solid'
import clsx from 'clsx'
import { Grid } from 'lucide-react'
+import { usePostHog } from 'posthog-js/react'
import { projects, deployments } from 'db/prisma'
+import { useSupabaseClient } from '@supabase/auth-helpers-react'
+import { useRouter } from 'next/router'
const navigation = [
{
@@ -24,7 +27,16 @@ export interface Props {
}
export default function AgentOverview({ projects }: Props) {
+ const supabaseClient = useSupabaseClient()
+ const router = useRouter()
const [sidebarOpen, setSidebarOpen] = useState(false)
+ const posthog = usePostHog()
+
+ async function signOut() {
+ await supabaseClient.auth.signOut()
+ posthog?.reset(true)
+ router.push('/sign')
+ }
const projectsWithDeployments = projects
.filter(p => p.deployments.length === 1 && p.deployments[0].auth !== null)
@@ -79,11 +91,11 @@ export default function AgentOverview({ projects }: Props) {
{/* Sidebar component, swap this element with another sidebar if you like */}
<div className="flex grow flex-col gap-y-5 overflow-y-auto bg-gray-900 px-6 ring-1 ring-white/10">
<div className="flex h-16 shrink-0 items-center">
- <img
+ {/* <img
className="h-8 w-auto"
src="https://tailwindui.com/img/logos/mark.svg?color=indigo&shade=500"
alt="Your Company"
- />
+ /> */}
</div>
<nav className="flex flex-1 flex-col">
<ul role="list" className="flex flex-1 flex-col gap-y-7">
@@ -130,11 +142,11 @@ export default function AgentOverview({ projects }: Props) {
{/* Sidebar component, swap this element with another sidebar if you like */}
<div className="flex grow flex-col gap-y-5 overflow-y-auto bg-black/10 px-6 ring-1 ring-white/5">
<div className="flex h-16 shrink-0 items-center">
- <img
+ {/* <img
className="h-8 w-auto"
src="https://tailwindui.com/img/logos/mark.svg?color=indigo&shade=500"
alt="Your Company"
- />
+ /> */}
</div>
<nav className="flex flex-1 flex-col">
<ul role="list" className="flex flex-1 flex-col gap-y-7">
@@ -159,18 +171,16 @@ export default function AgentOverview({ projects }: Props) {
</ul>
</li>
<li className="-mx-6 mt-auto">
- <a
- href="#"
- className="flex items-center gap-x-4 px-6 py-3 text-sm font-semibold leading-6 text-white hover:bg-gray-800"
+ <div
+ className="flex items-center gap-x-4 px-6 py-3 text-sm font-semibold leading-6 text-white"
>
- <img
- className="h-8 w-8 rounded-full bg-gray-800"
- src="https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=2&w=256&h=256&q=80"
- alt=""
- />
- <span className="sr-only">Your profile</span>
- <span aria-hidden="true">Tom Cook</span>
- </a>
+ <button
+ className="text-sm font-semibold text-white"
+ onClick={signOut}
+ >
+ Log out
+ </button>
+ </div>
</li>
</ul>
</nav>
diff --git a/components/Layout.tsx b/components/Layout.tsx
new file mode 100644
index 000000000..284f7cc34
--- /dev/null
+++ b/components/Layout.tsx
@@ -0,0 +1,40 @@
+import { useUser } from '@supabase/auth-helpers-react'
+import clsx from 'clsx'
+import { Inter } from 'next/font/google'
+import { PropsWithChildren, useEffect } from 'react'
+import { usePostHog } from 'posthog-js/react'
+
+const inter = Inter({
+ subsets: ['latin'],
+ variable: '--font-inter',
+})
+
+function Layout({ children }: PropsWithChildren) {
+ const user = useUser()
+ const posthog = usePostHog()
+
+ useEffect(function identifyUser() {
+ if (user) {
+ posthog?.identify(user.id, {
+ email: user.email,
+ })
+ }
+ }, [posthog, user])
+
+ return (
+ <div className={clsx(
+ inter.variable,
+ 'font-sans',
+ 'flex',
+ 'h-full',
+ 'w-full',
+ 'flex-1',
+ 'flex-col',
+ 'overflow-hidden',
+ )}>
+ {children}
+ </div>
+ )
+}
+
+export default Layout
diff --git a/next.config.js b/next.config.js
index ad8edae83..cfef0a6be 100644
--- a/next.config.js
+++ b/next.config.js
@@ -24,6 +24,14 @@ const nextConfig = {
experimental: {
swcPlugins: [['next-superjson-plugin', {}]],
},
+ async rewrites() {
+ return [
+ {
+ source: '/ingest/:path*',
+ destination: 'https://app.posthog.com/:path*',
+ },
+ ]
+ },
}
module.exports = nextConfig
diff --git a/package-lock.json b/package-lock.json
index 0c6bc8107..050b43ff9 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -36,6 +36,7 @@
"next": "13.2.3",
"next-superjson-plugin": "^0.5.6",
"openapi-typescript-fetch": "^1.1.3",
+ "posthog-js": "^1.63.3",
"prosemirror-state": "^1.4.2",
"prosemirror-view": "^1.30.2",
"react": "18.2.0",
@@ -3752,6 +3753,11 @@
"node": "^12.20 || >= 14.13"
}
},
+ "node_modules/fflate": {
+ "version": "0.4.8",
+ "resolved": "https://registry.npmjs.org/fflate/-/fflate-0.4.8.tgz",
+ "integrity": "sha512-FJqqoDBR00Mdj9ppamLa/Y7vxm+PRmNWA67N846RvsoYVMKB4q3y/de5PA7gUmRMYK/8CMz2GDZQmCRN1wBcWA=="
+ },
"node_modules/file-entry-cache": {
"version": "6.0.1",
"resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-6.0.1.tgz",
@@ -6848,6 +6854,14 @@
"node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1"
}
},
+ "node_modules/posthog-js": {
+ "version": "1.63.3",
+ "resolved": "https://registry.npmjs.org/posthog-js/-/posthog-js-1.63.3.tgz",
+ "integrity": "sha512-Ob2x1ENxx/mWAdhLhc6ouKAph9LwmitBy7M+hiaFoS0T9g7OVL4lYdrJbODbYV+HX/HeO3bsnQwYO1QLPWm/YA==",
+ "dependencies": {
+ "fflate": "^0.4.1"
+ }
+ },
"node_modules/prebuild-install": {
"version": "7.1.1",
"resolved": "https://registry.npmjs.org/prebuild-install/-/prebuild-install-7.1.1.tgz",
diff --git a/package.json b/package.json
index d9902e50b..5c2791b82 100644
--- a/package.json
+++ b/package.json
@@ -66,6 +66,7 @@
"next": "13.2.3",
"next-superjson-plugin": "^0.5.6",
"openapi-typescript-fetch": "^1.1.3",
+ "posthog-js": "^1.63.3",
"prosemirror-state": "^1.4.2",
"prosemirror-view": "^1.30.2",
"react": "18.2.0",
diff --git a/pages/_app.tsx b/pages/_app.tsx
index 92899062e..248c8753e 100644
--- a/pages/_app.tsx
+++ b/pages/_app.tsx
@@ -1,42 +1,56 @@
import type { AppProps } from 'next/app'
-import { Inter } from 'next/font/google'
import { createBrowserSupabaseClient } from '@supabase/auth-helpers-nextjs'
import { SessionContextProvider, Session } from '@supabase/auth-helpers-react'
-import { useState } from 'react'
-import clsx from 'clsx'
+import { useEffect, useState } from 'react'
import { projects } from '@prisma/client'
+import posthog from 'posthog-js'
+import { PostHogProvider } from 'posthog-js/react'
import 'styles/global.css'
import { Database } from 'db/supabase'
import { clientCreds } from 'db/credentials'
+import { useRouter } from 'next/router'
+import Layout from 'components/Layout'
-const inter = Inter({
- subsets: ['latin'],
- variable: '--font-inter',
-})
+// Initialize PostHog
+if (typeof window !== 'undefined' && process.env.NEXT_PUBLIC_POSTHOG_KEY) {
+ posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY, {
+ api_host: `${window.location.protocol}//${window.location.host}/ingest`,
+ // Enable debug mode in development
+ loaded: (posthog) => {
+ if (process.env.NODE_ENV === 'development') posthog.debug()
+ }
+ })
+}
-function App({ Component, pageProps }: AppProps<{ initialSession: Session, project?: projects }>) {
+function App({ Component, pageProps }: AppProps<{ initialSession?: Session, project?: projects }>) {
const [supabaseClient] = useState(() => createBrowserSupabaseClient<Database>(clientCreds))
+ const router = useRouter()
+
+ useEffect(function trackPageViews() {
+ // Track page views
+ const handleRouteChange = () => posthog?.capture('$pageview')
+ router.events.on('routeChangeComplete', handleRouteChange)
+
+ return () => {
+ router.events.off('routeChangeComplete', handleRouteChange)
+ }
+ }, [])
return (
- <div className={clsx(
- inter.variable,
- 'font-sans',
- 'flex',
- 'h-full',
- 'w-full',
- 'flex-1',
- 'flex-col',
- 'overflow-hidden',
- )}>
+ <PostHogProvider
+ client={posthog}
+ >
<SessionContextProvider
supabaseClient={supabaseClient}
initialSession={pageProps.initialSession}
>
- <Component {...pageProps} />
+ <Layout>
+ <Component {...pageProps} />
+ </Layout>
</SessionContextProvider>
- </div>
+ </PostHogProvider>
)
}
diff --git a/pages/agent/smol-developer/setup.tsx b/pages/agent/smol-developer/setup.tsx
index 9539c7648..5827b02de 100644
--- a/pages/agent/smol-developer/setup.tsx
+++ b/pages/agent/smol-developer/setup.tsx
@@ -10,6 +10,7 @@ import {
useUser,
useSupabaseClient,
} from '@supabase/auth-helpers-react'
+import { usePostHog } from 'posthog-js/react'
import Steps from 'components/Steps'
import SelectRepository from 'components/SelectRepository'
@@ -112,6 +113,8 @@ function Setup() {
const { repos, refetch } = useRepositories(github)
const [instructions, setInstructions] = useState('')
const [openAIAPIKey, setOpenAIAPIKey] = useState<string>('')
+ const posthog = usePostHog()
+
const handleMessageEvent = useCallback((event: MessageEvent) => {
if (event.data.accessToken) {
@@ -188,6 +191,7 @@ function Setup() {
async function signOut() {
await supabaseClient.auth.signOut()
+ posthog?.reset(true)
location.reload()
}
diff --git a/pages/index.tsx b/pages/index.tsx
index cbd24ed5d..bae398676 100644
--- a/pages/index.tsx
+++ b/pages/index.tsx
@@ -79,14 +79,6 @@ export const getServerSideProps: GetServerSideProps<Props> = async (ctx) => {
// Show projects from all teams.
const projects = user.users_teams.flatMap(t => t.teams.projects)
- if (projects.length === 0) {
- return {
- redirect: {
- permanent: false,
- destination: '/agent/smol-developer',
- },
- }
- }
return {
props: {
diff --git a/pages/sign.tsx b/pages/sign.tsx
index 9d388f1ec..a26c85c19 100644
--- a/pages/sign.tsx
+++ b/pages/sign.tsx
@@ -17,9 +17,6 @@ function SignIn() {
useEffect(() => {
if (user) {
router.push('/')
-
-
-
}
}, [user, router])