PHPackages                             internetrix/silverstripe-ga-measurement-protocol - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. internetrix/silverstripe-ga-measurement-protocol

ActiveSilverstripe-vendormodule

internetrix/silverstripe-ga-measurement-protocol
================================================

Send data to Google Analytics via server-side using Measurement Protocol

0.0.2(5y ago)01BSD-3-ClausePHPPHP &gt;=7.0

Since May 7Pushed 5y ago3 watchersCompare

[ Source](https://github.com/Internetrix/silverstripe-ga-measurement-protocol)[ Packagist](https://packagist.org/packages/internetrix/silverstripe-ga-measurement-protocol)[ RSS](/packages/internetrix-silverstripe-ga-measurement-protocol/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (3)Used By (0)

Google Analytics Measurement Protocol v1 for SilverStripe
=========================================================

[](#google-analytics-measurement-protocol-v1-for-silverstripe)

Introduction
------------

[](#introduction)

This module adds functionality to be able to send hits to Google Analytics via server side using Google's Measurement Protocol Version 1:

The module currently allows the sending of the following hits:

- pageviews
- events
- timing

This is accomplished by constructing a GET request and using Guzzle to send the request to the Google Analytics endpoint.

Requirements
------------

[](#requirements)

- SilverStripe CMS ^4.0
- Guzzle ^6.3

Installation
------------

[](#installation)

Install the module via composer:

```
composer require internetrix/silverstripe-ga-measurement-protocol

```

Configuration &amp; Setup
-------------------------

[](#configuration--setup)

1. Set the following variables for `Internetrix\GaMeasurementProtocol\Model\GAHit` in config:
    - **useProductionGAProperty:** Set this true if you want to use the Production tracking ID. Set to false if you want to send hits to staging.
    - **stagingTrackingID:** Google Analytics UA tracking ID for staging/testing property. Expected format: 'UA-XXXXXXXX-X'
    - **productionTrackingID:** Google Analytics UA tracking ID for production property. Expected format: 'UA-XXXXXXXX-X'

**Example YML configuration:**

```
Internetrix\GaMeasurementProtocol\Model\GAHit:
  useProductionGAProperty: true
  stagingTrackingID: 'UA-XXXXXXXX-X'
  productionTrackingID: 'UA-XXXXXXXX-X'
  useTestingEndpoint: false

```

**Optional**: Another useful config variable that is useful for debugging / testing is to set `useTestingEndpoint` to true in config:

```
Internetrix\GaMeasurementProtocol\Model\GAHit:
  useTestingEndpoint: true

```

By setting **useTestingEndpoint** to true, when sending hits, the module will send the request to Google's Validation Server.

As Google's GA Measurement Protocol does not return HTTP error codes, by sending requests to the Measurement Protocol Validation Server, you can test the response. If the current SS environment is set to dev, it will also output the response to the browser / terminal.

More information on Google's Measurement Protocol Validation Server can be found here:

Sending hits to a Production Google Analytics property
------------------------------------------------------

[](#sending-hits-to-a-production-google-analytics-property)

For hits to be sent to a Production property, all the following conditions must be met. Otherwise hits will ALWAYS be sent to staging property / debug endpoint.

- Condition 1 - SilverStripe `SS_ENVIRONMENT_TYPE` variable must be set to `live`
- Condition 2 - `useProductionGAProperty` must be set to true in config
- Condition 3 - `useTestingEndpoint` must be set to false in config

Generate ClientID
-----------------

[](#generate-clientid)

**IMPORTANT:** When sending a GA hit, always ensure that the hit type is set and that a unique client ID is given. This module contains a function called setClientID to help developers add a client ID. The first parameter of function tells the function if the \_ga cookie should be retrieved or not when creating a clientID. The second parameters allows a developer to override the clientID for a hit.

There are some ways this function can be used:

- Option 1: Retrieve the same ClientID using the \_ga cookie (shared by frontend)

    - `$pageHit->setClientID(true);`
- Option 2: Generate a completely new unique client ID. If this option is saved, the unique client ID should be saved by the developer and re-used so that Google Analytics recognises the same user

    - `$pageHit->setClientID(false);`
- Option 3: Override and set the GA Client ID to a value arbitrarily defined by a developer

    - `$pageHit->setClientID(false, ');`

Usage Examples
--------------

[](#usage-examples)

The PHPDocs in **GAHit.php** contains links explaining what parameters are required for each type of event. Otherwise, please refer to the [Official Google Analytics Measurement Protocol documentation](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters)

### Example 1: Sending a Pageview hit to GA

[](#example-1-sending-a-pageview-hit-to-ga)

Required Parameters:

- HitType = pageview
- Set a GA Client ID by calling: `$hit->setClientID`. See above Generate ClientID section for more details
- Include either (Document Hostname + Document Path + (optionally) Document Title) OR set the Document Location URL
- Set a User-Agent by calling: `$hit->setUserAgent()` otherwise Google Analytics may classify the hit as bot spam

```
$hit = GAHit::create();
$hit->setHitType(GAHit::PAGEVIEW);
$hit->setClientID(true);
$hit->setUserAgent();

// Option 1
$hit->setPageviewParameters('https://example.com.au', '/foo', 'Test Page');
// OR Option 2
$hit->setDocumentLocationURL('https://example.com.au/foo', 'Test Page');

// Send hit to Google Analytics
$hit->sendHit();

```

### Example 2: Sending an Event hit to GA

[](#example-2-sending-an-event-hit-to-ga)

Required Parameters:

- HitType = event
- Set a GA Client ID by calling: `$hit->setClientID`. See above Generate ClientID section for more details
- Set a User-Agent by calling: `$hit->setUserAgent()` otherwise Google Analytics may classify the hit as bot spam
- Event Category and Event Action are required parameters. Event Label and Event Value is optional. Set these parameters by calling: `$hit->setEventParameters('category', 'action', 'label', $value)`

```
$hit = GAHit::create();
$hit->setHitType(GAHit::EVENT);
$hit->setClientID(true);
$hit->setUserAgent();
$hit->setEventParameters('TestCategory', 'TestAction', 'TestLabel', 1);
$hit->setDocumentLocationURL('www.example.com.au/foo');

// (optional) Set as non-interaction hit if required
$hit->setNonInteractionHit();

// Send hit to Google Analytics
$hit->sendHit();

```

### Example 3: Sending a Timing hit to GA

[](#example-3-sending-a-timing-hit-to-ga)

```
        $hit = GAHit::create();
        $hit->setHitType(GAHit::TIMING);
        $hit->setClientID(true);
        $hit->setTimingParameters($category, $userTimingVariable, $timingTime, $optionalParameters);
        $hit->setDocumentLocationURL('www.example.com.au');

        // Send hit to Google Analytics
        $hit->sendHit();

```

Required Parameters:

- HitType = timing
- Set a GA Client ID by calling: `$hit->setClientID`. See above Generate ClientID section for more details
- Set a User-Agent by calling: `$hit->setUserAgent()` otherwise Google Analytics may classify the hit as bot spam
- Timing category, timing variable and timing timing are required. Set these parameters by calling: `$hit->setTimingParameters($category, $userTimingVariable, $timingTime, $optionalParameters)`

### Adding Custom Dimensions, Custom Metrics and / or adding extra parameters to a hit

[](#adding-custom-dimensions-custom-metrics-and--or-adding-extra-parameters-to-a-hit)

To add additional parameters like Custom Dimensions, Custom Metrics etc to a hit, use the `addParameters` method where `$parameters` should be an array with `parameter name` as the key, and the `parameter value` as the array value:

```
// Adding a Custom Dimension (with dimension index 1) and setting it to the value 'Sports'
$parameters['cd1'] = 'Sports';

// Adding a Custom Metric (with metric index of 1) and setting it to the int alue 47
$parameters['cm1'] = 47';

// Add custom dimension / metric to the hit
$hit->addParameters($parameters);

```

Todo
----

[](#todo)

- Add support for other type of hits such as transactions etc.

Licence
-------

[](#licence)

Please see [License File](LICENSE.md) for more information.

###  Health Score

18

—

LowBetter than 8% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity1

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity38

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

1831d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f2028df988ee004ba1951c38852161aef16c343d4831e37fa16edb7e77729c86?d=identicon)[Internetrix](/maintainers/Internetrix)

---

Top Contributors

[![jkersu](https://avatars.githubusercontent.com/u/38059212?v=4)](https://github.com/jkersu "jkersu (3 commits)")

---

Tags

silverstripegoogle-analyticsmeasurement protocolga measurement protocolanalytics tracking

### Embed Badge

![Health badge](/badges/internetrix-silverstripe-ga-measurement-protocol/health.svg)

```
[![Health](https://phpackages.com/badges/internetrix-silverstripe-ga-measurement-protocol/health.svg)](https://phpackages.com/packages/internetrix-silverstripe-ga-measurement-protocol)
```

###  Alternatives

[irazasyed/laravel-gamp

Send analytics data to Google Analytics from Laravel. A package for GA Measurement Protocol API

3361.6M1](/packages/irazasyed-laravel-gamp)[silverstripe/frameworktest

Aids core and module developers in testing their code against a set of sample data and behaviour.

17304.6k23](/packages/silverstripe-frameworktest)[cyber-duck/silverstripe-google-tag-manager

Add Tag Manager data layer, events, and ecommerce support for SilverStripe

1528.0k](/packages/cyber-duck-silverstripe-google-tag-manager)[silverstripe-terraformers/gridfield-rich-filter-header

Rich filter header component for GridField

1325.7k1](/packages/silverstripe-terraformers-gridfield-rich-filter-header)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
