Skip to content

Commit 2343b6a

Browse files
authored
Enable debug mode for individual events on live (#25)
2 parents 3a01297 + 8b6e877 commit 2343b6a

File tree

2 files changed

+53
-30
lines changed

2 files changed

+53
-30
lines changed

src/Model/Event.php

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,20 @@
77

88
abstract class Event extends ToArray implements Facade\Export
99
{
10+
private $debug = false;
11+
1012
/**
1113
* Return the name of the event
1214
*
1315
* @return string
1416
*/
1517
abstract public function getName(): string;
1618

17-
public static function fromArray(array $params = [])
19+
public function debug($on = true)
1820
{
19-
$event = static::new();
20-
21-
$insertables = array_unique(array_merge($event->getParams(), $event->getRequiredParams()));
22-
23-
foreach ($insertables as $insertable) {
24-
if (!in_array($insertable, array_keys($params)) || is_null($param = $params[$insertable])) {
25-
continue;
26-
}
27-
28-
$callableName = implode('', array_map('ucfirst', explode('_', $insertable)));
29-
30-
if (is_array($param)) {
31-
$callableName = substr($callableName, -1) === 's' ? substr($callableName, 0, -1) : $callableName;
32-
foreach ($param as $paramRow) {
33-
if (method_exists($event, ($method = 'add' . $callableName))) {
34-
$event->$method($paramRow);
35-
} elseif (method_exists($event, ($method = 'set' . $callableName))) {
36-
$event->$method($paramRow);
37-
}
38-
}
39-
} else {
40-
if (method_exists($event, ($method = 'add' . $callableName))) {
41-
$event->$method($param);
42-
} elseif (method_exists($event, ($method = 'set' . $callableName))) {
43-
$event->$method($param);
44-
}
45-
}
46-
}
47-
return $event;
21+
$this->debug = boolval($on);
22+
23+
return $this;
4824
}
4925

5026
/**
@@ -96,6 +72,10 @@ public function toArray(bool $isParent = false): array
9672

9773
$parent = parent::toArray(true);
9874

75+
if ($this->debug) {
76+
$parent['debug_mode'] = true;
77+
}
78+
9979
if (!$isParent && GA4Exception::hasStack()) {
10080
throw GA4Exception::getFinalStack();
10181
}
@@ -107,6 +87,39 @@ public function toArray(bool $isParent = false): array
10787
return $return;
10888
}
10989

90+
public static function fromArray(array $params = [])
91+
{
92+
$event = static::new();
93+
94+
$insertables = array_unique(array_merge($event->getParams(), $event->getRequiredParams()));
95+
96+
foreach ($insertables as $insertable) {
97+
if (!in_array($insertable, array_keys($params)) || is_null($param = $params[$insertable])) {
98+
continue;
99+
}
100+
101+
$callableName = implode('', array_map('ucfirst', explode('_', $insertable)));
102+
103+
if (is_array($param)) {
104+
$callableName = substr($callableName, -1) === 's' ? substr($callableName, 0, -1) : $callableName;
105+
foreach ($param as $paramRow) {
106+
if (method_exists($event, ($method = 'add' . $callableName))) {
107+
$event->$method($paramRow);
108+
} elseif (method_exists($event, ($method = 'set' . $callableName))) {
109+
$event->$method($paramRow);
110+
}
111+
}
112+
} else {
113+
if (method_exists($event, ($method = 'add' . $callableName))) {
114+
$event->$method($param);
115+
} elseif (method_exists($event, ($method = 'set' . $callableName))) {
116+
$event->$method($param);
117+
}
118+
}
119+
}
120+
return $event;
121+
}
122+
110123
public static function new()
111124
{
112125
return new static();

test/AnalyticsTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,16 @@ public function testPrebuildEvents()
267267
$this->assertTrue($this->analytics->post());
268268
}
269269

270+
public function testEventCanAddDebugParameter()
271+
{
272+
$this->prepareSituation();
273+
274+
$event = Event\Refund::new()->setTransactionId(1)->addItem($this->item)->debug()->toArray();
275+
276+
$this->assertArrayHasKey('debug_mode', $event['params']);
277+
$this->assertTrue($event['params']['debug_mode']);
278+
}
279+
270280
public function testBuildEventFromArray()
271281
{
272282
$this->prepareSituation();

0 commit comments

Comments
 (0)