Skip to content

Commit 80c0641

Browse files
author
Alex Westergaard
committed
Add default event parameters
1 parent e1887a7 commit 80c0641

File tree

4 files changed

+87
-7
lines changed

4 files changed

+87
-7
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace AlexWestergaard\PhpGa4\Facade\Type;
4+
5+
interface DefaultEventParams
6+
{
7+
public function setLanguage(string $lang);
8+
public function setPageLocation(string $url);
9+
public function setPageReferrer(string $url);
10+
public function setPageTitle(string $title);
11+
public function setScreenResolution(string $wxh);
12+
13+
public function toArray(): array;
14+
}

src/Facade/Type/Event.php

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

33
namespace AlexWestergaard\PhpGa4\Facade\Type;
44

5-
interface Event extends IO
5+
interface Event extends IO, DefaultEventParams
66
{
77
/** @return array<int,string> */
88
public const RESERVED_NAMES = [
@@ -36,10 +36,4 @@ interface Event extends IO
3636
* @return string snake_case
3737
*/
3838
public function getName(): string;
39-
40-
public function setLanguage(string $lang);
41-
public function setPageLocation(string $url);
42-
public function setPageReferrer(string $url);
43-
public function setPageTitle(string $title);
44-
public function setScreenResolution(string $wxh);
4539
}

src/Helper/AbstractEvent.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace AlexWestergaard\PhpGa4\Helper;
44

55
use AlexWestergaard\PhpGa4\Facade\Type\Event;
6+
use AlexWestergaard\PhpGa4\Facade\Type\DefaultEventParams;
67
use AlexWestergaard\PhpGa4\Exception\Ga4EventException;
78

89
abstract class AbstractEvent extends AbstractIO implements Event
@@ -43,6 +44,19 @@ public function setScreenResolution(string $wxh)
4344
return $this;
4445
}
4546

47+
public function setEventPage(DefaultEventParams $page)
48+
{
49+
$args = $page->toArray();
50+
51+
$this->language = $args['language'] ?? null;
52+
$this->page_location = $args['page_location'] ?? null;
53+
$this->page_referrer = $args['page_referrer'] ?? null;
54+
$this->page_title = $args['page_title'] ?? null;
55+
$this->screen_resolution = $args['screen_resolution'] ?? null;
56+
57+
return $this;
58+
}
59+
4660
public function toArray(): array
4761
{
4862
$return = [];

src/Helper/DefaultEventParams.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace AlexWestergaard\PhpGa4\Helper;
4+
5+
use AlexWestergaard\PhpGa4\Facade\Type\DefaultEventParams as TypeDefaultEventParams;
6+
7+
class DefaultEventParams implements TypeDefaultEventParams
8+
{
9+
public function __construct(
10+
protected null|string $language = null,
11+
protected null|string $page_location = null,
12+
protected null|string $page_referrer = null,
13+
protected null|string $page_title = null,
14+
protected null|string $screen_resolution = null
15+
) {
16+
}
17+
18+
public function setLanguage(string $lang)
19+
{
20+
$this->language = $lang;
21+
return $this;
22+
}
23+
24+
public function setPageLocation(string $url)
25+
{
26+
$this->page_location = $url;
27+
return $this;
28+
}
29+
30+
public function setPageReferrer(string $url)
31+
{
32+
$this->page_referrer = $url;
33+
return $this;
34+
}
35+
36+
public function setPageTitle(string $title)
37+
{
38+
$this->page_title = $title;
39+
return $this;
40+
}
41+
42+
public function setScreenResolution(string $wxh)
43+
{
44+
$this->screen_resolution = $wxh;
45+
return $this;
46+
}
47+
48+
public function toArray(): array
49+
{
50+
return [
51+
'language' => $this->language,
52+
'page_location' => $this->page_location,
53+
'page_referrer' => $this->page_referrer,
54+
'page_title' => $this->page_title,
55+
'screen_resolution' => $this->screen_resolution,
56+
];
57+
}
58+
}

0 commit comments

Comments
 (0)