Skip to content

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

feat: shared useGoogleAnalyticsProps

phamhieu10d52861dc2a
diff --git a/apps/docs/lib/fetchWrappers.tsx b/apps/docs/lib/fetchWrappers.tsx
index c573a0d37dc..0313e49565f 100644
--- a/apps/docs/lib/fetchWrappers.tsx
+++ b/apps/docs/lib/fetchWrappers.tsx
@@ -1,7 +1,5 @@
interface DataProps {
- referrer?: string
- title: string
- route?: string
+ [prop: string]: any
}
export const post = (url: string, data: DataProps, options = {}) => {
diff --git a/apps/docs/pages/_app.tsx b/apps/docs/pages/_app.tsx
index f66ae722cd3..e80d0dd598d 100644
--- a/apps/docs/pages/_app.tsx
+++ b/apps/docs/pages/_app.tsx
@@ -1,6 +1,6 @@
import { createBrowserSupabaseClient } from '@supabase/auth-helpers-nextjs'
import { SessionContextProvider } from '@supabase/auth-helpers-react'
-import { AuthProvider, ThemeProvider } from 'common'
+import { AuthProvider, ThemeProvider, useGoogleAnalyticsProps } from 'common'
import { useRouter } from 'next/router'
import { useEffect, useState } from 'react'
import ReactMarkdown from 'react-markdown'
@@ -19,6 +19,7 @@ import '../styles/prism-okaidia.scss'
function MyApp({ Component, pageProps }: AppPropsWithLayout) {
const router = useRouter()
+ const googleAnalyticsProps = useGoogleAnalyticsProps()
const [supabase] = useState(() =>
IS_PLATFORM || LOCAL_SUPABASE ? createBrowserSupabaseClient() : undefined
@@ -29,6 +30,10 @@ function MyApp({ Component, pageProps }: AppPropsWithLayout) {
referrer: document.referrer,
title: document.title,
route,
+ ga: {
+ screen_resolution: googleAnalyticsProps?.screenResolution,
+ language: googleAnalyticsProps?.language,
+ },
})
}
diff --git a/apps/www/components/Features/index.tsx b/apps/www/components/Features/index.tsx
index bb84b4dbf4c..2b14cc6fbfe 100644
--- a/apps/www/components/Features/index.tsx
+++ b/apps/www/components/Features/index.tsx
@@ -1,11 +1,11 @@
import { Badge } from 'ui'
import Solutions from 'data/Solutions.json'
-import useGoogleAnalyticsProps from '~/hooks/useGoogleAnalyticsProps'
import Telemetry from '~/lib/telemetry'
import gaEvents from '~/lib/gaEvents'
import SectionContainer from '../Layouts/SectionContainer'
import ProductIcon from '../ProductIcon'
import TextLink from '../TextLink'
+import { useGoogleAnalyticsProps } from 'common/hooks/useGoogleAnalyticsProps'
const Features = () => {
const googleAnalyticsProps = useGoogleAnalyticsProps()
diff --git a/apps/www/components/Hero.tsx b/apps/www/components/Hero.tsx
index 28cbc079bca..e8d336787af 100644
--- a/apps/www/components/Hero.tsx
+++ b/apps/www/components/Hero.tsx
@@ -4,7 +4,7 @@ import Link from 'next/link'
import SectionContainer from './Layouts/SectionContainer'
import Telemetry, { GoogleAnalyticsEvent } from '~/lib/telemetry'
import gaEvents from '~/lib/gaEvents'
-import useGoogleAnalyticsProps from '~/hooks/useGoogleAnalyticsProps'
+import { useGoogleAnalyticsProps } from 'common/hooks/useGoogleAnalyticsProps'
const Hero = () => {
const { basePath } = useRouter()
diff --git a/apps/www/hooks/useGoogleAnalyticsProps.tsx b/apps/www/hooks/useGoogleAnalyticsProps.tsx
deleted file mode 100644
index a3254a8b307..00000000000
--- a/apps/www/hooks/useGoogleAnalyticsProps.tsx
+++ /dev/null
@@ -1,13 +0,0 @@
-import { useRouter } from 'next/router'
-
-const useGoogleAnalyticsProps = () => {
- const { locale } = useRouter()
-
- return {
- screenResolution:
- typeof window !== 'undefined' ? `${window.innerWidth}x${window.innerHeight}` : undefined,
- language: locale ?? 'en-US',
- }
-}
-
-export default useGoogleAnalyticsProps
diff --git a/apps/www/package.json b/apps/www/package.json
index cadcf99b872..8242300daea 100644
--- a/apps/www/package.json
+++ b/apps/www/package.json
@@ -20,6 +20,7 @@
"@next/mdx": "^12.3.2",
"@supabase/auth-ui-react": "^0.1.6",
"@supabase/supabase-js": "^2.13.0",
+ "browser-session-tabs": "^3.0.0",
"classnames": "^2.3.1",
"cobe": "^0.6.2",
"common": "*",
diff --git a/apps/www/pages/_app.tsx b/apps/www/pages/_app.tsx
index dce4295802c..f2e7ad106a0 100644
--- a/apps/www/pages/_app.tsx
+++ b/apps/www/pages/_app.tsx
@@ -6,20 +6,35 @@ import { useEffect } from 'react'
import Meta from '~/components/Favicons'
import '../styles/index.css'
import { post } from '~/lib/fetchWrapper'
-import { AuthProvider, ThemeProvider } from 'common'
+import { AuthProvider, ThemeProvider, useGoogleAnalyticsProps } from 'common'
+import { BrowserTabTracker } from 'browser-session-tabs'
+import { v4 as uuidv4 } from 'uuid'
import Head from 'next/head'
export default function MyApp({ Component, pageProps }: AppProps) {
const router = useRouter()
+ const googleAnalyticsProps = useGoogleAnalyticsProps()
function handlePageTelemetry(route: string) {
return post(`${API_URL}/telemetry/page`, {
referrer: document.referrer,
title: document.title,
route,
+ ga: {
+ screen_resolution: googleAnalyticsProps?.screenResolution,
+ language: googleAnalyticsProps?.language,
+ },
})
}
+ useEffect(() => {
+ // Generate browser session id for anon tracking
+ BrowserTabTracker.initialize({
+ storageKey: 'supabase.browser.session',
+ sessionIdGenerator: () => uuidv4(),
+ })
+ }, [])
+
useEffect(() => {
function handleRouteChange(url: string) {
handlePageTelemetry(url)
diff --git a/package-lock.json b/package-lock.json
index 0881d78353b..efb8b271642 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -673,6 +673,7 @@
"@next/mdx": "^12.3.2",
"@supabase/auth-ui-react": "^0.1.6",
"@supabase/supabase-js": "^2.13.0",
+ "browser-session-tabs": "^3.0.0",
"classnames": "^2.3.1",
"cobe": "^0.6.2",
"common": "*",
@@ -12807,6 +12808,22 @@
"integrity": "sha512-9o5UecI3GhkpM6DrXr69PblIuWxPKk9Y0jHBRhdocZ2y7YECBFCsHm79Pr3OyR2AvjhDkabFJaDJMYRazHgsow==",
"dev": true
},
+ "node_modules/browser-session-tabs": {
+ "version": "3.0.0",
+ "resolved": "https://registry.npmjs.org/browser-session-tabs/-/browser-session-tabs-3.0.0.tgz",
+ "integrity": "sha512-HBJWkCbcDYFL+hlSGld54SF1SgaiSXnw9vaTPdDeOxT9HCTFt3hVdArIroQ3HDhh9vhqkbl6TkMjTFok0ZfwIA==",
+ "dependencies": {
+ "js-cookie": "^3.0.1"
+ }
+ },
+ "node_modules/browser-session-tabs/node_modules/js-cookie": {
+ "version": "3.0.5",
+ "resolved": "https://registry.npmjs.org/js-cookie/-/js-cookie-3.0.5.tgz",
+ "integrity": "sha512-cEiJEAEoIbWfCZYKWhVwFuvPX1gETRYPw6LlaTKoxD3s2AkXzkCjnp6h0V77ozyqj0jakteJ4YqDJT830+lVGw==",
+ "engines": {
+ "node": ">=14"
+ }
+ },
"node_modules/browserify-aes": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/browserify-aes/-/browserify-aes-1.2.0.tgz",
@@ -48910,6 +48927,21 @@
"integrity": "sha512-9o5UecI3GhkpM6DrXr69PblIuWxPKk9Y0jHBRhdocZ2y7YECBFCsHm79Pr3OyR2AvjhDkabFJaDJMYRazHgsow==",
"dev": true
},
+ "browser-session-tabs": {
+ "version": "3.0.0",
+ "resolved": "https://registry.npmjs.org/browser-session-tabs/-/browser-session-tabs-3.0.0.tgz",
+ "integrity": "sha512-HBJWkCbcDYFL+hlSGld54SF1SgaiSXnw9vaTPdDeOxT9HCTFt3hVdArIroQ3HDhh9vhqkbl6TkMjTFok0ZfwIA==",
+ "requires": {
+ "js-cookie": "^3.0.1"
+ },
+ "dependencies": {
+ "js-cookie": {
+ "version": "3.0.5",
+ "resolved": "https://registry.npmjs.org/js-cookie/-/js-cookie-3.0.5.tgz",
+ "integrity": "sha512-cEiJEAEoIbWfCZYKWhVwFuvPX1gETRYPw6LlaTKoxD3s2AkXzkCjnp6h0V77ozyqj0jakteJ4YqDJT830+lVGw=="
+ }
+ }
+ },
"browserify-aes": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/browserify-aes/-/browserify-aes-1.2.0.tgz",
@@ -68107,7 +68139,7 @@
"@types/react": "^17.0.37",
"@types/react-copy-to-clipboard": "^5.0.4",
"@types/react-dom": "^17.0.11",
- "@types/react-syntax-highlighter": "*",
+ "@types/react-syntax-highlighter": "^15.5.6",
"autoprefixer": "^10.4.2",
"clsx": "^1.2.1",
"cmdk-supabase": "^0.2.2",
@@ -69873,6 +69905,7 @@
"@types/react-dom": "^17.0.11",
"@types/react-syntax-highlighter": "^13.5.2",
"autoprefixer": "^10.4.12",
+ "browser-session-tabs": "^3.0.0",
"classnames": "^2.3.1",
"cobe": "^0.6.2",
"common": "*",
diff --git a/packages/common/hooks/index.ts b/packages/common/hooks/index.ts
index 7560f7ebcf5..1d07190bbf5 100644
--- a/packages/common/hooks/index.ts
+++ b/packages/common/hooks/index.ts
@@ -1,2 +1,3 @@
export * from './useBreakpoint'
export * from './useParams'
+export * from './useGoogleAnalyticsProps'
diff --git a/packages/common/hooks/useGoogleAnalyticsProps.ts b/packages/common/hooks/useGoogleAnalyticsProps.ts
new file mode 100644
index 00000000000..3b247008f9f
--- /dev/null
+++ b/packages/common/hooks/useGoogleAnalyticsProps.ts
@@ -0,0 +1,12 @@
+import { useRouter } from 'next/router'
+
+const isBrowser = typeof window !== 'undefined'
+
+export function useGoogleAnalyticsProps() {
+ const { locale } = useRouter()
+
+ return {
+ screenResolution: isBrowser ? `${window.innerWidth}x${window.innerHeight}` : undefined,
+ language: locale ?? 'en-US',
+ }
+}