Skip to content

Commit fee8a26

Browse files
authored
1 parent 3eeefb9 commit fee8a26

File tree

6 files changed

+181
-52
lines changed

6 files changed

+181
-52
lines changed

src/Event/PageView.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace AlexWestergaard\PhpGa4\Event;
4+
5+
use AlexWestergaard\PhpGa4\Helper\EventMainHelper;
6+
use AlexWestergaard\PhpGa4\Facade;
7+
8+
class PageView extends EventMainHelper implements Facade\Group\PageViewFacade
9+
{
10+
protected null|string $page_title;
11+
protected null|string $page_location;
12+
13+
public function getName(): string
14+
{
15+
return 'page_view';
16+
}
17+
18+
public function getParams(): array
19+
{
20+
return [
21+
'page_title',
22+
'page_location',
23+
];
24+
}
25+
26+
public function getRequiredParams(): array
27+
{
28+
return [];
29+
}
30+
31+
public function setPageTitle(null|string $title)
32+
{
33+
$this->page_title = $title;
34+
return $this;
35+
}
36+
37+
public function setPageLocation(null|string $url)
38+
{
39+
$this->page_location = $url;
40+
return $this;
41+
}
42+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace AlexWestergaard\PhpGa4\Facade\Group;
4+
5+
interface PageViewFacade
6+
{
7+
/**
8+
* The title of the page.
9+
*
10+
* @var page_title
11+
* @param string $title eg. "Page - Example"
12+
*/
13+
public function setPageTitle(null|string $title);
14+
15+
/**
16+
* The url of the page.
17+
*
18+
* @var page_location
19+
* @param string $url eg. https://www.example.com/page?query=value
20+
*/
21+
public function setPageLocation(null|string $url);
22+
}

src/Facade/Type/EventType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace AlexWestergaard\PhpGa4\Facade\Type;
44

5-
interface EventType extends IOType, DefaultEventParamsType
5+
interface EventType extends IOType
66
{
77
/** @return array<int,string> */
88
public const RESERVED_NAMES = [

src/Helper/EventHelper.php

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,16 @@
22

33
namespace AlexWestergaard\PhpGa4\Helper;
44

5-
use AlexWestergaard\PhpGa4\Facade\Type\GtmEventType;
6-
use AlexWestergaard\PhpGa4\Facade\Type\EventType;
75
use AlexWestergaard\PhpGa4\Facade\Type\DefaultEventParamsType;
8-
use AlexWestergaard\PhpGa4\Exception\Ga4EventException;
96

10-
abstract class EventHelper extends IOHelper implements EventType
7+
abstract class EventHelper extends EventMainHelper implements DefaultEventParamsType
118
{
129
protected null|string $language;
1310
protected null|string $page_location;
1411
protected null|string $page_referrer;
1512
protected null|string $page_title;
1613
protected null|string $screen_resolution;
1714

18-
protected null|string $session_id;
19-
protected null|int $engagement_time_msec;
20-
2115
protected array $campaign = [];
2216

2317
public function setLanguage(string $lang)
@@ -63,41 +57,9 @@ public function setEventPage(DefaultEventParamsType $page)
6357
return $this;
6458
}
6559

66-
public function setSessionId(string $id)
67-
{
68-
$this->session_id = $id;
69-
return $this;
70-
}
71-
72-
public function setEngagementTimeMSec(int $msec)
73-
{
74-
$this->engagement_time_msec = $msec;
75-
return $this;
76-
}
77-
7860
public function toArray(): array
7961
{
80-
$return = [];
81-
82-
if (!method_exists($this, 'getName')) {
83-
throw Ga4EventException::throwNameMissing();
84-
} else {
85-
$name = $this->getName();
86-
87-
if (empty($name)) {
88-
throw Ga4EventException::throwNameMissing();
89-
} elseif (strlen($name) > 40) {
90-
throw Ga4EventException::throwNameTooLong();
91-
} elseif (preg_match('/[^\w\d\-]|^\-|\-$/', $name)) {
92-
throw Ga4EventException::throwNameInvalid();
93-
} elseif (in_array($name, EventType::RESERVED_NAMES) && !($this instanceof GtmEventType)) {
94-
throw Ga4EventException::throwNameReserved($name);
95-
} else {
96-
$return['name'] = $name;
97-
}
98-
}
99-
100-
$return['params'] = parent::toArray();
62+
$return = parent::toArray();
10163

10264
if (!empty($this->campaign)) {
10365
$return['params'] = array_replace(
@@ -112,6 +74,7 @@ public function toArray(): array
11274
public function getAllParams(): array
11375
{
11476
return array_unique(array_merge(
77+
parent::getAllParams(),
11578
[
11679
'language',
11780
'page_location',
@@ -120,7 +83,7 @@ public function getAllParams(): array
12083
'screen_resolution',
12184
],
12285
$this->getParams(),
123-
$this->getRequiredParams()
86+
$this->getRequiredParams(),
12487
));
12588
}
12689

src/Helper/EventMainHelper.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace AlexWestergaard\PhpGa4\Helper;
4+
5+
use AlexWestergaard\PhpGa4\Facade\Type\GtmEventType;
6+
use AlexWestergaard\PhpGa4\Facade\Type\EventType;
7+
use AlexWestergaard\PhpGa4\Exception\Ga4EventException;
8+
9+
abstract class EventMainHelper extends IOHelper implements EventType
10+
{
11+
protected null|string $session_id;
12+
protected null|int $engagement_time_msec;
13+
14+
public function setSessionId(string $id)
15+
{
16+
$this->session_id = $id;
17+
return $this;
18+
}
19+
20+
public function setEngagementTimeMSec(int $msec)
21+
{
22+
$this->engagement_time_msec = $msec;
23+
return $this;
24+
}
25+
26+
public function toArray(): array
27+
{
28+
$return = [];
29+
30+
if (!method_exists($this, 'getName')) {
31+
throw Ga4EventException::throwNameMissing();
32+
} else {
33+
$name = $this->getName();
34+
35+
if (empty($name)) {
36+
throw Ga4EventException::throwNameMissing();
37+
} elseif (strlen($name) > 40) {
38+
throw Ga4EventException::throwNameTooLong();
39+
} elseif (preg_match('/[^\w\d\-]|^\-|\-$/', $name)) {
40+
throw Ga4EventException::throwNameInvalid();
41+
} elseif (in_array($name, EventType::RESERVED_NAMES) && !($this instanceof GtmEventType)) {
42+
throw Ga4EventException::throwNameReserved($name);
43+
} else {
44+
$return['name'] = $name;
45+
}
46+
}
47+
48+
$return['params'] = parent::toArray();
49+
50+
return $return;
51+
}
52+
53+
public function getAllParams(): array
54+
{
55+
return array_unique(array_merge(
56+
$this->getParams(),
57+
$this->getRequiredParams()
58+
));
59+
}
60+
61+
public static function new(): static
62+
{
63+
return new static();
64+
}
65+
}

test/Unit/EventTest.php

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,37 @@
44

55
use ReflectionClass;
66
use AlexWestergaard\PhpGa4\Helper\EventHelper;
7+
use AlexWestergaard\PhpGa4\Helper\EventMainHelper;
78
use AlexWestergaard\PhpGa4\Helper\ConvertHelper;
89
use AlexWestergaard\PhpGa4\Facade\Type;
910
use AlexWestergaard\PhpGa4\Facade\Group;
1011
use AlexWestergaard\PhpGa4\Exception\Ga4Exception;
1112
use AlexWestergaard\PhpGa4\Exception\Ga4EventException;
1213
use AlexWestergaard\PhpGa4\Event;
13-
use AlexWestergaard\PhpGa4\Campaign;
1414
use AlexWestergaard\PhpGa4Test\MeasurementTestCase;
1515

1616
final class EventTest extends MeasurementTestCase
1717
{
18+
public function test_page_view()
19+
{
20+
$event = new Event\PageView;
21+
22+
$this->assertEventNaming($event);
23+
$this->assertEventFills($this->populateEventByMethod(clone $event));
24+
$this->assertEventFills($this->populateEventByArrayable(clone $event));
25+
$this->assertEventFills($this->populateEventByFromArray(clone $event));
26+
27+
$this->assertImportableByConvertHelper(
28+
[
29+
[ConvertHelper::camel($event->getName()) => $this->populateEventByFromArray(clone $event)->toArray()]
30+
],
31+
$event
32+
);
33+
34+
$this->analytics->addEvent($this->populateEventByFromArray(clone $event));
35+
$this->analytics->post();
36+
}
37+
1838
public function test_addpaymentinfo()
1939
{
2040
$event = new Event\AddPaymentInfo;
@@ -738,7 +758,7 @@ public function test_exception()
738758
protected function assertEventNaming($event)
739759
{
740760
$this->assertInstanceOf(Type\EventType::class, $event);
741-
$this->assertInstanceOf(EventHelper::class, $event);
761+
$this->assertInstanceOf(EventMainHelper::class, $event);
742762

743763
$reflection = new ReflectionClass($event);
744764
$filename = $reflection->getFileName();
@@ -825,9 +845,9 @@ private function populateEventByFromArray(Type\EventType $event)
825845
private function populateEventByArrayable(Type\EventType $event)
826846
{
827847
$event['language'] = 'en-US';
828-
$event['page_location'] = '/';
848+
$event['page_location'] = 'https://example.com/';
829849
$event['page_referrer'] = 'https://example.com/';
830-
$event['page_title'] = 'Home - Site';
850+
$event['page_title'] = 'Home - Example';
831851
$event['screen_resolution'] = '1920x1080';
832852

833853
$event['description'] = 'This is a short description';
@@ -869,15 +889,32 @@ private function populateEventByArrayable(Type\EventType $event)
869889
}
870890

871891
private function populateEventByMethod(
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
892+
Type\EventType|Group\ExceptionFacade|Group\PageViewFacade|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
873893
) {
874894
$params = $event->getAllParams();
875895

876-
$event->setLanguage('en-US');
877-
$event->setPageLocation('/');
878-
$event->setPageReferrer('https://example.com/');
879-
$event->setPageTitle('Home - Site');
880-
$event->setScreenResolution('1920x1080');
896+
if ($event instanceof EventHelper) {
897+
$event->setLanguage('en-US');
898+
$event->setPageLocation('https://example.com/');
899+
$event->setPageReferrer('https://example.com/');
900+
$event->setPageTitle('Home - Example');
901+
$event->setScreenResolution('1920x1080');
902+
}
903+
904+
if (in_array('page_title', $params)) {
905+
$event->setPageTitle('Home - Example');
906+
}
907+
908+
if (in_array('page_title', $params)) {
909+
$event->setPageLocation('https://example.com/');
910+
}
911+
912+
if (in_array('description', $params)) {
913+
$event->setDescription("This is a short description");
914+
if (in_array('fatal', $params)) {
915+
$event->setFatal(1);
916+
}
917+
}
881918

882919
if (in_array('description', $params)) {
883920
$event->setDescription("This is a short description");

0 commit comments

Comments
 (0)