<?php
function trackConversion(string $scanSessionId, string $orderId, float $amount): void {
$payload = json_encode([
'site_id' => getenv('SCANOVA_SITE_ID'),
'event_name' => 'purchase',
'event_id' => sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
mt_rand(0, 0xffff), mt_rand(0, 0x0fff) | 0x4000,
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)),
'scan_session_id' => $scanSessionId,
'conversion_value' => ['amount' => $amount, 'currency' => 'USD'],
'properties' => ['order_id' => $orderId],
]);
$ch = curl_init('https://track.scanova.io/server-events');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'X-API-Key: ' . getenv('SCANOVA_API_KEY'),
],
]);
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status !== 200) {
throw new RuntimeException("Tracking failed: {$status} — {$response}");
}
}