|
|
@ -16,7 +16,7 @@ class Gaapi extends CI_Controller
|
|
|
|
|
|
|
|
|
|
|
|
public function user_track()
|
|
|
|
public function user_track()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$cid = $this->input->get_post('cid');
|
|
|
|
$cid = $this->input->get_post('cid');
|
|
|
|
$analytics = $this->initializeAnalytics();
|
|
|
|
$analytics = $this->initializeAnalytics();
|
|
|
|
$response = $this->getReport($analytics, $cid);
|
|
|
|
$response = $this->getReport($analytics, $cid);
|
|
|
|
$this->printResults($response);
|
|
|
|
$this->printResults($response);
|
|
|
@ -24,17 +24,17 @@ class Gaapi extends CI_Controller
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Initializes an Analytics Reporting API V4 service object.
|
|
|
|
* Initializes an Analytics Reporting API V4 service object.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return An authorized Analytics Reporting API V4 service object.
|
|
|
|
* @return An authorized Analytics Reporting API V4 service object.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
function initializeAnalytics()
|
|
|
|
function initializeAnalytics()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
// Use the developers console and download your service account
|
|
|
|
// Use the developers console and download your service account
|
|
|
|
// credentials in JSON format. Place them in this directory or
|
|
|
|
// credentials in JSON format. Place them in this directory or
|
|
|
|
// change the key file location if necessary.
|
|
|
|
// change the key file location if necessary.
|
|
|
|
$KEY_FILE_LOCATION = __DIR__ . '/gaapi_json/sylvan-box-234910-357cb59e6bf0.json';
|
|
|
|
$KEY_FILE_LOCATION = 'c:/gaapi_json/sylvan-box-234910-357cb59e6bf0.json';
|
|
|
|
|
|
|
|
|
|
|
|
// Create and configure a new client object.
|
|
|
|
// Create and configure a new client object.
|
|
|
|
$client = new Google_Client();
|
|
|
|
$client = new Google_Client();
|
|
|
@ -48,14 +48,14 @@ class Gaapi extends CI_Controller
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Queries the Analytics Reporting API V4.
|
|
|
|
* Queries the Analytics Reporting API V4.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param service An authorized Analytics Reporting API V4 service object.
|
|
|
|
* @param service An authorized Analytics Reporting API V4 service object.
|
|
|
|
* @return The Analytics Reporting API V4 response.
|
|
|
|
* @return The Analytics Reporting API V4 response.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
function getReport($analytics, $cid_no)
|
|
|
|
function getReport($analytics, $cid_no)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
//die($cid);
|
|
|
|
//die($cid);
|
|
|
|
|
|
|
|
|
|
|
|
// Replace with your view ID, for example XXXX.
|
|
|
|
// Replace with your view ID, for example XXXX.
|
|
|
|
$VIEW_ID = "68484932";
|
|
|
|
$VIEW_ID = "68484932";
|
|
|
@ -77,24 +77,24 @@ class Gaapi extends CI_Controller
|
|
|
|
$cid->setName("ga:dimension1");
|
|
|
|
$cid->setName("ga:dimension1");
|
|
|
|
$hitTime = new Google_Service_AnalyticsReporting_Dimension();
|
|
|
|
$hitTime = new Google_Service_AnalyticsReporting_Dimension();
|
|
|
|
$hitTime->setName("ga:dimension3");
|
|
|
|
$hitTime->setName("ga:dimension3");
|
|
|
|
$ip = new Google_Service_AnalyticsReporting_Dimension();
|
|
|
|
$ip = new Google_Service_AnalyticsReporting_Dimension();
|
|
|
|
$ip->setName("ga:dimension3");
|
|
|
|
$ip->setName("ga:dimension3");
|
|
|
|
|
|
|
|
|
|
|
|
// Create Dimension Filter 1
|
|
|
|
// Create Dimension Filter 1
|
|
|
|
$cidFilter = new Google_Service_AnalyticsReporting_DimensionFilter();
|
|
|
|
$cidFilter = new Google_Service_AnalyticsReporting_DimensionFilter();
|
|
|
|
$cidFilter->setDimensionName("ga:dimension1");
|
|
|
|
$cidFilter->setDimensionName("ga:dimension1");
|
|
|
|
$cidFilter->setOperator('EXACT');
|
|
|
|
$cidFilter->setOperator('EXACT');
|
|
|
|
$cidFilter->setExpressions($cid_no);
|
|
|
|
$cidFilter->setExpressions($cid_no);
|
|
|
|
|
|
|
|
|
|
|
|
// Create the DimensionFilterClauses
|
|
|
|
// Create the DimensionFilterClauses
|
|
|
|
$dimensionFilterClause = new Google_Service_AnalyticsReporting_DimensionFilterClause();
|
|
|
|
$dimensionFilterClause = new Google_Service_AnalyticsReporting_DimensionFilterClause();
|
|
|
|
$dimensionFilterClause->setFilters(array($cidFilter));
|
|
|
|
$dimensionFilterClause->setFilters(array($cidFilter));
|
|
|
|
|
|
|
|
|
|
|
|
// OrderBy maybe some bugs
|
|
|
|
// OrderBy maybe some bugs
|
|
|
|
$order = new Google_Service_AnalyticsReporting_OrderBy;
|
|
|
|
$order = new Google_Service_AnalyticsReporting_OrderBy;
|
|
|
|
$order->setFieldName("ga:dimension3");
|
|
|
|
$order->setFieldName("ga:dimension3");
|
|
|
|
$order->setOrderType("VALUE");
|
|
|
|
$order->setOrderType("VALUE");
|
|
|
|
$order->setSortOrder("DESCENDING");
|
|
|
|
$order->setSortOrder("DESCENDING");
|
|
|
|
|
|
|
|
|
|
|
|
// Create the ReportRequest object.
|
|
|
|
// Create the ReportRequest object.
|
|
|
|
$request = new Google_Service_AnalyticsReporting_ReportRequest();
|
|
|
|
$request = new Google_Service_AnalyticsReporting_ReportRequest();
|
|
|
@ -102,8 +102,8 @@ class Gaapi extends CI_Controller
|
|
|
|
$request->setDateRanges($dateRange);
|
|
|
|
$request->setDateRanges($dateRange);
|
|
|
|
$request->setMetrics(array($pv));
|
|
|
|
$request->setMetrics(array($pv));
|
|
|
|
$request->setDimensions(array($pageUrl, $cid, $hitTime));
|
|
|
|
$request->setDimensions(array($pageUrl, $cid, $hitTime));
|
|
|
|
$request->setDimensionFilterClauses(array($dimensionFilterClause));
|
|
|
|
$request->setDimensionFilterClauses(array($dimensionFilterClause));
|
|
|
|
$request->setOrderBys(array($order));
|
|
|
|
$request->setOrderBys(array($order));
|
|
|
|
|
|
|
|
|
|
|
|
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
|
|
|
|
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
|
|
|
|
$body->setReportRequests(array($request));
|
|
|
|
$body->setReportRequests(array($request));
|
|
|
@ -118,26 +118,26 @@ class Gaapi extends CI_Controller
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
function printResults($reports)
|
|
|
|
function printResults($reports)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$alais_array = array(
|
|
|
|
$alais_array = array(
|
|
|
|
"ga:dimension1" => "cid",
|
|
|
|
"ga:dimension1" => "cid",
|
|
|
|
"ga:dimension3" => "hitTime",
|
|
|
|
"ga:dimension3" => "hitTime",
|
|
|
|
"ga:pagePath" => "path"
|
|
|
|
"ga:pagePath" => "path"
|
|
|
|
);
|
|
|
|
);
|
|
|
|
$rs_array = array();
|
|
|
|
$rs_array = array();
|
|
|
|
for ($reportIndex = 0; $reportIndex < count($reports); $reportIndex++) {
|
|
|
|
for ($reportIndex = 0; $reportIndex < count($reports); $reportIndex++) {
|
|
|
|
$report = $reports[$reportIndex];
|
|
|
|
$report = $reports[$reportIndex];
|
|
|
|
$header = $report->getColumnHeader();
|
|
|
|
$header = $report->getColumnHeader();
|
|
|
|
$dimensionHeaders = $header->getDimensions();
|
|
|
|
$dimensionHeaders = $header->getDimensions();
|
|
|
|
$metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
|
|
|
|
$metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
|
|
|
|
$rows = $report->getData()->getRows();
|
|
|
|
$rows = $report->getData()->getRows();
|
|
|
|
$rs_row = array();
|
|
|
|
$rs_row = array();
|
|
|
|
for ($rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
|
|
|
|
for ($rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
|
|
|
|
$row = $rows[$rowIndex];
|
|
|
|
$row = $rows[$rowIndex];
|
|
|
|
$dimensions = $row->getDimensions();
|
|
|
|
$dimensions = $row->getDimensions();
|
|
|
|
$metrics = $row->getMetrics();
|
|
|
|
$metrics = $row->getMetrics();
|
|
|
|
for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
|
|
|
|
for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
|
|
|
|
//print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n");
|
|
|
|
//print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n");
|
|
|
|
$rs_row[$alais_array[$dimensionHeaders[$i]]] = $dimensions[$i];
|
|
|
|
$rs_row[$alais_array[$dimensionHeaders[$i]]] = $dimensions[$i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for ($j = 0; $j < count($metrics); $j++) {
|
|
|
|
for ($j = 0; $j < count($metrics); $j++) {
|
|
|
@ -145,14 +145,13 @@ class Gaapi extends CI_Controller
|
|
|
|
for ($k = 0; $k < count($values); $k++) {
|
|
|
|
for ($k = 0; $k < count($values); $k++) {
|
|
|
|
$entry = $metricHeaders[$k];
|
|
|
|
$entry = $metricHeaders[$k];
|
|
|
|
//print($entry->getName() . ": " . $values[$k] . "\n");
|
|
|
|
//print($entry->getName() . ": " . $values[$k] . "\n");
|
|
|
|
$rs_row[$entry->getName()] = $values[$k];
|
|
|
|
$rs_row[$entry->getName()] = $values[$k];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
array_push($rs_array, $rs_row);
|
|
|
|
array_push($rs_array, $rs_row);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
echo(json_encode($rs_array));
|
|
|
|
echo (json_encode($rs_array));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//end of gaapi
|
|
|
|
//end of gaapi
|
|
|
|