61 lines
1.5 KiB
PHP
61 lines
1.5 KiB
PHP
<?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(),
|
|||
|
|
];
|
|||
|
|
}
|
|||
|
|
}
|