Skip to content

Commit 5daf51a

Browse files
authored
Clean up for Version 2.0 (#101)
2 parents a5148fa + 20d1235 commit 5daf51a

17 files changed

+61
-259
lines changed

.github/dependabot.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
version: 2
22
updates:
3-
43
# Maintain dependencies for GitHub Actions
54
- package-ecosystem: "github-actions"
65
directory: "/"
@@ -9,7 +8,7 @@ updates:
98
day: "monday"
109
assignees:
1110
- "octocat"
12-
11+
1312
# Maintain dependencies for Composer
1413
- package-ecosystem: "composer"
1514
directory: "/"

README.md

Lines changed: 9 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Google Analytics 4 Server-Side PHP Package
2+
13
[![Version](https://img.shields.io/packagist/v/alexwestergaard/php-ga4?color=blue&label=stable%20release)](https://github.com/aawnu/php-ga4/releases/latest)
24
[![Version](https://img.shields.io/packagist/v/alexwestergaard/php-ga4?color=yellow&include_prereleases&label=latest%20release)](https://github.com/aawnu/php-ga4/releases)
35
![Code Coverage Badge](https://raw.githubusercontent.com/AlexWestergaard/php-ga4/image-data/coverage.svg)
@@ -22,7 +24,7 @@ The European Union have notified that Google Analytics does not comply with GDPR
2224

2325
Setup requires a **Measurement ID** and **API Secret**. Go to Administrator (Bottom left) -> Account -> Data Streams -> {Your Stream}. Here you should find Measurement ID at top and "Api Secrets for Measurement Protocol" a little down the page, where you can create yourself an `API secret`.
2426

25-
Go to `Administrator` (bottom left) and then select your `Account` -> `Data Streams` -> your stream.
27+
Go to `Administrator` (bottom left) and then select your `Account` -> `Data Streams` -> your stream.
2628
Here you will find `Measurement-ID` at top from and further down `Api Secrets for Measurement Protocol`, in there you can create yourself an `API Secret`.
2729

2830
Once you have obtained the credentials, you can initialise the Analytics like this:
@@ -47,7 +49,7 @@ Server Side Tagging is not supposed to replace the frontend Client and session i
4749
4. Server uses `_ga` (or `_gid`) to send/populate events
4850
- Eg. GenerateLead, Purchase, Refund and other backend handled events.
4951

50-
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. You can replace the `_ga` and `_gid` sessions with your own uniquely generated id.
52+
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. You can replace the `_ga` and `_gid` sessions with your own uniquely generated id.
5153

5254
All requests should follow this structure and contain at least 1 event for Google Analytics to accept it.
5355

@@ -87,7 +89,7 @@ $event->setPageReferrer(string $var);
8789
$event->setPageTitle(string $var);
8890
$event->setScreenResolution(string $var);
8991
// Fillable for multiple events
90-
$eventPage = AlexWestergaard\PhpGa4\Helper\EventParamsHelper();
92+
$eventPage = AlexWestergaard\PhpGa4\Helper\EventParamsHelper(...);
9193
$event->setEventPage($eventPage);
9294
```
9395

@@ -132,18 +134,19 @@ $event->setEventPage($eventPage);
132134

133135
## Frontend & Backend Communication
134136

135-
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.
137+
This library is built for backend server side tracking, but you will probably trigger most events through frontend with Javascript or Websockets.
138+
There will be 2 examples, one as pure backend for logged/queued events and one for frontend to backend communication.
136139

137140
### Logging / Queue
138141

139142
```php
143+
// require vendor/autoload.php
144+
140145
use AlexWestergaard\PhpGa4\Exception;
141146
use AlexWestergaard\PhpGa4\Analytics;
142147
use AlexWestergaard\PhpGa4\Event;
143148
use AlexWestergaard\PhpGa4\Item;
144149

145-
// require vendor/autoload.php
146-
147150
$visitors = getVisitorsAndEvents(); // pseudo function, make your own logic here
148151

149152
foreach ($visitors as $collection) {
@@ -185,54 +188,6 @@ foreach ($visitors as $collection) {
185188

186189
```
187190

188-
### Frontend => Backend
189-
190-
#### Frontend
191-
192-
```js
193-
// array< array< eventName, array<eventParams> > >
194-
axios.post("/your-api-endpoint/ga4-event-receiver", [
195-
// Note each event is its own object inside an array as
196-
// this allows to pass the same event type multiple times
197-
{
198-
addToCart: {
199-
currency: "EUR",
200-
value: 13.37,
201-
items: [
202-
{
203-
item_id: 1,
204-
item_name: "Cup",
205-
price: 13.37,
206-
quantity: 1,
207-
},
208-
],
209-
},
210-
},
211-
]);
212-
```
213-
214-
#### Backend
215-
216-
```php
217-
use AlexWestergaard\PhpGa4\Helper\ConvertHelper;
218-
use AlexWestergaard\PhpGa4\Exception;
219-
use AlexWestergaard\PhpGa4\Analytics;
220-
use AlexWestergaard\PhpGa4\Event;
221-
222-
// require vendor/autoload.php
223-
224-
try {
225-
$events = ConvertHelper::parseEvents($_POST);
226-
227-
Analytics::new($measurementId, $apiSecret)
228-
->addEvent(...$events)
229-
->post();
230-
} catch (Exception\Ga4Exception $exception) {
231-
// Handle exception
232-
// Exceptions might be stacked, check: $exception->getPrevious();
233-
}
234-
```
235-
236191
## Custom Events
237192

238193
You can build your own custom events. All you need is to implement and fullfill the `AlexWestergaard\PhpGa4\Facade\Type\EventType` facade/interface.
@@ -319,11 +274,6 @@ Two important points:
319274
- Events sent to the Validation Server will not show up in reports.
320275
- There is no way for events sent through measurement protocol (Server Side) to show up in the `debugView` in Google Analytics Admin.
321276

322-
## Additional information
323-
324-
- Geographic information is only available via automatic collection from gtag, Google Tag Manager, or Google Analytics for Firebase.
325-
- The `page_view` event works, however it's not documented in the official documentation, so do not rely on it.
326-
327277
## Documentation
328278

329279
- [Measurement Protocol](https://developers.google.com/analytics/devguides/collection/protocol/ga4)

phpunit.xml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php"
3-
colors="true" stopOnFailure="false" beStrictAboutTestsThatDoNotTestAnything="true"
4-
beStrictAboutTodoAnnotatedTests="true" testdox="true"
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
bootstrap="vendor/autoload.php"
4+
colors="true"
5+
stopOnFailure="false"
6+
beStrictAboutTestsThatDoNotTestAnything="true"
7+
testdox="true"
58
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd"
6-
cacheDirectory=".phpunit.cache" displayDetailsOnTestsThatTriggerDeprecations="true"
7-
failOnWarning="true">
9+
cacheDirectory=".phpunit.cache"
10+
displayDetailsOnTestsThatTriggerDeprecations="true"
11+
failOnWarning="true"
12+
displayDetailsOnPhpunitDeprecations="true">
813
<testsuites>
914
<testsuite name="Units">
1015
<directory>test/Unit</directory>

src/Analytics.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -197,27 +197,4 @@ public static function new(string $measurement_id, string $api_secret, bool $deb
197197
{
198198
return new static($measurement_id, $api_secret, $debug);
199199
}
200-
201-
/**
202-
* Deprecated references
203-
*/
204-
205-
/** @deprecated 1.1.9 Please use `Analytics->consent->setAdPersonalizationPermission()` instead */
206-
public function setNonPersonalizedAds(bool $exclude)
207-
{
208-
$this->consent->setAdPersonalizationPermission(!$exclude);
209-
return $this;
210-
}
211-
212-
/** @deprecated 1.1.1 Please use `Analytics->consent->setAdPersonalizationPermission()` instead */
213-
public function allowPersonalisedAds(bool $allow)
214-
{
215-
$this->consent->setAdPersonalizationPermission($allow);
216-
}
217-
218-
/** @deprecated 1.1.1 Please use `Analytics->setTimestampMicros()` instead */
219-
public function setTimestamp(int|float $microOrUnix)
220-
{
221-
$this->setTimestampMicros($microOrUnix);
222-
}
223200
}

src/Event/PageView.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/Facade/Group/AddToCartFacade.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,4 @@ public function setCurrency(string $iso);
2121
* @param integer|float $val eg. 7.77
2222
*/
2323
public function setValue(int|float $val);
24-
2524
}

src/Facade/Group/AnalyticsFacade.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,7 @@ public function setUserId(string $id);
2929
* @var timestamp_micros
3030
* @param integer|float $microOrUnix microtime(true) or time()
3131
*/
32-
public function setTimestamp(int|float $microOrUnix);
33-
34-
/**
35-
* Indicate if these events should be used for personalized ads.
36-
*
37-
* @var non_personalized_ads
38-
* @param boolean $allow
39-
*/
40-
public function allowPersonalisedAds(bool $allow);
32+
public function setTimestampMicros(int|float $microOrUnix);
4133

4234
/**
4335
* The user properties for the measurement

src/Facade/Type/AnalyticsType.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,6 @@ public function setUserId(string $id);
3434
*/
3535
public function setTimestampMicros(int|float $microOrUnix);
3636

37-
/**
38-
* Indicate if these events should be used for personalized ads.
39-
*
40-
* @var non_personalized_ads
41-
* @param boolean $allow
42-
*/
43-
public function setNonPersonalizedAds(bool $allow);
44-
4537
/**
4638
* The user properties for the measurement (Up to 25 custom per project, see link)
4739
*

src/Facade/Type/FirebaseType.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,6 @@ public function setUserId(string $id);
3434
*/
3535
public function setTimestampMicros(int|float $microOrUnix);
3636

37-
/**
38-
* Indicate if these events should be used for personalized ads.
39-
*
40-
* @var non_personalized_ads
41-
* @param boolean $allow
42-
*/
43-
public function setNonPersonalizedAds(bool $allow);
44-
4537
/**
4638
* The user properties for the measurement (Up to 25 custom per project, see link)
4739
*

src/Facade/Type/GtmEventType.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,4 @@
66
* Use this type to skip reserved names of events inside the EventHelper.
77
* NOTICE: reserved names will not pass debugging, be careful with this.
88
*/
9-
interface GtmEventType extends EventType
10-
{
11-
}
9+
interface GtmEventType extends EventType {}

0 commit comments

Comments
 (0)