| 1 |
<?php |
| 2 |
|
| 3 |
namespace LinnoSDK\Telemetry\Drivers; |
| 4 |
|
| 5 |
use LinnoSDK\Telemetry\Helpers\Utils; |
| 6 |
use PostHog\PostHog; |
| 7 |
|
| 8 |
class PostHogDriver implements DriverInterface |
| 9 |
{ |
| 10 |
protected $host; |
| 11 |
protected $apiKey; |
| 12 |
protected $lastError; |
| 13 |
|
| 14 |
public function __construct(string $host) |
| 15 |
{ |
| 16 |
$this->host = $host; |
| 17 |
} |
| 18 |
|
| 19 |
public function send(string $event, array $properties): bool |
| 20 |
{ |
| 21 |
try { |
| 22 |
PostHog::init($this->apiKey, ['host' => $this->host]); |
| 23 |
|
| 24 |
$identify = $properties['__identify'] ?? []; |
| 25 |
unset($properties['__identify']); |
| 26 |
|
| 27 |
PostHog::capture([ |
| 28 |
'distinctId' => Utils::getUniqueSiteId(), |
| 29 |
'event' => $event, |
| 30 |
'properties' => $properties, |
| 31 |
'$set' => $identify, |
| 32 |
]); |
| 33 |
|
| 34 |
PostHog::flush(); |
| 35 |
|
| 36 |
return true; |
| 37 |
} catch (\Exception $e) { |
| 38 |
$this->lastError = $e->getMessage(); |
| 39 |
return false; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
public function setApiKey(string $apiKey): void |
| 44 |
{ |
| 45 |
$this->apiKey = $apiKey; |
| 46 |
} |
| 47 |
|
| 48 |
public function getLastError(): ?string |
| 49 |
{ |
| 50 |
return $this->lastError; |
| 51 |
} |
| 52 |
} |
| 53 |
|