setono/google-analytics-measurement-protocol

A library that eases the generation of measurement protocol payloads

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

README

Latest Version Latest Unstable 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

Build your Hit using the HitBuilder:

<?php
use Setono\GoogleAnalyticsMeasurementProtocol\Hit\HitBuilder;
use Setono\GoogleAnalyticsMeasurementProtocol\Hit\HitBuilderInterface;

$hitBuilder = new HitBuilder(HitBuilderInterface::HIT_TYPE_PAGEVIEW);
$hitBuilder->setClientId('CLIENT_ID');
$hit = $hitBuilder->getHit('UA-1234-1');

echo $hit; // outputs v=1&t=pageview&cid=CLIENT_ID&tid=UA-1234-1

then use the Client to send the hit:

<?php
use Setono\GoogleAnalyticsMeasurementProtocol\Client\Client;

$client = new Client();
$client->sendHit($hit);