setono/google-analytics-measurement-protocol

A library that eases the generation of measurement protocol payloads

v0.4.3 2022-01-19 11:17 UTC

This package is auto-updated.

Last update: 2022-12-06 12:14:54 UTC


README

Latest Version Software License Build Status Code Coverage Mutation testing

Easily build payloads for the Google Analytics measurement protocol.

Version ^1.0 of this library supports the GA4 measurement protocol while < 1.0 supports the Universal Analytics measurement protocol.

Installation

composer require setono/google-analytics-measurement-protocol

Usage

<?php

require_once '../vendor/autoload.php';

use Setono\GoogleAnalyticsMeasurementProtocol\Client\Client;
use Setono\GoogleAnalyticsMeasurementProtocol\Request\Body\Body;
use Setono\GoogleAnalyticsMeasurementProtocol\Request\Body\Event\AddToCartEvent;
use Setono\GoogleAnalyticsMeasurementProtocol\Request\Body\Event\Item\Item;
use Setono\GoogleAnalyticsMeasurementProtocol\Request\Request;

$client = new Client();
$request = new Request(
    'YOUR_SECRET',
    'G-12341234',
    Body::create('CLIENT_ID')
        ->addEvent(
            AddToCartEvent::create()
                ->setCurrency('USD')
                ->setValue(123.45)
                ->addItem(Item::create()->setId('SKU1234')->setName('Blue t-shirt')),
        )->setTimestamp(1668509674013800),
);

$client->sendRequest($request);

References