Commit 5776b7c89a38c735e4489457e9528bb79275f34b

Authored by Alex Savenko
1 parent da7f3c1f

ga

Showing 1 changed file with 24 additions and 71 deletions   Show diff stats
backend/views/report/index.php
... ... @@ -15,82 +15,11 @@ use yii\helpers\Url;
15 15 $this->title = Yii::t('app', 'Report');
16 16 $this->params['breadcrumbs'][] = $this->title;
17 17 ?>
18   - <script>
19   - (function(w,d,s,g,js,fs){
20   - g=w.gapi||(w.gapi={});g.analytics={q:[],ready:function(f){this.q.push(f);}};
21   - js=d.createElement(s);fs=d.getElementsByTagName(s)[0];
22   - js.src='https://apis.google.com/js/platform.js';
23   - fs.parentNode.insertBefore(js,fs);js.onload=function(){g.load('analytics');};
24   - }(window,document,'script'));
25   - </script>
26 18  
27 19 <div>
28 20 <h1><?= Html::encode($this->title) ?></h1>
29 21 </div>
30 22  
31   - <div id="embed-api-auth-container"></div>
32   - <div id="chart-container"></div>
33   - <div id="view-selector-container"></div>
34   -
35   - <script>
36   -
37   - gapi.analytics.ready(function() {
38   -
39   - /**
40   - * Authorize the user immediately if the user has already granted access.
41   - * If no access has been created, render an authorize button inside the
42   - * element with the ID "embed-api-auth-container".
43   - */
44   - gapi.analytics.auth.authorize({
45   - container: 'embed-api-auth-container',
46   - clientid: '119240817'
47   - });
48   -
49   -
50   - /**
51   - * Create a new ViewSelector instance to be rendered inside of an
52   - * element with the id "view-selector-container".
53   - */
54   - var viewSelector = new gapi.analytics.ViewSelector({
55   - container: 'view-selector-container'
56   - });
57   -
58   - // Render the view selector to the page.
59   - viewSelector.execute();
60   -
61   -
62   - /**
63   - * Create a new DataChart instance with the given query parameters
64   - * and Google chart options. It will be rendered inside an element
65   - * with the id "chart-container".
66   - */
67   - var dataChart = new gapi.analytics.googleCharts.DataChart({
68   - query: {
69   - metrics: 'ga:sessions',
70   - dimensions: 'ga:date',
71   - 'start-date': '30daysAgo',
72   - 'end-date': 'yesterday'
73   - },
74   - chart: {
75   - container: 'chart-container',
76   - type: 'LINE',
77   - options: {
78   - width: '100%'
79   - }
80   - }
81   - });
82   -
83   -
84   - /**
85   - * Render the dataChart on the page whenever a new view is selected.
86   - */
87   - viewSelector.on('change', function(ids) {
88   - dataChart.set({query: {ids: ids}}).execute();
89   - });
90   -
91   - });
92   - </script>
93   -
94 23 <?php
95 24  
96 25 // Загрузка клиентской библиотеки PHP для Google API.
... ... @@ -116,6 +45,29 @@ if (isset($_SESSION[&#39;access_token&#39;]) &amp;&amp; $_SESSION[&#39;access_token&#39;]) {
116 45 // Вывод ответа.
117 46 printResults($response);
118 47  
  48 + private function queryCoreReportingApi() {
  49 + $optParams = array(
  50 + 'dimensions' => 'ga:source,ga:keyword',
  51 + 'sort' => '-ga:sessions,ga:source',
  52 + 'filters' => 'ga:medium==organic',
  53 + 'max-results' => '25');
  54 +
  55 + return $service->data_ga->get(
  56 + TABLE_ID,
  57 + '2010-01-01',
  58 + '2010-01-15',
  59 + 'ga:sessions',
  60 + $optParams);
  61 + }
  62 + try {
  63 + $results = queryCoreReportingApi();
  64 + // Success. Do something cool!
  65 +
  66 + } catch (apiServiceException $e) {
  67 + // Handle API service exceptions.
  68 + $error = $e->getMessage();
  69 + }
  70 +
119 71 } else {
120 72 $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/admin/report/callback';
121 73 header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
... ... @@ -179,3 +131,4 @@ function printResults($reports) {
179 131 }
180 132  
181 133  
  134 +
... ...