---
title: "Install DataPocket on PHP"
description: "Add DataPocket web analytics with revenue attribution to PHP. One file, no framework, no rewrites."
url: "https://datapocket.vo1d.studio/docs/php"
lang: "en"
updated: "2026-11-13"
---

# Install DataPocket on PHP

One file, no framework, no rewrites. Replace `YOUR_SITE_ID` and `YOUR_DOMAIN` with the values from your site's Install page. This guide serves the script from your own domain, so ad blockers can't tell it apart from your own code.

## Steps

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. 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. 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. 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>"}'
```

## Verify

1. Deploy or publish, then open your site in a normal browser window (ad blockers off).
2. In the browser's Network tab, look for a POST to `/api/event` (or `/dp/api/event` when proxied) that returns `{"ok":true}`.
3. The DataPocket Install page for this site switches to "Receiving data" within a few seconds.

## Related guides

- [React Router](https://datapocket.vo1d.studio/docs/react-router): Framework mode (Remix) and library mode. pushState navigations are tracked automatically.
- [Vue.js](https://datapocket.vo1d.studio/docs/vue): Vue 3 + Vue Router, Nuxt, Vite. History-mode routing is tracked automatically.
- [JavaScript apps](https://datapocket.vo1d.studio/docs/npm): React, Vue, Svelte or Angular: the script tag with typed calls, plus server-side payments and goals over HTTP.
- [Express](https://datapocket.vo1d.studio/docs/express): Serve the script and forward events from your Express server.
- [AI crawler tracking](https://datapocket.vo1d.studio/docs/ai-bots)
