Install DataPocket on Laravel

Updated

Routes with the Http client, excluded from CSRF. Sign in and these snippets fill in your site ID and domain. Until then, swap YOUR_SITE_ID and YOUR_DOMAIN with the values from your Install page.

  1. 1

    routes/web.php:

    php
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Http;
    use Illuminate\Support\Facades\Route;
    
    Route::get('/dp/js/{file}', function (string $file) {
        $r = Http::get("https://datapocket.vo1d.studio/js/{$file}");
        return response($r->body(), 200)
            ->header('Content-Type', 'application/javascript')
            ->header('Cache-Control', 'public, max-age=3600');
    })->where('file', 'script(\.[a-z]+)?(@\d+\.\d+\.\d+)?\.js');
    
    Route::post('/dp/api/event', function (Request $request) {
        $r = Http::withHeaders([
            'Content-Type' => 'text/plain',
            'User-Agent' => $request->userAgent() ?? '',
            'X-Forwarded-For' => $request->ip(),
        ])->withBody($request->getContent(), 'text/plain')->post('https://datapocket.vo1d.studio/api/event');
        return response($r->body(), $r->status())->header('Content-Type', 'application/json');
    })->withoutMiddleware([
        \Illuminate\Foundation\Http\Middleware\ValidateCsrfToken::class,
        \Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class,
    ]);
  2. 2

    In Blade (resources/views/layouts/app.blade.php) <head>:

    html
    <script
      defer
      data-site-id="YOUR_SITE_ID"
      data-domain="YOUR_DOMAIN"
      src="/dp/js/script.js"
    ></script>

Revenue attribution

  1. 1

    Pass the visitor cookie to Stripe Checkout so each sale is attributed to the channel that brought the buyer:

    php
    $session = $stripe->checkout->sessions->create([
        // …your line_items, mode, success_url…
        'metadata' => [
            'datapocket_visitor_id' => $_COOKIE['dp_vid'] ?? '',
            'datapocket_session_id' => $_COOKIE['dp_sid'] ?? '',
        ],
    ]);
  2. 2

    Using another payment provider? Send payments from your backend (create an API key in Settings):

    bash
    curl -X POST https://datapocket.vo1d.studio/api/v1/payments \
      -H "Authorization: Bearer dpk_…" \
      -H "Content-Type: application/json" \
      -d '{"amount": 29.99, "currency": "USD", "transaction_id": "order_123", "visitor_id": "<dp_vid cookie>"}'

Related guides

Install DataPocket on Laravel · DataPocket