Skip to content

Commit c9da065

Browse files
authored
Add Consent & Cleanup (#86)
2 parents 40e3966 + 5b996e7 commit c9da065

File tree

7 files changed

+54
-34
lines changed

7 files changed

+54
-34
lines changed

src/Analytics.php

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,6 @@ public function getRequiredParams(): array
6161
return $return;
6262
}
6363

64-
public function setNonPersonalizedAds(bool $exclude)
65-
{
66-
$this->non_personalized_ads = $exclude;
67-
return $this;
68-
}
69-
7064
public function setClientId(string $id)
7165
{
7266
$this->client_id = $id;
@@ -112,7 +106,7 @@ public function addEvent(Facade\Type\EventType ...$events)
112106
return $this;
113107
}
114108

115-
public function consent()
109+
public function consent(): ConsentHelper
116110
{
117111
return $this->consent;
118112
}
@@ -130,30 +124,23 @@ public function post(): void
130124
$url = $this->debug ? Facade\Type\AnalyticsType::URL_DEBUG : Facade\Type\AnalyticsType::URL_LIVE;
131125
$url .= '?' . http_build_query(['measurement_id' => $this->measurement_id, 'api_secret' => $this->api_secret]);
132126

133-
$body = $this->toArray();
134-
array_merge_recursive(
127+
$body = array_replace_recursive(
135128
$this->toArray(),
129+
["user_properties" => $this->user_properties],
136130
["consent" => $this->consent->toArray()],
137131
);
138132

139-
$chunkUserProperties = array_chunk($this->user_properties, 25, true);
140-
$this->user_properties = [];
141-
142133
$chunkEvents = array_chunk($this->events, 25);
143-
$this->events = [];
144134

145-
$chunkMax = count($chunkEvents) > count($chunkUserProperties) ? count($chunkEvents) : count($chunkUserProperties);
135+
if (count($chunkEvents) < 1) {
136+
throw Ga4Exception::throwMissingEvents();
137+
}
146138

147-
for ($chunk = 0; $chunk < $chunkMax; $chunk++) {
148-
$body['user_properties'] = $chunkUserProperties[$chunk] ?? [];
149-
if (empty($body['user_properties'])) {
150-
unset($body['user_properties']);
151-
}
139+
$this->user_properties = [];
140+
$this->events = [];
152141

153-
$body['events'] = $chunkEvents[$chunk] ?? [];
154-
if (empty($body['events'])) {
155-
unset($body['events']);
156-
}
142+
foreach ($chunkEvents as $events) {
143+
$body['events'] = $events;
157144

158145
$kB = 1024;
159146
if (($size = mb_strlen(json_encode($body))) > ($kB * 130)) {
@@ -204,13 +191,20 @@ public static function new(string $measurementId, string $apiSecret, bool $debug
204191
* Deprecated references
205192
*/
206193

207-
/** @deprecated 1.1.1 */
194+
/** @deprecated 1.1.9 Please use `Analytics->consent->setAdPersonalizationPermission()` instead */
195+
public function setNonPersonalizedAds(bool $exclude)
196+
{
197+
$this->consent->setAdPersonalizationPermission(!$exclude);
198+
return $this;
199+
}
200+
201+
/** @deprecated 1.1.1 Please use `Analytics->consent->setAdPersonalizationPermission()` instead */
208202
public function allowPersonalisedAds(bool $allow)
209203
{
210-
$this->setNonPersonalizedAds(!$allow);
204+
$this->consent->setAdPersonalizationPermission($allow);
211205
}
212206

213-
/** @deprecated 1.1.1 */
207+
/** @deprecated 1.1.1 Please use `Analytics->setTimestampMicros()` instead */
214208
public function setTimestamp(int|float $microOrUnix)
215209
{
216210
$this->setTimestampMicros($microOrUnix);

src/Exception/Ga4Exception.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,9 @@ public static function throwRequestInvalidBody(array $msg)
8181
static::REQUEST_INVALID_BODY
8282
);
8383
}
84+
85+
public static function throwMissingEvents()
86+
{
87+
return new static("Request must include at least 1 event with a name", static::REQUEST_EMPTY_EVENTLIST);
88+
}
8489
}

src/Facade/Type/AnalyticsType.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ public function setTimestampMicros(int|float $microOrUnix);
4343
public function setNonPersonalizedAds(bool $allow);
4444

4545
/**
46-
* The user properties for the measurement
46+
* The user properties for the measurement (Up to 25 custom per project, see link)
4747
*
4848
* @var user_properties
4949
* @param AlexWestergaard\PhpGa4\Facade\Type\UserProperty $prop
50+
* @link https://support.google.com/analytics/answer/14240153
5051
*/
5152
public function addUserProperty(UserPropertyType ...$props);
5253

src/Facade/Type/Ga4ExceptionType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ interface Ga4ExceptionType
2424
const REQUEST_INVALID_BODY = 104005;
2525
const REQUEST_MISSING_MEASUREMENT_ID = 104006;
2626
const REQUEST_MISSING_API_SECRET = 104007;
27+
const REQUEST_EMPTY_EVENTLIST = 104008;
2728
}

src/Helper/ConsentHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
class ConsentHelper
66
{
7-
const GRANTED = "granted";
8-
const DENIED = "denied";
7+
const GRANTED = "GRANTED";
8+
const DENIED = "DENIED";
99

1010
private ?string $ad_user_data = null;
1111
private ?string $ad_personalization = null;

test/Unit/AnalyticsTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use AlexWestergaard\PhpGa4\Facade;
77
use AlexWestergaard\PhpGa4\Event;
88
use AlexWestergaard\PhpGa4\Analytics;
9+
use AlexWestergaard\PhpGa4\Event\Login;
910
use AlexWestergaard\PhpGa4Test\TestCase;
1011

1112
final class AnalyticsTest extends TestCase
@@ -17,7 +18,6 @@ public function test_can_configure_and_export()
1718
$this->prefill['api_secret'],
1819
$debug = true
1920
)
20-
->setNonPersonalizedAds($nonPersonalisedAds = true)
2121
->setClientId($this->prefill['client_id'])
2222
->setUserId($this->prefill['user_id'])
2323
->setTimestampMicros($time = time())
@@ -27,7 +27,6 @@ public function test_can_configure_and_export()
2727
$asArray = $analytics->toArray();
2828
$this->assertIsArray($asArray);
2929

30-
$this->assertArrayHasKey('non_personalized_ads', $asArray);
3130
$this->assertArrayHasKey('timestamp_micros', $asArray);
3231
$this->assertArrayHasKey('client_id', $asArray);
3332
$this->assertArrayHasKey('user_id', $asArray);
@@ -36,7 +35,6 @@ public function test_can_configure_and_export()
3635

3736
$timeAsMicro = $time * 1_000_000;
3837

39-
$this->assertEquals($nonPersonalisedAds, $asArray['non_personalized_ads']);
4038
$this->assertEquals($timeAsMicro, $asArray['timestamp_micros']);
4139
$this->assertEquals($this->prefill['client_id'], $asArray['client_id']);
4240
$this->assertEquals($this->prefill['user_id'], $asArray['user_id']);
@@ -46,7 +44,7 @@ public function test_can_configure_and_export()
4644

4745
public function test_can_post_to_google()
4846
{
49-
$this->assertNull($this->analytics->post());
47+
$this->assertNull($this->analytics->addEvent(Login::new())->post());
5048
}
5149

5250
public function test_converts_to_full_microtime_stamp()
@@ -68,6 +66,8 @@ public function test_throws_if_microtime_older_than_three_days()
6866

6967
public function test_exports_userproperty_to_array()
7068
{
69+
$this->analytics->addEvent(Login::new());
70+
7171
$userProperty = UserProperty::new()
7272
->setName('customer_tier')
7373
->setValue('premium');
@@ -138,7 +138,7 @@ public function test_throws_on_too_large_request_package()
138138
$userProperty->setValue($overflowValue);
139139
}
140140

141-
$this->analytics->addUserProperty($userProperty)->post();
141+
$this->analytics->addEvent(Login::new())->addUserProperty($userProperty)->post();
142142
}
143143

144144
public function test_timeasmicro_throws_exceeding_max()

test/Unit/ConsentTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,25 @@
22

33
namespace AlexWestergaard\PhpGa4Test\Unit;
44

5+
use AlexWestergaard\PhpGa4\Event\Login;
56
use AlexWestergaard\PhpGa4\Helper\ConsentHelper;
67
use AlexWestergaard\PhpGa4Test\TestCase;
78

89
final class ConsentTest extends TestCase
910
{
1011
public function test_no_consent_is_empty()
1112
{
13+
$this->analytics->addEvent(Login::new());
14+
1215
$export = $this->analytics->consent()->toArray();
1316
$this->assertIsArray($export);
1417
$this->assertCount(0, $export);
1518
}
1619

1720
public function test_consent_ad_user_data_granted()
1821
{
22+
$this->analytics->addEvent(Login::new());
23+
1924
$this->analytics->consent()->setAdUserDataPermission(true);
2025

2126
$export = $this->analytics->consent()->toArray();
@@ -28,6 +33,8 @@ public function test_consent_ad_user_data_granted()
2833

2934
public function test_consent_ad_personalization_granted()
3035
{
36+
$this->analytics->addEvent(Login::new());
37+
3138
$this->analytics->consent()->setAdPersonalizationPermission(true);
3239

3340
$export = $this->analytics->consent()->toArray();
@@ -40,6 +47,8 @@ public function test_consent_ad_personalization_granted()
4047

4148
public function test_consent_granted()
4249
{
50+
$this->analytics->addEvent(Login::new());
51+
4352
$this->analytics->consent()->setAdUserDataPermission(true);
4453
$this->analytics->consent()->setAdPersonalizationPermission(true);
4554

@@ -54,6 +63,8 @@ public function test_consent_granted()
5463

5564
public function test_consent_granted_posted()
5665
{
66+
$this->analytics->addEvent(Login::new());
67+
5768
$this->analytics->consent()->setAdUserDataPermission(true);
5869
$this->analytics->consent()->setAdPersonalizationPermission(true);
5970

@@ -69,6 +80,8 @@ public function test_consent_granted_posted()
6980

7081
public function test_consent_ad_user_data_denied()
7182
{
83+
$this->analytics->addEvent(Login::new());
84+
7285
$this->analytics->consent()->setAdUserDataPermission(false);
7386

7487
$export = $this->analytics->consent()->toArray();
@@ -81,6 +94,8 @@ public function test_consent_ad_user_data_denied()
8194

8295
public function test_consent_ad_personalization_denied()
8396
{
97+
$this->analytics->addEvent(Login::new());
98+
8499
$this->analytics->consent()->setAdPersonalizationPermission(false);
85100

86101
$export = $this->analytics->consent()->toArray();
@@ -93,6 +108,8 @@ public function test_consent_ad_personalization_denied()
93108

94109
public function test_consent_denied()
95110
{
111+
$this->analytics->addEvent(Login::new());
112+
96113
$this->analytics->consent()->setAdUserDataPermission(false);
97114
$this->analytics->consent()->setAdPersonalizationPermission(false);
98115

@@ -107,6 +124,8 @@ public function test_consent_denied()
107124

108125
public function test_consent_denied_posted()
109126
{
127+
$this->analytics->addEvent(Login::new());
128+
110129
$this->analytics->consent()->setAdUserDataPermission(false);
111130
$this->analytics->consent()->setAdPersonalizationPermission(false);
112131

0 commit comments

Comments
 (0)