Source: https://datafa.st/docs/laravel
Markdown source: https://datafa.st/docs/laravel.md
Description: How to add DataFast to your Laravel project

# Add DataFast to your Laravel project

Follow these steps to integrate DataFast analytics into your Laravel application.

## 1. Add Script to Layout

The recommended way to add scripts to all pages in a Laravel application is by using the main layout file.

1. Open your project's main layout file, typically located at `resources/views/layouts/app.blade.php`.
2. Add the DataFast tracking script to the `<head>` section:

    ```php
    <!DOCTYPE html>
    <html>
    <head>
        <!-- ... other head elements ... -->
        <script
            defer
            data-website-id="{{ config('datafast.website_id') }}"
            data-domain="{{ config('datafast.domain') }}"
            src="https://datafa.st/js/script.js"
        ></script>
    </head>
    <body>
        @yield('content')
    </body>
    </html>
    ```

## 2. Add Configuration (Optional)

For better configuration management, create a DataFast config file:

1. Create `config/datafast.php`:

    ```php
    <?php

    return [
        'website_id' => env('DATAFAST_WEBSITE_ID'),
        'domain' => env('DATAFAST_DOMAIN'),
    ];
    ```

2. Add to your `.env` file:

    ```env
    DATAFAST_WEBSITE_ID=dfid_******
    DATAFAST_DOMAIN=your_domain.com
    ```

## 3. Server-side Revenue Tracking

### Connect your Payment Provider

Go to your website settings in DataFast and connect your Stripe or LemonSqueezy account.

### Pass metadata during checkout

#### Stripe

When creating a checkout session, include the DataFast cookies in the metadata:

```php
<?php

use Stripe\Stripe;
use Stripe\Checkout\Session;

Stripe::setApiKey(config('services.stripe.secret'));

$session = Session::create([
    'payment_method_types' => ['card'],
    'line_items' => [[
        'price' => 'price_1234567890',
        'quantity' => 1,
    ]],
    'mode' => 'payment',
    'success_url' => route('checkout.success'),
    'cancel_url' => route('checkout.cancel'),
    'metadata' => [
        'datafast_visitor_id' => request()->cookie('datafast_visitor_id'),
        'datafast_session_id' => request()->cookie('datafast_session_id')
    ]
]);

return redirect($session->url);
```

#### LemonSqueezy

For LemonSqueezy, include the DataFast cookies in the custom data:

```php
<?php

use Illuminate\Support\Facades\Http;

$response = Http::withHeaders([
    'Authorization' => 'Bearer ' . config('services.lemonsqueezy.api_key'),
    'Content-Type' => 'application/json'
])->post('https://api.lemonsqueezy.com/v1/checkouts', [
    'store_id' => config('services.lemonsqueezy.store_id'),
    'variant_id' => 'your_variant_id',
    'custom' => [
        'datafast_visitor_id' => request()->cookie('datafast_visitor_id'),
        'datafast_session_id' => request()->cookie('datafast_session_id')
    ]
]);

return redirect($response->json()['data']['attributes']['url']);
```

## 4. Verify installation

After implementing:
- Visit your live website
- Check your [DataFast dashboard](/dashboard) for incoming data
- It might take a few minutes for the first pageviews to appear
- Once a purchase is made, verify that revenue is being attributed correctly

> For advanced configuration options like localhost tracking, custom API endpoints, or cross-domain setup, see the [script configuration reference](/docs/script-configuration).
