PHPackages                             baibaratsky/yii2-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. [API Development](/categories/api)
4. /
5. baibaratsky/yii2-ga-measurement-protocol

ActiveYii2-extension[API Development](/categories/api)

baibaratsky/yii2-ga-measurement-protocol
========================================

Google Analytics Measurement Protocol for Yii2

v2.0.1(7y ago)13104.2k↓38.1%5[1 issues](https://github.com/baibaratsky/yii2-ga-measurement-protocol/issues)[1 PRs](https://github.com/baibaratsky/yii2-ga-measurement-protocol/pulls)BSD-3-ClausePHP

Since Apr 17Pushed 5y ago3 watchersCompare

[ Source](https://github.com/baibaratsky/yii2-ga-measurement-protocol)[ Packagist](https://packagist.org/packages/baibaratsky/yii2-ga-measurement-protocol)[ Docs](http://github.com/baibaratsky/yii2-ga-measurement-protocol)[ RSS](/packages/baibaratsky-yii2-ga-measurement-protocol/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (2)Versions (13)Used By (0)

Google Analytics Measurement Protocol for Yii2
==============================================

[](#google-analytics-measurement-protocol-for-yii2)

[![Packagist](https://camo.githubusercontent.com/f6c506a6da2f58310a26893dd650e012eead99dcf35a25a28e0c503f205353af/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6261696261726174736b792f796969322d67612d6d6561737572656d656e742d70726f746f636f6c2e737667)](https://github.com/baibaratsky/yii2-ga-measurement-protocol/blob/master/LICENSE.md)[![Dependency Status](https://camo.githubusercontent.com/df53f5c86f1c18882c7d84f787dc608789c9811d9ba236e92040bf676c4c8ee5/68747470733a2f2f7777772e76657273696f6e6579652e636f6d2f757365722f70726f6a656374732f3535393636336364363136363334303032323030303030322f62616467652e7376673f7374796c653d666c6174)](https://www.versioneye.com/user/projects/559663cd6166340022000002)[![Packagist](https://camo.githubusercontent.com/7b15be159f711796031e4b284b157c0986ce5bf6a79bf890fa0b022ddad937c4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6261696261726174736b792f796969322d67612d6d6561737572656d656e742d70726f746f636f6c2e737667)](https://packagist.org/packages/baibaratsky/yii2-ga-measurement-protocol)[![Packagist](https://camo.githubusercontent.com/1b7494b8953826fd2df1af190a6f2580a1511f6bbd401352ccd01841af47821d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6261696261726174736b792f796969322d67612d6d6561737572656d656e742d70726f746f636f6c2e737667)](https://packagist.org/packages/baibaratsky/yii2-ga-measurement-protocol)

> Interact with Google Analytics directly. No need of any JS code. Pure server-side.

Full support for all methods of the [Google Analytics Measurement Protocol](https://developers.google.com/analytics/devguides/collection/protocol/v1/)is provided.

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

[](#installation)

**Note:**Versions in 1.\* range are incompatible with PHP 7.2, use 2.\* with Yii 2.0.13+ instead.

1. The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

    To install, either run

    ```
    $ php composer.phar require baibaratsky/yii2-ga-measurement-protocol:2.0.*

    ```

    or add

    ```
    "baibaratsky/yii2-ga-measurement-protocol": "2.0.*"

    ```

    to the `require` section of your `composer.json` file.

    **For PHP versions prior to 7.2, use a release from 1.\* range:**

    ```
    "baibaratsky/yii2-ga-measurement-protocol": "1.2.*"

    ```
2. Add the component configuration in your `main.php` config file:

    ```
    'components' => [
        'ga' => [
            'class' => 'baibaratsky\yii\google\analytics\MeasurementProtocol',
            'trackingId' => 'UA-XXXX-Y', // Put your real tracking ID here

            // These parameters are optional:
            'useSsl' => true, // If you’d like to use a secure connection to Google servers
            'overrideIp' => false, // By default, IP is overridden by the user’s one, but you can disable this
            'anonymizeIp' => true, // If you want to anonymize the sender’s IP address
            'asyncMode' => true, // Enables the asynchronous mode (see below)
            'autoSetClientId' => true, // Try to set ClientId automatically from the “_ga” cookie (disabled by default)
        ],
    ],
    ```

Usage
-----

[](#usage)

This extension is just a wrapper around the [Google Analytics Measurement Protocol library for PHP](https://github.com/theiconic/php-ga-measurement-protocol). `request()` returns a `TheIconic\Tracking\GoogleAnalytics\Analytics` object, so all its methods will work seamlessly.

#### Basic Usage

[](#basic-usage)

```
\Yii::$app->ga->request()
    ->setClientId('12345678')
    ->setDocumentPath('/mypage')
    ->sendPageview();
```

#### Order Tracking with Enhanced E-commerce

[](#order-tracking-with-enhanced-e-commerce)

```
$request = \Yii::$app->ga->request();

// Build the order data programmatically, each product of the order included in the payload
// First, general and required hit data
$request->setClientId('12345678');
$request->setUserId('123');

// Then, include the transaction data
$request->setTransactionId('7778922')
    ->setAffiliation('THE ICONIC')
    ->setRevenue(250.0)
    ->setTax(25.0)
    ->setShipping(15.0)
    ->setCouponCode('MY_COUPON');

// Include a product, the only required fields are SKU and Name
$productData1 = [
    'sku' => 'AAAA-6666',
    'name' => 'Test Product 2',
    'brand' => 'Test Brand 2',
    'category' => 'Test Category 3/Test Category 4',
    'variant' => 'yellow',
    'price' => 50.00,
    'quantity' => 1,
    'coupon_code' => 'TEST 2',
    'position' => 2
];

$request->addProduct($productData1);

// You can include as many products as you need, this way
$productData2 = [
    'sku' => 'AAAA-5555',
    'name' => 'Test Product',
    'brand' => 'Test Brand',
    'category' => 'Test Category 1/Test Category 2',
    'variant' => 'blue',
    'price' => 85.00,
    'quantity' => 2,
    'coupon_code' => 'TEST',
    'position' => 4
];

$request->addProduct($productData2);

// Don't forget to set the product action, which is PURCHASE in the example below
$request->setProductActionToPurchase();

// Finally, you need to send a hit; in this example, we are sending an Event
$request->setEventCategory('Checkout')
    ->setEventAction('Purchase')
    ->sendEvent();
```

Asynchronous Mode
-----------------

[](#asynchronous-mode)

By default, sending a hit to Google Analytics will be a synchronous request, and it will block the execution of the script until the latter gets a response from the server or terminates by timeout after 100 seconds (throwing a Guzzle exception). However, if you turn the asynchronous mode on in the component config, asynchronous non-blocking requests will be used.

```
'asyncMode' => true,
```

This means that we are sending the request and not waiting for response. The `TheIconic\Tracking\GoogleAnalytics\AnalyticsResponse` object that you will get back has `null` for HTTP status code.

You can also send an asynchronous request even if you haven’t turned it on in the config. Just call `setAsyncRequest(true)`before sending the hit:

```
\Yii::$app->ga->request()
    ->setClientId('12345678')
    ->setDocumentPath('/mypage')
    ->setAsyncRequest(true)
    ->sendPageview();
```

Auto set clientId from cookie
-----------------------------

[](#auto-set-clientid-from-cookie)

If you set `autoSetClientId` to `true` durning component configuration you must disable `enableCookieValidation`. You can do this by configuring `request` component. Otherwise, the auto set clientId will not work.

```
'components' => [
    'request' => [
        'enableCookieValidation' => false,
    ],
]
```

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity39

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 73.9% 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 ~128 days

Recently: every ~118 days

Total

12

Last Release

2635d ago

Major Versions

v1.x-dev → v2.0.02018-01-21

### Community

Maintainers

![](https://www.gravatar.com/avatar/5f7d017b3e4596f4d1dd702da14192fd966e18ca3b21ba25b33f43803b2ce18a?d=identicon)[baibaratsky](/maintainers/baibaratsky)

---

Top Contributors

[![baibaratsky](https://avatars.githubusercontent.com/u/1774061?v=4)](https://github.com/baibaratsky "baibaratsky (17 commits)")[![terales](https://avatars.githubusercontent.com/u/1920639?v=4)](https://github.com/terales "terales (5 commits)")[![morawskim](https://avatars.githubusercontent.com/u/1105278?v=4)](https://github.com/morawskim "morawskim (1 commits)")

---

Tags

google-analyticsyii2yii2-extensionyii2yiigoogle-analyticsanalytics.jsgameasurement protocol

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/baibaratsky-yii2-ga-measurement-protocol/health.svg)](https://phpackages.com/packages/baibaratsky-yii2-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)[monitorbacklinks/yii2-wordpress

Yii2 component for integration with Wordpress CMS via XML-RPC API

3326.9k](/packages/monitorbacklinks-yii2-wordpress)[sonko-dmitry/yii2-telegram-bot-api

Telegram bot api component for Yii2

2132.2k](/packages/sonko-dmitry-yii2-telegram-bot-api)[skeeks/yii2-google-api

Component for work with google api based on google/apiclient

1243.1k1](/packages/skeeks-yii2-google-api)

PHPackages © 2026

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