Skip to content

Commit 4773171

Browse files
author
Alex Westergaard
committed
Update example of logging/queue
1 parent 89d85b0 commit 4773171

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

README.md

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ composer require alexwestergaard/php-ga4
2727
- [E-commerce](#e-commerce)
2828
- [Engagement / Gaming](#engagement--gaming)
2929
- [Frontend \& Backend Communication](#frontend--backend-communication)
30-
- [Logging / Queues](#logging--queues)
30+
- [Logging / Queue](#logging--queue)
3131
- [Frontend =\> Backend](#frontend--backend)
3232
- [Frontend](#frontend)
3333
- [Backend](#backend)
@@ -149,54 +149,65 @@ $event->setEventPage($eventPage);
149149

150150
This library is built for backend server side tracking, but you will probably trigger most events through frontend with Javascript or Websockets. There will be 2 examples, one as pure backend for logged/queued events and one for frontend to backend communication.
151151

152-
### Logging / Queues
152+
### Logging / Queue
153153

154154
```php
155-
156155
use AlexWestergaard\PhpGa4\Exception;
157156
use AlexWestergaard\PhpGa4\Analytics;
158157
use AlexWestergaard\PhpGa4\Event;
159158
use AlexWestergaard\PhpGa4\Item;
160159

161160
// require vendor/autoload.php
162161

163-
// If gtag.js, this can be the _ga or _gid cookie
164-
// This can be any kind of session identifier
165-
$session = $_COOKIE['_ga'] ?? $_COOKIE['_gid'] ?? $_COOKIE['PHPSESSID'];
166-
167-
// Render events grouped on time
168-
foreach ($groups as $time => $data) {
169-
try {
170-
$analytics = Analytics::new($measurementId, $apiSecret)
171-
->setClientId($session)
172-
->setTimestampMicros($time);
173-
174-
// load logged in user/visitor
175-
if ($auth) {
176-
// This can be any kind of identifier, readable is easier for you
177-
// Just be wary not to use GDPR sensitive information
178-
$analytics->setUserId($auth->id);
179-
}
180-
181-
$analytics->addUserParameter(...$data['userParameters']);
182-
$analytics->addEvent(...$data['events']);
162+
$visitors = getVisitorsAndEvents(); // pseudo function, make your own logic here
183163

184-
$analytics->post();
185-
} catch (Exception\Ga4Exception $exception) {
186-
// Handle exception
187-
// Exceptions might be stacked, check: $exception->getPrevious();
164+
foreach ($visitors as $collection) {
165+
// Group of events, perhaps need logic to change from json or array to event objects
166+
// Maybe its formatted well for the > ConvertHelper::parseEvents([...]) < helper
167+
$groups = $collection['events'];
168+
169+
// If gtag.js, this can be the _ga or _gid cookie
170+
// This can be any kind of session identifier
171+
// Usually derives from $_COOKIE['_ga'] or $_COOKIE['_gid'] set by GTAG.js
172+
$visitor = $collection['session_id'];
173+
174+
// load logged in user/visitor
175+
// This can be any kind of unique identifier, readable is easier for you
176+
// Just be wary not to use GDPR sensitive information
177+
$user = $collection['user_id'];
178+
179+
// Render events grouped on time (max offset is 3 days from NOW)
180+
foreach ($groups as $time => $data) {
181+
try {
182+
$analytics = Analytics::new($measurementId, $apiSecret)
183+
->setClientId($visitor)
184+
->setTimestampMicros($time);
185+
186+
if ($user !== null) {
187+
$analytics->setUserId($user);
188+
}
189+
190+
$analytics->addUserParameter(...$data['userParameters']); // pseudo logic for adding user parameters
191+
$analytics->addEvent(...$data['events']); // pseudo logic for adding events
192+
193+
$analytics->post(); // send events to Google Analytics
194+
} catch (Exception\Ga4Exception $exception) {
195+
// Handle exception
196+
// Exceptions might be stacked, check: $exception->getPrevious();
197+
}
188198
}
189199
}
200+
190201
```
191202

192203
### Frontend => Backend
193204

194205
#### Frontend
195206

196207
```js
197-
// array<array<eventName,eventParams>>
208+
// array< array< eventName, array<eventParams> > >
198209
axios.post(
199-
'/api/ga4',
210+
'/your-api-endpoint/ga4-event-receiver',
200211
[
201212
// Note each event is its own object inside an array as
202213
// this allows to pass the same event type multiple times

0 commit comments

Comments
 (0)