Skip to content
New Bring your history: import it from Google Analytics, Plausible, Simple Analytics, Umami, Matomo or GoatCounter, on every plan

Go

The mettr-go module sends a Go app's events, crawler visits, 404s, panics and response times, with middleware for net/http, chi, Gin and Echo and the standard library only.

Go 1.23 or later, the standard library only. The source is on GitHub, MIT licensed.

Install

go get github.com/mettrhq/mettr-go

mettr.New reads the environment; Options win.

Variable What
METTR_SECRET_KEY the site's secret key (the site's settings, Tracking); keep it on the server
METTR_PUBLIC_KEY the site's public key, for the script tag and the proxy
METTR_ENABLED true or false; by default on once a key is set
METTR_PROXY_PATH the path on your own domain, _live by default; empty loads the script from mettr
METTR_ENDPOINT where mettr runs, https://mettr.app

net/http, chi and gorilla/mux

client := mettr.New(mettr.Options{})
defer client.Close()

mux := http.NewServeMux()
mux.HandleFunc("GET /posts/{id}", showPost)

http.ListenAndServe(":8080", client.Middleware(mux))

The route comes from ServeMux's pattern. With chi, pass Route: func(r *http.Request) string { return chi.RouteContext(r.Context()).RoutePattern() } and use the middleware inside the router (r.Use(client.Middleware)). Behind a proxy of your own, set TrustForwarded: true.

Gin

import "github.com/mettrhq/mettr-go/mettrgin"

router.Use(gin.Recovery(), mettrgin.Middleware(client))

Echo

import "github.com/mettrhq/mettr-go/mettrecho"

e.Use(mettrecho.Middleware(client))

The script in your pages

template.HTML(client.ScriptTag())

The tag loads the browser script from /_live/script.js and sends its events to /_live/events, which the middleware serves: ad blockers mostly leave first-party requests alone, and the events carry the visitor's address and user as your server saw them. Only what a browser may send passes.

What it sends on its own

  • Crawler visits: a crawler's request, with its page and status. mettr names the crawler and checks it against its owner's network.
  • 404s of people, as errors.
  • Panics, by their type only, never the message; then the panic goes on.
  • Response times of one request in twenty, by route.
  • Nothing for /up (Ignore), for staff, or while it is off.

Sending never panics into your app: a failed batch answers false and is passed to OnError, without its events.

Your own events

func signup(w http.ResponseWriter, r *http.Request) {
    mettr.SetUser(r.Context(), user.ID) // the request's events carry it
    client.Event(r.Context(), "signup", mettr.Props{"plan": "pro"})
    client.Revenue(r.Context(), "order", 49.5, "EUR", nil)
}

Props are flat: up to 30, each a string, number, boolean or nil. Events wait a second to go together; a program that ends calls client.Close(). client.Exception(ctx, err) sends an error you handled, by its type.

Subscriptions

When your billing is your own (anything but Stripe, Paddle or Polar, which mettr reads itself), send each subscription whenever it changes, and every one once for the history:

client.Subscription(mettr.Subscription{
    ID: "sub_1", Status: "active", Amount: 19, Currency: "USD",
    Interval: "month", UserID: "42", Plan: "Pro",
})

See the Subscriptions API for what each field means.

Your MCP server

If your app runs an MCP server, wrap its handler outside its auth, so a 401 that sends a client to sign in counts too:

server := mcp.NewStreamableHTTPHandler(getServer, nil) // any MCP server's http.Handler
mux.Handle("/mcp", client.Mcp(requireAuth(server), mettr.McpOptions{
    User: func(r *http.Request) string { return userID(r) }, // or mettr.SetUser inside
}))

// Gin and Echo: mount it the same way
router.Any("/mcp", gin.WrapH(client.Mcp(server, mettr.McpOptions{})))

Each handshake, tool call, resource read and prompt becomes an mcp.* event with the client, how it ended and how long it took, a streamed answer read to its end; never the arguments or the answer, and a failed call's message only with Errors: true. The middleware and the Gin and Echo adapters send no crawl, 404 or timing for its requests. More in MCP analytics.

History and deploys

client.Import(rows) // each with TS and SourceID: a second run adds nothing
client.Annotation("Deployed v2.4", "https://github.com/acme/app/releases/v2.4", time.Now())

Rows are those of the Import API; notes are those of the Annotations API.

Updated · This page as Markdown · Something unclear? [email protected]

Search the docs, features, comparisons, blog and changelog