-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b32bd1d
Showing
5 changed files
with
246 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea | ||
vendor/ | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
Google Analytics Measurement Protocol for Yii2 is free software. | ||
It is released under the terms of the following BSD License. | ||
|
||
Copyright © 2015 by Andrei Baibaratsky. All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions | ||
are met: | ||
|
||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above copyright | ||
notice, this list of conditions and the following disclaimer in | ||
the documentation and/or other materials provided with the | ||
distribution. | ||
* The names of the copyright holders and contributors may not be | ||
used to endorse or promote products derived from this software | ||
without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace baibaratsky\yii\google\analytics; | ||
|
||
use TheIconic\Tracking\GoogleAnalytics\Analytics; | ||
use yii\base\Object; | ||
|
||
/** | ||
* Class MeasurementProtocol | ||
* @package baibaratsky\yii\google\analytics | ||
*/ | ||
class MeasurementProtocol extends Object | ||
{ | ||
/** @var string Tracking ID (UA-XXXX-Y) */ | ||
public $trackingId; | ||
|
||
/** @var string Protocol version */ | ||
public $version = '1'; | ||
|
||
/** @var bool Use HTTPS instead of plain HTTP */ | ||
public $useSsl = false; | ||
|
||
/** @var bool Override the IP address by the user’s one */ | ||
public $overrideIp = true; | ||
|
||
/** @var bool Anonymize the IP address of the sender */ | ||
public $anonymizeIp = false; | ||
|
||
/** @var bool Use asynchronous requests (not waiting for a response) */ | ||
public $asyncMode = false; | ||
|
||
/** | ||
* @return Analytics | ||
*/ | ||
public function request() | ||
{ | ||
$request = new Analytics($this->useSsl); | ||
$request->setTrackingId($this->trackingId) | ||
->setProtocolVersion($this->version) | ||
->setAsyncRequest($this->asyncMode); | ||
|
||
if ($this->overrideIp) { | ||
if (isset(\Yii::$app->request)) { | ||
$request->setIpOverride(\Yii::$app->request->userIP); | ||
} else { | ||
\Yii::warning('Cant’t access the request component to override the IP address. Is it a web application?'); | ||
} | ||
} | ||
|
||
if ($this->anonymizeIp) { | ||
$request->setAnonymizeIp(1); | ||
} | ||
|
||
return $request; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
Google Analytics Measurement Protocol for Yii2 | ||
============================================== | ||
|
||
>Interact with Google Analytics directly. No need of any JS code. Pure server-side. | ||
Full support for all of the | ||
[Google Analytics Measurement Protocol](https://developers.google.com/analytics/devguides/collection/protocol/v1/) | ||
methods provided. | ||
|
||
|
||
Installation | ||
------------ | ||
0. 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:1.0.* | ||
``` | ||
or add | ||
``` | ||
"baibaratsky/yii2-ga-measurement-protocol": "1.0.*" | ||
``` | ||
to the `require` section of your `composer.json` file. | ||
|
||
0. Add the component configuration in your `main.php` config file: | ||
```php | ||
'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 the Google servers | ||
'overrideIp' => false, // IP is overrided by default by the user’s one, but you can turn it off | ||
'anonymizeIp' => true, // If you want to anonymize the IP address of the sender | ||
'asyncMode' => true, // Enables the asynchronous mode (see below) | ||
], | ||
], | ||
``` | ||
|
||
|
||
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 | ||
```php | ||
\Yii::$app->ga->request() | ||
->setClientId('12345678') | ||
->setDocumentPath('/mypage') | ||
->sendPageview(); | ||
``` | ||
|
||
#### Order Tracking with Enhanced E-commerce | ||
|
||
```php | ||
$request = \Yii::$app->ga->request(); | ||
|
||
// Build the order data programmatically, including each order product 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, 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, in this case to PURCHASE | ||
$request->setProductActionToPurchase(); | ||
|
||
// Finally, you must send a hit, in this case we send an Event | ||
$request->setEventCategory('Checkout') | ||
->setEventAction('Purchase') | ||
->sendEvent(); | ||
``` | ||
|
||
|
||
Asynchronous Mode | ||
----------------- | ||
By default, sending a hit to GA will be a synchronous request, and block the execution of the script until it gets | ||
a response from the server or timeouts 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. | ||
```php | ||
'asyncMode' => true, | ||
``` | ||
This means that we are sending the request and not waiting for a 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 turn it on in the config. Just call `setAsyncRequest(true)` | ||
before sending the hit: | ||
```php | ||
\Yii::$app->ga->request() | ||
->setClientId('12345678') | ||
->setDocumentPath('/mypage') | ||
->setAsyncRequest(true) | ||
->sendPageview(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "baibaratsky/yii2-ga-measurement-protocol", | ||
"description": "Google Analytics Measurement Protocol for Yii2", | ||
"type": "yii2-extension", | ||
"keywords": ["yii", "yii2", "ga", "google analytics", "measurement protocol", "analytics.js"], | ||
"license": "BSD-3-Clause", | ||
"homepage": "http://github.com/baibaratsky/yii2-ga-measurement-protocol", | ||
"authors": [ | ||
{ | ||
"name": "Andrei Baibaratsky", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"theiconic/php-ga-measurement-protocol": "1.1.*", | ||
"yiisoft/yii2": "*" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"baibaratsky\\yii\\google\\analytics\\": "" | ||
} | ||
} | ||
} |