PHP code example of immera / analytics

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

    

immera / analytics example snippets


Analytics::store([
    'action' => 'test',
    'project' => 'analytics',
    'price' => rand(0, 10000) / 100,
    'quantity' => rand(0, 8),
])

$result = Analytics::query()
    ->match(['action' => 'test'])
    ->project(['action' => 1, 'project' => 1])
    ->limit(1)
    ->fetchJson();

$result = Analytics::query()
    ->match([
        'action' => 'test',
        'created_at.year' => 2022,
    ])
    ->group([
        '_id' => [
            'year' => '$created_at.year',
            'month' => '$created_at.month',
            'day' => '$created_at.day',
            'hour' => '$created_at.hour',
        ],
        'count' => [
            '$sum' => 1,
        ],
    ])
    ->sort([
        '_id.year' => -1,
        '_id.month' => -1,
        '_id.day' => -1,
        '_id.hour' => -1,
    ])
    ->limit(100)
    ->fetchJson();

$result = Analytics::query()
    ->match([
        'action' => 'test',
    ])
    ->group([
        '_id' => [
            'year' => '$created_at.year',
            'month' => '$created_at.month',
        ],
        'totalPrice' => [
            '$sum' => '$price',
        ],
        'averageQuantity' => [
            '$avg' => '$quantity',
        ],
    ])
    ->sort([
        '_id.year' => -1,
        '_id.month' => -1,
    ])
    ->limit(100)
    ->fetchJson();