Skip to Content
🚀 We just launched! Please star us on Github!

Laravel

Integrating Rybbit Analytics with your Laravel application is straightforward. You’ll typically add the Rybbit tracking script to your main Blade layout file.

1. Get Your Tracking Script

First, you’ll need your Rybbit tracking script. You can find this in your Rybbit dashboard under Site Settings > Tracking Code. It will look something like this:

<script async defer src="https://app.rybbit.io/api/script.js" data-site-id="YOUR_SITE_ID"></script>

Replace YOUR_SITE_ID with your actual Site ID from your Rybbit dashboard.

2. Locate Your Main Blade Layout File

In a standard Laravel application, you’ll have a main layout file that other Blade views extend. This file is often located at:

  • resources/views/layouts/app.blade.php (common for applications using Laravel’s authentication scaffolding)
  • Or resources/views/layouts/main.blade.php or a similar custom name.
  • If you’re using Laravel Jetstream or Breeze, the main layout file might be in a slightly different location within resources/views/.

Identify the primary layout file that wraps most or all of your site’s pages.

3. Add the Tracking Script to the Layout

Open your main Blade layout file. Paste the Rybbit tracking script just before the closing </body> tag.

<!DOCTYPE html> <html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>{{ config('app.name', 'Laravel') }}</title> {{-- Stylesheets, etc. --}} @vite(['resources/css/app.css', 'resources/js/app.js']) {{-- Example for Vite --}} </head> <body class="font-sans antialiased"> {{-- Your page content, often using @yield or <slot> --}} @yield('content') {{-- Other scripts --}} {{-- Rybbit Analytics Script --}} @if(app()->environment('production')) <script async defer src="{{ config('services.rybbit.instance_url', 'https://app.rybbit.io') }}/api/script.js" data-site-id="{{ config('services.rybbit.site_id') }}"></script> @endif </body> </html>

Explanation:

  • @if(app()->environment('production')): This Blade directive ensures the script is only included when your Laravel application is running in the production environment. This prevents tracking during local development.
  • {{ config('services.rybbit.instance_url', 'https://app.rybbit.io') }}/api/script.js and {{ config('services.rybbit.site_id') }}: This is a recommended way to manage your Rybbit credentials using Laravel’s configuration system. The config('services.rybbit.instance_url') should resolve to your Rybbit instance’s base URL (e.g., https://app.rybbit.io or your self-hosted URL), and /api/script.js is appended to it. The fallback shown ensures it defaults to the cloud-hosted Rybbit script. See step 4 for .env configuration.

If you prefer not to use the config helper immediately, you can hardcode the values:

{{-- Rybbit Analytics Script --}} @if(app()->environment('production')) <script async defer src="https://app.rybbit.io/api/script.js" data-site-id="YOUR_SITE_ID"></script> @endif

Remember to replace placeholders with your actual script URL and Site ID.

It’s best practice to store your Rybbit Site ID and instance URL in your .env file and access them via Laravel’s configuration.

a. Add to .env file: Open your .env file and add:

RYBBIT_INSTANCE_URL=https://app.rybbit.io RYBBIT_SITE_ID=YOUR_SITE_ID

b. Add to config/services.php: Open (or create if it doesn’t exist) config/services.php and add a configuration for Rybbit:

<?php return [ // ... other services 'rybbit' => [ 'instance_url' => env('RYBBIT_INSTANCE_URL'), 'site_id' => env('RYBBIT_SITE_ID'), ], ];

Now, the Blade template code from Step 3 using config('services.rybbit.instance_url') and config('services.rybbit.site_id') will work correctly.

Make sure to run php artisan config:clear if you’ve cached your configuration.

5. Verify Integration

  • Deploy your Laravel application to your production environment (or set APP_ENV=production locally for testing, but be mindful of tracking local data).
  • Open your live Laravel website in a browser.
  • Navigate through a few pages.
  • Check your Rybbit dashboard for incoming data. It might take a few minutes for the first events to appear.

That’s it! Rybbit Analytics is now integrated with your Laravel application and will only track visits in your production environment.

Last updated on
Rybbit
Copyright 2025 © Rybbit.