Skip to content

Commit 3eeefb9

Browse files
authored
1 parent 5daf51a commit 3eeefb9

File tree

3 files changed

+120
-1
lines changed

3 files changed

+120
-1
lines changed

src/Event/Exception.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace AlexWestergaard\PhpGa4\Event;
4+
5+
use AlexWestergaard\PhpGa4\Helper\EventHelper;
6+
use AlexWestergaard\PhpGa4\Facade;
7+
use AlexWestergaard\PhpGa4\Facade\Type\Ga4ExceptionType;
8+
9+
class Exception extends EventHelper implements Facade\Group\ExceptionFacade
10+
{
11+
protected null|string $description;
12+
protected null|bool $fatal;
13+
14+
public function getName(): string
15+
{
16+
return 'exception';
17+
}
18+
19+
public function getParams(): array
20+
{
21+
return [
22+
'description',
23+
'fatal',
24+
];
25+
}
26+
27+
public function getRequiredParams(): array
28+
{
29+
return [];
30+
}
31+
32+
public function setDescription(null|string $description)
33+
{
34+
$this->description = $description;
35+
return $this;
36+
}
37+
38+
public function setFatal(null|bool $isFatal)
39+
{
40+
$this->fatal = $isFatal;
41+
return $this;
42+
}
43+
44+
public function parseException(\Exception $exception, $isFatal = false)
45+
{
46+
if ($exception instanceof Ga4ExceptionType) {
47+
return $this;
48+
}
49+
50+
$this->setDescription($exception->getMessage());
51+
$this->setFatal($isFatal);
52+
53+
return $this;
54+
}
55+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace AlexWestergaard\PhpGa4\Facade\Group;
4+
5+
use Exception;
6+
7+
interface ExceptionFacade
8+
{
9+
/**
10+
* Report exception data
11+
*
12+
* @var description
13+
* @param string $description eg. Exception->Message()
14+
*/
15+
public function setDescription(null|string $description);
16+
17+
/**
18+
* Report if the exception is fatal
19+
*
20+
* @var fatal
21+
* @param bool $isFatal
22+
*/
23+
public function setFatal(null|bool $isFatal);
24+
25+
/**
26+
* Attempt to parse the message from the Exception and error own known GA4 Exceptions
27+
*
28+
* @param \Exception $exception
29+
*/
30+
public function parseException(Exception $exception);
31+
}

test/Unit/EventTest.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,26 @@ public function getName(): string
715715
$class->toArray();
716716
}
717717

718+
public function test_exception()
719+
{
720+
721+
$event = new Event\Exception;
722+
$this->assertEventNaming($event);
723+
$this->assertEventFills($this->populateEventByMethod(clone $event));
724+
$this->assertEventFills($this->populateEventByArrayable(clone $event));
725+
$this->assertEventFills($this->populateEventByFromArray(clone $event));
726+
727+
$this->assertImportableByConvertHelper(
728+
[
729+
[ConvertHelper::camel($event->getName()) => $this->populateEventByFromArray(clone $event)->toArray()]
730+
],
731+
$event
732+
);
733+
734+
$this->analytics->addEvent($this->populateEventByFromArray(clone $event));
735+
$this->analytics->post();
736+
}
737+
718738
protected function assertEventNaming($event)
719739
{
720740
$this->assertInstanceOf(Type\EventType::class, $event);
@@ -765,6 +785,9 @@ private function populateEventByFromArray(Type\EventType $event)
765785
'page_title' => 'Home - Site',
766786
'screen_resolution' => '1920x1080',
767787
// ---
788+
'description' => "This is a short description",
789+
'fatal' => true,
790+
// ---
768791
'currency' => $this->prefill['currency'],
769792
'value' => 9.99,
770793
'affiliation' => 'affiliation',
@@ -807,6 +830,9 @@ private function populateEventByArrayable(Type\EventType $event)
807830
$event['page_title'] = 'Home - Site';
808831
$event['screen_resolution'] = '1920x1080';
809832

833+
$event['description'] = 'This is a short description';
834+
$event['fatal'] = true;
835+
810836
$event['currency'] = $this->prefill['currency'];
811837
$event['value'] = 9.99;
812838
$event['affiliation'] = 'affiliation';
@@ -843,7 +869,7 @@ private function populateEventByArrayable(Type\EventType $event)
843869
}
844870

845871
private function populateEventByMethod(
846-
Type\EventType|Group\AddPaymentInfoFacade|Group\AddShippingInfoFacade|Group\AddToCartFacade|Group\AddToWishlistFacade|Group\AnalyticsFacade|Group\BeginCheckoutFacade|Group\EarnVirtualCurrencyFacade|Group\ExportFacade|Group\GenerateLeadFacade|Group\ItemFacade|Group\JoinGroupFacade|Group\LevelUpFacade|Group\LoginFacade|Group\PostScoreFacade|Group\PurchaseFacade|Group\RefundFacade|Group\RemoveFromCartFacade|Group\SearchFacade|Group\SelectContentFacade|Group\SelectItemFacade|Group\SelectPromotionFacade|Group\ShareFacade|Group\SignUpFacade|Group\SpendVirtualCurrencyFacade|Group\UnlockAchievementFacade|Group\ViewCartFacade|Group\ViewItemFacade|Group\ViewItemListFacade|Group\ViewPromotionFacade|Group\ViewSearchResultsFacade|Group\hasItemsFacade $event
872+
Type\EventType|Group\ExceptionFacade|Group\AddPaymentInfoFacade|Group\AddShippingInfoFacade|Group\AddToCartFacade|Group\AddToWishlistFacade|Group\AnalyticsFacade|Group\BeginCheckoutFacade|Group\EarnVirtualCurrencyFacade|Group\ExportFacade|Group\GenerateLeadFacade|Group\ItemFacade|Group\JoinGroupFacade|Group\LevelUpFacade|Group\LoginFacade|Group\PostScoreFacade|Group\PurchaseFacade|Group\RefundFacade|Group\RemoveFromCartFacade|Group\SearchFacade|Group\SelectContentFacade|Group\SelectItemFacade|Group\SelectPromotionFacade|Group\ShareFacade|Group\SignUpFacade|Group\SpendVirtualCurrencyFacade|Group\UnlockAchievementFacade|Group\ViewCartFacade|Group\ViewItemFacade|Group\ViewItemListFacade|Group\ViewPromotionFacade|Group\ViewSearchResultsFacade|Group\hasItemsFacade $event
847873
) {
848874
$params = $event->getAllParams();
849875

@@ -853,6 +879,13 @@ private function populateEventByMethod(
853879
$event->setPageTitle('Home - Site');
854880
$event->setScreenResolution('1920x1080');
855881

882+
if (in_array('description', $params)) {
883+
$event->setDescription("This is a short description");
884+
if (in_array('fatal', $params)) {
885+
$event->setFatal(1);
886+
}
887+
}
888+
856889
if (in_array('currency', $params)) {
857890
$event->setCurrency($this->prefill['currency']);
858891
if (in_array('value', $params)) {

0 commit comments

Comments
 (0)