📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
display connected GSC email address
Blue Mousee35906bbe79f
diff --git a/backend/apps/cloud/src/project/entity/project.entity.ts b/backend/apps/cloud/src/project/entity/project.entity.tsindex e1f531fc6..598633865 100644--- a/backend/apps/cloud/src/project/entity/project.entity.ts+++ b/backend/apps/cloud/src/project/entity/project.entity.ts@@ -150,4 +150,7 @@ export class Project {@Column('varchar', { nullable: true, default: null, length: 512 })gscScope: string | null++ @Column('varchar', { nullable: true, default: null, length: 256 })+ gscAccountEmail: string | null}diff --git a/backend/apps/cloud/src/project/gsc.controller.ts b/backend/apps/cloud/src/project/gsc.controller.tsindex 6d749e3c2..2f0d615b5 100644--- a/backend/apps/cloud/src/project/gsc.controller.ts+++ b/backend/apps/cloud/src/project/gsc.controller.ts@@ -52,8 +52,7 @@ export class GSCController {async status(@Param('pid') pid: string, @CurrentUserId() uid: string) {const project = await this.projectService.getRedisProject(pid)this.projectService.allowedToManage(project, uid)- const connected = await this.gscService.isConnected(pid)- return { connected }+ return this.gscService.getStatus(pid)}@ApiBearerAuth()diff --git a/backend/apps/cloud/src/project/gsc.service.ts b/backend/apps/cloud/src/project/gsc.service.tsindex 23d84b8e2..136f39d8f 100644--- a/backend/apps/cloud/src/project/gsc.service.ts+++ b/backend/apps/cloud/src/project/gsc.service.ts@@ -64,7 +64,10 @@ export class GSCService {const url = oauth2Client.generateAuthUrl({access_type: 'offline',- scope: ['https://www.googleapis.com/auth/webmasters.readonly'],+ scope: [+ 'https://www.googleapis.com/auth/webmasters.readonly',+ 'https://www.googleapis.com/auth/userinfo.email',+ ],prompt: 'consent',state,})@@ -82,6 +85,17 @@ export class GSCService {const oauth2Client = this.getOAuthClient()const { tokens } = await oauth2Client.getToken(code)+ oauth2Client.setCredentials(tokens)++ // Try to fetch connected Google account email+ let accountEmail: string | null = null+ try {+ const oauth2 = google.oauth2({ version: 'v2', auth: oauth2Client })+ const { data } = await oauth2.userinfo.get({})+ accountEmail = (data as any)?.email || null+ } catch {+ // ignore email fetch failures+ }const toStore: StoredTokens = {access_token: tokens.access_token,@@ -94,6 +108,12 @@ export class GSCService {await this.setStoredTokens(pid, toStore)+ // Persist connected account email separately+ await this.projectService.update(+ { id: pid },+ { gscAccountEmail: accountEmail } as any,+ )+// One-time stateawait redis.del(REDIS_STATE_PREFIX + state)@@ -157,6 +177,7 @@ export class GSCService {gscTokenExpiry: null,gscScope: null,gscPropertyUri: null,+ gscAccountEmail: null,} as any)}@@ -165,6 +186,16 @@ export class GSCService {return !_isEmpty(tokens?.refresh_token || tokens?.access_token)}+ async getStatus(pid: string): Promise<{ connected: boolean; email: string | null }> {+ const connected = await this.isConnected(pid)+ if (!connected) return { connected, email: null }+ const project = await this.projectService.findOne({+ where: { id: pid },+ select: ['gscAccountEmail'],+ })+ return { connected, email: project?.gscAccountEmail || null }+ }+private async getAuthedClientForPid(pid: string) {const tokens = await this.getStoredTokens(pid)if (_isEmpty(tokens)) {diff --git a/backend/migrations/mysql/2025_10_29.sql b/backend/migrations/mysql/2025_10_29.sqlindex 7bcb8038f..518da4cde 100644--- a/backend/migrations/mysql/2025_10_29.sql+++ b/backend/migrations/mysql/2025_10_29.sql@@ -4,3 +4,5 @@ ALTER TABLE projectADD COLUMN `gscRefreshTokenEnc` text DEFAULT NULL AFTER `gscAccessTokenEnc`,ADD COLUMN `gscTokenExpiry` bigint DEFAULT NULL AFTER `gscRefreshTokenEnc`,ADD COLUMN `gscScope` varchar(512) DEFAULT NULL AFTER `gscTokenExpiry`;++ALTER TABLE project ADD COLUMN `gscAccountEmail` varchar(256) DEFAULT NULL AFTER `gscScope`;diff --git a/web/app/api/index.ts b/web/app/api/index.tsindex f3ae6aca4..5bcdc6f4e 100644--- a/web/app/api/index.ts+++ b/web/app/api/index.ts@@ -1527,7 +1527,7 @@ export const generateGSCAuthURL = (pid: string) =>export const getGSCStatus = (pid: string) =>api.get(`v1/project/gsc/${pid}/status`)- .then((response): { connected: boolean } => response.data)+ .then((response): { connected: boolean; email?: string | null } => response.data).catch((error) => {throw _isEmpty(error.response.data?.message) ? error.response.data : error.response.data.message})diff --git a/web/app/pages/Project/Settings/ProjectSettings.tsx b/web/app/pages/Project/Settings/ProjectSettings.tsxindex b42b3f7b5..3d2c0e814 100644--- a/web/app/pages/Project/Settings/ProjectSettings.tsx+++ b/web/app/pages/Project/Settings/ProjectSettings.tsx@@ -354,6 +354,7 @@ const ProjectSettings = () => {// Google Search Console integration stateconst [gscConnected, setGscConnected] = useState<boolean | null>(null)const [gscProperties, setGscProperties] = useState<{ siteUrl: string; permissionLevel?: string }[]>([])+ const [gscEmail, setGscEmail] = useState<string | null>(null)// for reset data via filtersconst [activeFilter, setActiveFilter] = useState<string[]>([])@@ -423,8 +424,9 @@ const ProjectSettings = () => {const refreshGSCStatus = useCallback(async () => {try {- const { connected } = await getGSCStatus(id)+ const { connected, email } = await getGSCStatus(id)setGscConnected(connected)+ setGscEmail(email || null)if (connected) {try {const props = await getGSCProperties(id)@@ -844,96 +846,95 @@ const ProjectSettings = () => {</div>) : (<div className='flex flex-col gap-3'>- {_isEmpty(gscProperties) ? (- <p className='text-sm text-gray-800 dark:text-gray-200'>- {t('project.settings.gsc.accountConnected')}- </p>- ) : (- <div className='flex items-center gap-3'>- <div className='flex-1'>- <Select- items={_map(gscProperties, (p) => ({ key: p.siteUrl, label: p.siteUrl }))}- keyExtractor={(item) => item.key}- labelExtractor={(item) => item.label}- onSelect={(item: { key: string; label: string }) => {- setForm((prevForm) => ({- ...prevForm,- gscPropertyUri: item.key,- }))- }}- title={form.gscPropertyUri || t('project.settings.gsc.selectProperty')}- selectedItem={- form.gscPropertyUri- ? { key: form.gscPropertyUri, label: form.gscPropertyUri }- : undefined- }- />- </div>- <Button- type='button'- onClick={async () => {- if (!form.gscPropertyUri) return- try {- await setGSCProperty(id, form.gscPropertyUri)- toast.success(t('project.settings.gsc.propertyConnected'))- } catch (reason: any) {- toast.error(- typeof reason === 'string' ? reason : t('apiNotifications.somethingWentWrong'),- )- }- }}- primary- regular- >- {t('common.save')}- </Button>- </div>- )}- <div>- <Button- type='button'- onClick={async () => {- try {- await disconnectGSC(id)- setGscConnected(false)- setGscProperties([])- setForm((prevForm) => ({- ...prevForm,- gscPropertyUri: null,- }))- toast.success(t('project.settings.gsc.disconnected'))- } catch (reason: any) {- toast.error(- typeof reason === 'string' ? reason : t('apiNotifications.somethingWentWrong'),- )- }- }}- regular- >- {t('common.disconnect')}- </Button>- </div>+ <Input className='lg:w-1/2' label={'Linked Google account'} value={gscEmail || ''} disabled />++ <Button+ type='button'+ className='max-w-max'+ semiDanger+ regular+ onClick={async () => {+ try {+ await disconnectGSC(id)+ setGscConnected(false)+ setGscProperties([])+ setGscEmail(null)+ setForm((prevForm) => ({+ ...prevForm,+ gscPropertyUri: null,+ }))+ toast.success(t('project.settings.gsc.disconnected'))+ } catch (reason: any) {+ toast.error(typeof reason === 'string' ? reason : t('apiNotifications.somethingWentWrong'))+ }+ }}+ >+ {t('common.disconnect')}+ </Button>++ <Select+ fieldLabelClassName='mt-4 max-w-max'+ className='lg:w-1/2'+ hintClassName='lg:w-2/3'+ label={t('project.settings.gsc.websiteProperty')}+ hint={t('project.settings.gsc.websitePropertyHint')}+ items={_map(gscProperties, (p) => ({ key: p.siteUrl, label: p.siteUrl }))}+ keyExtractor={(item) => item.key}+ labelExtractor={(item) => item.label}+ onSelect={(item: { key: string; label: string }) => {+ setForm((prevForm) => ({+ ...prevForm,+ gscPropertyUri: item.key,+ }))+ }}+ title={form.gscPropertyUri || t('project.settings.gsc.selectProperty')}+ selectedItem={+ form.gscPropertyUri ? { key: form.gscPropertyUri, label: form.gscPropertyUri } : undefined+ }+ />++ <Button+ type='button'+ className='max-w-max'+ onClick={async () => {+ if (!form.gscPropertyUri) return+ try {+ await setGSCProperty(id, form.gscPropertyUri)+ toast.success(t('project.settings.gsc.propertyConnected'))+ } catch (reason: any) {+ toast.error(typeof reason === 'string' ? reason : t('apiNotifications.somethingWentWrong'))+ }+ }}+ primary+ regular+ >+ {t('common.save')}+ </Button></div>)}- <hr className='-mx-4 mt-4 mb-4 border-gray-200 dark:border-slate-800' />-- <p className='text-sm text-gray-800 dark:text-gray-200'>- <Trans- t={t}- i18nKey='project.settings.gsc.connectDisclamer'- components={{- url: (- <a- href='https://search.google.com/search-console/about'- target='_blank'- rel='noopener noreferrer'- className='font-medium text-indigo-600 hover:underline dark:text-indigo-400'- />- ),- }}- />- </p>+ {gscConnected ? null : (+ <>+ <hr className='-mx-4 mt-4 mb-4 border-gray-200 dark:border-slate-800' />++ <p className='text-sm text-gray-800 dark:text-gray-200'>+ <Trans+ t={t}+ i18nKey='project.settings.gsc.connectDisclamer'+ components={{+ url: (+ <a+ href='https://search.google.com/search-console/about'+ target='_blank'+ rel='noopener noreferrer'+ className='font-medium text-indigo-600 hover:underline dark:text-indigo-400'+ />+ ),+ }}+ />+ </p>+ </>+ )}</div></div>) : null}diff --git a/web/app/ui/Button.tsx b/web/app/ui/Button.tsxindex 471aaf62a..88c9d298a 100644--- a/web/app/ui/Button.tsx+++ b/web/app/ui/Button.tsx@@ -57,7 +57,7 @@ const Button = ({'border-transparent bg-slate-300 text-slate-900 hover:bg-slate-200': secondary,'border-transparent bg-white text-gray-700 hover:bg-gray-50': white,'border-transparent bg-red-500 text-gray-50 hover:bg-red-600 dark:bg-red-600 dark:hover:bg-red-700': danger,- 'border-1 border-red-600 text-red-500 hover:text-red-600 dark:border-red-500 dark:text-red-300 dark:hover:text-red-400':+ 'border-1 border-red-600 text-red-500 hover:bg-red-500 hover:text-white dark:border-red-500 dark:text-red-200 dark:hover:bg-red-600':semiDanger,'border-none text-gray-700 focus:border-none focus:ring-0 focus:ring-offset-0 dark:text-white': noBorder,'px-2.5 py-1.5 text-xs': small,diff --git a/web/app/ui/Select.tsx b/web/app/ui/Select.tsxindex 84fa2e9a0..af582c9d5 100644--- a/web/app/ui/Select.tsx+++ b/web/app/ui/Select.tsx@@ -1,12 +1,4 @@-import {- Listbox,- Transition,- Description,- Label,- ListboxButton,- ListboxOptions,- ListboxOption,-} from '@headlessui/react'+import { Listbox, Transition, Label, ListboxButton, ListboxOptions, ListboxOption } from '@headlessui/react'import { CheckIcon, ChevronUpDownIcon } from '@heroicons/react/24/solid'import cx from 'clsx'import _map from 'lodash/map'@@ -21,6 +13,7 @@ interface SelectProps<T> {fieldLabelClassName?: stringlabelClassName?: stringbuttonClassName?: string+ hintClassName?: stringcapitalise?: booleanitems: T[]id?: string@@ -48,6 +41,7 @@ function Select<T>({labelClassName,fieldLabelClassName,selectedItem,+ hintClassName,}: SelectProps<T>) {const isItemSelected = (item: T): boolean => {if (!selectedItem) return false@@ -159,9 +153,9 @@ function Select<T>({</Transition></div>{hint ? (- <Description className='mt-2 text-sm whitespace-pre-line text-gray-500 dark:text-gray-300'>+ <p className={cx('mt-2 text-sm whitespace-pre-line text-gray-500 dark:text-gray-300', hintClassName)}>{hint}- </Description>+ </p>) : null}</>)}diff --git a/web/public/locales/en.json b/web/public/locales/en.jsonindex e9063996d..34e6eecc2 100644--- a/web/public/locales/en.json+++ b/web/public/locales/en.json@@ -1134,12 +1134,13 @@"access": "Access settings","gsc": {"connect": "Connect your project to your Google Search Console account to get stats like what keywords people are using to find your website.",- "accountConnected": "Your account is connected. If you do not see your property list, ensure the account has access to your site in Search Console.","connected": "The project has been connected to your Google Search Console account.","disconnected": "The project has been disconnected from your Google Search Console account.","selectProperty": "Select a property","connectDisclamer": "For the integration to work, make sure you have set up your website on the <url>Google Search Console</url> first.",- "propertyConnected": "Selected property saved to your project"+ "propertyConnected": "Selected property saved to your project",+ "websiteProperty": "Website property",+ "websitePropertyHint": "Select the Google Search Console property you want to see the keywords for. If you don't see your domain here, make sure you have added and verified it on the Google Search Console."},"tabs": {"general": "General",