Files
2025-09-20 16:47:41 +03:00

61 lines
1.5 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
class GoatCounterService
{
protected string $endpoint;
protected string $token;
public function __construct()
{
$this->endpoint = 'http://gc.c0rrup7.net/api/v0/count';
$this->token = '13nus05lo9d441ihqhk5d3w15s1hoi0h9pr013j1jwqyebgma8un'; // store in config/services.php
}
/**
* Track a single pageview
*/
public function trackPageview(string $path, string $title = null, string $referrer = null): array
{
return $this->sendHits([[
'path' => $path,
'title' => $title,
'ref' => $referrer,
]]);
}
/**
* Track a custom event
*/
public function trackEvent(string $event, string $title = null, string $referrer = null): array
{
return $this->sendHits([[
'event' => $event,
'title' => $title,
'ref' => $referrer,
]]);
}
/**
* Send multiple hits in one request
*/
public function sendHits(array $hits): array
{
$response = Http::withToken($this->token)
->acceptJson()
->asJson()
->post($this->endpoint, [
'no_sessions' => true, // don’t start session cookies from backend
'hits' => $hits,
]);
return [
'status' => $response->status(),
'response' => $response->json(),
];
}
}