Analytics
docs

Document Views

Show per-document analytics right beside the editor. The plugin ships two document-view factories you can add to any document type — one for Lighthouse, one for Search Console + Lighthouse together.

Both are added through the Structure tool's defaultDocumentNode. They resolve a document's public URL from the same siteUrl + documentTypes mapping used by the Lighthouse config.

createSeoView — SEO & Performance panel

A compact panel for a single document: its Search Console clicks, impressions, CTR, and average position (colour-coded), plus the stored Lighthouse scores for that URL. Open a post, switch to the SEO tab, and read how that page performs without leaving the editor.

sanity.config.ts
import { defineConfig } from 'sanity'
import { structureTool } from 'sanity/structure'
import { createSeoView } from 'sanity-plugin-analytics'

const seoView = createSeoView({
  siteUrl: 'https://mysite.com',
  searchConsoleApiUrl: '/api/search-console', // default
  range: '28',                                // 7 | 28 | 90 | 180 (days)
  documentTypes: [
    { type: 'post', slugField: 'slug', pathPrefix: 'blog' },
  ],
})

export default defineConfig({
  plugins: [
    structureTool({
      defaultDocumentNode: (S, { schemaType }) => {
        if (schemaType === 'post') {
          return S.document().views([
            S.view.form(),
            S.view.component(seoView).title('SEO'),
          ])
        }
        return S.document().views([S.view.form()])
      },
    }),
  ],
})
OptionTypeDefaultDescription
siteUrlstringPublic site origin used to build each document's URL.
documentTypesLighthouseDocumentTypeConfig[]Maps a document type to its URL (slugField, pathPrefix).
resolveUrl(doc, ctx) => stringOptional custom URL resolver, overrides documentTypes.
searchConsoleApiUrlstring'/api/search-console'Search Console proxy route the panel reads from.
range'7' | '28' | '90' | '180''28'Search Console window (days) to pull per-page stats for.

The panel reads Lighthouse scores from the analytics.lighthouseReport documents produced by the Lighthouse view below — run Lighthouse once and the scores appear in both places.

createLighthouseView — full Lighthouse report

A complete PageSpeed Insights report inside the editor: category gauges, real-user (CrUX) field data, lab metrics, a loading filmstrip, a clickable screenshot, and expandable audit tables. Run mobile or desktop; results are stored as analytics.lighthouseReport documents so they persist and feed the SEO view.

sanity.config.ts
import { createLighthouseView } from 'sanity-plugin-analytics'

const lighthouseView = createLighthouseView({
  apiUrl: '/api/pagespeed', // default
  siteUrl: 'https://mysite.com',
  documentTypes: [
    { type: 'post', slugField: 'slug', pathPrefix: 'blog' },
  ],
})

// inside defaultDocumentNode:
S.document().views([
  S.view.form(),
  S.view.component(seoView).title('SEO'),
  S.view.component(lighthouseView).title('Lighthouse'),
])

The Lighthouse view stores scores in the analytics.lighthouseReport schema, which the plugin registers automatically. See the Lighthouse Schema for the stored fields.