1. Go to this page and download the library: Download guillaumevar/php-ga4-mp library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
guillaumevar / php-ga4-mp example snippets
use Br33f\Ga4\MeasurementProtocol\Service;
use Br33f\Ga4\MeasurementProtocol\Dto\Request\BaseRequest;
use Br33f\Ga4\MeasurementProtocol\Dto\Event\ViewItemEvent;
use Br33f\Ga4\MeasurementProtocol\Dto\Parameter\ItemParameter;
// Create service instance
$ga4Service = new Service('MEASUREMENT_PROTOCOL_API_SECRET', 'MEASUREMENT_ID');
// Create base request with em to viewItemEventData
$viewItemEventData->addItem($viewedItem)
// Add event to base request (you can add up to 25 events to single request)
$baseRequest->addEvent($viewItemEventData);
// We have all the data we need. Just send the request.
$ga4Service->send($baseRequest);
use Br33f\Ga4\MeasurementProtocol\Service;
use Br33f\Ga4\MeasurementProtocol\Dto\Request\BaseRequest;
use Br33f\Ga4\MeasurementProtocol\Dto\Event\PurchaseEvent;
use Br33f\Ga4\MeasurementProtocol\Dto\Parameter\ItemParameter;
// Create service instance
$ga4Service = new Service('MEASUREMENT_PROTOCOL_API_SECRET', 'MEASUREMENT_ID');
// Create base request with );
// Add this item to purchaseEventData
$purchaseEventData->addItem($purchasedItem1)
// You can also fill item data via constructor
$purchaseEventData->addItem(new ItemParameter([
'item_id' => 'SECOND_ITEM_ID',
'item_name' => 'SECOND_ITEM_NAME',
'price' => 50.00,
'quantity' => 1
]));
// Add event to base request (you can add up to 25 events to single request)
$baseRequest->addEvent($purchaseEventData);
// We have all the data we need. Just send the request.
$ga4Service->send($baseRequest);
use Br33f\Ga4\MeasurementProtocol\Service;
use Br33f\Ga4\MeasurementProtocol\Dto\Request\BaseRequest;
use Br33f\Ga4\MeasurementProtocol\Dto\Event\BaseEvent;
// Create Service and request same as above
// ...
// Create Base Event Data (for example: 'share' event - https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events#share)
$eventName = 'share';
$anyEventData = new BaseEvent($eventName);
$purchaseEventData
->setMethod('Twitter')
->setContentType('Post')
->setItemId('example_item_id')
->setAnyParamYouWish('test'); // means 'any_param_you_wish' is set
// Add event to base request (you can add up to 25 events to single request) and send, same as above
// ...
use Br33f\Ga4\MeasurementProtocol\Service;
use Br33f\Ga4\MeasurementProtocol\Dto\Request\BaseRequest;
use Br33f\Ga4\MeasurementProtocol\Dto\Event\AddToCartEvent;
use Br33f\Ga4\MeasurementProtocol\Dto\Parameter\ItemParameter;
// Create service instance
$ga4Service = new Service('MEASUREMENT_PROTOCOL_API_SECRET', 'MEASUREMENT_ID");
// Create base request with price' => 99.99,
'quantity' => 1
]));
// Add event to base request (you can add up to 25 events to single request)
$baseRequest->addEvent($addToCartEventData);
// Instead of sending data to production Measurement Protocol endpoint
// $ga4Service->send($baseRequest);
// Send data to validation endpoint, which responds with status cude and validation messages.
$debugResponse = $ga4Service->sendDebug($baseRequest);
// Now debug response contains status code, and validation messages if request is invalid
var_dump($debugResponse->getStatusCode());
var_dump($debugResponse->getValidationMessages());
composer install
php vendor/bin/phpunit
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.