From 6384c575806c9b8027b189e7a90d4b805c5880f5 Mon Sep 17 00:00:00 2001 From: LMR <59361885@qq.com> Date: Tue, 16 Apr 2019 13:58:53 +0800 Subject: [PATCH] =?UTF-8?q?google=20ga=E8=AE=A4=E8=AF=81=E8=BF=81=E7=A7=BB?= =?UTF-8?q?=E5=88=B0=E6=95=B0=E6=8D=AE=E4=B8=AD=E5=BF=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/controllers/gaapi.php | 157 ------------------------------ 1 file changed, 157 deletions(-) delete mode 100644 application/controllers/gaapi.php diff --git a/application/controllers/gaapi.php b/application/controllers/gaapi.php deleted file mode 100644 index 2027f22f..00000000 --- a/application/controllers/gaapi.php +++ /dev/null @@ -1,157 +0,0 @@ -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