PHP code example of gradosevic / easy-ga

1. Go to this page and download the library: Download gradosevic/easy-ga library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gradosevic / easy-ga example snippets


$config = [
  'tracking_id' => 'UA-XXXXXXXX-1'
];

$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();

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();

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();

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();

$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 = (new Product())
    ->setCategory('Product category')
    ->setBrand('Product brand')
     // ...
    ->setVariant($variant);

Analytics::create($config)
    ->exception('IOException')
    ->send();

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();