Skip to content

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

feat: send GA4 props on telemetry call

phamhieu4779ded07e1c
diff --git a/studio/components/interfaces/SQLEditor/TabSqlQuery/FavouriteButton.tsx b/studio/components/interfaces/SQLEditor/TabSqlQuery/FavouriteButton.tsx
index 2fa72f0da54..19ab8769d49 100644
--- a/studio/components/interfaces/SQLEditor/TabSqlQuery/FavouriteButton.tsx
+++ b/studio/components/interfaces/SQLEditor/TabSqlQuery/FavouriteButton.tsx
@@ -44,7 +44,10 @@ const FavouriteButton = () => {
*/
const { query, name, desc } = sqlEditorStore.activeTab || {}
sqlEditorStore.addToFavorite(id, query, name, desc)
- Telemetry.sendEvent('sql_editor', 'sql_favourited', name)
+ Telemetry.sendEvent(
+ { category: 'sql_editor', action: 'sql_favourited', label: name },
+ ui.googleAnalyticsProps
+ )
/*
* reload sql data in store and re-select tab
@@ -82,7 +85,10 @@ const FavouriteButton = () => {
*/
const { name } = sqlEditorStore.activeTab || {}
sqlEditorStore.unFavorite(id)
- Telemetry.sendEvent('sql_editor', 'sql_unfavourited', name)
+ Telemetry.sendEvent(
+ { category: 'sql_editor', action: 'sql_unfavourited', label: name },
+ ui.googleAnalyticsProps
+ )
/*
* reload sql data in store and re-select tab
diff --git a/studio/components/to-be-cleaned/SqlEditor/TabSqlQuery.js b/studio/components/to-be-cleaned/SqlEditor/TabSqlQuery.js
index c377465d834..d4cde836715 100644
--- a/studio/components/to-be-cleaned/SqlEditor/TabSqlQuery.js
+++ b/studio/components/to-be-cleaned/SqlEditor/TabSqlQuery.js
@@ -234,14 +234,20 @@ const ResultsDropdown = observer(() => {
function onDownloadCSV() {
csvRef.current?.link.click()
- Telemetry.sendEvent('sql_editor', 'sql_download_csv', '')
+ Telemetry.sendEvent(
+ { category: 'sql_editor', action: 'sql_download_csv', label: '' },
+ ui.googleAnalyticsProps
+ )
}
function onCopyAsMarkdown() {
if (navigator) {
copyToClipboard(sqlEditorStore.activeTab.markdownData, () => {
ui.setNotification({ category: 'success', message: 'Copied results to clipboard' })
- Telemetry.sendEvent('sql_editor', 'sql_copy_as_markdown', '')
+ Telemetry.sendEvent(
+ { category: 'sql_editor', action: 'sql_copy_as_markdown', label: '' },
+ ui.googleAnalyticsProps
+ )
})
}
}
diff --git a/studio/components/to-be-cleaned/SqlEditor/TabWelcome.js b/studio/components/to-be-cleaned/SqlEditor/TabWelcome.js
index b488d7bd9c5..9faa88f801b 100644
--- a/studio/components/to-be-cleaned/SqlEditor/TabWelcome.js
+++ b/studio/components/to-be-cleaned/SqlEditor/TabWelcome.js
@@ -37,7 +37,14 @@ const TabWelcome = observer(() => {
sql={x.sql}
onClick={(sql, title) => {
handleNewQuery({ sql, name: title })
- Telemetry.sendEvent('scripts', 'script_clicked', x.title)
+ Telemetry.sendEvent(
+ {
+ category: 'scripts',
+ action: 'script_clicked',
+ label: x.title,
+ },
+ ui.googleAnalyticsProps
+ )
}}
/>
))}
@@ -64,7 +71,14 @@ const TabWelcome = observer(() => {
sql={x.sql}
onClick={(sql, title) => {
handleNewQuery({ sql, name: title })
- Telemetry.sendEvent('quickstart', 'quickstart_clicked', x.title)
+ Telemetry.sendEvent(
+ {
+ category: 'quickstart',
+ action: 'quickstart_clicked',
+ label: x.title,
+ },
+ ui.googleAnalyticsProps
+ )
}}
/>
))}
diff --git a/studio/components/ui/PageTelemetry.tsx b/studio/components/ui/PageTelemetry.tsx
index 6d8e082e707..30b26b70ddd 100644
--- a/studio/components/ui/PageTelemetry.tsx
+++ b/studio/components/ui/PageTelemetry.tsx
@@ -1,6 +1,7 @@
import { useStore } from 'hooks'
import { post } from 'lib/common/fetch'
import { API_URL, IS_PLATFORM } from 'lib/constants'
+import { getScreenResolution } from 'lib/telemetry'
import { observer } from 'mobx-react-lite'
import { useRouter } from 'next/router'
import { FC, useEffect } from 'react'
@@ -10,25 +11,12 @@ const PageTelemetry: FC = ({ children }) => {
const router = useRouter()
const { ui } = useStore()
const { profile } = ui
- let clientId: string
useEffect(() => {
function handleRouteChange() {
handlePageTelemetry(profile)
}
- // @ts-ignore
- window?.gtag(
- 'get',
- `'${process.env.NEXT_PUBLIC_GA4_MEASUREMENT_ID}'`,
- 'client_id',
- // @ts-ignore
- (client_id) => {
- console.log(client_id)
- clientId = client_id
- }
- )
-
// Listen for page changes after a navigation or when the query changes
router.events.on('routeChangeComplete', handleRouteChange)
return () => {
@@ -53,8 +41,6 @@ const PageTelemetry: FC = ({ children }) => {
*/
let referrer: string | undefined = document.referrer
- console.log('my clientId = ', clientId)
-
/**
* Send page telemetry
*
@@ -64,6 +50,7 @@ const PageTelemetry: FC = ({ children }) => {
post(`${API_URL}/telemetry/page`, {
referrer: referrer,
title: document.title,
+ ga: ui.googleAnalyticsProps,
})
}
}
diff --git a/studio/hooks/misc/useStore.tsx b/studio/hooks/misc/useStore.tsx
index ad858c288b4..4b5b42297d5 100644
--- a/studio/hooks/misc/useStore.tsx
+++ b/studio/hooks/misc/useStore.tsx
@@ -83,6 +83,21 @@ export const StoreProvider: FC<StoreProvider> = ({ children, rootStore }) => {
}
}
})
+
+ /**
+ * get Ga4 client_id when it's available
+ * */
+ // @ts-ignore
+ window?.gtag(
+ 'get',
+ `${process.env.NEXT_PUBLIC_GA4_MEASUREMENT_ID}`,
+ 'client_id',
+ // @ts-ignore
+ (client_id) => {
+ ui.setGaClientId(client_id)
+ }
+ )
+
return () =>
window
.matchMedia('(prefers-color-scheme: dark)')
diff --git a/studio/lib/telemetry.ts b/studio/lib/telemetry.ts
index 488c9a6d998..44c3b5e3041 100644
--- a/studio/lib/telemetry.ts
+++ b/studio/lib/telemetry.ts
@@ -2,21 +2,57 @@ import { post } from 'lib/common/fetch'
import { API_URL, IS_PLATFORM } from 'lib/constants'
import { User } from 'types'
-const sendEvent = (category: string, action: string, label: string, value?: string) => {
+export interface GoogleAnalyticsProps {
+ clientId?: string
+ screenResolution?: string
+ language: string
+}
+
+export const getScreenResolution = () => {
+ return typeof window !== 'undefined' ? `${window.innerWidth}x${window.innerHeight}` : undefined
+}
+
+const sendEvent = (
+ event: {
+ category: string
+ action: string
+ label: string
+ value?: string
+ },
+ gaProps?: GoogleAnalyticsProps
+) => {
if (!IS_PLATFORM) return
+ const { category, action, label, value } = event
+
return post(`${API_URL}/telemetry/event`, {
action: action,
category: category,
label: label,
value: value,
+ ga: {
+ client_id: gaProps?.clientId,
+ screen_resolution: gaProps?.screenResolution,
+ language: gaProps?.language,
+ },
})
}
-const sendIdentify = (user: User) => {
+/**
+ * TODO: GA4 doesn't have identify method.
+ * We may or may not need gaClientId here
+ */
+const sendIdentify = (user: User, gaProps?: GoogleAnalyticsProps) => {
if (!IS_PLATFORM) return
- return post(`${API_URL}/telemetry/identify`, { user })
+ return post(`${API_URL}/telemetry/identify`, {
+ user,
+ ga: {
+ client_id: gaProps?.clientId,
+ screen_resolution: gaProps?.screenResolution,
+ language: gaProps?.language,
+ },
+ })
}
export default {
diff --git a/studio/stores/UiStore.ts b/studio/stores/UiStore.ts
index b9bbd8a9a4a..5872763c42f 100644
--- a/studio/stores/UiStore.ts
+++ b/studio/stores/UiStore.ts
@@ -2,7 +2,7 @@ import { uuidv4 } from 'lib/helpers'
import { action, makeAutoObservable } from 'mobx'
import { Project, Notification, User, Organization, ProjectBase, Permission } from 'types'
import { IRootStore } from './RootStore'
-import Telemetry from 'lib/telemetry'
+import Telemetry, { getScreenResolution, GoogleAnalyticsProps } from 'lib/telemetry'
export interface IUiStore {
language: 'en_US'
@@ -18,6 +18,8 @@ export interface IUiStore {
profile?: User
permissions?: Permission[]
+ googleAnalyticsProps?: GoogleAnalyticsProps
+
load: () => void
setTheme: (theme: 'dark' | 'light') => void
onThemeOptionChange: (themeOption: 'dark' | 'light' | 'system') => void
@@ -26,6 +28,7 @@ export interface IUiStore {
setNotification: (notification: Notification) => string
setProfile: (value?: User) => void
setPermissions: (permissions?: Permission[]) => void
+ setGaClientId: (clientId?: string) => void
}
export default class UiStore implements IUiStore {
rootStore: IRootStore
@@ -39,6 +42,9 @@ export default class UiStore implements IUiStore {
profile?: User
permissions?: Permission[] = []
+ // GA4 client id. It's synced with every Telemetry call
+ gaClientId?: string
+
constructor(rootStore: IRootStore) {
this.rootStore = rootStore
makeAutoObservable(this, {
@@ -98,6 +104,14 @@ export default class UiStore implements IUiStore {
return this.theme === 'dark'
}
+ get googleAnalyticsProps() {
+ return {
+ clientId: this.gaClientId,
+ screenResolution: getScreenResolution(),
+ language: this.language,
+ }
+ }
+
load() {
if (typeof window === 'undefined') return
const localStorageThemeOption = window.localStorage.getItem('theme')
@@ -149,7 +163,7 @@ export default class UiStore implements IUiStore {
setProfile(value?: User) {
if (value && value?.id !== this.profile?.id) {
- Telemetry.sendIdentify(value)
+ Telemetry.sendIdentify(value, this.googleAnalyticsProps)
}
this.profile = value
@@ -158,4 +172,8 @@ export default class UiStore implements IUiStore {
setPermissions(permissions?: any) {
this.permissions = permissions
}
+
+ setGaClientId(clientId?: string) {
+ this.gaClientId = clientId
+ }
}