Sites, goals & shared links
Provision analytics from code — handy for multi-tenant products that give each customer their own Plausible site. All three APIs share the sites rate-limit bucket.
Sites
The Sites API is an Enterprise plan feature and needs a Sites API key — a Stats API key is refused there (see API keys under Configuration).
use RoundlyConsulting\Plausible\Facades\Plausible;
use RoundlyConsulting\Plausible\Requests\Sites\CreateSiteRequest;
use RoundlyConsulting\Plausible\Requests\Sites\UpdateSiteRequest;
$site = Plausible::sites()->create(CreateSiteRequest::make(
domain: 'my.domain.tld',
timezone: 'Europe/Bratislava', // default 'Etc/UTC'
)); // -> SiteResult
$site = Plausible::sites()->get('my.domain.tld'); // -> SiteResult
$site->domain; // 'my.domain.tld'
$site->timezone; // 'Europe/Bratislava'
$site->data; // the full payload
$sites = Plausible::sites()->list(); // -> list<SiteResult>
Plausible::sites()->update(UpdateSiteRequest::make(
site: 'my.domain.tld', // the current domain
domain: 'new.domain.tld', // optional: new domain
timezone: 'Europe/Prague', // optional: new timezone
)); // -> SiteResult
$deleted = Plausible::sites()->delete('my.domain.tld'); // -> boolcreate(), get(), list() and update() return typed SiteResult objects; delete() returns true unless Plausible answers deleted: false, and a failed request throws. list() follows Plausible’s cursor pagination (meta.after, 100 per page) and returns every site. Site and goal ids are URL-encoded into the request path, so a value containing / or ? can’t reach another endpoint.
Goals
Goals are part of the Sites API — an Enterprise plan feature that needs a Sites API key. CreateGoalRequest::event() creates a custom-event goal, ::page() a pageview goal, and list() walks every page:
use RoundlyConsulting\Plausible\Requests\Sites\CreateGoalRequest;
$goal = Plausible::goals()->create(CreateGoalRequest::event('my-site.com', 'Signup')); // -> GoalResult
Plausible::goals()->create(CreateGoalRequest::page('my-site.com', '/checkout'));
$goal->id; // 42
$goal->goalType; // 'event' or 'page'
$goal->eventName; // 'Signup'
$goal->pagePath; // null for event goals
$goals = Plausible::goals()->list('my-site.com'); // -> list<GoalResult>, every page
Plausible::goals()->delete('my-site.com', $goal->id); // -> boolShared links
Shared links are part of the Sites API too. create() is find-or-create — Plausible returns the existing link when one with that name exists — and the endpoint takes only a site and a name, so there is no password option:
use RoundlyConsulting\Plausible\Requests\Sites\CreateSharedLinkRequest;
// Find-or-create: Plausible returns the existing link when one with that name exists
$link = Plausible::sharedLinks()->create('my-site.com', 'Public dashboard'); // -> SharedLinkResult
$link->name; // 'Public dashboard'
$link->url; // shareable dashboard URL
// Or pass an explicit request object
Plausible::sharedLinks()->createFrom(CreateSharedLinkRequest::make('my-site.com', 'Board'));Tenant lifecycle
Put together, analytics can be ready on a tenant’s first day and gone when they leave:
// Provision analytics when a tenant signs up …
Plausible::sites()->create(CreateSiteRequest::make(domain: $tenant->domain, timezone: $tenant->timezone));
Plausible::goals()->create(CreateGoalRequest::event($tenant->domain, 'Signup'));
$dashboard = Plausible::sharedLinks()->create($tenant->domain, 'Customer dashboard')->url;
// … and tear it down when they leave
Plausible::sites()->delete($tenant->domain);Show your open-source love
This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.
More ways to support, including cryptoBy donating, you agree to our donation terms.
Want this built into your product?
We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.