Laravel
Add the script to your main Blade layout to record visits, then send server events from controllers.
Add the script
Set your Client ID
DATABUDDY_CLIENT_ID=YOUR_CLIENT_ID'databuddy' => [
'client_id' => env('DATABUDDY_CLIENT_ID'),
'api_key' => env('DATABUDDY_API_KEY'),
],Your Client ID is in the Databuddy dashboard under your website's Settings → General. Run php artisan config:clear if your config is cached.
Add it to your layout
@production
<script
src="https://cdn.databuddy.cc/databuddy.js"
data-client-id="{{ config('services.databuddy.client_id') }}"
crossorigin="anonymous"
async
></script>
@endproduction
</body>@production loads it only when APP_ENV=production, so local visits aren't counted.
Deploy, visit a few pages, and they appear on your dashboard.
Track clicks in Blade views
Add data-track-attributes to the script tag, then name elements in any view:
<a href="/pricing" data-track="pricing_link_click" data-location="header">View pricing</a>Each click sends a pricing_link_click event with location: "header". Put data-track on buttons and links, not on a whole form, or every click inside the form sends an event.
Send an event from JavaScript
window.databuddy?.track("plan_selected", { plan: "pro" });Send an event from the server
Add an API key with the track:events scope in Organization settings → API keys, and add it to .env as DATABUDDY_API_KEY.
use Illuminate\Support\Facades\Http;
Http::withToken(config('services.databuddy.api_key'))
->post('https://basket.databuddy.cc/track', [
'name' => 'order_placed',
'websiteId' => config('services.databuddy.client_id'),
'source' => 'server',
'properties' => [
'order_id' => $order->id,
'total' => $order->total,
],
]);Server events show as custom events. They don't carry the visitor's referrer, campaign or device.
Related
How is this guide?