# Install on a PHP site

> Add Reveata to a PHP site through the header file every page includes.

Most PHP sites build each page from a shared header file (`header.php`, `head.php` or a template). Paste the snippet there once and it is on every page.

Copy the snippet from your Reveata dashboard (it contains your website’s tracking id):

```html
<script
  defer
  data-website-id="ws_your_tracking_id"
  src="https://reveata.com/js/script.js"
></script>
```

## Add it to your site

1. Open the file that prints `<head>` for your pages, for example the `header.php` you `include` or `require` at the top of each page.
2. Paste the snippet before `</head>`:

```php
<?php // header.php, included by every page ?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title><?= htmlspecialchars($title ?? 'My site') ?></title>
  <script defer data-website-id="ws_your_tracking_id" src="https://reveata.com/js/script.js"></script>
</head>
```

3. Upload the file. If your pages don’t share a header, paste the snippet before `</head>` on each page.

For WordPress, use the [WordPress plugin](https://reveata.com/docs/wordpress) instead. For Laravel, see [Install on Laravel](https://reveata.com/docs/laravel).

## Check it works

Open your site in a private window, then check that the visit appears in your Reveata dashboard. If it doesn’t, follow [Verify your installation](https://reveata.com/docs/verify-your-installation). The script ignores `localhost` by default, so local development visits stay out of your numbers.

## Goals

Add `data-reveata-goal="signup"` to a button or link, or call `reveata('goal', 'signup')` from JavaScript. See [Custom goals](https://reveata.com/docs/custom-goals).

## Serve it from your own domain

Two small PHP files can pass the script and events through your own domain, with the visitor’s address and your proxy secret: see [Proxy with plain PHP](https://reveata.com/docs/proxy-php). If you manage the web server, NGINX or Caddy can do it instead: [Proxy through NGINX](https://reveata.com/docs/nginx#proxy-through-nginx), [Proxy through Caddy](https://reveata.com/docs/caddy#proxy-through-caddy). On shared hosting without either, use the [managed proxy](https://reveata.com/docs/managed-proxy).

## Revenue

First connect your payment provider in Reveata: see [Connect a payment provider](https://reveata.com/docs/connect-a-payment-provider). Then pass the Reveata visitor id with each checkout, so the payment is credited to the visit that brought the buyer.

On the page with your buy button, add two hidden fields and fill them when the form is sent:

```html
<form method="post" action="/checkout">
  <input type="hidden" name="reveata_visitor_id">
  <input type="hidden" name="reveata_session_id">
  <button>Buy</button>
</form>
<script>
  // Fill the Reveata ids just before any form with these fields is sent.
  document.addEventListener('submit', function (e) {
    var ids = window.reveata && window.reveata.metadata ? window.reveata.metadata() : {};
    Object.keys(ids).forEach(function (name) {
      var input = e.target.querySelector('input[name="' + name + '"]');
      if (input) input.value = ids[name];
    });
  });
</script>
```

In `checkout.php`, copy them into the metadata of the Stripe Checkout Session (with `stripe/stripe-php`):

```php
<?php
require __DIR__ . '/vendor/autoload.php';

$stripe = new \Stripe\StripeClient(getenv('STRIPE_SECRET_KEY'));
$ids = array_filter([
    'reveata_visitor_id' => $_POST['reveata_visitor_id'] ?? '',
    'reveata_session_id' => $_POST['reveata_session_id'] ?? '',
]);
$session = $stripe->checkout->sessions->create([
    'mode' => 'payment',
    'line_items' => [['price' => 'price_123', 'quantity' => 1]],
    'success_url' => 'https://example.com/thanks?session_id={CHECKOUT_SESSION_ID}',
    'metadata' => $ids,
]);
header('Location: ' . $session->url, true, 303);
```

If Reveata doesn’t connect to your provider, send each payment with the [Payments API](https://reveata.com/docs/payments-api):

```php
<?php
$ch = curl_init('https://reveata.com/api/v1/payments');
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 10,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . getenv('REVEATA_SITE_KEY'),
        'Content-Type: application/json',
    ],
    CURLOPT_POSTFIELDS => json_encode([
        'transactionId' => 'order_' . $order['id'],
        'amount' => (float) $order['total_without_tax'], // major units, e.g. 49.00
        'currency' => 'USD',
        'email' => $order['email'],
        'visitorId' => $order['reveata_visitor_id'], // saved from the hidden field
    ]),
]);
curl_exec($ch);
```

## Common pitfalls

- **Pages without the shared header.** Standalone pages need the snippet pasted in themselves.
- **Output before the header.** If `checkout.php` prints anything before `header()`, the redirect fails; keep it free of output.
- **Testing on localhost.** The script ignores `localhost` unless you add `data-allow-localhost="true"`, so test the deployed site.
- **A Content Security Policy.** If your app sends one, allow the Reveata script and its endpoint: see [Content Security Policy](https://reveata.com/docs/content-security-policy).
- **Counting payments twice.** If a provider is connected in Reveata, don’t also send its payments with the Payments API.
