Skip to content

Add trackr.link analytics to Laravel

Add the trackr.link snippet to your Blade layout, count Inertia and Livewire navigation, and send custom events from the browser or after a redirect.

A Laravel app needs the snippet once, in the <head> of the Blade layout its pages share. Inertia and Livewire navigation is counted on its own, so you don't need a route-change listener.

1. Copy your snippet

Open your site in trackr.link and copy its snippet. It looks like this, with your own site id:

<script defer data-site="YOUR_SITE_ID" src="https://pulse.trackr.link/t.js"></script>

2. Add it to your layout

Put the snippet in the <head> of the layout every page extends, such as resources/views/layouts/app.blade.php or resources/views/components/layouts/app.blade.php. With Inertia, that is the root view, resources/views/app.blade.php:

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script defer data-site="YOUR_SITE_ID" src="https://pulse.trackr.link/t.js"></script>
    @vite(['resources/css/app.css', 'resources/js/app.js'])
</head>

Many apps have more than one layout, for example one for guests and one for signed-in users. Add the snippet to each layout once, and check that no page renders two of them, because that page would count every view twice.

To keep the site id out of your templates, read it from config. Add it to config/services.php:

'trackr' => [
    'site_id' => env('TRACKR_SITE_ID'),
],

Then set TRACKR_SITE_ID in .env, and render the snippet only when the id is set:

@if (config('services.trackr.site_id'))
    <script defer data-site="{{ config('services.trackr.site_id') }}" src="https://pulse.trackr.link/t.js"></script>
@endif

3. Navigation with Inertia and Livewire

Inertia links and router.visit() change the URL with history.pushState, and the tracker records each of those as a pageview, as it does the back and forward buttons. A partial reload that stays on the same path isn't counted.

Livewire's wire:navigate also changes the URL with history.pushState, and those visits are counted too. Livewire runs a script in the <head> once, on the first visit, but it runs every script in the <body> again after each navigation. Keep the snippet in the <head>. If it has to be in the <body>, add data-navigate-once to it. A copy that runs again counts later pageviews more than once.

The tracker records the path only. /pricing?plan=pro and /pricing#faq count as /pricing, and UTM tags in the query are kept as campaign data.

4. Send custom events

Events are sent from the browser, where the tracker runs. Laravel runs on the server, so it can't call the tracker directly.

For a button or link, add data-trackr-event="cta_click" in Blade, plus props such as data-trackr-prop-location="hero". The tracker sends the event on click:

<a href="{{ route('pricing') }}" data-trackr-event="cta_click" data-trackr-prop-location="hero">See pricing</a>

With Inertia, call the global trackr function when a form submit succeeds:

import { useForm } from '@inertiajs/react';

const form = useForm({ email: '' });

form.post('/signup', {
    onSuccess: () => window.trackr?.('event', 'signup', { plan: 'free' }),
});

With TypeScript, declare the function once, for example in resources/js/types/global.d.ts:

declare global {
    interface Window {
        trackr?: (
            command: 'event',
            name: string,
            props?: Record<string, string | number | boolean>,
        ) => void;
    }
}

export {};

For a classic Blade form that posts and redirects, flash the event name from the controller:

return redirect()->route('dashboard')->with('trackr_event', 'signup');

Then render it in your layout, after the snippet. The stub keeps the call until the tracker loads, and the tracker sends it on load:

@if (session('trackr_event'))
    <script>
        window.trackr = window.trackr || function () {
            (window.trackr.q = window.trackr.q || []).push(arguments);
        };
        window.trackr('event', @js(session('trackr_event')));
    </script>
@endif

Inertia visits after the first don't render the Blade layout again, so with Inertia send events from onSuccess instead.

5. Local development

trackr.link records hits from your site's domain, its subdomains and localhost. Visits to http://localhost:8000 are counted. Visits to a Herd or Valet domain such as my-app.test, or to http://127.0.0.1:8000, are not, because they aren't on your domain. To keep development out of your numbers, render the snippet only in production:

@production
    <script defer data-site="YOUR_SITE_ID" src="https://pulse.trackr.link/t.js"></script>
@endproduction

Or leave TRACKR_SITE_ID empty in your local .env if you used the config check from step 2.

6. Content Security Policy

If your app sends a Content-Security-Policy header, from middleware or a package such as spatie/laravel-csp, allow the tracker's origin in both script-src and connect-src. The script loads from it and sends its hits back to it. If you use a nonce with Vite::useCspNonce(), add it to the snippet too:

<script defer nonce="{{ Vite::cspNonce() }}" data-site="YOUR_SITE_ID" src="https://pulse.trackr.link/t.js"></script>

7. Check that it works

Deploy, then open your site in an ordinary browser window and click through a few pages. Your dashboard opens when the first pageview arrives. Hits from automated browsers, such as Dusk, Playwright or Puppeteer, are dropped as bots, so test by hand. If nothing arrives, view the page source and search for data-site. A missing tag usually means a page uses a layout without the snippet, or that TRACKR_SITE_ID isn't set on the server. After you change .env in production, run php artisan config:cache again.

If you use Claude Code or Codex, copy the setup prompt from your site's page in trackr.link instead. The agent adds the snippet and your events, and saves your goals and funnels.

8. Read your first visits

Every visit counts in a channel, such as Organic Search, Email or AI. Channels and UTM parameters lists the rules that decide which one, and AI traffic shows how visits from ChatGPT, Claude, Perplexity and other assistants are recognized. To tag the links you share in campaigns, use the UTM builder.

Free for one site. No credit card.

Create free account