Skip to content

Instantly share code, notes, and snippets.

@holtwick
Last active December 3, 2021 02:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holtwick/cf952d02be5f2960e2742676461564fe to your computer and use it in GitHub Desktop.
Save holtwick/cf952d02be5f2960e2742676461564fe to your computer and use it in GitHub Desktop.
<?php
// Adjust these for your needs
$umamiCollect = 'https://z-umami.holtwick.de/api/collect';
$website = "6893805d-a37d-4d63-a043-5baec4a6b32c";
// Collected info from original call
$language = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$ua = $_SERVER['HTTP_USER_AGENT'];
$referrer = $_SERVER['HTTP_REFERER']; // 1x"r" vs 2x"r" !!!
$ip = $_SERVER['REMOTE_ADDR'];
$host = $_SERVER['HTTP_HOST'];
$requestUrl = $_SERVER['REQUEST_URI'];
// General /collect call
function trackUmami($json_array)
{
global $website, $umamiCollect, $language, $ua, $referrer, $ip;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $umamiCollect);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Content-Type: application/json; charset=utf-8",
"Accept-Language: $language",
"User-Agent: $ua",
"Referer: $referrer",
"cf-connecting-ip: $ip"
]);
$body = json_encode($json_array);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_exec($ch);
curl_close($ch);
}
function trackUmamiEvent($type, $value, $url = '')
{
global $website, $umamiCollect, $language, $ua, $referrer, $ip, $host, $requestUrl;
if (empty($url)) $url = $requestUrl;
$json_array = [
'payload' => [
'website' => $website,
'hostname' => $host,
'url' => $url,
'event_type' => $type,
'event_value' => $value,
'language' => $language,
'referrer' => $referrer
],
'type' => 'event'
];
trackUmami($json_array);
}
function trackUmamiPageView($url = '')
{
global $website, $umamiCollect, $language, $ua, $referrer, $ip, $host, $requestUrl;
if (empty($url)) $url = $requestUrl;
$json_array = [
'payload' => [
'website' => $website,
'hostname' => $host,
'url' => $url,
'language' => $language,
'referrer' => $referrer
],
'type' => 'pageview'
];
trackUmami($json_array);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment