Skip to content

Commit 6b766a2

Browse files
authored
Update EventName validator (#70)
- Will check if event names starts or ends with invalid character - Minor changes to readme
2 parents eed5a5f + 0b4137f commit 6b766a2

File tree

3 files changed

+59
-16
lines changed

3 files changed

+59
-16
lines changed

README.md

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,25 +73,29 @@ $analytics = Analytics::new(
7373

7474
1. Acquire proper GDPR Consent
7575
2. Client/GTAG.js sends session_start and first_visit to GA4
76-
3. GA4 sends \_ga and \_gid cookies back to Client/GTAG.js
77-
4. Server uses \_ga (or \_gid; or your unique session_id) to populate events
76+
3. GA4 sends `_ga` and `_gid` cookies back to Client/GTAG.js
77+
4. Server uses `_ga` (or `_gid`; or your unique session_id) to populate events
7878

7979
Note: It is entirely possible to push events to backend without acquiring the session cookies from Google Analytics; you will however lose information bundled inside the `GTAG.js` request if you do not figure out how to push that via backend too.
8080

8181
### Layers
8282

83-
The code is following 3 layers, that should be considered.
83+
The code is following 3 layers that should be considered; 5 layers at max.
8484

8585
```txt
8686
Analytics [
87-
Event [
88-
Event Parameters
89-
? Items [
90-
Item Parameters
91-
]
87+
Events [
88+
Event {
89+
Parameters
90+
? Items [
91+
Item Parameters
92+
]
93+
}
9294
]
9395
User Properties [
94-
Properties {Key, Value} pairs
96+
Properties {
97+
Key: Value
98+
}
9599
]
96100
]
97101
```
@@ -259,7 +263,8 @@ try {
259263

260264
## Custom Events
261265

262-
You can build your own custom events. All you need is to implement and fullfill the `AlexWestergaard\PhpGa4\Facade\Type\EventType` facade/interface. If you want ease of life features, then you can extend your event from `AlexWestergaard\PhpGa4\Helper\EventHelper` and overwrite as you see fit.
266+
You can build your own custom events. All you need is to implement and fullfill the `AlexWestergaard\PhpGa4\Facade\Type\EventType` facade/interface.
267+
If you want ease of life features, then you can extend your event from `AlexWestergaard\PhpGa4\Helper\EventHelper` and overwrite as you see fit.
263268

264269
```php
265270

@@ -309,17 +314,19 @@ class ExampleEvent extends AlexWestergaard\PhpGa4\Helper\EventHelper
309314

310315
## Debug
311316

312-
Measurement protocol for GA4 has a debug functionality that can be enabled with the `debug` parameter in the constructor.
317+
Measurement protocol for GA4 has debug functionality that can be enabled with the `debug` parameter in the Analytics constructor.
313318

314319
```php
315320
$analytics = Analytics::new(
316321
measurement_id: 'G-XXXXXXXX',
317322
api_secret: 'xYzzX_xYzzXzxyZxX',
318-
debug: true
323+
debug: true // default: false
319324
);
320325
```
321326

322-
Once set, events are sent to `https://www.google-analytics.com/debug/mp/collect` which will return a validation response such as
327+
When `Debug` is enabled then events are sent to `https://www.google-analytics.com/debug/mp/collect` where issues will be caught with
328+
[GA4Exception](https://github.com/aawnu/php-ga4/blob/master/src/Exception/Ga4Exception.php) (Be aware of `$exception->getPrevious()` stacks);
329+
such response will look as follows:
323330

324331
```json
325332
{
@@ -333,12 +340,12 @@ Once set, events are sent to `https://www.google-analytics.com/debug/mp/collect`
333340
}
334341
```
335342

336-
This library already validates that events are properly formatted and it is unlikely, you will experience any validation messages.
343+
Notice: This library already validates that events are properly formatted when added to analytics (`$analytics->addEvent(...)`).
337344

338345
Two important points:
339346

340347
- Events sent to the Validation Server will not show up in reports.
341-
- There is no way for events sent through measurement protocol to show up in the `debugView` in Google Analytics Admin.
348+
- There is no way for events sent through measurement protocol (Server Side) to show up in the `debugView` in Google Analytics Admin.
342349

343350
## Additional information
344351

src/Helper/EventHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function toArray(): array
7373
throw Ga4EventException::throwNameMissing();
7474
} elseif (strlen($name) > 40) {
7575
throw Ga4EventException::throwNameTooLong();
76-
} elseif (preg_match('/[^\w\d\-]/', $name)) {
76+
} elseif (preg_match('/[^\w\d\-]|^\-|\-$/', $name)) {
7777
throw Ga4EventException::throwNameInvalid();
7878
} elseif (in_array($name, EventType::RESERVED_NAMES) && !($this instanceof GtmEventType)) {
7979
throw Ga4EventException::throwNameReserved($name);

test/Unit/EventTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,42 @@ public function getName(): string
679679
$class->toArray();
680680
}
681681

682+
public function test_throw_name_invalid_starting_line()
683+
{
684+
$mock = new class extends Event\Refund
685+
{
686+
public function getName(): string
687+
{
688+
return '-almost-valid-name';
689+
}
690+
};
691+
692+
$class = $mock::new()->setTransactionId(1);
693+
694+
$this->expectException(Ga4EventException::class);
695+
$this->expectExceptionCode(Ga4Exception::EVENT_NAME_INVALID);
696+
697+
$class->toArray();
698+
}
699+
700+
public function test_throw_name_invalid_ending_line()
701+
{
702+
$mock = new class extends Event\Refund
703+
{
704+
public function getName(): string
705+
{
706+
return 'almost-valid-name-';
707+
}
708+
};
709+
710+
$class = $mock::new()->setTransactionId(1);
711+
712+
$this->expectException(Ga4EventException::class);
713+
$this->expectExceptionCode(Ga4Exception::EVENT_NAME_INVALID);
714+
715+
$class->toArray();
716+
}
717+
682718
public function test_throw_name_reserved()
683719
{
684720
$mock = new class extends Event\Refund

0 commit comments

Comments
 (0)