analytic.php 9.61 KB
<?php
    use artbox\gentelella\widgets\XPanel;
    use dosamigos\highcharts\HighCharts;
    use yii\helpers\VarDumper;
    
    $client = new Google_Client();
    
    $client->setAuthConfig(\Yii::getAlias('@common/config/Artbox-85b8559147bc.json'));
    $client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);
    
    $analytics = new Google_Service_AnalyticsReporting($client);
    
    $profile_id = "119240817";
    
    $dateRange = new Google_Service_AnalyticsReporting_DateRange();
    $dateRange->setStartDate("30daysAgo");
    $dateRange->setEndDate("today");
    
    $sessions = new Google_Service_AnalyticsReporting_Metric();
    $sessions->setExpression('ga:sessions');
    $sessions->setAlias('Сеансы');
    
    $users = new Google_Service_AnalyticsReporting_Metric();
    $users->setExpression('ga:users');
    $users->setAlias('Пользователи');
    
    $views = new Google_Service_AnalyticsReporting_Metric();
    $views->setExpression('ga:pageviews');
    $views->setAlias('Просмотры');
    
    $new_sessions = new Google_Service_AnalyticsReporting_Metric();
    $new_sessions->setExpression('ga:percentNewSessions');
    $new_sessions->setAlias('Новые сессии');
    
    $dimensions = new Google_Service_AnalyticsReporting_Dimension();
    $dimensions->setName('ga:date');
    
    $request = new Google_Service_AnalyticsReporting_ReportRequest();
    $request->setViewId($profile_id);
    $request->setDateRanges($dateRange);
    $request->setMetrics(
        [
            $sessions,
            $users,
            $views,
            $new_sessions,
        ]
    );
    $request->setDimensions($dimensions);
    
    $body = new Google_Service_AnalyticsReporting_GetReportsRequest();
    $body->setReportRequests([ $request ]);
    $response = $analytics->reports->batchGet($body);
    
    //    VarDumper::dump($response, 10, true);die();
    
    function printResults($reports)
    {
        $data = [];
        for ($reportIndex = 0; $reportIndex < count($reports); $reportIndex++) {
            $report = $reports[ $reportIndex ];
            $header = $report->getColumnHeader();
            $dimensionHeaders = $header->getDimensions();
            $metricHeaders = $header->getMetricHeader()
                                    ->getMetricHeaderEntries();
            $rows = $report->getData()
                           ->getRows();
            $totals = $report->getData()
                             ->getTotals();
            $total_values = $totals[ 0 ]->getValues();
            
            $data[ 'sessions' ] = $total_values[ 0 ];
            $data[ 'users' ] = $total_values[ 1 ];
            $data[ 'views' ] = $total_values[ 2 ];
            $data[ 'new' ] = $total_values[ 3 ];
            
            for ($rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
                $row = $rows[ $rowIndex ];
                $dimensions = $row->getDimensions();
                $metrics = $row->getMetrics();
                for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
                    //                    print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n");
                }
                
                for ($j = 0; $j < count($metricHeaders) && $j < count($metrics); $j++) {
                    $values = $metrics[ $j ];
                    for ($valueIndex = 0; $valueIndex < count($values->getValues()); $valueIndex++) {
                        $value = $values->getValues()[ $valueIndex ];
                        $data[ $valueIndex ][] = (int) $value;
                    }
                }
            }
        }
        
        return $data;
    }
    
    $data = printResults($response->getReports());

?>

