Skip to content

Commit efb75e8

Browse files
authored
Update readme & examples (#59)
2 parents 89d85b0 + 8b34ce5 commit efb75e8

File tree

1 file changed

+46
-31
lines changed

1 file changed

+46
-31
lines changed

README.md

Lines changed: 46 additions & 31 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)
@@ -67,10 +67,14 @@ $analytics = Analytics::new(
6767

6868
### Data flow
6969

70+
`session_id` > Google Analytics does not specify a required type of **session or user id**. You are free to use any kind of **unique identifier** you want; the catch, however, is that Google Analytics populates some internal data with `gtag.js`, that is then referenced to their `_ga` cookie session id. Just be aware that `gtag.js` is using *client-side Javascript* and can therefore have some **GDPR complications** as requests back to Google Analytics contains client information; such as their IP Address.
71+
7072
1. Acquire proper GDPR Consent
7173
2. Client/GTAG.js sends session_start and first_visit to GA4
72-
3. GA4 sends _ga and _gid cookies to Client/GTAG.js
73-
4. Server uses _ga to populate events
74+
3. GA4 sends _ga and _gid cookies back to Client/GTAG.js
75+
4. Server uses _ga (or _gid; or your unique session_id) to populate events
76+
77+
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.
7478

7579
### Layers
7680

@@ -149,54 +153,65 @@ $event->setEventPage($eventPage);
149153

150154
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.
151155

152-
### Logging / Queues
156+
### Logging / Queue
153157

154158
```php
155-
156159
use AlexWestergaard\PhpGa4\Exception;
157160
use AlexWestergaard\PhpGa4\Analytics;
158161
use AlexWestergaard\PhpGa4\Event;
159162
use AlexWestergaard\PhpGa4\Item;
160163

161164
// require vendor/autoload.php
162165

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']);
166+
$visitors = getVisitorsAndEvents(); // pseudo function, make your own logic here
183167

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

192207
### Frontend => Backend
193208

194209
#### Frontend
195210

196211
```js
197-
// array<array<eventName,eventParams>>
212+
// array< array< eventName, array<eventParams> > >
198213
axios.post(
199-
'/api/ga4',
214+
'/your-api-endpoint/ga4-event-receiver',
200215
[
201216
// Note each event is its own object inside an array as
202217
// this allows to pass the same event type multiple times

0 commit comments

Comments
 (0)