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:
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 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 and server API key in your .env file and access them via Laravel's configuration.
a. Add to .env file:
Open your .env file and add:
DATABUDDY_CLIENT_ID=your-client-id-here
DATABUDDY_API_KEY=your-api-key-here
DATABUDDY_WEBSITE_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'),
'api_key' => env('DATABUDDY_API_KEY'),
'website_id' => env('DATABUDDY_WEBSITE_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
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
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 $apiKey;
protected ?string $websiteId;
protected string $apiUrl;
public function __construct()
{
$this->apiKey = config('services.databuddy.api_key');
$this->websiteId = config('services.databuddy.website_id');
$this->apiUrl = 'https://basket.databuddy.cc';
}
public function track(string $name, array $properties = []): void
{
Http::withToken($this->apiKey)->post("{$this->apiUrl}/track", [
'name' => $name,
'properties' => $properties,
'websiteId' => $this->websiteId,
'timestamp' => now()->toISOString(),
'source' => 'server',
]);
}
}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
Events Not Tracking
Environment-Specific Issues
Related Integrations
Need help with your Laravel integration? Contact us at help@databuddy.cc.
How is this guide?