<div class="row">
    <div class="animated flipInY col-lg-3 col-md-3 col-sm-6 col-xs-12">
        <div class="tile-stats">
            <div class="icon"><i class="fa fa-clock-o"></i>
            </div>
            <div class="count"><?= $data[ 'sessions' ] ?></div>
            
            <h3>Sessions</h3>
            <p>Lorem ipsum psdea itgum rixt.</p>
        </div>
    </div>
    <div class="animated flipInY col-lg-3 col-md-3 col-sm-6 col-xs-12">
        <div class="tile-stats">
            <div class="icon"><i class="fa fa-user"></i>
            </div>
            <div class="count"><?= $data[ 'users' ] ?></div>
            
            <h3>Users</h3>
            <p>Lorem ipsum psdea itgum rixt.</p>
        </div>
    </div>
    <div class="animated flipInY col-lg-3 col-md-3 col-sm-6 col-xs-12">
        <div class="tile-stats">
            <div class="icon"><i class="fa fa-eye"></i>
            </div>
            <div class="count"><?= $data[ 'views' ] ?></div>
            
            <h3>Page views</h3>
            <p>Lorem ipsum psdea itgum rixt.</p>
        </div>
    </div>
    <div class="animated flipInY col-lg-3 col-md-3 col-sm-6 col-xs-12">
        <div class="tile-stats">
            <div class="icon"><i class="fa fa-plus"></i>
            </div>
            <div class="count"><?= round(intval($data[ 'new' ]), 2) ?> %</div>
            
            <h3>New sessions</h3>
            <p>Lorem ipsum psdea itgum rixt.</p>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <?php $panel = XPanel::begin(
            [
                'title'         => 'Analytics',
                'toolbarLayout' => '{collapse}',
            ]
        ); ?>
        
        <?php
            echo HighCharts::widget(
                [
                    'clientOptions' => [
                        'colors'      => [
                            '#9ABCC3',
                            '#A8E3D6',
                        ],
                        'chart'       => [
                            'type'     => 'area',
                            'zoomType' => 'x',
                        ],
                        'title'       => [
                            'text' => 'Analytics',
                        ],
                        'yAxis'       => [
                            'title' => [
                                'text' => 'Sessions count',
                            ],
                        ],
                        'series'      => [
                            [
                                'name' => 'Sessions',
                                'data' => $data[ 0 ],
                            ],
                            [
                                'name' => 'Users',
                                'data' => $data[ 1 ],
                            ],
                        ],
                        'credits'     => [
                            'enabled' => false,
                        ],
                        'plotOptions' => [
                            'area' => [
                                'marker' => [
                                    'enabled' => false,
                                    'symbol'  => 'circle',
                                    'radius'  => 2,
                                    'states'  => [
                                        'hover' => [
                                            'enabled' => true,
                                        ],
                                    ],
                                ],
                            ],
                        ],
                    ],
                ]
            );
        ?>
        
        <?php $panel::end(); ?>
    
    </div>
</div>

<div class="row">
    <div class="col-lg-5 col-md-5 col-sm-12 col-xs-12">
        <?php $panel = XPanel::begin(
            [
                'title'         => 'Analytics',
                'toolbarLayout' => '{collapse}',
            ]
        ); ?>
        
        <?php
            echo HighCharts::widget(
                [
                    'clientOptions' => [
                        'exporting'   => [
                            'enabled' => false,
                        ],
                        'colors'      => [
                            '#9ABCC3',
                            '#A8E3D6',
                        ],
                        'chart'       => [
                            'plotBackgroundColor' => null,
                            'plotBorderWidth'     => null,
                            'plotShadow'          => false,
                            'type'                => 'pie',
                        ],
                        'title'       => [
                            'text' => 'Analytics',
                        ],
                        'series'      => [
                            [
                                'name' => 'Sessions',
                                'data' => [
                                    [
                                        'name' => 'New visitor',
                                        'y'    => round(intval($data[ 'new' ]), 2),
                                    ],
                                    [
                                        'name' => 'Returning Visitor',
                                        'y'    => 100 - round(intval($data[ 'new' ]), 2),
                                    ],
                                ],
                            ],
                        ],
                        'credits'     => [
                            'enabled' => false,
                        ],
                        'plotOptions' => [
                            'pie' => [
                                'allowPointSelect' => true,
                                'cursor'           => 'pointer',
                            ],
                        ],
                    ],
                ]
            );
        ?>
        
        <?php $panel::end(); ?>
    </div>

</div>