NewWe open-sourced 50+ Laravel packages
Custom AI apps, agents and automation — Roundly ConsultingRoundly
All packages
Plausible for Laravel

Model events (Trackable)

Add the Trackable trait to emit Plausible events on Eloquent lifecycle hooks. Map a hook to an event name with $plausibleEvents, and optionally build per-model props:

use Illuminate\Database\Eloquent\Model;
use RoundlyConsulting\Plausible\Concerns\Trackable;

class Order extends Model
{
    use Trackable;

    /** @var array<string, string> */
    protected array $plausibleEvents = [
        'created' => 'Order Placed',
        'updated' => 'Order Updated',
        'deleted' => 'Order Cancelled',
    ];

    /** @return array<string, mixed> */
    protected function plausibleEventProps(string $event): array
    {
        return ['total' => $this->total, 'plan' => $this->plan];
    }
}

Any Eloquent model event works as a key — created, updated, deleted and so on. The map is read from the property’s default value when the model boots.

Customising the payload

Override these protected methods; the first two receive the Plausible event name (for example Order Placed):

  • plausibleEventProps(string $event) — custom props sent with the event (none by default).
  • plausibleEventUrl(string $event) — the URL attributed to the event; defaults to the app URL plus the model’s table and key.
  • plausibleEventSite() — the site the event goes to; defaults to config('plausible.site'), and null skips the event.
// The URL attributed to the event — defaults to {app.url}/{table}/{key}
protected function plausibleEventUrl(string $event): string
{
    return route('orders.show', $this);
}

// The site the event goes to — defaults to config('plausible.site'); null skips the event
protected function plausibleEventSite(): ?string
{
    return $this->tenant->analytics_domain;
}

Firing an event manually

// Same site, visitor and queue rules as the lifecycle hooks — any event name
$order->dispatchPlausibleEvent('Order Refunded');

Visitor, site and queue

The visitor comes from the current request when there is one. When no HTTP request is in scope — queues, console commands — it falls back to plausible.tracking.user_agent and plausible.tracking.ip_address. Events only fire when a site resolves, and captures are queued unless plausible.tracking.queue is false.

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 crypto

By 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.