public function process() { $code = $this->getProperty('code'); if (!empty($code)) { $client = $this->bigbrother->loadOAuth(); // https://developers.google.com/identity/protocols/OAuth2InstalledApp $authParams = array('code' => $code, 'redirect_uri' => 'urn:ietf:wg:oauth:2.0:oob', 'scope' => 'https://www.googleapis.com/auth/analytics.readonly', 'grant_type' => 'authorization_code'); $result = false; try { $result = $client->getAccessToken($this->bigbrother->oauthTokenEndpoint, 'authorization_code', $authParams); } catch (Exception $e) { $this->modx->log(modX::LOG_LEVEL_ERROR, 'Exception during getAccessToken: ' . $e->getMessage()); } if (is_array($result) && $result['code'] == 200) { $accessToken = $result['result']['access_token']; $refreshToken = $result['result']['refresh_token']; $expiresIn = $result['result']['expires_in']; $this->modx->getCacheManager()->set('access_token', $accessToken, $expiresIn, $this->bigbrother->cacheOptions); $this->bigbrother->updateOption('refresh_token', $refreshToken, 'text-password'); return $this->success('', array('text' => $this->modx->lexicon('bigbrother.authorize_success'))); } return $this->failure('Unable to complete oAuth2 flow: <pre>' . print_r($result, true) . '</pre>'); } return $this->failure('No code provided.'); }
/** * Set a serie for the current loaded report * @return void */ public function addSerie() { $row = array(); $dimName = $this->getProperty('dimensions'); foreach ($this->ga->report["rows"] as $key => $value) { $row['name'] = strtoupper($this->ga->getDimensionName($value[0])); $row['y'] = intval($value[1]); $this->series['data'][] = $row; } }
/** * Compare new report to the main set of data and update each key accordingly * @return void */ public function compareData() { foreach ($this->ga->report['totalsForAllResults'] as $key => $v) { if (isset($this->results[$key])) { $cls = ''; $value = $this->results[$key]['value']; $compare = intval($v); if ($compare <= 0) { $compare = 1; } $progression = round($value * 100 / $compare - 100, 1); if ($progression > 0) { $progression = '+ ' . $progression; $cls = 'up'; } elseif ($progression < 0) { $progression = str_replace('-', '- ', $progression); $cls = 'down'; } $this->results[$key]['progression'] = $progression; $this->results[$key]['progressionCls'] = $cls; $this->results[$key]['value'] = $this->ga->formatValue($key, $value); } } $this->formatOutput(); }
/** * Set a serie for the current loaded report * @return void */ public function addSerie() { $serie = $row = array(); foreach ($this->ga->report["rows"] as $key => $value) { // Convert date in javascript format $date = strtotime($value[0]) * 1000; if (empty($serie)) { // Create entries for each requested metrics $serie['begin'] = $date; $serie['name'] = strtoupper($this->ga->getName($this->getProperty('metrics', null))); $serie['data'] = array(); } $row[] = $date; $row[] = intval($value[1]); array_push($serie['data'], $row); $row = array(); } $this->series[] = $serie; }
/** * Set a serie for the current loaded report * @param bool $delayed */ public function addSerie($delayed = false) { $serie = $row = array(); foreach ($this->ga->report["rows"] as $key => $value) { // Convert date in javascript format $date = strtotime($value[0]) * 1000; if (empty($serie)) { // Create entries for each requested metrics $serie['begin'] = $date; $labelDate = $this->ga->getDates('d M Y', $delayed); $serie['name'] = strtoupper($labelDate['begin'] . ' - ' . $labelDate['end']); $serie['data'] = array(); } $row[] = $date; $row[] = intval($value[1]); array_push($serie['data'], $row); $row = array(); } $this->series[] = $serie; }
/** * Call the GA API via curl * @param string $url * @return string */ public function callAPI($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->ga->createAuthHeader($url, 'GET'))); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); if (curl_errno($ch)) { $this->error = curl_error($ch); $this->modx->log(modX::LOG_LEVEL_ERROR, 'cURL Error on API call to ' . $url . ': ' . $this->error); return false; } $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($http_code !== 200) { $this->error = $result; $this->modx->log(modX::LOG_LEVEL_ERROR, 'Non-200 HTTP Code returned from calling ' . $url . ': ' . $http_code . ' Result: ' . $this->error); return false; } curl_close($ch); return $this->modx->fromJSON($result); }
public function process() { $date = $this->ga->getDates(); $url = $this->ga->buildUrl($date['begin'], $date['end'], $this->dimension, $this->metrics, array('-ga:visits'), $this->filters, $this->limit); $cacheKey = $this->ga->cacheKey; $fromCache = $this->modx->cacheManager->get($cacheKey); if (!empty($fromCache)) { return $this->success('Fetched data from cache', $fromCache, true); } if (!$this->ga->loadOAuth()) { return $this->failure('Could not load the OAuth file.'); } if (!$this->ga->getReport($url)) { return $this->failure($this->ga->getOutput()); } $this->visits = $this->ga->getTotalVisits($date['begin'], $date['end']); $response = $this->iterate(); $this->modx->cacheManager->set($cacheKey, $response, $this->ga->getOption('cache_timeout')); return $this->success('Fetched data from Google', $response); }
public function render() { $this->bigbrother = new BigBrother($this->modx); // Make sure we have a valid access token _before_ showing the widget $this->bigbrother->createAuthHeader(); $this->modx->controller->addCss($this->bigbrother->config['css_url'] . 'dashboard.css'); //jQuery + charts class $this->modx->controller->addJavascript($this->bigbrother->config['assets_url'] . 'mgr/lib/jquery.min.js'); $this->modx->controller->addJavascript($this->bigbrother->config['assets_url'] . 'mgr/lib/highcharts.js'); //Basic reusable panels $this->modx->controller->addJavascript($this->bigbrother->config['assets_url'] . 'mgr/lib/classes.js'); $this->modx->controller->addJavascript($this->bigbrother->config['assets_url'] . 'mgr/lib/charts.js'); $account = $this->bigbrother->getOption('account'); if ($account == null) { $this->modx->controller->addJavascript($this->bigbrother->config['assets_url'] . 'dashboard/notlogged.js'); } else { $this->modx->controller->addJavascript($this->bigbrother->config['assets_url'] . 'dashboard/dashboard.js'); } $date = $this->bigbrother->getDates('d M Y'); /** @var $page modAction */ $page = $this->modx->getObject('modAction', array('namespace' => 'bigbrother', 'controller' => 'index')); $url = $this->bigbrother->getManagerLink() . '?a=' . $page->get('id'); $this->modx->controller->addHtml('<script type="text/javascript"> BigBrother.RedirectUrl = "' . $url . '"; BigBrother.ConnectorUrl = "' . $this->bigbrother->config['connector_url'] . '"; BigBrother.DateBegin = "' . $date['begin'] . '"; BigBrother.DateEnd = "' . $date['end'] . '"; BigBrother.account = "' . $this->bigbrother->getOption('account') . '"; BigBrother.accountName = "' . $this->bigbrother->getOption('account_name') . '"; Ext.applyIf(MODx.lang, ' . $this->modx->toJSON($this->modx->lexicon->loadCache('bigbrother', 'dashboard')) . '); Ext.onReady(function() { MODx.load({ xtype: "bb-panel" ,user: "******" // Not needed, yet ? }); }); </script>'); return '<div id="bb-panel"></div>'; }