chore(umami.php): clean up analytics
This commit is contained in:
parent
60be0ed01d
commit
f4a995e8bf
5 changed files with 41 additions and 40 deletions
|
@ -1,42 +1,42 @@
|
||||||
<?php
|
<?php
|
||||||
$remoteUrl = 'https://stats.apps.coryd.dev/script.js';
|
|
||||||
$cacheKey = 'remote_stats_script';
|
|
||||||
$ttl = 3600;
|
|
||||||
$js = null;
|
|
||||||
$code = 200;
|
|
||||||
$redis = null;
|
|
||||||
|
|
||||||
try {
|
$umamiHost = 'https://stats.apps.coryd.dev';
|
||||||
if (extension_loaded('redis')) {
|
$requestUri = $_SERVER['REQUEST_URI'];
|
||||||
$redis = new Redis();
|
$method = $_SERVER['REQUEST_METHOD'];
|
||||||
$redis->connect('127.0.0.1', 6379);
|
$targetUrl = $umamiHost . $requestUri;
|
||||||
|
$headers = [
|
||||||
|
'Content-Type: application/json',
|
||||||
|
'Accept: application/json',
|
||||||
|
];
|
||||||
|
$ch = curl_init($targetUrl);
|
||||||
|
|
||||||
if ($redis->exists($cacheKey)) $js = $redis->get($cacheKey);
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
|
||||||
|
if (isset($_SERVER['HTTP_USER_AGENT'])) $headers[] = 'User-Agent: ' . $_SERVER['HTTP_USER_AGENT'];
|
||||||
|
|
||||||
|
if ($method === 'POST') {
|
||||||
|
$body = file_get_contents('php://input');
|
||||||
|
$data = json_decode($body, true);
|
||||||
|
|
||||||
|
if (strpos($requestUri, '/api/send') === 0 && is_array($data)) {
|
||||||
|
$data['payload']['ip'] = $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
|
||||||
|
$data['payload']['website'] = 'a30f4735-6c27-4761-b180-a8f247a4a3a3';
|
||||||
}
|
}
|
||||||
} catch (Exception $e) {
|
|
||||||
error_log("Redis unavailable: " . $e->getMessage());
|
curl_setopt($ch, CURLOPT_POST, true);
|
||||||
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
||||||
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||||
|
} else {
|
||||||
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is_string($js)) {
|
$response = curl_exec($ch);
|
||||||
$ch = curl_init($remoteUrl);
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
|
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
|
||||||
|
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
curl_close($ch);
|
||||||
curl_setopt($ch, CURLOPT_HEADER, false);
|
http_response_code($httpCode);
|
||||||
|
|
||||||
$js = curl_exec($ch);
|
if ($contentType) header("Content-Type: $contentType");
|
||||||
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
||||||
|
|
||||||
curl_close($ch);
|
echo $response ?: '';
|
||||||
|
|
||||||
if ($redis && $code === 200 && $js) $redis->setex($cacheKey, $ttl, $js);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!is_string($js) || trim($js) === '') {
|
|
||||||
$js = '// Failed to fetch remote script';
|
|
||||||
$code = 502;
|
|
||||||
}
|
|
||||||
|
|
||||||
http_response_code($code);
|
|
||||||
header('Content-Type: application/javascript; charset=UTF-8');
|
|
||||||
header('Cache-Control: public, max-age=60');
|
|
||||||
echo $js;
|
|
||||||
|
|
10
package-lock.json
generated
10
package-lock.json
generated
|
@ -1,12 +1,12 @@
|
||||||
{
|
{
|
||||||
"name": "coryd.dev",
|
"name": "coryd.dev",
|
||||||
"version": "5.0.0",
|
"version": "5.0.1",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "coryd.dev",
|
"name": "coryd.dev",
|
||||||
"version": "5.0.0",
|
"version": "5.0.1",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"html-minifier-terser": "7.2.0",
|
"html-minifier-terser": "7.2.0",
|
||||||
|
@ -1737,9 +1737,9 @@
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
"node_modules/electron-to-chromium": {
|
"node_modules/electron-to-chromium": {
|
||||||
"version": "1.5.153",
|
"version": "1.5.154",
|
||||||
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.153.tgz",
|
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.154.tgz",
|
||||||
"integrity": "sha512-4bwluTFwjXZ0/ei1qDpHDGzVveuBfx4wiZ9VQ8j/30+T2JxSF2TfZ00d1X+wNMeDyUdZXgLkJFbarJdAMtd+/w==",
|
"integrity": "sha512-G4VCFAyKbp1QJ+sWdXYIRYsPGvlV5sDACfCmoMFog3rjm1syLhI41WXm/swZypwCIWIm4IFLWzHY14joWMQ5Fw==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "ISC"
|
"license": "ISC"
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "coryd.dev",
|
"name": "coryd.dev",
|
||||||
"version": "5.0.0",
|
"version": "5.0.1",
|
||||||
"description": "The source for my personal site. Built using 11ty (and other tools).",
|
"description": "The source for my personal site. Built using 11ty (and other tools).",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"engines": {
|
"engines": {
|
||||||
|
|
|
@ -28,7 +28,7 @@
|
||||||
eleventy:eleventy
|
eleventy:eleventy
|
||||||
%}
|
%}
|
||||||
<script defer src="/assets/scripts/index.js?v={% appVersion %}"></script>
|
<script defer src="/assets/scripts/index.js?v={% appVersion %}"></script>
|
||||||
<script defer src="/assets/scripts/util.js?v={% appVersion %}" data-host-url="https://stats.apps.coryd.dev" data-website-id="a30f4735-6c27-4761-b180-a8f247a4a3a3"></script>
|
<script defer src="/assets/scripts/util.js?v={% appVersion %}"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="main-wrapper">
|
<div class="main-wrapper">
|
||||||
|
|
|
@ -61,6 +61,7 @@ RewriteRule ^oembed/(.+)$ /api/oembed.php?url=https://www.coryd.dev/$1 [L,QSA]
|
||||||
|
|
||||||
## analytics
|
## analytics
|
||||||
RewriteRule ^assets/scripts/util\.js$ /api/umami.php [L]
|
RewriteRule ^assets/scripts/util\.js$ /api/umami.php [L]
|
||||||
|
RewriteRule ^assets/scripts/api/send$ /api/umami.php [L]
|
||||||
|
|
||||||
{% for redirect in redirects -%}
|
{% for redirect in redirects -%}
|
||||||
Redirect {{ redirect.status_code | default: "301" }} {{ redirect.source_url }} {{ redirect.destination_url }}
|
Redirect {{ redirect.status_code | default: "301" }} {{ redirect.source_url }} {{ redirect.destination_url }}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue