Laravel
Integrate AddonPulse Analytics with your Laravel application using Blade templates
Integrating AddonPulse Analytics with your Laravel application is straightforward. You'll typically add the AddonPulse tracking script to your main Blade layout file.
Get Your Tracking Script
First, you'll need your AddonPulse tracking script. You can find this in your AddonPulse dashboard under Site Settings > Tracking Code. It will look something like this:
<script async defer src="https://app.addonpulse.com/api/script.js" data-site-id="YOUR_SITE_ID"></script>Replace YOUR_SITE_ID with your actual Site ID from your AddonPulse 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 AddonPulse 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 --}}
{{-- AddonPulse Analytics Script --}}
@if(app()->environment('production'))
<script async defer src="{{ config('services.addonpulse.instance_url', 'https://app.addonpulse.com') }}/api/script.js" data-site-id="{{ config('services.addonpulse.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.addonpulse.instance_url', 'https://app.addonpulse.com') }}/api/script.jsand{{ config('services.addonpulse.site_id') }}: This is a recommended way to manage your AddonPulse credentials using Laravel's configuration system. Theconfig('services.addonpulse.instance_url')should resolve to your AddonPulse instance's base URL (e.g.,https://app.addonpulse.com), and/api/script.jsis appended to it.
If you prefer not to use the config helper immediately, you can hardcode the values:
{{-- AddonPulse Analytics Script --}}
@if(app()->environment('production'))
<script async defer src="https://app.addonpulse.com/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 AddonPulse 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:
ADDONPULSE_INSTANCE_URL=https://app.addonpulse.com
ADDONPULSE_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 AddonPulse:
<?php
return [
// ... other services
'addonpulse' => [
'instance_url' => env('ADDONPULSE_INSTANCE_URL'),
'site_id' => env('ADDONPULSE_SITE_ID'),
],
];Now, the Blade template code from Step 3 using config('services.addonpulse.instance_url') and config('services.addonpulse.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 AddonPulse dashboard for incoming data. It might take a few minutes for the first events to appear.
That's it! AddonPulse Analytics is now integrated with your Laravel application and will only track visits in your production environment.