Skip to content

Commit 7e64dd5

Browse files
author
Alex Westergaard
committed
Manipulate data send to GA to fit js objects rather than arrays
1 parent 0ef9772 commit 7e64dd5

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

src/Analytics.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
class Analytics extends Helper\IOHelper implements Facade\Type\AnalyticsType
1111
{
12+
private Guzzle $guzzle;
13+
1214
protected null|bool $non_personalized_ads = false;
1315
protected null|int $timestamp_micros;
1416
protected null|string $client_id;
@@ -22,6 +24,7 @@ public function __construct(
2224
private bool $debug = false
2325
) {
2426
parent::__construct();
27+
$this->guzzle = new Guzzle();
2528
}
2629

2730
public function getParams(): array
@@ -107,6 +110,7 @@ public function post(): void
107110
if (empty($this->measurement_id)) {
108111
throw Ga4Exception::throwMissingMeasurementId();
109112
}
113+
110114
if (empty($this->api_secret)) {
111115
throw Ga4Exception::throwMissingApiSecret();
112116
}
@@ -126,19 +130,30 @@ public function post(): void
126130

127131
for ($chunk = 0; $chunk < $chunkMax; $chunk++) {
128132
$body['user_properties'] = $chunkUserProperties[$chunk] ?? [];
129-
if (empty($body['user_properties'])) unset($body['user_properties']);
133+
if (empty($body['user_properties'])) {
134+
unset($body['user_properties']);
135+
}
130136

131137
$body['events'] = $chunkEvents[$chunk] ?? [];
132-
if (empty($body['events'])) unset($body['events']);
138+
if (empty($body['events'])) {
139+
unset($body['events']);
140+
}
133141

134142
$kB = 1024;
135143
if (($size = mb_strlen(json_encode($body))) > ($kB * 130)) {
136144
Ga4Exception::throwRequestTooLarge(intval($size / $kB));
137145
continue;
138146
}
139147

140-
$guzzle = new Guzzle();
141-
$res = $guzzle->request('POST', $url, ['json' => $body]);
148+
$jsonBody = json_encode($body);
149+
$jsonBody = strtr($jsonBody, [':[]' => ':{}']);
150+
151+
$res = $this->guzzle->request('POST', $url, [
152+
'headers' => [
153+
'content-type' => 'application/json;charset=utf-8'
154+
],
155+
'body' => $jsonBody,
156+
]);
142157

143158
if (!in_array(($code = $res?->getStatusCode() ?? 0), Facade\Type\AnalyticsType::ACCEPT_RESPONSE_HEADERS)) {
144159
Ga4Exception::throwRequestWrongResponceCode($code);
@@ -147,10 +162,10 @@ public function post(): void
147162
if ($code !== 204) {
148163
$callback = @json_decode($res->getBody()->getContents(), true);
149164

150-
if (empty($callback)) {
151-
Ga4Exception::throwRequestEmptyResponse();
152-
} elseif (json_last_error() != JSON_ERROR_NONE) {
165+
if (json_last_error() != JSON_ERROR_NONE) {
153166
Ga4Exception::throwRequestInvalidResponse();
167+
} elseif (empty($callback)) {
168+
Ga4Exception::throwRequestEmptyResponse();
154169
} elseif (!empty($callback['validationMessages'])) {
155170
foreach ($callback['validationMessages'] as $msg) {
156171
Ga4Exception::throwRequestInvalidBody($msg);

0 commit comments

Comments
 (0)