forked from DeuxHuitHuit/google_analytics_dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.driver.php
228 lines (193 loc) · 6.93 KB
/
extension.driver.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?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>");
require_once(EXTENSIONS . '/google_analytics_dashboard/vendor/autoload.php');
/**
*
* @author Deux Huit Huit
* https://deuxhuithuit.com/
*
*/
class extension_google_analytics_dashboard extends Extension {
/**
* Name of the extension
* @var string
*/
const EXT_NAME = 'Google Analytics Dashboard';
/**
* Name of the extension
* @var string
*/
const PANEL_NAME = 'Google Analytics';
private static $CHART_TYPES = array('LINE', 'COLUMN', 'BAR', 'TABLE', 'GEO', 'PIE');
/* ********* DELEGATES ******* */
public function getSubscribedDelegates() {
return array(
array(
'page' => '/backend/',
'delegate' => 'DashboardPanelRender',
'callback' => 'dashboard_render_panel'
),
array(
'page' => '/backend/',
'delegate' => 'DashboardPanelTypes',
'callback' => 'dashboard_panel_types'
),
array(
'page' => '/backend/',
'delegate' => 'DashboardPanelOptions',
'callback' => 'dashboard_panel_options'
),
array(
'page' => '/backend/',
'delegate' => 'DashboardPanelValidate',
'callback' => 'dashboard_panel_validate'
),
);
}
public function dashboard_render_panel($context) {
if ($context['type'] != self::PANEL_NAME) {
return;
}
$config = $context['config'];
$height = isset($config['height']) ? $config['height'] : '225px';
$i = new XMLElement('iframe', null, array(
'src' => SYMPHONY_URL . '/extension/google_analytics_dashboard/?p=' . $context['id'],
'style' => "width:100%;height:$height;",
'frameborder' => 'no',
));
$context['panel']->appendChild($i);
}
public function dashboard_panel_types($context) {
$context['types'][self::PANEL_NAME] = self::PANEL_NAME;
}
public function dashboard_panel_options($context) {
if ($context['type'] != self::PANEL_NAME) {
return;
}
$config = $context['existing_config'];
if (empty($config)) {
$handle = General::createHandle(self::EXT_NAME);
$settings = Symphony::Configuration()->get($handle);
if (!empty($settings)) {
$config = $settings;
}
}
$wrapper = new XMLElement('div');
$fieldset = new XMLElement('fieldset', null, array('class' => 'settings'));
$fieldset->appendChild(new XMLElement('legend', 'Google Analytics Options'));
$label = Widget::Label('Google Analytics Service account email', Widget::Input('config[email]', $config['email']));
$fieldset->appendChild($label);
$label = Widget::Label('Google Analytics p12 key file path (absolute or DOCROOT relative)', Widget::Input('config[keyfile]', $config['keyfile']));
$fieldset->appendChild($label);
$wrapper->appendChild($fieldset);
$fieldset = new XMLElement('fieldset', null, array('class' => 'settings'));
$fieldset->appendChild(new XMLElement('legend', 'Reporting Options'));
$layout = new XMLElement('div', null, array('class' => 'two columns'));
$fieldset->appendChild($layout);
if (!$config['start-date']) {
$config['start-date'] = '30daysAgo';
}
$label = Widget::Label('Start date', Widget::Input('config[start-date]', $config['start-date']), 'column');
$layout->appendChild($label);
if (!$config['end-date']) {
$config['end-date'] = 'yesterday';
}
$label = Widget::Label('End date', Widget::Input('config[end-date]', $config['end-date']), 'column');
$layout->appendChild($label);
if (!$config['dimensions']) {
$config['dimensions'] = 'ga:date';
}
$label = Widget::Label('Dimensions', Widget::Input('config[dimensions]', $config['dimensions']), 'column');
$layout->appendChild($label);
if (!$config['metrics']) {
$config['metrics'] = 'ga:sessions';
}
$label = Widget::Label('Metrics', Widget::Input('config[metrics]', $config['metrics']), 'column');
$layout->appendChild($label);
if (!$config['type']) {
$config['type'] = 'LINE';
}
$lineOptions = array();
foreach (self::$CHART_TYPES as $type) {
$lineOptions[] = array($type, $config['type'] == $type, strtolower($type));
}
$label = Widget::Label('Chart type', Widget::Select('config[type]', $lineOptions), 'column');
$layout->appendChild($label);
$link = Widget::Anchor('Click here to explore available dimensions and metrics', 'https://developers.google.com/analytics/devguides/reporting/core/dimsmets', null, null, null, array('target' => '_blank'));
$label = Widget::Label('Help <br />', $link, 'column');
$layout->appendChild($label);
$wrapper->appendChild($fieldset);
$fieldset = new XMLElement('fieldset', null, array('class' => 'settings'));
$fieldset->appendChild(new XMLElement('legend', 'Display Options'));
$label = Widget::Label('Height (include units)', Widget::Input('config[height]', $config['height']));
$fieldset->appendChild($label);
$label = Widget::Label('Save as default', Widget::Input('default', 'on', 'checkbox'));
$fieldset->appendChild($label);
$wrapper->appendChild($fieldset);
$context['form'] = $wrapper;
}
public function dashboard_panel_validate($context) {
if ($context['type'] != self::PANEL_NAME) {
return;
}
if (isset($_POST['default']) && $_POST['default'] == 'on') {
$config = $context['existing_config'];
if ($config == false) {
$config = array();
}
$handle = General::createHandle(self::EXT_NAME);
$client = static::createClient($config, $context['id']);
if (!isset($config['at'])) {
$config['at'] = $client->getAccessToken();
}
foreach ($config as $key => $value) {
Symphony::Configuration()->set($key, $value, $handle);
}
Symphony::Configuration()->write();
}
}
/* ********* GOOGLE CLIENT ******* */
public static function createClient(array $config, $panelId) {
$client = new Google_Client();
$client->setClassConfig('Google_Cache_File', 'directory', TMP);
$keyfile = $config['keyfile'];
if (strpos($keyfile, '/') !== 0) {
$keyfile = DOCROOT . '/' . $keyfile;
}
$key = @file_get_contents($keyfile);
if (!!$key) {
$cred = new Google_Auth_AssertionCredentials(
$config['email'],
array(Google_Service_Analytics::ANALYTICS_READONLY),
$key
);
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
}
return $client;
}
/* ********* INSTALL/UPDATE/UNINSTALL ******* */
/**
* Creates the table needed for the settings of the field
*/
public function install() {
return true;
}
/**
* This method will update the extension according to the
* previous and current version parameters.
* @param string $previousVersion
*/
public function update($previousVersion = false) {
return true;
}
public function uninstall() {
return true;
}
}