Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Add tracking for API docs usage (#39307)

Pamela Chia5b2351da8697
diff --git a/apps/studio/components/interfaces/Auth/Users/UsersV2.tsx b/apps/studio/components/interfaces/Auth/Users/UsersV2.tsx
index 6570110caf0..fd4f05d9128 100644
--- a/apps/studio/components/interfaces/Auth/Users/UsersV2.tsx
+++ b/apps/studio/components/interfaces/Auth/Users/UsersV2.tsx
@@ -503,7 +503,9 @@ export const UsersV2 = () => {
</div>
<div className="flex items-center gap-x-2">
- {isNewAPIDocsEnabled && <APIDocsButton section={['user-management']} />}
+ {isNewAPIDocsEnabled && (
+ <APIDocsButton section={['user-management']} source="auth-users" />
+ )}
<Button
size="tiny"
icon={<RefreshCw />}
diff --git a/apps/studio/components/interfaces/Docs/CodeSnippet.tsx b/apps/studio/components/interfaces/Docs/CodeSnippet.tsx
index 813a346d4df..fe2db105f64 100644
--- a/apps/studio/components/interfaces/Docs/CodeSnippet.tsx
+++ b/apps/studio/components/interfaces/Docs/CodeSnippet.tsx
@@ -1,3 +1,6 @@
+import { useParams } from 'common'
+import { useSendEventMutation } from 'data/telemetry/send-event-mutation'
+import { useSelectedOrganizationQuery } from 'hooks/misc/useSelectedOrganization'
import { SimpleCodeBlock } from 'ui'
interface CodeSnippetProps {
@@ -10,11 +13,29 @@ interface CodeSnippetProps {
}
const CodeSnippet = ({ selectedLang, snippet }: CodeSnippetProps) => {
+ const { ref: projectRef } = useParams()
+ const { data: org } = useSelectedOrganizationQuery()
+ const { mutate: sendEvent } = useSendEventMutation()
+
+ const handleCopy = () => {
+ sendEvent({
+ action: 'api_docs_code_copy_button_clicked',
+ properties: {
+ title: snippet.title,
+ selectedLanguage: selectedLang,
+ },
+ groups: {
+ project: projectRef ?? 'Unknown',
+ organization: org?.slug ?? 'Unknown',
+ },
+ })
+ }
+
if (!snippet[selectedLang]) return null
return (
<div className="codeblock-container">
<h4>{snippet.title}</h4>
- <SimpleCodeBlock className={snippet[selectedLang]?.language}>
+ <SimpleCodeBlock className={snippet[selectedLang]?.language} onCopy={handleCopy}>
{snippet[selectedLang]?.code}
</SimpleCodeBlock>
</div>
diff --git a/apps/studio/components/interfaces/ProjectAPIDocs/Content/Introduction.tsx b/apps/studio/components/interfaces/ProjectAPIDocs/Content/Introduction.tsx
index e8d906fe5da..38528091254 100644
--- a/apps/studio/components/interfaces/ProjectAPIDocs/Content/Introduction.tsx
+++ b/apps/studio/components/interfaces/ProjectAPIDocs/Content/Introduction.tsx
@@ -3,6 +3,8 @@ import { Button, Input, copyToClipboard } from 'ui'
import { getKeys, useAPIKeysQuery } from 'data/api-keys/api-keys-query'
import { useProjectSettingsV2Query } from 'data/config/project-settings-v2-query'
+import { useSendEventMutation } from 'data/telemetry/send-event-mutation'
+import { useSelectedOrganizationQuery } from 'hooks/misc/useSelectedOrganization'
import { Copy } from 'lucide-react'
import { useEffect, useState } from 'react'
import ContentSnippet from '../ContentSnippet'
@@ -13,6 +15,8 @@ const Introduction = ({ showKeys, language, apikey, endpoint }: ContentProps) =>
const { ref } = useParams()
const { data: apiKeys } = useAPIKeysQuery({ projectRef: ref })
const { data } = useProjectSettingsV2Query({ projectRef: ref })
+ const { data: org } = useSelectedOrganizationQuery()
+ const { mutate: sendEvent } = useSendEventMutation()
const [copied, setCopied] = useState<'anon' | 'service'>()
@@ -54,6 +58,17 @@ const Introduction = ({ showKeys, language, apikey, endpoint }: ContentProps) =>
onClick={() => {
setCopied('anon')
copyToClipboard(anonApiKey ?? 'SUPABASE_CLIENT_ANON_KEY')
+ sendEvent({
+ action: 'api_docs_code_copy_button_clicked',
+ properties: {
+ title: 'Client API key',
+ selectedLanguage: language,
+ },
+ groups: {
+ project: ref ?? 'Unknown',
+ organization: org?.slug ?? 'Unknown',
+ },
+ })
}}
>
{copied === 'anon' ? 'Copied' : 'Copy'}
@@ -87,6 +102,17 @@ const Introduction = ({ showKeys, language, apikey, endpoint }: ContentProps) =>
onClick={() => {
setCopied('service')
copyToClipboard(serviceApiKey)
+ sendEvent({
+ action: 'api_docs_code_copy_button_clicked',
+ properties: {
+ title: 'Service key',
+ selectedLanguage: language,
+ },
+ groups: {
+ project: ref ?? 'Unknown',
+ organization: org?.slug ?? 'Unknown',
+ },
+ })
}}
>
{copied === 'service' ? 'Copied' : 'Copy'}
diff --git a/apps/studio/components/interfaces/ProjectAPIDocs/ContentSnippet.tsx b/apps/studio/components/interfaces/ProjectAPIDocs/ContentSnippet.tsx
index 6a8a8cd60a5..7c9cc1a9f51 100644
--- a/apps/studio/components/interfaces/ProjectAPIDocs/ContentSnippet.tsx
+++ b/apps/studio/components/interfaces/ProjectAPIDocs/ContentSnippet.tsx
@@ -3,6 +3,8 @@ import { useParams } from 'common'
import { SimpleCodeBlock } from 'ui'
import { Markdown } from '../Markdown'
import { PropsWithChildren } from 'react'
+import { useSendEventMutation } from 'data/telemetry/send-event-mutation'
+import { useSelectedOrganizationQuery } from 'hooks/misc/useSelectedOrganization'
interface ContentSnippetProps {
apikey?: string
@@ -26,11 +28,28 @@ const ContentSnippet = ({
children,
}: PropsWithChildren<ContentSnippetProps>) => {
const { ref: projectRef } = useParams()
+ const { data: org } = useSelectedOrganizationQuery()
+ const { mutate: sendEvent } = useSendEventMutation()
+
const codeSnippet = snippet[selectedLanguage]?.(apikey, endpoint).replaceAll(
'[ref]',
projectRef ?? ''
)
+ const handleCopy = () => {
+ sendEvent({
+ action: 'api_docs_code_copy_button_clicked',
+ properties: {
+ title: snippet.title,
+ selectedLanguage,
+ },
+ groups: {
+ project: projectRef ?? 'Unknown',
+ organization: org?.slug ?? 'Unknown',
+ },
+ })
+ }
+
return (
<div id={snippet.key} className="space-y-4 py-6 pb-2 last:pb-6">
<div className="px-4 space-y-4">
@@ -50,7 +69,9 @@ const ContentSnippet = ({
{codeSnippet !== undefined && (
<div className="px-4 codeblock-container">
<div className="bg rounded p-2">
- <SimpleCodeBlock className={selectedLanguage}>{codeSnippet}</SimpleCodeBlock>
+ <SimpleCodeBlock className={selectedLanguage} onCopy={handleCopy}>
+ {codeSnippet}
+ </SimpleCodeBlock>
</div>
</div>
)}
diff --git a/apps/studio/components/interfaces/ProjectAPIDocs/ResourceContent.tsx b/apps/studio/components/interfaces/ProjectAPIDocs/ResourceContent.tsx
index de50d604b47..35f375305d1 100644
--- a/apps/studio/components/interfaces/ProjectAPIDocs/ResourceContent.tsx
+++ b/apps/studio/components/interfaces/ProjectAPIDocs/ResourceContent.tsx
@@ -2,6 +2,8 @@ import { SimpleCodeBlock } from 'ui'
import { useParams } from 'common'
import { DocsButton } from 'components/ui/DocsButton'
import { Markdown } from '../Markdown'
+import { useSendEventMutation } from 'data/telemetry/send-event-mutation'
+import { useSelectedOrganizationQuery } from 'hooks/misc/useSelectedOrganization'
interface ResourceContentProps {
selectedLanguage: 'js' | 'bash'
@@ -16,6 +18,22 @@ interface ResourceContentProps {
const ResourceContent = ({ selectedLanguage, snippet, codeSnippets }: ResourceContentProps) => {
const { ref: projectRef } = useParams()
+ const { data: org } = useSelectedOrganizationQuery()
+ const { mutate: sendEvent } = useSendEventMutation()
+
+ const handleCopy = (title: string) => {
+ sendEvent({
+ action: 'api_docs_code_copy_button_clicked',
+ properties: {
+ title,
+ selectedLanguage,
+ },
+ groups: {
+ project: projectRef ?? 'Unknown',
+ organization: org?.slug ?? 'Unknown',
+ },
+ })
+ }
return (
<div id={snippet.key} className="space-y-4 py-6">
@@ -40,7 +58,10 @@ const ResourceContent = ({ selectedLanguage, snippet, codeSnippets }: ResourceCo
<p className="text-sm text-foreground-light">{codeSnippet.title}</p>
<div className="codeblock-container">
<div className="bg rounded p-2">
- <SimpleCodeBlock className={selectedLanguage}>
+ <SimpleCodeBlock
+ className={selectedLanguage}
+ onCopy={() => handleCopy(codeSnippet.title)}
+ >
{codeSnippet[selectedLanguage]}
</SimpleCodeBlock>
</div>
diff --git a/apps/studio/components/interfaces/Sidebar.tsx b/apps/studio/components/interfaces/Sidebar.tsx
index 72eea15c4c9..9b9700a3ee2 100644
--- a/apps/studio/components/interfaces/Sidebar.tsx
+++ b/apps/studio/components/interfaces/Sidebar.tsx
@@ -13,6 +13,7 @@ import {
generateToolRoutes,
} from 'components/layouts/ProjectLayout/NavigationBar/NavigationBar.utils'
import { ProjectIndexPageLink } from 'data/prefetchers/project.$ref'
+import { useSendEventMutation } from 'data/telemetry/send-event-mutation'
import { useHideSidebar } from 'hooks/misc/useHideSidebar'
import { useIsFeatureEnabled } from 'hooks/misc/useIsFeatureEnabled'
import { useLints } from 'hooks/misc/useLints'
@@ -223,9 +224,11 @@ const ProjectLinks = () => {
const router = useRouter()
const { ref } = useParams()
const { data: project } = useSelectedProjectQuery()
+ const { data: org } = useSelectedOrganizationQuery()
const snap = useAppStateSnapshot()
const { securityLints, errorLints } = useLints()
const showReports = useIsFeatureEnabled('reports:all')
+ const { mutate: sendEvent } = useSendEventMutation()
const isNewAPIDocsEnabled = useIsAPIDocsSidePanelEnabled()
const isStorageV2 = useIsNewStorageUIEnabled()
@@ -297,18 +300,37 @@ const ProjectLinks = () => {
<Separator className="w-[calc(100%-1rem)] mx-auto" />
<SidebarGroup className="gap-0.5">
{otherRoutes.map((route, i) => {
- if (route.key === 'api' && isNewAPIDocsEnabled) {
+ if (route.key === 'api') {
+ const handleApiClick = () => {
+ if (isNewAPIDocsEnabled) {
+ snap.setShowProjectApiDocs(true)
+ }
+ sendEvent({
+ action: 'api_docs_opened',
+ properties: {
+ source: 'sidebar',
+ },
+ groups: {
+ project: ref ?? 'Unknown',
+ organization: org?.slug ?? 'Unknown',
+ },
+ })
+ }
+
return (
<SideBarNavLink
key={`other-routes-${i}`}
- route={{
- label: route.label,
- icon: route.icon,
- key: route.key,
- }}
- onClick={() => {
- snap.setShowProjectApiDocs(true)
- }}
+ route={
+ isNewAPIDocsEnabled
+ ? {
+ label: route.label,
+ icon: route.icon,
+ key: route.key,
+ }
+ : route
+ }
+ active={activeRoute === route.key}
+ onClick={handleApiClick}
/>
)
} else if (route.key === 'advisors') {
diff --git a/apps/studio/components/interfaces/Storage/StorageExplorer/FileExplorerHeader.tsx b/apps/studio/components/interfaces/Storage/StorageExplorer/FileExplorerHeader.tsx
index 7025cf213cd..c42bde0e89c 100644
--- a/apps/studio/components/interfaces/Storage/StorageExplorer/FileExplorerHeader.tsx
+++ b/apps/studio/components/interfaces/Storage/StorageExplorer/FileExplorerHeader.tsx
@@ -517,7 +517,7 @@ export const FileExplorerHeader = ({
<>
<div className="h-6 border-r border-control" />
<div className="mx-2">
- <APIDocsButton section={['storage', selectedBucket.name]} />
+ <APIDocsButton section={['storage', selectedBucket.name]} source="storage" />
</div>
</>
)}
diff --git a/apps/studio/components/interfaces/TableGridEditor/GridHeaderActions.tsx b/apps/studio/components/interfaces/TableGridEditor/GridHeaderActions.tsx
index a2a99dfcd3e..179e3f46ee2 100644
--- a/apps/studio/components/interfaces/TableGridEditor/GridHeaderActions.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/GridHeaderActions.tsx
@@ -527,7 +527,9 @@ export const GridHeaderActions = ({ table, isRefetching }: GridHeaderActionsProp
<RoleImpersonationPopover serviceRoleLabel="postgres" />
- {doesHaveAutoGeneratedAPIDocs && <APIDocsButton section={['entities', table.name]} />}
+ {doesHaveAutoGeneratedAPIDocs && (
+ <APIDocsButton section={['entities', table.name]} source="table_editor" />
+ )}
<RefreshButton tableId={table.id} isRefetching={isRefetching} />
</div>
diff --git a/apps/studio/components/layouts/EdgeFunctionsLayout/EdgeFunctionDetailsLayout.tsx b/apps/studio/components/layouts/EdgeFunctionsLayout/EdgeFunctionDetailsLayout.tsx
index f37b37a7a44..998ab9bf59d 100644
--- a/apps/studio/components/layouts/EdgeFunctionsLayout/EdgeFunctionDetailsLayout.tsx
+++ b/apps/studio/components/layouts/EdgeFunctionsLayout/EdgeFunctionDetailsLayout.tsx
@@ -168,6 +168,7 @@ const EdgeFunctionDetailsLayout = ({
section={
functionSlug !== undefined ? ['edge-functions', functionSlug] : ['edge-functions']
}
+ source="edge-functions"
/>
)}
<DocsButton href={`${DOCS_URL}/guides/functions`} />
diff --git a/apps/studio/components/ui/APIDocsButton.tsx b/apps/studio/components/ui/APIDocsButton.tsx
index 4a2f4971e49..dc1d3fc072e 100644
--- a/apps/studio/components/ui/APIDocsButton.tsx
+++ b/apps/studio/components/ui/APIDocsButton.tsx
@@ -1,13 +1,20 @@
import { BookOpenText } from 'lucide-react'
+import { useParams } from 'common'
+import { useSendEventMutation } from 'data/telemetry/send-event-mutation'
+import { useSelectedOrganizationQuery } from 'hooks/misc/useSelectedOrganization'
import { useAppStateSnapshot } from 'state/app-state'
import { ButtonTooltip } from './ButtonTooltip'
interface APIDocsButtonProps {
section?: string[]
+ source: string
}
-export const APIDocsButton = ({ section }: APIDocsButtonProps) => {
+export const APIDocsButton = ({ section, source }: APIDocsButtonProps) => {
const snap = useAppStateSnapshot()
+ const { ref } = useParams()
+ const { data: org } = useSelectedOrganizationQuery()
+ const { mutate: sendEvent } = useSendEventMutation()
return (
<ButtonTooltip
@@ -16,6 +23,17 @@ export const APIDocsButton = ({ section }: APIDocsButtonProps) => {
onClick={() => {
if (section) snap.setActiveDocsSection(section)
snap.setShowProjectApiDocs(true)
+
+ sendEvent({
+ action: 'api_docs_opened',
+ properties: {
+ source,
+ },
+ groups: {
+ project: ref ?? 'Unknown',
+ organization: org?.slug ?? 'Unknown',
+ },
+ })
}}
icon={<BookOpenText />}
className="h-7 w-7"
diff --git a/packages/common/telemetry-constants.ts b/packages/common/telemetry-constants.ts
index facb0c6d658..a0838b8d702 100644
--- a/packages/common/telemetry-constants.ts
+++ b/packages/common/telemetry-constants.ts
@@ -1861,6 +1861,46 @@ export interface TableRLSEnabledEvent {
groups: Partial<TelemetryGroups>
}
+/**
+ * User opened API docs panel.
+ *
+ * @group Events
+ * @source studio
+ * @page Various pages with API docs button
+ */
+export interface ApiDocsOpenedEvent {
+ action: 'api_docs_opened'
+ properties: {
+ /**
+ * Source of the API docs button click, e.g. table_editor, sidebar
+ */
+ source: string
+ }
+ groups: Partial<TelemetryGroups>
+}
+
+/**
+ * User clicked copy button in API docs panel.
+ *
+ * @group Events
+ * @source studio
+ * @page API docs panel
+ */
+export interface ApiDocsCodeCopyButtonClickedEvent {
+ action: 'api_docs_code_copy_button_clicked'
+ properties: {
+ /**
+ * Title of the content being copied
+ */
+ title?: string
+ /**
+ * Selected programming language, e.g. js, bash
+ */
+ selectedLanguage?: string
+ }
+ groups: Partial<TelemetryGroups>
+}
+
/**
* @hidden
*/
@@ -1868,6 +1908,8 @@ export type TelemetryEvent =
| SignUpEvent
| SignInEvent
| ConnectionStringCopiedEvent
+ | ApiDocsOpenedEvent
+ | ApiDocsCodeCopyButtonClickedEvent
| CronJobCreatedEvent
| CronJobUpdatedEvent
| CronJobDeletedEvent
diff --git a/packages/ui/src/components/SimpleCodeBlock/SimpleCodeBlock.tsx b/packages/ui/src/components/SimpleCodeBlock/SimpleCodeBlock.tsx
index 20e15661f36..cb84acae791 100644
--- a/packages/ui/src/components/SimpleCodeBlock/SimpleCodeBlock.tsx
+++ b/packages/ui/src/components/SimpleCodeBlock/SimpleCodeBlock.tsx
@@ -25,6 +25,7 @@ interface SimpleCodeBlockProps {
parentClassName?: string
className?: string
showCopy?: boolean
+ onCopy?: () => void
}
export const SimpleCodeBlock = ({
@@ -32,6 +33,7 @@ export const SimpleCodeBlock = ({
parentClassName,
className: languageClassName,
showCopy = true,
+ onCopy,
}: PropsWithChildren<SimpleCodeBlockProps>) => {
const { resolvedTheme } = useTheme()
const [showCopied, setShowCopied] = useState(false)
@@ -52,6 +54,7 @@ export const SimpleCodeBlock = ({
const handleCopyCode = (code: any) => {
copyToClipboard(code, () => setShowCopied(true))
+ onCopy?.()
}
return (