dev-master
9999999-devPHP Google Analytics library for easy use
MIT
The Requires
The Development Requires
by Goran Radosevic
1.0
1.0.0.0PHP Google Analytics library for easy use
MIT
The Requires
The Development Requires
by Goran Radosevic
Wallogit.com
2017 © Pedro Peláez
PHP Google Analytics library for easy use
PHP Google Analytics library for quick and easy use, (*1)
This library was created for people who want to start using Google Analytics Measurement Protocol with minimum time for setup. It is basically a wrapper for theiconic/php-ga-measurement-protocol with easy to use objects., (*3)
For more information about API please check: - The Iconic API - Working with Measurement Protocol - Measurement Protocol Parameter Reference, (*4)
composer require gradosevic/easy-ga$config = [ 'tracking_id' => 'UA-XXXXXXXX-1' ];
or, (*5)
$config = 'UA-XXXXXXXX-1';
$config = [
'tracking_id' => '',
'protocol_version' => 1,
'client_id' => 1,
'user_id' => 1,
'is_async' => true
];
use Gradosevic\EasyGA\Analytics;
Analytics::create($config)
->event('Simple Event Group', 'Simple Event Action')
->send();
that's it!, (*6)
Analytics::create($config)
->event('Event Constructor', 'Event Constr-Action', 'Event Constr-Label', 45)
->send();
Analytics::create($config)
->event()
->setCategory('Event Methods')
->setAction('Event Methods-Action')
->setLabel('Event Methods-Label')
->setValue(11)
->send();
Analytics::create($config)
->page('/simple/page/view', 'Simple Page View')
->send();
$path = '/document/page/from/methods';
$title = 'Page Complete From Methods';
$hostname = 'mydomain.com';
$referrer = 'myblog.com';
Analytics::create($config)
->page()
->setDocumentPath($path)
->setDocumentTitle($title)
->setDocumentHostName($hostname)
->setDocumentReferrer($referrer)
->send();
To send custom data we need to define it in Google Analytics first: - Log in to Google Analytics - Go to Admin tab - Select Account - In property column click on Custom Definitions->Custom Dimensions - Click on +New Custom Dimension, give it a name, scope -> Hit and make sure it's active - Click Create - Remember index (it will be used in Easy GA) - Click on Custom Definitions->Custom Metrics - Click on +New Custom Dimension, give it a name, scope -> Hit, Type -> Integer, make sure it's active - Click Create - We have created custom dimension with index 1. Repeat the process for new data, (*7)
Analytics::create($config)
->event()
->setCategory('Custom Data')
->setAction('Custom Data Action')
->setLabel('Sent Custom Values')
->setCustomDimension('custom value a', 1) // Index is 1
->setCustomDimension('custom value b', 2) // Index is 2
->setValue(9) // Event value
->send();
To read custom data:, (*8)
use Gradosevic\EasyGA\Analytics;
use Gradosevic\EasyGA\Product;
Analytics::create($config)
->transaction('TransactionID-2342541')
->setProduct(Product::create('MINPRODUCT-56471', 'Min Product', 1.99))
->sendPurchase();
$transactionID = 2315;
$affiliation = 'Affiliate Name';
$revenue = 456.99;
$tax = 10.0;
$shipping = 9.99;
$coupon = '20OFF';
Analytics::create($config)
->transaction($transactionID, $affiliation, $revenue, $tax, $shipping, $coupon)
->sendPurchase();
In case you need to loop throug products, you should unchain Easy GA like this:, (*9)
$products = array(); //Load your products
$transaction = Analytics::create($config)->transaction('2384287');
foreach($products as $product){
$transaction->setProduct(Product::create($product['sku'], $product['name'], $product['price']))
}
$transaction->sendPurchase();
Product has following properties: - category - brand - coupon - name - position - price - quantity - sku - variant, (*10)
To set all product's properties, create new product object and use setter methods:, (*11)
$product = (new Product())
->setCategory('Product category')
->setBrand('Product brand')
// ...
->setVariant($variant);
To read transactions: - Wait for about 10 minutes or less to see the data - Go to Reporting -> Conversions -> E-commerce -> Overview - Click on time filter on right (Hourly, Day) and Revenue Sources below to see the data - Click on other options under E-commerce to see other reports - Important: Sometimes the data is not shown and you need to click on some filters to show it (time filters or links below), (*12)
Analytics::create($config)
->exception('IOException')
->send();
To show custom exceptions in Dashboard please follow this tutorial:, (*13)
Easy GA objects provide simplest possible set of data to send to GA. If you need to set more data, you can always access underlying library API and all it's methods, simple by using api() method whenever you need it in code.
Example: Use Easy GA for required data and add other data from API:, (*14)
Analytics::create($config)
// Using Easy GA Event constructor to pass data
->event('Advanced Event Group', 'Advanced Event Action')
// Using Easy GA class methods
->setLabel('Event wcd label')
// The moment when we switch to API
->api()
// Using API method
->setDocumentReferrer('referrer.com')
// Using API method
->setDocumentPath('/event/document/path')
// Important: We can not use Easy GA send() method any more.
// We have to use API send method now.
->sendEvent();
For other examples how to use this library, please look at the tests in the library, (*15)
Easy GA is licensed under the MIT license, (*16)
Author: Goran Radosevic, (*17)
PHP Google Analytics library for easy use
MIT
PHP Google Analytics library for easy use
MIT