PHP code example of webiny / analytics-db

1. Go to this page and download the library: Download webiny/analytics-db 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/ */

    

webiny / analytics-db example snippets


// get analytics instance
$mongo = new \Webiny\Component\Mongo\Mongo('127.0.0.1:27017', 'webiny');
$analytics = new \Webiny\AnalyticsDb\AnalyticsDb($mongo);

// store some visitor data
$analytics->log('visitor')->addDimension('browser', 'chrome')->addDimension('country', 'UK');

// store some revenue data
$analytics->log('revenue', 0, 120.00)
    ->addAttribute('brand', 'Foo')
    ->addDimension('product', 'hdd', 79.50)
    ->addDimension('product', 'mouse', 20.50)
    ->addDimension('tax', 'in-country', 20);

// save the data
$analytics->save();

// query data
$query = $a->query('revenue', 0, DateHelper::rangeLast30Days());

// get total number of visitors for the last 30 days, and group them by day
$result = $query->stats()->getResult();

// get total number of visitors for the last year, and group them by month
$query = $a->query('revenue', 0, DateHelper::rangeYear());
$result = $query->stats()->monthly()->groupByTimestamp()->sortByTimestamp(1)->getResult();

// get revenue for last quarter and group it by revenue type
$query = $a->query('revenue', 0, DateHelper::rangeQ1());
$result = $query->groupByDimensionName()->sortByCount(-1)->getResult();

// track a view on the red product
$analytics->log('product', 'A')
  ->addDimension('color', 'red');

// track a view on the blue product
$analytics->log('product', 'A')
  ->addDimension('color', 'blue');

$query = $a->query('product', 'A', DateHelper::rangeLast30Days());
$result = $query->dimension('color', 'red')->groupByDimensionValue()->sortByCount(-1)->getResult();

$analytics->log('product', 'A')->addAttribute('brand', '10');

// show me top 10 products for brand "10" for last 30 days
$query = $a->query('product', null, DateHelper::rangeLast30Days());
$result = $query->stats()->addAttributeFilter('brand', 10)->sortByCount('-1')->limit(10);

$analytics->log('visitor')->addDimension('browser', 'chrome')->addDimension('country', 'UK');

$analytics->log('page', 123)->addDimension('browser', 'chrome')->addDimension('country', 'UK');

$analytics->log('revenue', 0, 120.00);

$query = $a->query('revenue', 0, DateHelper::rangeLast30Days());

$query = $a->query('revenue', 0, DateHelper::rangeLast30Days());

// get data by day
$result = $query->stats()->getResult();

// get data by month
$result = $query->stats()->monthly()->getResult();

$query = $a->query('revenue', 0, DateHelper::rangeYear());

// show me the revenue breakdown by item type (eg, product, tax)
$result = $query->dimension()->groupByDimensionName()->sortByCount(-1)->getResult();

// show me total revenue just from products
$result = $query->dimension('product')->getResult();

// show me total revenue breakdown by product type
$result = $query->dimension('product')->groupByDimensionValue()->sortByCount(-1)->getResult();

// show me revenue for `HDD` product by days
$result = $query->dimension('package', 'PAYG')->getResult();

// show me total revenue for `HDD` product
$result = $query->dimension('package', 'PAYG')->groupByDimensionName()->getResult();