import React, { useEffect, useState } from 'react'; import { __, sprintf } from '@wordpress/i18n'; import { api } from '../api'; import { Card, Button, Notice, KpiTile, LineChart, ChipBar, Advisor, Badge } from '../components/ui'; /** * Measure, on one screen. * * Search Console and Analytics are two Google products behind one sign-in, * and the plugin showed them on two screens with two period pickers and two * "not connected" states. What a site owner asks is one question: is the * site getting found, and are the people who find it staying. So this puts * the search numbers and the visit numbers side by side for the same days, * each against the same days before, and leaves the detail to the two * screens behind it. */ const PERIODS = [ { id: '7', label: '7' }, { id: '28', label: '28' }, { id: '90', label: '90' }, ]; const num = (n) => Number(n || 0).toLocaleString(); const pct = (n, d) => (d > 0 ? Math.round(((n - d) / d) * 100) : null); export default function Measure() { const [days, setDays] = useState('28'); const [status, setStatus] = useState(null); const [gsc, setGsc] = useState(null); const [ga, setGa] = useState(null); const [bing, setBing] = useState(null); const [error, setError] = useState(null); const [loading, setLoading] = useState(false); useEffect(() => { api.get('/gsc/status').then(setStatus).catch((e) => setError(e.message)); api.get('/bing').then(setBing).catch(() => setBing(null)); }, []); useEffect(() => { if (!status?.connected) return; setLoading(true); Promise.allSettled([api.get(`/gsc/performance?days=${days}`), api.get(`/analytics/report?days=${days}`)]).then(([g, a]) => { setGsc(g.status === 'fulfilled' ? g.value : { error: g.reason?.message }); setGa(a.status === 'fulfilled' ? a.value : { error: a.reason?.message }); setLoading(false); }); }, [status?.connected, days]); const go = (hash) => { window.location.hash = hash; }; if (error) return {error}; if (!status) return {__('Reading the connections…', 'ai-rank-suite')}; if (!status.connected) { return (
go('/search-console')} />
  • {__('Search: clicks, impressions, click-through rate and average position, against the same number of days before.', 'ai-rank-suite')}
  • {__('Visits: users, sessions, page views and engagement from Google Analytics 4, for the same period.', 'ai-rank-suite')}
  • {__('The two lines on one time axis, and the top pages with both numbers next to each other.', 'ai-rank-suite')}
  • {__('Bing beside Google, once its API key is stored.', 'ai-rank-suite')}
); } const t = gsc?.totals || {}; const p = gsc?.previous || {}; const g = ga?.totals || {}; const tone = (delta, invert = false) => (delta === null || delta === undefined ? 'plain' : (invert ? -delta : delta) >= 0 ? 'green' : 'red'); const delta = (now, before) => { const d = pct(Number(now || 0), Number(before || 0)); return d === null ? undefined : d; }; // Top pages from both sources on one table, joined on the path. const gaPages = new Map((ga?.pages || []).map((r) => [String(r.path || r.page || '').replace(/\/$/, ''), r])); const joined = (gsc?.pages || []).slice(0, 10).map((r) => { let path = ''; try { path = new URL(r.page || r.key || r.url || '', window.location.origin).pathname.replace(/\/$/, ''); } catch (e) { path = String(r.page || '').replace(/\/$/, ''); } const a = gaPages.get(path) || gaPages.get(path + '/') || null; return { path: path || '/', clicks: r.clicks, impressions: r.impressions, position: r.position, users: a ? a.users : null, views: a ? a.views ?? a.pageviews : null }; }); return (
go('/search-console')} />
({ id: o.id, label: sprintf(__('%s days', 'ai-rank-suite'), o.label) }))} value={days} onChange={setDays} label={__('Period', 'ai-rank-suite')} /> {loading && {__('Reading…', 'ai-rank-suite')}}
go('/search-console')}>{__('Queries and pages', 'ai-rank-suite')}}> {gsc?.error ? ( {gsc.error} ) : (
)} {gsc?.series?.length > 2 && (
({ date: s.date, value: s.clicks }))} label={__('Clicks', 'ai-rank-suite')} color="var(--ars-brand)" height={120} formatValue={(v) => num(v)} bleed />
)}
go('/analytics')}>{__('Analytics', 'ai-rank-suite')}}> {ga?.error ? ( {ga.error} ) : ga && !ga.totals ? ( {__('Analytics is not set up for this site yet. Pick the GA4 property under Analytics; the same Google sign-in covers it.', 'ai-rank-suite')} ) : (
)} {ga?.series?.length > 2 && (
num(v)} bleed />
)}
{joined.length > 0 && ( {joined.map((r) => ( ))}
{__('Page', 'ai-rank-suite')} {__('Clicks', 'ai-rank-suite')} {__('Impressions', 'ai-rank-suite')} {__('Position', 'ai-rank-suite')} {__('Users', 'ai-rank-suite')}
{r.path} {num(r.clicks)} {num(r.impressions)} {Number(r.position || 0).toFixed(1)} {r.users === null ? '–' : num(r.users)}
)} go('/search-console/bing')}>{__('Bing', 'ai-rank-suite')}} />
); }