📈 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
Add annotations to SEO View
Blue Mouse0d3cff9c7d0d
diff --git a/web/app/pages/Project/tabs/SEO/SEOView.tsx b/web/app/pages/Project/tabs/SEO/SEOView.tsxindex bdbe338f8..0adf94922 100644--- a/web/app/pages/Project/tabs/SEO/SEOView.tsx+++ b/web/app/pages/Project/tabs/SEO/SEOView.tsx@@ -11,6 +11,7 @@ import {ChartBarIcon,TrendUpIcon,} from '@phosphor-icons/react'+import dayjs from 'dayjs'import _isEmpty from 'lodash/isEmpty'import _round from 'lodash/round'import React, {@@ -26,10 +27,13 @@ import { Link } from '~/ui/Link'import { useLoaderData, useSearchParams } from 'react-router'import { useGSCDashboardProxy } from '~/hooks/useAnalyticsProxy'+import { useAnnotations } from '~/hooks/useAnnotations'import { DOCS_URL } from '~/lib/constants'import type { TimeBucket } from '~/lib/constants'import type { Entry } from '~/lib/models/Entry'import BillboardChart from '~/ui/BillboardChart'+import AnnotationModal from '~/modals/AnnotationModal'+import { ChartContextMenu } from '~/pages/Project/View/components/ChartContextMenu'import DashboardHeader from '~/pages/Project/View/components/DashboardHeader'import { MainChart } from '~/pages/Project/View/components/MainChart'import Filters from '~/pages/Project/View/components/Filters'@@ -95,7 +99,7 @@ const SEOViewInner = ({ projectId, tnMapping }: SEOViewProps) => {i18n: { language },} = useTranslation('common')const { theme } = useTheme()- const { id } = useCurrentProject()+ const { id, allowedToManage } = useCurrentProject()const {period,timezone,@@ -116,6 +120,21 @@ const SEOViewInner = ({ projectId, tnMapping }: SEOViewProps) => {resetData: resetCompareData,} = useGSCDashboardProxy()const [searchParams, setSearchParams] = useSearchParams()+ const {+ annotations,+ isAnnotationModalOpen,+ annotationToEdit,+ annotationModalDate,+ annotationActionLoading,+ contextMenu,+ onAnnotationCreate,+ onAnnotationUpdate,+ onAnnotationDelete,+ openAnnotationModal,+ closeAnnotationModal,+ handleChartContextMenu,+ closeContextMenu,+ } = useAnnotations()const [activeMetrics, setActiveMetrics] = useState<Record<SEOMetricKey, boolean>@@ -348,6 +367,28 @@ const SEOViewInner = ({ projectId, tnMapping }: SEOViewProps) => {[compareData?.dateSeries, seoTimeBucket],)+ const chartXAxisData = useMemo(+ () => aggregatedSeries.map((entry) => entry.date),+ [aggregatedSeries],+ )++ const filteredAnnotations = useMemo(() => {+ if (!annotations?.length || !chartXAxisData.length) return annotations || []++ const rangeStart = dayjs(chartXAxisData[0]).startOf('day')+ const rangeEnd = dayjs(chartXAxisData[chartXAxisData.length - 1]).endOf(+ 'day',+ )++ return annotations.filter((annotation) => {+ const date = dayjs(annotation.date)+ return (+ (date.isAfter(rangeStart) || date.isSame(rangeStart, 'day')) &&+ (date.isBefore(rangeEnd) || date.isSame(rangeEnd, 'day'))+ )+ })+ }, [annotations, chartXAxisData])+const topPagesAsEntries: Entry[] = useMemo(() =>(data?.topPages || []).map(@@ -516,6 +557,7 @@ const SEOViewInner = ({ projectId, tnMapping }: SEOViewProps) => {seoTimeBucket,t,!noRegionPeriods.includes(period),+ filteredAnnotations,isActiveCompare && compareEnabled && aggregatedCompareSeries.length? aggregatedCompareSeries: undefined,@@ -529,6 +571,7 @@ const SEOViewInner = ({ projectId, tnMapping }: SEOViewProps) => {compareEnabled,aggregatedCompareSeries,period,+ filteredAnnotations,],)@@ -894,19 +937,27 @@ const SEOViewInner = ({ projectId, tnMapping }: SEOViewProps) => {/></div>{anyMetricActive && !_isEmpty(aggregatedSeries) ? (- <MainChart- chartId='seo-main-chart'- options={chartOptions}- className='h-80 [&_svg]:overflow-visible!'- deps={[- aggregatedSeries,- aggregatedCompareSeries,- activeMetrics,- seoTimeBucket,- timeFormat,- period,- ]}- />+ <div+ onContextMenu={(event) =>+ handleChartContextMenu(event, chartXAxisData)+ }+ className='relative'+ >+ <MainChart+ chartId='seo-main-chart'+ options={chartOptions}+ className='h-80 [&_svg]:overflow-visible!'+ deps={[+ aggregatedSeries,+ aggregatedCompareSeries,+ activeMetrics,+ seoTimeBucket,+ timeFormat,+ period,+ filteredAnnotations,+ ]}+ />+ </div>) : null}</div>@@ -1084,6 +1135,44 @@ const SEOViewInner = ({ projectId, tnMapping }: SEOViewProps) => {dataLoading={gscLoading}/></div>++ <AnnotationModal+ isOpened={isAnnotationModalOpen}+ onClose={closeAnnotationModal}+ onSubmit={annotationToEdit ? onAnnotationUpdate : onAnnotationCreate}+ onDelete={annotationToEdit ? onAnnotationDelete : undefined}+ loading={annotationActionLoading}+ annotation={annotationToEdit}+ defaultDate={annotationModalDate}+ allowedToManage={allowedToManage}+ />+ <ChartContextMenu+ isOpen={contextMenu.isOpen}+ x={contextMenu.x}+ y={contextMenu.y}+ onClose={closeContextMenu}+ onAddAnnotation={() => {+ openAnnotationModal(contextMenu.date || undefined)+ }}+ onEditAnnotation={+ contextMenu.annotation+ ? () =>+ openAnnotationModal(+ contextMenu.annotation?.date,+ contextMenu.annotation!,+ )+ : undefined+ }+ onDeleteAnnotation={+ contextMenu.annotation+ ? () => {+ onAnnotationDelete(contextMenu.annotation!)+ }+ : undefined+ }+ existingAnnotation={contextMenu.annotation}+ allowedToManage={allowedToManage}+ /></>)}diff --git a/web/app/pages/Project/tabs/SEO/seo-chart-options.ts b/web/app/pages/Project/tabs/SEO/seo-chart-options.tsindex 06e92d8fc..c4db13df1 100644--- a/web/app/pages/Project/tabs/SEO/seo-chart-options.ts+++ b/web/app/pages/Project/tabs/SEO/seo-chart-options.ts@@ -1,10 +1,11 @@-import type { ChartOptions } from 'billboard.js'+import type { ChartOptions, GridLineOptions } from 'billboard.js'import { area, bar, donut, line, scatter } from 'billboard.js'import * as d3 from 'd3'import dayjs from 'dayjs'import _round from 'lodash/round'import type { TFunction } from 'i18next'+import type { Annotation } from '~/lib/models/Project'import { escapeHtml, nFormatter } from '~/utils/generic'import {SEO_IMPRESSION_POSITION_BUCKETS,@@ -30,6 +31,8 @@ const SEO_MAIN_METRIC_COMPARE_COLORS: Record<SEOMetricKey, string> = {ctr: 'rgba(13, 148, 136, 0.4)',}+const ANNOTATION_CHART_TEXT_MAX_LENGTH = 25+const formatSEOMetricValue = (metric: SEOMetricKey, value: number) => {if (metric === SEO_METRICS.ctr) {return `${Number(value).toFixed(1)}%`@@ -46,12 +49,27 @@ const getSEOMetricValue = (entry: DateSeriesEntry, metric: SEOMetricKey) => {return entry[metric] ?? 0}+const getAnnotationLines = (+ annotations?: Annotation[],+): GridLineOptions[] => {+ return (annotations || []).map<GridLineOptions>((annotation) => ({+ value: dayjs(annotation.date).toDate(),+ text:+ annotation.text.length > ANNOTATION_CHART_TEXT_MAX_LENGTH+ ? `${annotation.text.substring(0, ANNOTATION_CHART_TEXT_MAX_LENGTH)}...`+ : annotation.text,+ class: `annotation-line annotation-id-${annotation.id}`,+ position: 'start',+ }))+}+export function buildMainChartOptions(series: DateSeriesEntry[],activeMetrics: Record<SEOMetricKey, boolean>,timeBucket: string,t: TFunction,applyRegions: boolean,+ annotations?: Annotation[],compareSeries?: DateSeriesEntry[],): ChartOptions {if (!series.length) return {}@@ -211,6 +229,9 @@ export function buildMainChartOptions(r: 2,},grid: {+ x: {+ lines: getAnnotationLines(annotations),+ },y: {show: true,},