From b32bd1d7fb02e9d33adaff2f7d05245793b27727 Mon Sep 17 00:00:00 2001 From: baibaratsky Date: Fri, 17 Apr 2015 16:33:03 +0300 Subject: [PATCH] Initial commit --- .gitignore | 3 + LICENSE.md | 31 ++++++++++ MeasurementProtocol.php | 56 +++++++++++++++++ README.md | 133 ++++++++++++++++++++++++++++++++++++++++ composer.json | 23 +++++++ 5 files changed, 246 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE.md create mode 100644 MeasurementProtocol.php create mode 100644 README.md create mode 100644 composer.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..157ff0c --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +vendor/ +composer.lock diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..b395016 --- /dev/null +++ b/LICENSE.md @@ -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. diff --git a/MeasurementProtocol.php b/MeasurementProtocol.php new file mode 100644 index 0000000..5b3b046 --- /dev/null +++ b/MeasurementProtocol.php @@ -0,0 +1,56 @@ +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; + } +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..d6972f4 --- /dev/null +++ b/README.md @@ -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(); +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..0cd5f0c --- /dev/null +++ b/composer.json @@ -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": "andrei@baibaratsky.com" + } + ], + "require": { + "theiconic/php-ga-measurement-protocol": "1.1.*", + "yiisoft/yii2": "*" + }, + "autoload": { + "psr-4": { + "baibaratsky\\yii\\google\\analytics\\": "" + } + } +}