2017 © Pedro PelĂĄez
 

yii2-extension yii2-ga-measurement-protocol

Google Analytics Measurement Protocol for Yii2

image

baibaratsky/yii2-ga-measurement-protocol

Google Analytics Measurement Protocol for Yii2

  • Sunday, January 21, 2018
  • by baibaratsky
  • Repository
  • 4 Watchers
  • 9 Stars
  • 18,851 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 12 Versions
  • 10 % Grown

The README.md

Google Analytics Measurement Protocol for Yii2

Packagist Dependency Status Packagist Packagist, (*1)

Interact with Google Analytics directly. No need of any JS code. Pure server-side., (*2)

Full support for all methods of the Google Analytics Measurement Protocol is provided., (*3)

Installation

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

  1. The preferred way to install this extension is through composer., (*5)

    To install, either run, (*6)

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

    or add, (*7)

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

    to the require section of your composer.json file., (*8)

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

    "baibaratsky/yii2-ga-measurement-protocol": "1.2.*"
    
  2. Add the component configuration in your main.php config file:, (*10)

    '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

This extension is just a wrapper around the Google Analytics Measurement Protocol library for PHP. request() returns a TheIconic\Tracking\GoogleAnalytics\Analytics object, so all its methods will work seamlessly., (*11)

Basic Usage

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

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

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., (*12)

'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., (*13)

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:, (*14)

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

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., (*15)

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

The Versions

21/01 2018

dev-master

9999999-dev http://github.com/baibaratsky/yii2-ga-measurement-protocol

Google Analytics Measurement Protocol for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

by Andrei Baibaratsky

yii2 yii google analytics analytics.js ga measurement protocol

21/01 2018

v2.0.0

2.0.0.0 http://github.com/baibaratsky/yii2-ga-measurement-protocol

Google Analytics Measurement Protocol for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

by Andrei Baibaratsky

yii2 yii google analytics analytics.js ga measurement protocol

21/01 2018

v1.x-dev

1.9999999.9999999.9999999-dev http://github.com/baibaratsky/yii2-ga-measurement-protocol

Google Analytics Measurement Protocol for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

by Andrei Baibaratsky

yii2 yii google analytics analytics.js ga measurement protocol

21/01 2018

v1.2.1

1.2.1.0 http://github.com/baibaratsky/yii2-ga-measurement-protocol

Google Analytics Measurement Protocol for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

by Andrei Baibaratsky

yii2 yii google analytics analytics.js ga measurement protocol

15/11 2017

v1.2.0

1.2.0.0 http://github.com/baibaratsky/yii2-ga-measurement-protocol

Google Analytics Measurement Protocol for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

by Andrei Baibaratsky

yii2 yii google analytics analytics.js ga measurement protocol

21/10 2016

v1.1.4

1.1.4.0 http://github.com/baibaratsky/yii2-ga-measurement-protocol

Google Analytics Measurement Protocol for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

by Andrei Baibaratsky

yii2 yii google analytics analytics.js ga measurement protocol

20/09 2016

v1.1.3

1.1.3.0 http://github.com/baibaratsky/yii2-ga-measurement-protocol

Google Analytics Measurement Protocol for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

by Andrei Baibaratsky

yii2 yii google analytics analytics.js ga measurement protocol

02/08 2016

v1.1.2

1.1.2.0 http://github.com/baibaratsky/yii2-ga-measurement-protocol

Google Analytics Measurement Protocol for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

by Andrei Baibaratsky

yii2 yii google analytics analytics.js ga measurement protocol

19/07 2015

v1.1.1

1.1.1.0 http://github.com/baibaratsky/yii2-ga-measurement-protocol

Google Analytics Measurement Protocol for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

by Andrei Baibaratsky

yii2 yii google analytics analytics.js ga measurement protocol

03/07 2015

v1.1.0

1.1.0.0 http://github.com/baibaratsky/yii2-ga-measurement-protocol

Google Analytics Measurement Protocol for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

by Andrei Baibaratsky

yii2 yii google analytics analytics.js ga measurement protocol

19/04 2015

v1.0.1

1.0.1.0 http://github.com/baibaratsky/yii2-ga-measurement-protocol

Google Analytics Measurement Protocol for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

by Andrei Baibaratsky

yii2 yii google analytics analytics.js ga measurement protocol

17/04 2015

v1.0.0

1.0.0.0 http://github.com/baibaratsky/yii2-ga-measurement-protocol

Google Analytics Measurement Protocol for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

by Andrei Baibaratsky

yii2 yii google analytics analytics.js ga measurement protocol