PHP Proxy
Use a small PHP proxy when you don’t have a full framework but can run PHP on your server.
Create the script proxy
Create
public/cvs.js.php(or configure your web server to route/cvs.jsto this file):<?phpheader('Content-Type: application/javascript');header('Cache-Control: public, max-age=86400');$cacheFile = sys_get_temp_dir() . '/usertrax-cvs.js';$maxAge = 86400;if (file_exists($cacheFile) && (time() - filemtime($cacheFile)) < $maxAge) {readfile($cacheFile);exit;}$script = file_get_contents('https://usertrax.io/cvs.js');if ($script !== false) {file_put_contents($cacheFile, $script);echo $script;} else {http_response_code(502);}Create the events proxy
Create
public/api/usertrax/events.php:<?phpif ($_SERVER['REQUEST_METHOD'] !== 'POST') {http_response_code(405);exit;}$body = file_get_contents('php://input');$headers = ['Content-Type: application/json','Accept: application/json','User-Agent: ' . ($_SERVER['HTTP_USER_AGENT'] ?? ''),'X-Auth-Key: ' . ($_SERVER['HTTP_X_AUTH_KEY'] ?? ''),];$context = stream_context_create(['http' => ['method' => 'POST','header' => implode("\r\n", $headers),'content' => $body,'ignore_errors' => true,],]);$response = file_get_contents('https://api.usertrax.io/api/events', false, $context);if ($response === false) {http_response_code(502);exit;}preg_match('/HTTP\/\d\.\d\s+(\d+)/', $http_response_header[0] ?? '', $matches);http_response_code((int) ($matches[1] ?? 200));header('Content-Type: application/json');echo $response;Configure URL rewriting
Apache (
.htaccess):RewriteEngine OnRewriteRule ^cvs\.js$ cvs.js.php [L]RewriteRule ^api/usertrax/events$ api/usertrax/events.php [L]Nginx:
location = /cvs.js {fastcgi_pass unix:/var/run/php/php8.4-fpm.sock;include fastcgi_params;fastcgi_param SCRIPT_FILENAME /path/to/public/cvs.js.php;}location = /api/usertrax/events {fastcgi_pass unix:/var/run/php/php8.4-fpm.sock;include fastcgi_params;fastcgi_param SCRIPT_FILENAME /path/to/public/api/usertrax/events.php;}Update your HTML
<script>window.usertraxConfig = {endpoint: "/api/usertrax/events",};</script><script src="/cvs.js" data-key="YOUR_API_KEY" defer></script>
Verification
Section titled “Verification”- Visit your website
- Open the Network tab
- Confirm requests go through your domain
Troubleshooting
Section titled “Troubleshooting”- 502 errors: Ensure
allow_url_fopenis enabled or use cURL instead offile_get_contents - 401 Unauthorized: Verify
HTTP_X_AUTH_KEYis passed — some PHP setups require explicit header forwarding in Nginx