Laravel
Integrate analytics-tube Analytics with your Laravel application using Blade templates
Integrating analytics-tube Analytics with your Laravel application is straightforward. You'll typically add the analytics-tube tracking script to your main Blade layout file.
Get Your Tracking Script
First, you'll need your analytics-tube tracking script. You can find this in your analytics-tube dashboard under Site Settings > Tracking Code. It will look something like this:
<script async defer src="https://app.analytics.tube/api/script.js" data-site-id="YOUR_SITE_ID"></script>Replace YOUR_SITE_ID with your actual Site ID from your analytics-tube dashboard.
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.phpor 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.
Add the Tracking Script to the Layout
Open your main Blade layout file. Paste the analytics-tube 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 --}}
{{-- analytics-tube Analytics Script --}}
@if(app()->environment('production'))
<script async defer src="{{ config('services.analytics-tube.instance_url', 'https://app.analytics.tube') }}/api/script.js" data-site-id="{{ config('services.analytics-tube.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 theproductionenvironment. This prevents tracking during local development.{{ config('services.analytics-tube.instance_url', 'https://app.analytics.tube') }}/api/script.jsand{{ config('services.analytics-tube.site_id') }}: This is a recommended way to manage your analytics-tube credentials using Laravel's configuration system. Theconfig('services.analytics-tube.instance_url')should resolve to your analytics-tube instance's base URL (e.g.,https://app.analytics.tubeor your self-hosted URL), and/api/script.jsis appended to it. The fallback shown ensures it defaults to the cloud-hosted analytics-tube script. See step 4 for.envconfiguration.
If you prefer not to use the config helper immediately, you can hardcode the values:
{{-- analytics-tube Analytics Script --}}
@if(app()->environment('production'))
<script async defer src="https://app.analytics.tube/api/script.js" data-site-id="YOUR_SITE_ID"></script>
@endifRemember to replace placeholders with your actual script URL and Site ID.
Configure Environment Variables (Recommended)
It's best practice to store your analytics-tube 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:
analytics-tube_INSTANCE_URL=https://app.analytics.tube
analytics-tube_SITE_ID=YOUR_SITE_IDb. Add to config/services.php:
Open (or create if it doesn't exist) config/services.php and add a configuration for analytics-tube:
<?php
return [
// ... other services
'analytics-tube' => [
'instance_url' => env('analytics-tube_INSTANCE_URL'),
'site_id' => env('analytics-tube_SITE_ID'),
],
];Now, the Blade template code from Step 3 using config('services.analytics-tube.instance_url') and config('services.analytics-tube.site_id') will work correctly.
Make sure to run php artisan config:clear if you've cached your configuration.
Verify Integration
- Deploy your Laravel application to your production environment (or set
APP_ENV=productionlocally for testing, but be mindful of tracking local data). - Open your live Laravel website in a browser.
- Navigate through a few pages.
- Check your analytics-tube dashboard for incoming data. It might take a few minutes for the first events to appear.
That's it! analytics-tube Analytics is now integrated with your Laravel application and will only track visits in your production environment.