Analytics
docs

Lighthouse Schema

The plugin registers an analytics.lighthouseReport document type in your Sanity schema to persist audit results.

Auto-registered. When you use lighthousePlugin() or sanityAnalyticsPlugin(), the schema type is automatically added to your studio — you don't need to add it manually.

Schema Definition

Schema exports
// Exported from 'sanity-plugin-analytics'
import { analyticsSchemas, lighthouseReport } from 'sanity-plugin-analytics'

// analyticsSchemas = [lighthouseReport]
// Already registered by the plugin — only import if you need manual control
analytics.lighthouseReport schema
// analytics.lighthouseReport
{
  name: 'analytics.lighthouseReport',
  title: 'Lighthouse Report',
  type: 'document',
  fields: [
    { name: 'documentId',    type: 'string'   },  // Sanity document ID
    { name: 'documentType',  type: 'string'   },  // e.g. 'post'
    { name: 'title',         type: 'string'   },  // display name
    { name: 'url',           type: 'url'      },  // audited URL
    { name: 'strategy',      type: 'string'   },  // 'mobile' | 'desktop'
    { name: 'lastGenerated', type: 'datetime' },  // ISO timestamp
    { name: 'performance',   type: 'number'   },  // 0.0 – 1.0
    { name: 'accessibility', type: 'number'   },  // 0.0 – 1.0
    { name: 'bestPractices', type: 'number'   },  // 0.0 – 1.0
    { name: 'seo',           type: 'number'   },  // 0.0 – 1.0
    { name: 'report',        type: 'text'     },  // raw JSON from PageSpeed API
  ],
}

Fields Reference

FieldTypeDescription
documentIdstringID of the source Sanity document (drafts. prefix stripped).
documentTypestringSanity _type of the source document.
titlestringDisplay title resolved from the document's titleField.
urlurlThe public URL that was audited.
strategystring'mobile' or 'desktop'.
lastGenerateddatetimeWhen this report was last generated.
performancenumberPerformance score 0.0–1.0 (multiply × 100 for display).
accessibilitynumberAccessibility score 0.0–1.0.
bestPracticesnumberBest Practices score 0.0–1.0.
seonumberSEO score 0.0–1.0.
reporttextRaw JSON response from the PageSpeed Insights API.

GROQ Queries

You can query Lighthouse reports in GROQ just like any other document:

GROQ examples
// All reports, newest first
*[_type == "analytics.lighthouseReport"] | order(lastGenerated desc) {
  _id, url, title, strategy, lastGenerated,
  performance, accessibility, bestPractices, seo
}

// Only mobile reports with performance < 0.5 (poor)
*[_type == "analytics.lighthouseReport" && strategy == "mobile" && performance < 0.5] {
  url, title, performance, lastGenerated
}

// Latest report per URL (mobile)
*[_type == "analytics.lighthouseReport" && strategy == "mobile"] | order(lastGenerated desc) {
  url, title, performance, accessibility, bestPractices, seo
}

TypeScript Types

TypeScript types
import type {
  LighthouseCandidate,
  LighthouseConfig,
  LighthouseDocumentTypeConfig,
  LighthouseReportSummary,
  LighthouseResolveContext,
  LighthouseStrategy,
} from 'sanity-plugin-analytics'

// LighthouseReportSummary matches the stored document shape
const report: LighthouseReportSummary = {
  _id: 'analytics-lighthouse-mobile-abc123',
  url: 'https://mysite.com/blog/my-post',
  title: 'My Post',
  documentId: 'abc123',
  documentType: 'post',
  strategy: 'mobile',
  lastGenerated: '2025-06-01T12:00:00Z',
  performance: 0.87,
  accessibility: 0.95,
  bestPractices: 0.92,
  seo: 1.0,
}

Manual schema registration. If you need to add the schema without using the plugin (e.g. to use it in another tool), import and register it directly:

Manual schema registration
import { defineConfig } from 'sanity'
import { analyticsSchemas } from 'sanity-plugin-analytics'

export default defineConfig({
  schema: {
    types: [...analyticsSchemas, /* your types */],
  },
})