Laravel
Add Databuddy's privacy-first analytics to your Laravel application by adding the tracking script to your main Blade layout file.
How to Add Databuddy to Laravel
Get Your Tracking Script
Get your Databuddy tracking script from your Databuddy dashboard. It will look like this:
<script
src="https://cdn.databuddy.cc/databuddy.js"
data-client-id="YOUR_CLIENT_ID"
async
></script>Replace YOUR_CLIENT_ID with your actual Client ID from your Databuddy 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 Databuddy 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'])
</head>
<body class="font-sans antialiased">
{{-- Your page content --}}
@yield('content')
{{-- Other scripts --}}
{{-- Databuddy Analytics Script --}}
@if(app()->environment('production'))
<script
src="https://cdn.databuddy.cc/databuddy.js"
data-client-id="{{ config('services.databuddy.client_id') }}"
async
></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.databuddy.client_id') }}: This accesses your Databuddy Client ID from Laravel's configuration system. See step 4 for.envconfiguration.
If you prefer not to use the config helper immediately, you can hardcode the value:
{{-- Databuddy Analytics Script --}}
@if(app()->environment('production'))
<script
src="https://cdn.databuddy.cc/databuddy.js"
data-client-id="YOUR_CLIENT_ID"
async
></script>
@endifRemember to replace YOUR_CLIENT_ID with your actual Client ID.
Configure Environment Variables (Recommended)
It's best practice to store your Databuddy Client ID in your .env file and access it via Laravel's configuration.
a. Add to .env file:
Open your .env file and add:
DATABUDDY_CLIENT_ID=your-client-id-hereb. Add to config/services.php:
Open (or create if it doesn't exist) config/services.php and add a configuration for Databuddy:
<?php
return [
// ... other services
'databuddy' => [
'client_id' => env('DATABUDDY_CLIENT_ID'),
],
];Now, the Blade template code from Step 3 using config('services.databuddy.client_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 Databuddy dashboard for incoming data. It might take a few minutes for the first events to appear
Configuration Options
Enable additional tracking features by adding data attributes to your script tag:
{{-- Databuddy Analytics Script with Options --}}
@if(app()->environment('production'))
<script
src="https://cdn.databuddy.cc/databuddy.js"
data-client-id="{{ config('services.databuddy.client_id') }}"
data-track-attributes
data-track-outgoing-links
data-track-interactions
data-track-performance
data-track-web-vitals
data-track-errors
data-track-scroll-depth
async
></script>
@endifCustom Event Tracking
Track custom events from your Laravel Blade templates or JavaScript:
In Blade Templates
<button
onclick="if(window.databuddy) window.databuddy.track('button_click', { button_id: 'cta' })"
>
Click Me
</button>Using Data Attributes
Enable automatic tracking with data attributes:
{{-- Enable data-track attributes in script tag --}}
<script
src="https://cdn.databuddy.cc/databuddy.js"
data-client-id="{{ config('services.databuddy.client_id') }}"
data-track-attributes
async
></script>
{{-- Then use in your templates --}}
<button data-track="cta_click" data-button-type="primary">
Get Started
</button>
<a href="/pricing" data-track="pricing_link_click" data-link-location="header">
View Pricing
</a>In JavaScript Files
If you're using Vite or Laravel Mix, you can track events from your JavaScript:
// Track custom events
function trackButtonClick(buttonId) {
if (window.databuddy) {
window.databuddy.track('button_click', {
button_id: buttonId,
page_path: window.location.pathname
});
}
}
// Track form submissions
document.addEventListener('DOMContentLoaded', function() {
const forms = document.querySelectorAll('form[data-track]');
forms.forEach(function(form) {
form.addEventListener('submit', function() {
if (window.databuddy) {
window.databuddy.track('form_submit', {
form_type: form.getAttribute('data-form-type') || 'contact',
page_path: window.location.pathname
});
}
});
});
});Server-Side Tracking
For server-side tracking in Laravel, use the Databuddy Node SDK or make HTTP requests directly:
# Install HTTP client (Guzzle is included with Laravel)
# No additional package needed<?php
namespace AppServices;
use IlluminateSupportFacadesHttp;
class DatabuddyService
{
protected string $clientId;
protected string $apiUrl;
public function __construct()
{
$this->clientId = config('services.databuddy.client_id');
$this->apiUrl = 'https://basket.databuddy.cc';
}
public function track(string $eventName, array $properties = []): void
{
Http::post("{$this->apiUrl}/events", [
'clientId' => $this->clientId,
'eventName' => $eventName,
'properties' => $properties,
'timestamp' => now()->timestamp,
]);
}
}Then use it in your controllers:
use AppServicesDatabuddyService;
class OrderController extends Controller
{
public function store(Request $request, DatabuddyService $databuddy)
{
// Process order...
// Track order event
$databuddy->track('order_placed', [
'order_id' => $order->id,
'total' => $order->total,
'currency' => 'USD',
]);
return redirect()->route('orders.show', $order);
}
}Common Use Cases
E-commerce Tracking
Track product views and purchases:
{{-- Product view page --}}
@push('scripts')
<script>
if (window.databuddy) {
window.databuddy.track('product_view', {
product_id: '{{ $product->id }}',
product_name: '{{ $product->name }}',
price: {{ $product->price }},
category: '{{ $product->category }}'
});
}
</script>
@endpushForm Submissions
Track form submissions:
<form method="POST" action="{{ route('contact.store') }}" data-track="form_submit" data-form-type="contact">
@csrf
<!-- form fields -->
<button type="submit">Submit</button>
</form>Troubleshooting
Script Not Loading
- Verify the script is in the correct Blade layout file
- Check that
APP_ENV=productionin your.envfile (or remove the@ifcondition for testing) - Check browser console for errors
- Ensure your Client ID is correct in the config
- Run
php artisan config:clearif you've updated configuration
Events Not Tracking
- Confirm
window.databuddyexists before calling tracking methods - Check browser console for any errors
- Verify events appear in your Databuddy dashboard after 2-3 minutes
- Use browser dev tools Network tab to confirm requests are being sent
Environment-Specific Issues
- The script only loads in production by default (due to
@if(app()->environment('production'))) - For local testing, temporarily remove the environment check or set
APP_ENV=productionin your.env - Remember to restore the environment check before deploying
Need help with your Laravel integration? Contact us at help@databuddy.cc.
How is this guide?