Note (2026): This article preserves a 2018 Universal Analytics (UA) integration. Standard UA properties stopped processing new data on July 1, 2023, and the 360 extension ended on July 1, 2024. The examples below use the retired UA protocol and are not a guide for new integrations. Use the GA4 Measurement Protocol reference today: it uses HTTPS POST to
/mp/collect, an API secret and a measurement ID, and a JSON body containing events. Changing the URL alone will not migrate these examples.
In 2018, integrating Google Analytics into a shop usually meant adding a pre-generated client-side JavaScript snippet. It was straightforward and worked out of the box. So why use a server-side integration? There could be a helluva lot of reasons. You might want to track events from server-side workers, where involving a browser would be ugly and unreliable. Or you might want to build a sales funnel combining client-side events with CRM events.
This walkthrough used the Universal Analytics Measurement Protocol to send those events over HTTP. The examples use PHP with Guzzle, though the same approach worked in other languages. They assume an existing client-side UA integration. The functions below are abbreviated methods from a class with a request object, a Guzzle client and a UA tracking ID.
Identifying the user
It’s important to understand who made an action such as a pageview or a transaction on your website. If you are able to identify a user, you can build a funnel, look at a conversion and a traffic source. Otherwise, you won’t be able to understand how events are interconnected at all. Google Analytics stores the user’s identifier in a specific cookie named “_ga” (please, note, its name can be changed, so check your concrete client integration options). It looks like this: “_ga=1.2.286403989.1366364567”, the last two numbers form a clientId, used by Google Analytics to identify a user. Let’s create a function to parse it. If there is no cookie named “_ga”, we’ll generate our own identifier:
This parser illustrates the UA cookie format used in the original integration. GA4 still uses _ga to distinguish users and _ga_<container-id> for session state, but that does not make this parser a general GA4 recipe. For GA4, retrieve client_id through the tag API.
function getClientId() {
$cookie = $this->request->cookies->get('_ga');
if ($cookie) {
$cv = explode('.', $cookie);
return $cv[2] . '.' . $cv[3];
}
return uniqid();
}We also want to gather the request’s campaign context. These are the legacy UA Measurement Protocol field names, mapped from the incoming UTM query parameters:
function getContext() {
$context = [];
if ($utmSource = $this->request->query->get('utm_source')) {
$context['cs'] = $utmSource;
}
if ($utmMedium = $this->request->query->get('utm_medium')) {
$context['cm'] = $utmMedium;
}
if ($utmCampaign = $this->request->query->get('utm_campaign')) {
$context['cn'] = $utmCampaign;
}
if ($utmTerm = $this->request->query->get('utm_term')) {
$context['ck'] = $utmTerm;
}
if ($utmContent = $this->request->query->get('utm_content')) {
$context['cc'] = $utmContent;
}
return $context;
}You need to store such data somewhere in your database if you want to be able to report any events for this user later. You can’t do it at a runtime as there could be no cookies or GET variables at the time.
Tracking transactions
For e-commerce analytics, decide which business event counts as a transaction before sending it. Creating an order and receiving payment are different points in a funnel; choose the one your revenue reporting is meant to measure.
function saveTransaction($clientId, $transactionId, $revenue, array $context) {
$transactionParams = [
'v' => 1, // protocol version
't' => 'transaction', // event type
'tid' => $this->identifier, // your UA-XXXXX-1 identifier
'cid' => $clientId, // client identifier
'ti' => $transactionId, // transaction unique identifier
'tr' => $revenue, // transaction revenue
'cu' => 'USD', // currency
];
// merge utm context from "getContext()"
$formParams = array_merge($transactionParams, $context);
// historical UA endpoint: set "base_uri" to "https://www.google-analytics.com/"
$this->client->request('POST', 'collect', ['form_params' => $formParams]);
}UA used a separate item hit for each product. Call this method once per item, using the same client and transaction IDs as the transaction hit:
function saveItem($clientId, $transactionId, array $item) {
$itemParams = [
'v' => 1, // protocol version
't' => 'item', // event type
'tid' => $this->identifier, // your UA-XXXXX-1 identifier
'cid' => $clientId, // client identifier, same as above
'ti' => $transactionId, // transaction unique identifier, same as above
'in' => $item['name'], // for example "blue pants"
'ip' => $item['price'], // price of the specific item
'iq' => $item['quantity'], // quantity of the specific item
'ic' => $item['article'], // code of the specific item, for example "pants-001"
'iv' => $item['category'], // category of item, for example "pants"
'cu' => 'USD'
];
$this->client->request('POST', 'collect', ['form_params' => $itemParams]);
}That’s it.






