207 lines
7.4 KiB
PHP
207 lines
7.4 KiB
PHP
<?php
|
|
|
|
namespace BRSystems\Core\Library\cGoogleApi;
|
|
|
|
use Google_Client;
|
|
use Google_Service_Analytics;
|
|
use Google_Service_AnalyticsReporting;
|
|
use Google_Service_AnalyticsReporting_DateRange;
|
|
use Google_Service_AnalyticsReporting_Dimension;
|
|
use Google_Service_AnalyticsReporting_GetReportsRequest;
|
|
use Google_Service_AnalyticsReporting_Metric;
|
|
use Google_Service_AnalyticsReporting_ReportRequest;
|
|
|
|
/**
|
|
* Class for
|
|
*
|
|
* @category LIB cGoogleApi
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @since Version 1.0
|
|
*/
|
|
class cGoogleApi
|
|
{
|
|
public $KEY_FILE_LOCATION = "";
|
|
public $oClient;
|
|
private $GOOGLE_ANALYTICS_VIEW_ID;
|
|
|
|
/**
|
|
* Class constructor
|
|
*/
|
|
public function __construct($sAppTitle, $ANALYTICS_VIEW_ID)
|
|
{
|
|
if (strlen($sAppTitle) && strlen($ANALYTICS_VIEW_ID)) {
|
|
// Make a curl function if doesn't exists for google api
|
|
if (!function_exists('curl_reset')) {
|
|
function curl_reset(&$ch)
|
|
{
|
|
$ch = curl_init();
|
|
}
|
|
}
|
|
|
|
$this->GOOGLE_ANALYTICS_VIEW_ID = $ANALYTICS_VIEW_ID;
|
|
|
|
// Use the developers console and download your service account
|
|
// credentials in JSON format. Place them in this directory or
|
|
// change the key file location if necessary.
|
|
$this->KEY_FILE_LOCATION = __DIR__ . '/client_secret.json';
|
|
|
|
// Create and configure a new client object.
|
|
$this->oClient = new Google_Client();
|
|
$this->oClient->setApplicationName($sAppTitle);
|
|
$this->oClient->setAuthConfig($this->KEY_FILE_LOCATION);
|
|
$this->oClient->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Function to get total active visitors from real time data
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category LIB cGoogleApi
|
|
* @return string - active visitors
|
|
*/
|
|
public function getTotalVisitors()
|
|
{
|
|
$service = new Google_Service_Analytics($this->oClient);
|
|
$activeVisitors = $service->data_realtime->get(
|
|
$this->GOOGLE_ANALYTICS_VIEW_ID,
|
|
'rt:activeVisitors'
|
|
);
|
|
|
|
return $activeVisitors->totalsForAllResults['rt:activeVisitors'];
|
|
}
|
|
|
|
/**
|
|
* Function to get user per day for one this and last month
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @param array $vDates ('s' => start date, 'e' => end date)
|
|
* @category LIB cGoogleApi
|
|
* @return array of data
|
|
*/
|
|
public function getUsersPerDayForOneMonth($vDates)
|
|
{
|
|
$analytics = new Google_Service_AnalyticsReporting($this->oClient);
|
|
|
|
// Create the DateRange object and set start/end dates.
|
|
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
|
|
$dateRange->setStartDate(date('Y-m-d', $vDates['s']));
|
|
$dateRange->setEndDate(date('Y-m-d', $vDates['e']));
|
|
|
|
// Create the Metrics object.
|
|
$data = new Google_Service_AnalyticsReporting_Metric();
|
|
$data->setExpression("ga:users");
|
|
$data->setAlias("users a day:");
|
|
|
|
//Create the Dimensions object.
|
|
$dimension = new Google_Service_AnalyticsReporting_Dimension();
|
|
$dimension->setName("ga:date");
|
|
|
|
// Create the ReportRequest object.
|
|
$request = new Google_Service_AnalyticsReporting_ReportRequest();
|
|
$request->setViewId($this->GOOGLE_ANALYTICS_VIEW_ID);
|
|
$request->setDateRanges($dateRange);
|
|
$request->setDimensions(array($dimension));
|
|
$request->setMetrics(array($data));
|
|
|
|
// Get and work with data
|
|
$vReturn = array();
|
|
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
|
|
$body->setReportRequests(array($request));
|
|
$vData = $analytics->reports->batchGet($body);
|
|
if (!empty($vData)) {
|
|
for ($index = 0; $index < count($vData); $index++) {
|
|
$d = $vData[$index];
|
|
$rows = $d->getData()->getRows();
|
|
|
|
for ($rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
|
|
$row = $rows[$rowIndex];
|
|
$dimensions = $row->getDimensions();
|
|
$metrics = $row->getMetrics();
|
|
if (!empty($dimensions[0])) {
|
|
for ($j = 0; $j < count($metrics); $j++) {
|
|
$values = $metrics[$j]->getValues();
|
|
if (!empty($values[0])) {
|
|
$key = date("n", strtotime($dimensions[0]));
|
|
$subKey = date("j", strtotime($dimensions[0]));
|
|
$vReturn[$key][$subKey] = $values[0];
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
return $vReturn;
|
|
}
|
|
|
|
/**
|
|
* Function to get user per month for this and last year
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @param array $vDates ('s' => start date, 'e' => end date)
|
|
* @category LIB cGoogleApi
|
|
* @return array of data
|
|
*/
|
|
public function getUsersPerMonthForYear($vDates)
|
|
{
|
|
$analytics = new Google_Service_AnalyticsReporting($this->oClient);
|
|
|
|
// Create the DateRange object and set start/end dates.
|
|
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
|
|
$dateRange->setStartDate(date('Y-m-d', $vDates['s']));
|
|
$dateRange->setEndDate(date('Y-m-d', $vDates['e']));
|
|
|
|
// Create the Metrics object.
|
|
$data = new Google_Service_AnalyticsReporting_Metric();
|
|
$data->setExpression("ga:users");
|
|
$data->setAlias("users a month:");
|
|
|
|
//Create the Dimensions object.
|
|
$dimension = new Google_Service_AnalyticsReporting_Dimension();
|
|
$dimension->setName("ga:year");
|
|
//Create the Dimensions object.
|
|
$dimension2 = new Google_Service_AnalyticsReporting_Dimension();
|
|
$dimension2->setName("ga:month");
|
|
|
|
// Create the ReportRequest object.
|
|
$request = new Google_Service_AnalyticsReporting_ReportRequest();
|
|
$request->setViewId($this->GOOGLE_ANALYTICS_VIEW_ID);
|
|
$request->setDateRanges($dateRange);
|
|
$request->setDimensions(array($dimension, $dimension2));
|
|
$request->setMetrics(array($data));
|
|
|
|
// Get and work with data
|
|
$vReturn = array();
|
|
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
|
|
$body->setReportRequests(array($request));
|
|
$vData = $analytics->reports->batchGet($body);
|
|
|
|
if (!empty($vData)) {
|
|
for ($index = 0; $index < count($vData); $index++) {
|
|
$d = $vData[$index];
|
|
$rows = $d->getData()->getRows();
|
|
|
|
for ($rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
|
|
$row = $rows[$rowIndex];
|
|
$dimensions = $row->getDimensions();
|
|
$metrics = $row->getMetrics();
|
|
|
|
if (!empty($dimensions[0]) && !empty($dimensions[1])) {
|
|
for ($j = 0; $j < count($metrics); $j++) {
|
|
$values = $metrics[$j]->getValues();
|
|
if (!empty($values[0]))
|
|
$vReturn[$dimensions[0]][intval($dimensions[1])] = $values[0];
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
return $vReturn;
|
|
}
|
|
}
|