Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 27 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| HeapAnalyticsService | |
0.00% |
0 / 27 |
|
0.00% |
0 / 3 |
56 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| trackEvent | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
| addUserProperties | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Services; |
| 4 | |
| 5 | use GuzzleHttp\Client; |
| 6 | use GuzzleHttp\Exception\RequestException; |
| 7 | |
| 8 | class HeapAnalyticsService |
| 9 | { |
| 10 | protected $heapApiKey; |
| 11 | protected $client; |
| 12 | |
| 13 | public function __construct() |
| 14 | { |
| 15 | $this->heapApiKey = env('HEAP_API_KEY') ?? 3450027184; |
| 16 | $this->client = new Client(['base_uri' => 'https://heapanalytics.com/api/']); |
| 17 | } |
| 18 | |
| 19 | public function trackEvent($eventName, $properties = [], $identity = null) |
| 20 | { |
| 21 | try { |
| 22 | $response = $this->client->post('track', [ |
| 23 | 'json' => [ |
| 24 | 'app_id' => $this->heapApiKey, |
| 25 | 'event' => $eventName, |
| 26 | 'identity' => $identity, |
| 27 | 'properties' => $properties, |
| 28 | ], |
| 29 | ]); |
| 30 | |
| 31 | return json_decode($response->getBody(), true); |
| 32 | } catch (RequestException $e) { |
| 33 | if ($e->hasResponse()) { |
| 34 | // Log de erro ou exceção |
| 35 | \Log::error('Heap Analytics Error: ' . $e->getResponse()->getBody()->getContents()); |
| 36 | } |
| 37 | return false; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | public function addUserProperties($identity, $properties) |
| 42 | { |
| 43 | try { |
| 44 | $response = $this->client->post('add_user_properties', [ |
| 45 | 'json' => [ |
| 46 | 'app_id' => $this->heapApiKey, |
| 47 | 'identity' => $identity, |
| 48 | 'properties' => $properties, |
| 49 | ], |
| 50 | ]); |
| 51 | |
| 52 | return json_decode($response->getBody(), true); |
| 53 | } catch (RequestException $e) { |
| 54 | if ($e->hasResponse()) { |
| 55 | \Log::error('Heap Analytics Add User Properties Error: ' . $e->getResponse()->getBody()->getContents()); |
| 56 | } |
| 57 | return false; |
| 58 | } |
| 59 | } |
| 60 | } |