Download the PHP package br33f/php-ga4-mp without Composer
On this page you can find all versions of the php package br33f/php-ga4-mp. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download br33f/php-ga4-mp
More information about br33f/php-ga4-mp
Files in br33f/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.
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 || ^7.0.0
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 | |
add_to_wishlist | AddToWishlistEvent | 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.
Firebase Support
It is possible to use this library to send Firebase events. To do so, just initialize Service and BaseRequest as in following example:
Consent mode v2
This library supports consent mode v2 : see documentation
This mode replaces now obsolete / deprecated non_personalized_ads
:
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:
Get response as stream
It is possible to get response as stream. This is useful when you want to process response further and you don't want the stream to be consumed in BaseResponse.
Method sendStream($request)
returns StreamResponse
object, which is hydrated with response data such
as: status_code
(just as usual) and body
which is a stream resource.
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.