/
content.index.php
113 lines (99 loc) · 2.82 KB
/
content.index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/*
Copyrights: Deux Huit Huit 2015
LICENCE: MIT http://deuxhuithuit.mit-license.org;
*/
if(!defined("__IN_SYMPHONY__")) die("<h2>Error</h2><p>You cannot directly access this file</p>");
/**
*
* @author Deux Huit Huit
* https://deuxhuithuit.com/
*
*/
class contentExtensionGoogle_analytics_dashboardIndex extends HTMLPage {
/**
* Builds the content view
*/
public function build() {
if (!Symphony::isLoggedIn()) {
Administration::instance()->throwCustomError(
__('You are not authorised to access this page.'),
__('Access Denied'),
Page::HTTP_STATUS_UNAUTHORIZED
);
return;
}
// this loads our classes
$ext = Symphony::ExtensionManager()->create('dashboard');
$ext = Symphony::ExtensionManager()->create('google_analytics_dashboard');
// html head
$this->Html->setDTD('<!DOCTYPE html>');
$this->Html->setAttribute('lang', Lang::get());
$this->Head->appendChild(new XMLElement('title', extension_google_analytics_dashboard::EXT_NAME));
// html body
$html = '';
$PANEL_ID = General::sanitize($_REQUEST['p']);
$panel = Extension_Dashboard::getPanel($PANEL_ID);
$config = unserialize($panel['config']);
$client = extension_google_analytics_dashboard::createClient($config, $panel['id']);
if (!isset($config['at'])) {
$config['at'] = $client->getAccessToken();
}
if (!($at = @json_decode($config['at']))) {
$html = <<<HTML
<h1>Server auth failed! Please check your configuration.</h1>
HTML;
}
else {
$html = <<<HTML
<section id="auth-button"></section>
<section id="view-selector"></section>
<section id="timeline"></section>
<script>
(function(w,d,s,g,js,fjs){
g=w.gapi||(w.gapi={});g.analytics={q:[],ready:function(cb){this.q.push(cb)}};
js=d.createElement(s);fjs=d.getElementsByTagName(s)[0];
js.src='https://apis.google.com/js/platform.js';
fjs.parentNode.insertBefore(js,fjs);js.onload=function(){g.load('analytics')};
}(window,document,'script'));
</script>
<script>
gapi.analytics.ready(function() {
gapi.analytics.auth.authorize({
serverAuth: {
access_token: '$at->access_token'
}
});
console.log('Autorized ? ' + gapi.analytics.auth.isAuthorized());
var viewSelector = new gapi.analytics.ViewSelector({
container: 'view-selector'
});
var timeline = new gapi.analytics.googleCharts.DataChart({
reportType: 'ga',
query: {
'dimensions': 'ga:date',
'metrics': 'ga:sessions',
'start-date': '30daysAgo',
'end-date': 'yesterday',
},
chart: {
type: 'LINE',
container: 'timeline'
}
});
viewSelector.on('change', function(ids) {
var newIds = {
query: {
ids: ids
}
}
timeline.set(newIds).execute();
});
viewSelector.execute();
});
</script>
HTML;
}
$this->Body->setValue($html);
}
}