setono/google-analytics-measurement-protocol

A library that eases the generation of measurement protocol payloads

v2.0.0-beta.2 2023-09-12 09:27 UTC

README

Latest Version Software License Build Status Code Coverage Mutation testing

Easily build payloads for the Google Analytics measurement protocol.

Installation

composer require setono/google-analytics-measurement-protocol

Usage

<?php

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

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

$client = new Client();
$request = (new Request(
    'YOUR_SECRET',
    'G-12341234',
    '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