Install DataPocket on PHP

Updated

One file, no framework, no rewrites. 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

    Save as dp.php in your web root:

    php
    <?php
    // DataPocket first-party proxy
    $PD = 'https://datapocket.vo1d.studio';
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $ip = trim(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'] ?? '')[0]);
        $ctx = stream_context_create(['http' => [
            'method' => 'POST', 'ignore_errors' => true, 'timeout' => 5,
            'header' => "Content-Type: text/plain\r\nUser-Agent: " . ($_SERVER['HTTP_USER_AGENT'] ?? '') . "\r\nX-Forwarded-For: $ip\r\n",
            'content' => file_get_contents('php://input'),
        ]]);
        $body = file_get_contents("$PD/api/event", false, $ctx);
        preg_match('#HTTP/\S+ (\d+)#', $http_response_header[0] ?? '', $m);
        http_response_code((int) ($m[1] ?? 502));
        header('Content-Type: application/json');
        echo $body;
        exit;
    }
    $file = $_GET['file'] ?? 'script.js';
    if (!preg_match('/^script(\.[a-z]+)?(@\d+\.\d+\.\d+)?\.js$/', $file)) { http_response_code(404); exit; }
    header('Content-Type: application/javascript');
    header('Cache-Control: public, max-age=3600');
    echo file_get_contents("$PD/js/$file");
  2. 2

    Load the script through it. data-api sends events back to dp.php:

    html
    <script
      defer
      data-site-id="YOUR_SITE_ID"
      data-domain="YOUR_DOMAIN"
      data-api="/dp.php"
      src="/dp.php?file=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 PHP · DataPocket