Embed stats in an admin page
Show one site's numbers inside an admin page you already have, for yourself or for a client. You can see it working in the demo inside a mock admin page.
Pick one of two ways
| A. Read-only key | B. Framed dashboard | |
|---|---|---|
| You build the page | Yes, from the numbers | No, Gaugemint's dashboard is shown |
| What you keep secret | A read-only key, on your server | The embed address, which works for anyone who has it |
| Set up in Gaugemint | Create a key | Create a share link, list the allowed origins |
A. Read-only key, called from your server
- Sign in, open Account, then API keys. Name the key, choose Read-only (stats only) and tick the sites it may read. The key is shown once.
- Put it in your server's secret store, never in code that reaches the browser. In Next.js that means no
NEXT_PUBLIC_prefix.GAUGEMINT_URL=https://gaugemint.com GAUGEMINT_READ_KEY=your read-only key GAUGEMINT_SITE=client-shop.example.com - Call the stats API from your server and send the result to your page.
// Server code only: GAUGEMINT_READ_KEY is a read-only key and must never reach the browser. export async function getStats(site, period = '30d') { const url = `${process.env.GAUGEMINT_URL}/api/v1/stats/aggregate` + `?site_id=${encodeURIComponent(site)}&period=${period}&metrics=visitors,pageviews`; const res = await fetch(url, { headers: { Authorization: `Bearer ${process.env.GAUGEMINT_READ_KEY}` }, signal: AbortSignal.timeout(5000), // a slow Gaugemint must not hang your admin page }); if (!res.ok) throw new Error(`stats unavailable (${res.status})`); return (await res.json()).results; }
A read-only key can call the stats endpoints (/api/v1/stats/* and /api/v2/query) for its sites and nothing else. A site it may not read answers exactly like a site that does not exist. Details are in the Stats API page.
B. Framed dashboard (iframe)
- Open the site in Gaugemint and choose Create share link. The embed address is that link in a frame; revoking the link closes the embed too.
- Under Embed in another site, list the origins that may show it, one per line, up to 5, for example
https://admin.client-shop.example.com. Origins are https with no path or wildcard (plainhttp://localhost:3000is accepted while you develop). Save, and copy the embed address (https://gaugemint.com/embed/...). - Put the address in
GAUGEMINT_EMBED_URLon your server and render the frame behind your admin check.
Next.js (App Router)
export default async function EmbeddedAnalytics() {
if (!(await isAdmin())) notFound(); // your own admin check, first
const src = process.env.GAUGEMINT_EMBED_URL; // the embed address from Gaugemint
if (!src) return <p role="alert">GAUGEMINT_EMBED_URL is not set.</p>;
return (
<iframe src={src} title="Gaugemint stats" width="100%" height={720} loading="lazy"
style={{ border: 0, maxWidth: 1100 }} referrerPolicy="no-referrer" />
);
}
Express
app.get('/admin/analytics-embed', requireAdmin, (req, res) => {
const src = process.env.GAUGEMINT_EMBED_URL;
if (!src) return res.status(500).send('GAUGEMINT_EMBED_URL is not set');
res.set('Cache-Control', 'private, no-store').type('html').send(`<!doctype html><meta charset="utf-8"><meta name="robots" content="noindex"><title>Stats</title>
<h1>Site stats</h1><iframe src="${esc(src)}" title="Gaugemint stats" width="100%" height="720" style="border:0;max-width:1100px" loading="lazy" referrerpolicy="no-referrer"></iframe>`);
});
If your own site sends a Content-Security-Policy, allow the frame on the admin pages:
Content-Security-Policy: frame-src 'self' https://gaugemint.com
Two settings, two directions: the allowed origins in Gaugemint say who may frame the dashboard, and your frame-src says what your page may frame. Both must allow it. The framed page shows the dashboard without owner controls, uses no cookies, follows the viewer's light or dark setting (add ?theme=light or ?theme=dark to force one) and is laid out by the frame's width.
Security notes
- Your admin check comes first. A read-only key limits what Gaugemint gives your server; your server decides who sees it. Check the admin login in the page and in every route that returns stats.
- The embed address is a capability. Anyone who has it can open the dashboard directly, with no login. The allowed origins stop other sites from displaying it in a frame; they do not stop someone who has the address. Treat it like a password and put it only on pages behind your login.
- If the numbers must stay private even from a leaked address, use A.
- Rotate. To replace an embed address, revoke the share link and create a new one; the old address stops answering at once. Revoke a key on the Account page to stop it on its next request.
- Fix the site on the server. Do not take the domain from the query string.
- One key or link per client site keeps one client's numbers apart from the others, and lets you revoke one without touching the rest.
If Gaugemint is slow or down
- Gaugemint is in beta and the terms promise no level of availability. Build the admin page so it works without the stats.
- With a key: keep the 5 second timeout from the code above, catch the error and show a short message such as "Stats are not available right now". Cache the last good answer for a minute or so so that a short outage is not visible.
- With a frame: the browser shows an empty or failed frame while the rest of your page loads. Give the frame a fixed height and put a line of text near it, so the page does not look broken.
- Counting is separate from reading: visits to the client's site are recorded by the script on that site, and a failed read in your admin page does not lose them.
- A read-only key has a per-minute request limit. Over it the key answers
429with aRetry-Afterheader, so cache the answer on your server for a minute rather than calling on every page view.