Skip to content

Commit 8700c3f

Browse files
authored
Feature: Add Consent (#87)
2 parents c9da065 + 6018dbd commit 8700c3f

File tree

1 file changed

+40
-28
lines changed

1 file changed

+40
-28
lines changed

README.md

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,26 @@ $analytics = Analytics::new(
6565
api_secret: 'xYzzX_xYzzXzxyZxX',
6666
debug: true|false
6767
);
68+
69+
// You can set CONSENT here if not done through the gtat.js
70+
// Read full docs here: https://support.google.com/tagmanager/answer/13802165
71+
$consent = $analytics->consent(); // returns a consent handler
72+
73+
// Sets consent for sending user data from the request's events
74+
// and user properties to Google for advertising purposes.
75+
$consent->setAdUserDataPermission();
76+
$consent->getAdUserDataPermission();
77+
$consent->clearAdUserDataPermission();
78+
79+
// Sets consent for personalized advertising for the user.
80+
$consent->setAdPersonalizationPermission();
81+
$consent->getAdPersonalizationPermission();
82+
$consent->clearAdPersonalizationPermission();
6883
```
6984

7085
### Data flow
7186

72-
`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.
87+
`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.
7388

7489
1. Acquire proper GDPR Consent
7590
2. Client/GTAG.js sends session_start and first_visit to GA4
@@ -143,7 +158,7 @@ $event->setEventPage($eventPage);
143158
![badge](https://shields.io/badge/AddShippingInfo-informational)
144159
![badge](https://shields.io/badge/Purchase-informational)
145160
![badge](https://shields.io/badge/Refund-informational)
146-
161+
147162
### Engagement / Gaming
148163

149164
![badge](https://shields.io/badge/EarnVirtualCurrency-informational)
@@ -175,12 +190,12 @@ foreach ($visitors as $collection) {
175190
// Group of events, perhaps need logic to change from json or array to event objects
176191
// Maybe its formatted well for the > ConvertHelper::parseEvents([...]) < helper
177192
$groups = $collection['events'];
178-
193+
179194
// If gtag.js, this can be the _ga or _gid cookie
180195
// This can be any kind of session identifier
181196
// Usually derives from $_COOKIE['_ga'] or $_COOKIE['_gid'] set by GTAG.js
182197
$visitor = $collection['session_id'];
183-
198+
184199
// load logged in user/visitor
185200
// This can be any kind of unique identifier, readable is easier for you
186201
// Just be wary not to use GDPR sensitive information
@@ -192,14 +207,14 @@ foreach ($visitors as $collection) {
192207
$analytics = Analytics::new($measurementId, $apiSecret)
193208
->setClientId($visitor)
194209
->setTimestampMicros($time);
195-
210+
196211
if ($user !== null) {
197212
$analytics->setUserId($user);
198213
}
199-
214+
200215
$analytics->addUserParameter(...$data['userParameters']); // pseudo logic for adding user parameters
201216
$analytics->addEvent(...$data['events']); // pseudo logic for adding events
202-
217+
203218
$analytics->post(); // send events to Google Analytics
204219
} catch (Exception\Ga4Exception $exception) {
205220
// Handle exception
@@ -216,27 +231,24 @@ foreach ($visitors as $collection) {
216231

217232
```js
218233
// array< array< eventName, array<eventParams> > >
219-
axios.post(
220-
'/your-api-endpoint/ga4-event-receiver',
221-
[
222-
// Note each event is its own object inside an array as
223-
// this allows to pass the same event type multiple times
234+
axios.post("/your-api-endpoint/ga4-event-receiver", [
235+
// Note each event is its own object inside an array as
236+
// this allows to pass the same event type multiple times
237+
{
238+
addToCart: {
239+
currency: "EUR",
240+
value: 13.37,
241+
items: [
224242
{
225-
addToCart: {
226-
currency: 'EUR',
227-
value: 13.37,
228-
items: [
229-
{
230-
'item_id': 1,
231-
'item_name': 'Cup',
232-
'price': 13.37,
233-
'quantity': 1
234-
}
235-
]
236-
}
237-
}
238-
]
239-
)
243+
item_id: 1,
244+
item_name: "Cup",
245+
price: 13.37,
246+
quantity: 1,
247+
},
248+
],
249+
},
250+
},
251+
]);
240252
```
241253

242254
#### Backend
@@ -274,7 +286,7 @@ class ExampleEvent extends AlexWestergaard\PhpGa4\Helper\EventHelper
274286
// variables should be nullable as unset() will set variable as null
275287
protected null|mixed $my_variable;
276288
protected null|mixed $my_required_variable;
277-
289+
278290
// Arrays should always be instanciated empty
279291
protected array $my_array = [];
280292

0 commit comments

Comments
 (0)