Backend frameworks
Add Loogika to Laravel
A Laravel site wraps its pages in a Blade layout, and that layout file is where the Loogika tag belongs. The site ID comes from .env through config/services.php. It stays out of the template that way, and each environment can have its own value.
Last updated .
What do you need before you start?
Copy your script tag from Site Settings in Loogika, under the Tracking Script tab. The 12-character site ID in the tag is the value you add to .env below.
<script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>
Where do you store the site ID in Laravel?
Add the site ID to the .env file:
LOOGIKA_SITE_ID=YOUR_SITE_ID
Then register the value in config/services.php, next to the other services:
<?php
return [
// ... other services
'loogika' => [
'site_id' => env('LOOGIKA_SITE_ID'),
],
];
If your configuration is cached, clear it with php artisan config:clear. Laravel does not pick up the new key from a cached configuration.
How do you add the tag to the Blade layout?
The tag goes inside the head element of the Blade layout around your pages. The table shows where the layout sits in common setups.
| Setup | Layout file |
|---|---|
| Default install | resources/views/layouts/app.blade.php |
| Inertia | resources/views/app.blade.php |
| Jetstream and Breeze | resources/views/layouts/app.blade.php and resources/views/layouts/guest.blade.php |
The head of the layout then looks like this:
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{ config('app.name', 'Laravel') }}</title>
@vite(['resources/css/app.css', 'resources/js/app.js'])
@production
<script src="https://app.loogika.com/api/script.js?siteId={{ config('services.loogika.site_id') }}" defer></script>
@endproduction
</head>
Because of the @production directive, the tag is printed only when APP_ENV=production, which keeps local development out of your data. To collect data from a staging site as well, take the directive out.
What if the app has several layouts?
Add the tag to each layout, for example both app and guest. Another option is a partial that holds the tag, for example resources/views/partials/loogika.blade.php. Every layout then pulls it in with @include.
How do you check that Loogika is installed?
Load the production site in a fresh browser tab and visit a few pages. The pageviews appear in the Loogika dashboard within a few seconds.
If nothing arrives, open the page source and look for script.js?siteId=. When the tag is on the page, check the Network tab of your browser for requests to /api/track with status 200. Ad blockers and other causes are covered on the script troubleshooting page.
Why is the tag missing from a Laravel page?
| Cause | Fix |
|---|---|
| Production serves cached config or compiled views | Old values survive in cached config and in compiled views. After a change to .env or config/services.php, run php artisan view:clear and php artisan config:clear |
| The tag is missing on your own machine | With any APP_ENV other than production, the @production directive hides the tag. Set that value for a short test, or remove the directive |