Download the PHP package guillaumevar/php-ga4-mp without Composer
On this page you can find all versions of the php package guillaumevar/php-ga4-mp. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download guillaumevar/php-ga4-mp
More information about guillaumevar/php-ga4-mp
Files in guillaumevar/php-ga4-mp
Package php-ga4-mp
Short Description PHP GoogleAnalytics4 Measurement Protocol Library
License MIT
Informations about the package php-ga4-mp
Google Analytics 4 Measurement Protocol PHP Library
Overview
This is a PHP Library facilitating the use of Google Analytics 4 (GA4) Measurement Protocol. Measurement Protocol allows developers to send events directly from server-side PHP to Google Analytics.
Please note that GA4 Measurement Protocol is in alpha and might encounter breaking changes. Full documentation is available here: https://developers.google.com/analytics/devguides/collection/protocol/ga4
Requirements
- PHP >= 7.1
- ext-json
- guzzlehttp/guzzle: ^6.5.5
dev:
- phpunit/phpunit: "^9.5"
- fakerphp/faker: "^1.14"
Installation
The recommended way to install this library is via Composer (packagist package: br33f/php-ga4-mp).
Install by composer command:
or package.json
Usage
Send View Item Event
Send Purchase Event
At the moment, the library contains the defined structures of the following events: | Event name | Structure | Documentation |
---|---|---|---|
add_payment_info | AddPaymentInfoEvent | see documentation | |
add_shipping_info | AddShippingInfoEvent | see documentation | |
add_to_cart | AddToCartEvent | see documentation | |
begin_checkout | BeginCheckoutEvent | see documentation | |
login | LoginEvent | see documentation | |
purchase | PurchaseEvent | see documentation | |
refund | RefundEvent | see documentation | |
remove_from_cart | RemoveFromCartEvent | see documentation | |
search | SearchEvent | see documentation | |
select_item | SelectItemEvent | see documentation | |
sign_up | SignUpEvent | see documentation | |
view_cart | ViewCartEvent | see documentation | |
view_item | ViewItemEvent | see documentation | |
view_search_results | ViewSearchResultsEvent | see documentation |
These events are sent analogously to the examples presented above.
Other events
In order to send any event one can use BaseEvent
structure and add any data. Please note that specific event structure should be used instead if already defined, since BaseEvent does not force any structure or provide data validation.
Debug event data and requests
Debuging event data is possible by sending them to debug endpoint (Measurement Protocol Validation Server), since default endpoint for Google Analytics 4 Measurement Protocol does not return any HTTP error codes or messages. In order to validate event one should use sendDebug($request)
method instead of send($request)
.
Method sendDebug($request)
returns DebugResponse
object, which is hydrated with response data such as: status_code
and validation_messages
.
Example:
Unit Testing
Unit Testing for this module is done using PHPUnit 9.
Running unit tests:
License
This library is released under the MIT License.