|
1 | 1 | # php-ga4 |
2 | 2 | PHP Wrapper for Google Analytics 4 |
| 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 | + |
| 41 | +## Example |
| 42 | +```php |
| 43 | +<?php |
| 44 | + |
| 45 | +/** |
| 46 | + * This is psuedo code for visual representation. |
| 47 | + */ |
| 48 | + |
| 49 | +use AlexWestergaard\PhpGa4\GA4Exception; |
| 50 | +use AlexWestergaard\PhpGa4\Analytics; |
| 51 | +use AlexWestergaard\PhpGa4\Event; |
| 52 | +use AlexWestergaard\PhpGa4\Item; |
| 53 | + |
| 54 | +require_once __DIR__ . '/vendor/autoload.php'; |
| 55 | + |
| 56 | +try { |
| 57 | + $analytics = new Analytics('G-XXXXXXXX', 'gDS1gs423dDSH34sdfa'); |
| 58 | + $analytics->setClientId($_COOKIE['_ga'] ?? $_COOKIE['_gid'] ?? $fallback); |
| 59 | + if ($loggedIn) { |
| 60 | + $analytics->setUserId($uniqueUserId); |
| 61 | + } |
| 62 | + |
| 63 | + $viewCart = new Event\ViewCart(); |
| 64 | + $viewCart->setCurrency('EUR'); |
| 65 | + |
| 66 | + $totalPrice = 0; |
| 67 | + foreach ($cartItems as $item) { |
| 68 | + $product = new Item(); |
| 69 | + $product->setItemId($item['id']); |
| 70 | + $product->setItemName($item['name']); |
| 71 | + $product->setQuantity($item['qty']); |
| 72 | + $totalPrice += $item['price_total']; |
| 73 | + $product->setPrice(round($item['price_total'] / $item['qty'], 2)); // unit price |
| 74 | + $product->setItemVariant($item['colorName']); |
| 75 | + |
| 76 | + $viewCart->addItem($product); |
| 77 | + } |
| 78 | + |
| 79 | + $viewCart->setValue($totalPrice); |
| 80 | + |
| 81 | + $analytics->addEvent($viewCart); |
| 82 | + |
| 83 | + if (!$analytics->post()) { |
| 84 | + // Handling if post was unsuccessfull |
| 85 | + } |
| 86 | + |
| 87 | + // Handling if post was successfull |
| 88 | +} catch (GA4Exception $gErr) { |
| 89 | + // Handle exception |
| 90 | + // Exceptions might be stacked, make sure to check $gErr->getPrevious(); |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +### Request |
| 95 | +```json |
| 96 | +{ |
| 97 | + "client_id": "GA0.43535.234234", |
| 98 | + "user_id": "m6435", |
| 99 | + "events": [ |
| 100 | + { |
| 101 | + "name": "view_cart", |
| 102 | + "params": { |
| 103 | + "currency": "EUR", |
| 104 | + "value": 50.55, |
| 105 | + "items": [ |
| 106 | + { |
| 107 | + "item_id": "1", |
| 108 | + "item_name": "product name", |
| 109 | + "item_variant": "white", |
| 110 | + "price": 17.79, |
| 111 | + "quantity": 2 |
| 112 | + }, |
| 113 | + { |
| 114 | + "item_id": "2", |
| 115 | + "item_name": "another product name", |
| 116 | + "item_variant": "gold", |
| 117 | + "price": 4.99, |
| 118 | + "quantity": 3 |
| 119 | + } |
| 120 | + ] |
| 121 | + } |
| 122 | + } |
| 123 | + ] |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +### Response |
| 128 | +```json |
| 129 | +{ |
| 130 | + "validationMessages": [] |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +## Custom Events |
| 135 | +You can build your own custom events by extending on the Model\Event abstraction class; example |
| 136 | + |
| 137 | +```php |
| 138 | +<?php |
| 139 | + |
| 140 | +use AlexWestergaard\PhpGa4\Model; |
| 141 | + |
| 142 | +class ExampleEvent extends Model\Event |
| 143 | +{ |
| 144 | + protected $my_variable; |
| 145 | + protected $my_required_variable; |
| 146 | + |
| 147 | + public function getName(): string |
| 148 | + { |
| 149 | + return 'example_event'; |
| 150 | + } |
| 151 | + |
| 152 | + public function getParams(): array |
| 153 | + { |
| 154 | + return [ |
| 155 | + 'my_variable', |
| 156 | + 'my_required_variable', |
| 157 | + ]; |
| 158 | + } |
| 159 | + |
| 160 | + public function getRequiredParams(): array |
| 161 | + { |
| 162 | + return [ |
| 163 | + 'my_required_variable', |
| 164 | + ]; |
| 165 | + } |
| 166 | + |
| 167 | + public function setMyVariable(string $value) |
| 168 | + { |
| 169 | + $this->my_variable = $value; |
| 170 | + } |
| 171 | + |
| 172 | + public function setMyRequiredVariable(string $value) |
| 173 | + { |
| 174 | + $this->my_required_variable = $value; |
| 175 | + } |
| 176 | +} |
| 177 | +``` |
| 178 | + |
| 179 | +It's important that you extend the Model\Event class because Analytics checks inheritance towards that class to ensure we grap all parameters and ensures required parameters. |
| 180 | + |
| 181 | +Property name and value will be used as parameter name and value. |
| 182 | + |
| 183 | +Just make sure not to use any [Reserved Event Names](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference?client_type=gtag#reserved_event_names) |
| 184 | + |
| 185 | +## Dependencies |
| 186 | +- [GuzzleHttp/Guzzle: 6.x](https://packagist.org/packages/guzzlehttp/guzzle) |
| 187 | + - Please update [composer.json](composer.json) Guzzle to version 7.x for PHP 8.0+ |
| 188 | + |
| 189 | +## Source Documentation |
| 190 | +- [Measurement Protocol](https://developers.google.com/analytics/devguides/collection/protocol/ga4) |
| 191 | +- [Measurement Protocol: Reference](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference?client_type=gtag) |
| 192 | +- [Measurement Protocol: User Properties](https://developers.google.com/analytics/devguides/collection/protocol/ga4/user-properties?client_type=gtag) |
| 193 | +- [Measurement Protocol: Events](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events) |
| 194 | +- [Measurement Protocol: Validation](https://developers.google.com/analytics/devguides/collection/protocol/ga4/validating-events?client_type=gtag) |
0 commit comments