Skip to content

Commit dbf77c7

Browse files
author
Alex Westergaard
authored
Merge pull request #6 from AlexWestergaard/v1.x
Clean up dependency versioning and add strict test on event name compared to filename, fx "add_to_cart" and "AddToCart(.php)" is similar when removing underscores and lowercase. This should help prevent sending events with the wrong name and that way help prevent weird tagging.
2 parents b90ce46 + 00f1283 commit dbf77c7

File tree

5 files changed

+27
-7
lines changed

5 files changed

+27
-7
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
}
1010
},
1111
"require": {
12-
"guzzlehttp/guzzle": "^6.0"
12+
"php": "^7.0 || ^8.0",
13+
"guzzlehttp/guzzle": "^6.0 || ^7.0"
1314
},
1415
"require-dev": {
15-
"phpunit/phpunit": "^9.0"
16+
"phpunit/phpunit": "^7.0 || ^8.0 || ^9.0"
1617
}
1718
}

src/Analytics.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
use AlexWestergaard\PhpGa4\Facade;
77
use AlexWestergaard\PhpGa4\Model;
88

9+
/**
10+
* Foundation class to collect all information and events to send to Google Analytics \
11+
* Make sure to get you Measurement ID and a API Secret
12+
* @link https://analytics.google.com/ -> Settings -> Data stream -> API Secrets & Measurement Protocol -> Create
13+
*/
914
class Analytics extends Model\ToArray implements Facade\Analytics, Facade\Export
1015
{
1116
const URL_LIVE = 'https://www.google-analytics.com/mp/collect';
@@ -43,9 +48,17 @@ public function getParams(): array
4348

4449
public function getRequiredParams(): array
4550
{
46-
return [
47-
'client_id'
48-
];
51+
$return = [];
52+
53+
// Either client_id OR user_id MUST to be set
54+
if (
55+
(!isset($this->client_id) || empty($this->client_id))
56+
&& (!isset($this->user_id) || empty($this->user_id))
57+
) {
58+
$return[] = 'client_id';
59+
}
60+
61+
return $return;
4962
}
5063

5164
public function allowPersonalisedAds(bool $allow)

src/Event/RemoveFromCart.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class RemoveFromCart extends Model\Event implements Facade\RemoveFromCart
1515

1616
public function getName(): string
1717
{
18-
return 'remove_to_cart';
18+
return 'remove_from_cart';
1919
}
2020

2121
public function getParams(): array

src/Event/Search.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Search extends Model\Event implements Facade\Search
1111

1212
public function getName(): string
1313
{
14-
return 'search_term';
14+
return 'search';
1515
}
1616

1717
public function getParams(): array

test/AnalyticsTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ public function testPrebuildEvents()
106106
$params = array_unique(array_merge($event->getParams(), $required));
107107

108108
try {
109+
$this->assertEquals(
110+
strtolower(basename($file, '.php')),
111+
strtolower(strtr($event->getName(), ['_' => ''])),
112+
strtolower(basename($file, '.php')) . ' is not equal to ' . strtolower(strtr($event->getName(), ['_' => '']))
113+
);
114+
109115
if (in_array('currency', $params)) {
110116
$event->setCurrency($this->prefill['currency']);
111117
if (in_array('value', $params)) {

0 commit comments

Comments
 (0)