Skip to content

Commit a722ac6

Browse files
author
Alex Westergaard
committed
Update README example and remove tests/examples
1 parent 6d909db commit a722ac6

File tree

3 files changed

+88
-174
lines changed

3 files changed

+88
-174
lines changed

README.md

Lines changed: 88 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,117 @@
11
# php-ga4
22
PHP Wrapper for Google Analytics 4
33

4+
## Pre-built Events
5+
List of all pre-defined events ready to be used as recommended by the Google Analytics Measurement Protocol.
6+
7+
- Share
8+
- Signup
9+
- Login
10+
- Search
11+
- SelectContent
12+
- SelectItem
13+
- SelectPromotion
14+
- ViewItem
15+
- ViewItemList
16+
- ViewPromotion
17+
- ViewSearchResults
18+
19+
### E-commerce
20+
- GenerateLead
21+
- AddToWishlist
22+
- AddToCart
23+
- ViewCart
24+
- RemoveFromCart
25+
- BeginCheckout
26+
- AddPaymentInfo
27+
- AddShippingInfo
28+
- Purchase
29+
- Refund
30+
31+
### Engagement (Gaming?)
32+
- EarnVirtualCurrency
33+
- SpendVirtualCurrency
34+
- LevelUp
35+
- PostScore
36+
- TutorialBegin
37+
- TutorialComplete
38+
- UnlockAchievement
39+
- JoinGroup
40+
441
## Example
542
```php
643
<?php
744

8-
use AlexWestergaard\PhpGa4\Analytics;
9-
use AlexWestergaard\PhpGa4\UserProperty;
1045
use AlexWestergaard\PhpGa4\GA4Exception;
46+
use AlexWestergaard\PhpGa4\Analytics;
1147
use AlexWestergaard\PhpGa4\Event;
1248
use AlexWestergaard\PhpGa4\Item;
1349

14-
require_once __DIR__ . '/../vendor/autoload.php';
15-
16-
$measurement_id = 'G-XXXXXX';
17-
$api_secret = md5('GA4');
18-
$debug = true; // Ensures we don't push live data
19-
20-
$client_id = 'GA0.5632897.54363853.TEST';
21-
$user_id = 'GA4_VALIDATE';
22-
23-
// -----------------------------------------
24-
// Analytics
25-
$analytics = new Analytics($measurement_id, $api_secret, $debug);
26-
$analytics->setClientId($client_id);
27-
$analytics->setUserId($user_id);
28-
29-
// -----------------------------------------
30-
// Item
31-
$item = new Item();
32-
$item->setItemId('SKU_1');
33-
$item->setItemBrand('My Awesome Product 3000');
34-
$item->addItemCategory('Awesome');
35-
$item->addItemCategory('2022');
36-
$item->setCurrency('USD');
37-
$item->setPrice(4.99);
38-
39-
// -----------------------------------------
40-
// User Property
41-
42-
$UserProperty = new UserProperty();
43-
$UserProperty->setName('customer_tier');
44-
$UserProperty->setValue('premium');
50+
require_once __DIR__ . '/vendor/autoload.php';
4551

46-
$analytics->addUserProperty($UserProperty);
47-
48-
// -----------------------------------------
49-
// Event
50-
51-
$addPaymentInfo = new Event\AddPaymentInfo();
52-
$addPaymentInfo->setCurrency('USD');
53-
$addPaymentInfo->setValue(4.99);
54-
$addPaymentInfo->addItem($item);
55-
56-
$analytics->addEvent($addPaymentInfo);
57-
58-
// -----------------------------------------
59-
// Push Analytics to Google for Validation
6052
try {
61-
$validate = true; // Outputs request + response to/from Google Analytics
62-
$analytics->post($validate);
63-
} catch (GA4Exception $e) {
53+
$analytics = new Analytics('G-XXXXXXXX', 'gDS1gs423dDSH34sdfa');
54+
$analytics->setClientId($_COOKIE['_ga'] ?? $_COOKIE['_gid'] ?? $fallback);
55+
if ($loggedIn) {
56+
$analytics->setUserId($uniqueUserId);
57+
}
58+
59+
$viewCart = new Event\ViewCart();
60+
$viewCart->setCurrency('EUR');
61+
62+
$totalPrice = 0;
63+
foreach ($cartItems as $item) {
64+
$product = new Item();
65+
$product->setItemId($item['id']);
66+
$product->setItemName($item['name']);
67+
$product->setQuantity($item['qty']);
68+
$totalPrice += $item['price_total'];
69+
$product->setPrice(round($item['price_total'] / $item['qty'], 2)); // unit price
70+
$product->setItemVariant($item['colorName']);
71+
72+
$viewCart->addItem($product);
73+
}
74+
75+
$viewCart->setValue($totalPrice);
76+
77+
$analytics->addEvent($viewCart);
78+
79+
if (!$analytics->post()) {
80+
// Handling if post was unsuccessfull
81+
}
82+
83+
// Handling if post was successfull
84+
} catch (GA4Exception $gErr) {
6485
// Handle exception
65-
print_r($e);
86+
// Exceptions might be stacked, make sure to check $gErr->getPrevious();
6687
}
6788
```
6889

6990
### Request
7091
```json
7192
{
72-
"client_id": "GA0.5632897.54363853.TEST",
73-
"user_id": "GA4_VALIDATE",
74-
"user_properties": {
75-
"customer_tier": {
76-
"value": "premium"
77-
}
78-
},
93+
"client_id": "GA0.43535.234234",
94+
"user_id": "m6435",
7995
"events": [
8096
{
81-
"name": "add_payment_info",
97+
"name": "view_cart",
8298
"params": {
83-
"currency": "USD",
84-
"value": 4.99,
99+
"currency": "EUR",
100+
"value": 50.55,
85101
"items": [
86102
{
87-
"item_id": "SKU_1",
88-
"currency": "USD",
89-
"item_brand": "My Awesome Product 3000",
103+
"item_id": "1",
104+
"item_name": "product name",
105+
"item_variant": "white",
106+
"price": 17.79,
107+
"quantity": 2
108+
},
109+
{
110+
"item_id": "2",
111+
"item_name": "another product name",
112+
"item_variant": "gold",
90113
"price": 4.99,
91-
"item_category": "Awesome",
92-
"item_category2": "2022"
114+
"quantity": 3
93115
}
94116
]
95117
}

example/readme.md

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

example/shop-cart.php

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

0 commit comments

Comments
 (0)