google ga认证迁移到数据中心

feature/trippest
LMR 6 years ago
parent c392462857
commit 6384c57580

@ -1,157 +0,0 @@
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Gaapi extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->permission->is_admin();
$this->site_code = $this->config->item('site_code');
//ga verder
$this->load->library('MY_Composer');
}
public function user_track()
{
$cid = $this->input->get_post('cid');
$analytics = $this->initializeAnalytics();
$response = $this->getReport($analytics, $cid);
$this->printResults($response);
}
/**
* Initializes an Analytics Reporting API V4 service object.
*
* @return An authorized Analytics Reporting API V4 service object.
*/
function initializeAnalytics()
{
// 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.
$KEY_FILE_LOCATION = 'c:/gaapi_json/sylvan-box-234910-357cb59e6bf0.json';
// Create and configure a new client object.
$client = new Google_Client();
$client->setApplicationName("User Tracker");
$client->setAuthConfig($KEY_FILE_LOCATION);
$client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
$analytics = new Google_Service_AnalyticsReporting($client);
return $analytics;
}
/**
* Queries the Analytics Reporting API V4.
*
* @param service An authorized Analytics Reporting API V4 service object.
* @return The Analytics Reporting API V4 response.
*/
function getReport($analytics, $cid_no)
{
//die($cid);
// Replace with your view ID, for example XXXX.
$VIEW_ID = "68484932";
// Create the DateRange object.
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate("30daysAgo");
$dateRange->setEndDate("today");
// Create the Metrics object.
$pv = new Google_Service_AnalyticsReporting_Metric();
$pv->setExpression("ga:pageviews");
$pv->setAlias("pageviews");
//Create the dimensions
$pageUrl = new Google_Service_AnalyticsReporting_Dimension();
$pageUrl->setName("ga:pagePath");
$cid = new Google_Service_AnalyticsReporting_Dimension();
$cid->setName("ga:dimension1");
$hitTime = new Google_Service_AnalyticsReporting_Dimension();
$hitTime->setName("ga:dimension3");
$ip = new Google_Service_AnalyticsReporting_Dimension();
$ip->setName("ga:dimension3");
// Create Dimension Filter 1
$cidFilter = new Google_Service_AnalyticsReporting_DimensionFilter();
$cidFilter->setDimensionName("ga:dimension1");
$cidFilter->setOperator('EXACT');
$cidFilter->setExpressions($cid_no);
// Create the DimensionFilterClauses
$dimensionFilterClause = new Google_Service_AnalyticsReporting_DimensionFilterClause();
$dimensionFilterClause->setFilters(array($cidFilter));
// OrderBy maybe some bugs
$order = new Google_Service_AnalyticsReporting_OrderBy;
$order->setFieldName("ga:dimension3");
$order->setOrderType("VALUE");
$order->setSortOrder("DESCENDING");
// Create the ReportRequest object.
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($VIEW_ID);
$request->setDateRanges($dateRange);
$request->setMetrics(array($pv));
$request->setDimensions(array($pageUrl, $cid, $hitTime));
$request->setDimensionFilterClauses(array($dimensionFilterClause));
$request->setOrderBys(array($order));
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests(array($request));
return $analytics->reports->batchGet($body);
}
/**
* Parses and prints the Analytics Reporting API V4 response.
*
* @param An Analytics Reporting API V4 response.
*/
function printResults($reports)
{
$alais_array = array(
"ga:dimension1" => "cid",
"ga:dimension3" => "hitTime",
"ga:pagePath" => "path"
);
$rs_array = array();
for ($reportIndex = 0; $reportIndex < count($reports); $reportIndex++) {
$report = $reports[$reportIndex];
$header = $report->getColumnHeader();
$dimensionHeaders = $header->getDimensions();
$metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
$rows = $report->getData()->getRows();
$rs_row = array();
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");
$rs_row[$alais_array[$dimensionHeaders[$i]]] = $dimensions[$i];
}
for ($j = 0; $j < count($metrics); $j++) {
$values = $metrics[$j]->getValues();
for ($k = 0; $k < count($values); $k++) {
$entry = $metricHeaders[$k];
//print($entry->getName() . ": " . $values[$k] . "\n");
$rs_row[$entry->getName()] = $values[$k];
}
}
array_push($rs_array, $rs_row);
}
}
echo (json_encode($rs_array));
}
}
//end of gaapi
Loading…
Cancel
Save