PHP proxy

Proxy attribu.tech analytics through PHP.

This is a server-specific guide. See Proxy the tracking script for an overview of why and how proxying works.

Create the script proxy

Create script.php in your web root. It fetches the tracking script from attribu.tech and serves it with the correct content type.

<?php
// script.php - Proxy the attribu.tech tracking script

$ch = curl_init("https://attribu.tech/js/script.js");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

header("Content-Type: application/javascript");
header("Cache-Control: public, max-age=3600");
http_response_code($status);
echo $body;

Create the events proxy

Create events.php. It forwards the POST body and relevant headers to the attribu.tech event API.

<?php
// events.php - Proxy event data to attribu.tech

$body = file_get_contents("php://input");

$headers = [
    "Content-Type: application/json",
    "X-Forwarded-For: " . $_SERVER["REMOTE_ADDR"],
];

$ch = curl_init("https://attribu.tech/api/events");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

header("Content-Type: application/json");
http_response_code($status);
echo $response;

Apache rewrite rules

Add these rules to your .htaccess so visitors hit clean URLs instead of the .php files directly:

RewriteEngine On

# Serve the tracking script
RewriteRule ^js/attribu\.js$ script.php [L]

# Forward event API calls
RewriteRule ^api/proxy/events$ events.php [L]

Update your script tag

Point the script src to your proxied path and set data-api-url to your proxied API endpoint:

<script
  defer
  data-website-id="YOUR_SITE_ID"
  data-domain="yourdomain.com"
  data-api-url="https://yourdomain.com/api/proxy/events"
  src="/js/attribu.js"
></script>
Replace YOUR_SITE_ID with your actual Website ID from attribu.tech. Replace yourdomain.com with your website's root domain.

Verify installation

  1. Visit your site in an incognito/private window.
  2. Open your browser's Developer Tools and go to the Network tab.
  3. Confirm the script loads from /js/attribu.js on your domain.
  4. Confirm the pageview POST goes to /api/proxy/events on your domain.
  5. Check your attribu.tech dashboard. A new pageview should appear within a few seconds.

What's next

Copied