Skip to content

Instantly share code, notes, and snippets.

@mskian
Last active June 5, 2023 10:07
Show Gist options
  • Save mskian/74f7221187cc4600ed7b30a1401f007b to your computer and use it in GitHub Desktop.
Save mskian/74f7221187cc4600ed7b30a1401f007b to your computer and use it in GitHub Desktop.
Umami API PHP proxy
<?php
header('X-Frame-Options: DENY');
header('X-XSS-Protection: 1; mode=block');
header('X-Content-Type-Options: nosniff');
header('Strict-Transport-Security: max-age=63072000');
header('Content-type:application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET,POST');
header('Access-Control-Allow-Headers: Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With');
header('X-Robots-Tag: noindex, nofollow', true);
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://umami.example.com/api/websites/<SITE ID>/stats?startAt=1672511400000&endAt=1703961000000",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <YOUR AUTH TOKEN>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo trim($response, '[]');;
}
?>
<?php
header('X-Frame-Options: DENY');
header('X-XSS-Protection: 1; mode=block');
header('X-Content-Type-Options: nosniff');
header('Strict-Transport-Security: max-age=63072000');
header('Content-type:application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET,POST');
header('Access-Control-Allow-Headers: Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With');
header('X-Robots-Tag: noindex, nofollow', true);
function get_percentage($total, $number)
{
if ( $total > 0 ) {
return round(($number * 100) / $total, 2);
} else {
return 0;
}
}
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://umami.example.com/api/websites/<SITE ID>/stats?startAt=1672511400000&endAt=1703961000000",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <YOUR AUTH TOKEN>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$obj = json_decode($response);
$pageview = ($obj->pageviews->value);
$uniques = ($obj->uniques->value);
$bounce = ($obj->bounces->value);
$total = get_percentage($pageview,$uniques).'%';
$data_clear = ["pageview"=>number_format($pageview), "uniques"=>number_format($uniques), "bounce"=>$total];
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo json_encode($data_clear);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment