Integrations

MCP

Metrix integrates with Craft’s Model Context Protocol (MCP) server so an AI client can read your analytics. For example, you can ask a connected client how traffic did last week, which pages were most popular in your news section this month, or what a widget on your marketing dashboard is showing.

This integration requires Craft 5.12 or later. Metrix still works on earlier supported Craft versions, but its MCP tools aren’t available there.

Connecting a Client

Follow Craft’s MCP setup instructions (opens new window) to connect a client through the authenticated HTTP server or the local mcp/serve command. Metrix registers its tools automatically, so there’s no separate Metrix server to install or enable. These tools aren’t exposed through Craft’s public MCP server.

Every Metrix tool only reads data, so a connection authorised for read-only access can use all of them. They never change Sources, Views or widgets, and their results never include a Source’s credentials or provider settings.

Permissions

The client acts as the signed-in user and follows Metrix’s permissions. Someone with Dashboard can list Sources and Views, and read the widgets on each View they hold View “{view}” widgets for. Querying a Source directly also needs Manage views and dashboard layouts, which is what the dashboard requires before someone can build a widget of their own. Admins can use every tool.

Each query runs through a View, and the View’s analytics scope filters the data just as it does for that View’s widgets. If a client doesn’t name a View, Metrix uses the first View the user can access. A user who can only see a View scoped to /fr therefore only ever gets French paths, whichever metric or dimension they ask for.

Metrix Tools

ToolPurpose
metrix_sources_listLists the enabled Sources with their provider, connection status, capabilities, and the common metrics and dimensions each supports. It also lists the periods the other tools accept, the default period, and whether the user can query Sources.
metrix_sources_schemaLists every metric and dimension a Source accepts, including the provider’s own. Needs Manage views and dashboard layouts.
metrix_views_listLists the Views the user can access, with each View’s analytics scope and widgets.
metrix_widgets_dataReturns a widget’s data as the dashboard shows it. Pass a period to change the date range of a widget that uses the dashboard date range, as the dashboard header does. Widgets with their own period keep it.
metrix_queryQueries a metric from a Source over a period, as a total, a series over time or a breakdown by dimension. Needs Manage views and dashboard layouts.

Clients usually start with metrix_sources_list, then ask metrix_query or metrix_widgets_data for the numbers.

Querying a Source

metrix_query takes a Source handle, a metric and a period. Metrics and dimensions can be one of Metrix’s common values, which mean the same thing across providers, or a provider’s own value from metrix_sources_schema. The common metrics are visitors, pageviews, sessions, bounce_rate, avg_duration and events. The common dimensions are page, entry_page, source, referrer, country, region, device, browser and os. Not every provider supports every one, and metrix_sources_list shows which ones each Source does.

Periods use a short handle, such as today, lastWeek, last30Days or allTime, and only the periods enabled for the dashboard are available. A custom period’s handle is its class name with a lowercase first letter, and the full class name works too. Without a period, queries use Last 30 Days, the dashboard’s default.

For example, if your Plausible Source’s handle is plausible, these arguments ask how many people visited last week:

{
  "source": "plausible",
  "metric": "visitors",
  "period": "lastWeek"
}

The result describes the Source, View, scope, metric and the exact dates of the period and the period it’s compared with, followed by the data. A total comes back as one row holding the value and its percentage change from the previous period, so "rows": [[1240, 12.5]] means 1,240 visitors, up 12.5% on the week before. Each column has a format, such as percentage, duration or percentageChange, so the client knows how to read the numbers.

Add "series": true to get a value for each hour, day or month of the period instead, depending on its length, with the previous period’s values alongside in comparisonRows. Add a dimension, such as "dimension": "page", for the top values of that dimension sorted by the metric. Dimension results return 10 rows unless you pass a limit up to 500, and they have no comparison.

Each kind of result has the same shape as one of Metrix’s widgets. A total matches a Counter, a series matches a Line chart and a dimension breakdown matches a Table. If enabledWidgetTypes turns one of those widget types off, the matching kind of query isn’t available either.

Caching and Refreshing

Results use the same cache as the dashboard, following the enableCache and cacheDuration settings. A query with the same Source, View, metric, dimension and period as a widget of the matching type shares that widget’s cached data, so a client asking about your dashboard doesn’t make extra requests to the provider. Each result includes fromCache and fetchedAt, which say whether the data came from the cache and when Metrix last fetched it.

Pass "refresh": true to metrix_query or metrix_widgets_data to fetch fresh data from the provider. Like the dashboard’s Refresh action, each user can refresh the same widget or query once every five seconds. A widget refreshed from the dashboard and from a client shares that limit.

When a provider returns an error, the tool returns the same message the dashboard shows for that widget. If a Source’s OAuth connection has expired, Metrix disconnects it and the message says to reconnect it in Metrix → Sources, which needs Manage source credentials and connections.