|
1 | 1 | # php-ga4 |
2 | 2 | PHP Wrapper for Google Analytics 4 |
3 | 3 |
|
| 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 | + |
4 | 41 | ## Example |
5 | 42 | ```php |
6 | 43 | <?php |
7 | 44 |
|
8 | | -use AlexWestergaard\PhpGa4\Analytics; |
9 | | -use AlexWestergaard\PhpGa4\UserProperty; |
10 | 45 | use AlexWestergaard\PhpGa4\GA4Exception; |
| 46 | +use AlexWestergaard\PhpGa4\Analytics; |
11 | 47 | use AlexWestergaard\PhpGa4\Event; |
12 | 48 | use AlexWestergaard\PhpGa4\Item; |
13 | 49 |
|
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'; |
45 | 51 |
|
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 |
60 | 52 | 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) { |
64 | 85 | // Handle exception |
65 | | - print_r($e); |
| 86 | + // Exceptions might be stacked, make sure to check $gErr->getPrevious(); |
66 | 87 | } |
67 | 88 | ``` |
68 | 89 |
|
69 | 90 | ### Request |
70 | 91 | ```json |
71 | 92 | { |
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", |
79 | 95 | "events": [ |
80 | 96 | { |
81 | | - "name": "add_payment_info", |
| 97 | + "name": "view_cart", |
82 | 98 | "params": { |
83 | | - "currency": "USD", |
84 | | - "value": 4.99, |
| 99 | + "currency": "EUR", |
| 100 | + "value": 50.55, |
85 | 101 | "items": [ |
86 | 102 | { |
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", |
90 | 113 | "price": 4.99, |
91 | | - "item_category": "Awesome", |
92 | | - "item_category2": "2022" |
| 114 | + "quantity": 3 |
93 | 115 | } |
94 | 116 | ] |
95 | 117 | } |
|
0 commit comments