├── .gitignore ├── composer.json ├── LICENSE.md ├── MeasurementProtocol.php └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | vendor/ 3 | composer.lock 4 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "baibaratsky/yii2-ga-measurement-protocol", 3 | "description": "Google Analytics Measurement Protocol for Yii2", 4 | "type": "yii2-extension", 5 | "keywords": ["yii", "yii2", "ga", "google analytics", "measurement protocol", "analytics.js"], 6 | "license": "BSD-3-Clause", 7 | "homepage": "http://github.com/baibaratsky/yii2-ga-measurement-protocol", 8 | "authors": [ 9 | { 10 | "name": "Andrei Baibaratsky", 11 | "email": "andrei@baibaratsky.com" 12 | } 13 | ], 14 | "require": { 15 | "theiconic/php-ga-measurement-protocol": "2.7.*", 16 | "yiisoft/yii2": ">=2.0.13" 17 | }, 18 | "autoload": { 19 | "psr-4": { 20 | "baibaratsky\\yii\\google\\analytics\\": "" 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Google Analytics Measurement Protocol for Yii2 is free software. 2 | It is released under the terms of the following BSD License. 3 | 4 | Copyright © 2015 by Andrei Baibaratsky. All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions 8 | are met: 9 | 10 | * Redistributions of source code must retain the above copyright 11 | notice, this list of conditions and the following disclaimer. 12 | * Redistributions in binary form must reproduce the above copyright 13 | notice, this list of conditions and the following disclaimer in 14 | the documentation and/or other materials provided with the 15 | distribution. 16 | * The names of the copyright holders and contributors may not be 17 | used to endorse or promote products derived from this software 18 | without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 | COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 28 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 30 | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 | POSSIBILITY OF SUCH DAMAGE. 32 | -------------------------------------------------------------------------------- /MeasurementProtocol.php: -------------------------------------------------------------------------------- 1 | useSsl); 41 | $request->setTrackingId($this->trackingId) 42 | ->setProtocolVersion($this->version) 43 | ->setAsyncRequest($this->asyncMode); 44 | 45 | if ($this->overrideIp && isset(\Yii::$app->request->userIP)) { 46 | $request->setIpOverride(\Yii::$app->request->userIP); 47 | } 48 | 49 | if ($this->anonymizeIp) { 50 | $request->setAnonymizeIp(1); 51 | } 52 | 53 | if ($this->autoSetClientId) { 54 | $clientId = $this->extractClientIdFromCookie(); 55 | if (!empty($clientId)) { 56 | $request->setClientId($clientId); 57 | } 58 | } 59 | 60 | return $request; 61 | } 62 | 63 | /** 64 | * @return string 65 | */ 66 | protected function extractClientIdFromCookie() 67 | { 68 | $cookie = \Yii::$app->request->cookies->getValue('_ga', ''); 69 | $cookieParts = explode('.', $cookie); 70 | $clientIdParts = array_slice($cookieParts, -2); 71 | return implode('.', $clientIdParts); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Google Analytics Measurement Protocol for Yii2 2 | ============================================== 3 | 4 | [![Packagist](https://img.shields.io/packagist/l/baibaratsky/yii2-ga-measurement-protocol.svg)](https://github.com/baibaratsky/yii2-ga-measurement-protocol/blob/master/LICENSE.md) 5 | [![Dependency Status](https://www.versioneye.com/user/projects/559663cd6166340022000002/badge.svg?style=flat)](https://www.versioneye.com/user/projects/559663cd6166340022000002) 6 | [![Packagist](https://img.shields.io/packagist/v/baibaratsky/yii2-ga-measurement-protocol.svg)](https://packagist.org/packages/baibaratsky/yii2-ga-measurement-protocol) 7 | [![Packagist](https://img.shields.io/packagist/dt/baibaratsky/yii2-ga-measurement-protocol.svg)](https://packagist.org/packages/baibaratsky/yii2-ga-measurement-protocol) 8 | 9 | >Interact with Google Analytics directly. No need of any JS code. Pure server-side. 10 | 11 | Full support for all methods of the 12 | [Google Analytics Measurement Protocol](https://developers.google.com/analytics/devguides/collection/protocol/v1/) 13 | is provided. 14 | 15 | 16 | Installation 17 | ------------ 18 | 19 | **Note:** 20 | Versions in 1.* range are incompatible with PHP 7.2, use 2.* with Yii 2.0.13+ instead. 21 | 22 | 1. The preferred way to install this extension is through [composer](http://getcomposer.org/download/). 23 | 24 | To install, either run 25 | ``` 26 | $ php composer.phar require baibaratsky/yii2-ga-measurement-protocol:2.0.* 27 | ``` 28 | or add 29 | ``` 30 | "baibaratsky/yii2-ga-measurement-protocol": "2.0.*" 31 | ``` 32 | to the `require` section of your `composer.json` file. 33 | 34 | **For PHP versions prior to 7.2, use a release from 1.\* range:** 35 | ``` 36 | "baibaratsky/yii2-ga-measurement-protocol": "1.2.*" 37 | ``` 38 | 39 | 1. Add the component configuration in your `main.php` config file: 40 | ```php 41 | 'components' => [ 42 | 'ga' => [ 43 | 'class' => 'baibaratsky\yii\google\analytics\MeasurementProtocol', 44 | 'trackingId' => 'UA-XXXX-Y', // Put your real tracking ID here 45 | 46 | // These parameters are optional: 47 | 'useSsl' => true, // If you’d like to use a secure connection to Google servers 48 | 'overrideIp' => false, // By default, IP is overridden by the user’s one, but you can disable this 49 | 'anonymizeIp' => true, // If you want to anonymize the sender’s IP address 50 | 'asyncMode' => true, // Enables the asynchronous mode (see below) 51 | 'autoSetClientId' => true, // Try to set ClientId automatically from the “_ga” cookie (disabled by default) 52 | ], 53 | ], 54 | ``` 55 | 56 | 57 | Usage 58 | ----- 59 | This extension is just a wrapper around the 60 | [Google Analytics Measurement Protocol library for PHP](https://github.com/theiconic/php-ga-measurement-protocol). 61 | `request()` returns a `TheIconic\Tracking\GoogleAnalytics\Analytics` object, so all its methods will work seamlessly. 62 | 63 | #### Basic Usage 64 | ```php 65 | \Yii::$app->ga->request() 66 | ->setClientId('12345678') 67 | ->setDocumentPath('/mypage') 68 | ->sendPageview(); 69 | ``` 70 | 71 | #### Order Tracking with Enhanced E-commerce 72 | 73 | ```php 74 | $request = \Yii::$app->ga->request(); 75 | 76 | // Build the order data programmatically, each product of the order included in the payload 77 | // First, general and required hit data 78 | $request->setClientId('12345678'); 79 | $request->setUserId('123'); 80 | 81 | // Then, include the transaction data 82 | $request->setTransactionId('7778922') 83 | ->setAffiliation('THE ICONIC') 84 | ->setRevenue(250.0) 85 | ->setTax(25.0) 86 | ->setShipping(15.0) 87 | ->setCouponCode('MY_COUPON'); 88 | 89 | // Include a product, the only required fields are SKU and Name 90 | $productData1 = [ 91 | 'sku' => 'AAAA-6666', 92 | 'name' => 'Test Product 2', 93 | 'brand' => 'Test Brand 2', 94 | 'category' => 'Test Category 3/Test Category 4', 95 | 'variant' => 'yellow', 96 | 'price' => 50.00, 97 | 'quantity' => 1, 98 | 'coupon_code' => 'TEST 2', 99 | 'position' => 2 100 | ]; 101 | 102 | $request->addProduct($productData1); 103 | 104 | // You can include as many products as you need, this way 105 | $productData2 = [ 106 | 'sku' => 'AAAA-5555', 107 | 'name' => 'Test Product', 108 | 'brand' => 'Test Brand', 109 | 'category' => 'Test Category 1/Test Category 2', 110 | 'variant' => 'blue', 111 | 'price' => 85.00, 112 | 'quantity' => 2, 113 | 'coupon_code' => 'TEST', 114 | 'position' => 4 115 | ]; 116 | 117 | $request->addProduct($productData2); 118 | 119 | // Don't forget to set the product action, which is PURCHASE in the example below 120 | $request->setProductActionToPurchase(); 121 | 122 | // Finally, you need to send a hit; in this example, we are sending an Event 123 | $request->setEventCategory('Checkout') 124 | ->setEventAction('Purchase') 125 | ->sendEvent(); 126 | ``` 127 | 128 | 129 | Asynchronous Mode 130 | ----------------- 131 | By default, sending a hit to Google Analytics will be a synchronous request, and it will block the execution of 132 | the script until the latter gets a response from the server or terminates by timeout after 100 seconds (throwing a Guzzle exception). 133 | However, if you turn the asynchronous mode on in the component config, asynchronous non-blocking requests will be used. 134 | ```php 135 | 'asyncMode' => true, 136 | ``` 137 | This means that we are sending the request and not waiting for response. 138 | The `TheIconic\Tracking\GoogleAnalytics\AnalyticsResponse` object that you will get back has `null` for HTTP status code. 139 | 140 | You can also send an asynchronous request even if you haven’t turned it on in the config. Just call `setAsyncRequest(true)` 141 | before sending the hit: 142 | ```php 143 | \Yii::$app->ga->request() 144 | ->setClientId('12345678') 145 | ->setDocumentPath('/mypage') 146 | ->setAsyncRequest(true) 147 | ->sendPageview(); 148 | ``` 149 | 150 | Auto set clientId from cookie 151 | ----------------------------- 152 | If you set `autoSetClientId` to `true` durning component configuration you must disable `enableCookieValidation`. 153 | You can do this by configuring `request` component. Otherwise, the auto set clientId will not work. 154 | 155 | ```php 156 | 'components' => [ 157 | 'request' => [ 158 | 'enableCookieValidation' => false, 159 | ], 160 | ] 161 | ``` 162 | --------------------------------------------------------------------------------