public function getGoogleApiAuthenticationUrl($clientId, $clientSecret, $trackingId) { $serviceLocator = $this->getServiceLocator(); $session = $this->getSessionContainer(self::SESSION_NAMESPACE); $api = new GoogleApi(); $api->getClient()->setClientId($clientId); $authenticationUrl = $api->getClient()->createAuthUrl(); $session[self::SESSION_KEY_CLIENT_ID] = $clientId; $session[self::SESSION_KEY_CLIENT_SECRET] = $clientSecret; $session[self::SESSION_KEY_TRACKING_ID] = $trackingId; return array('authenticationUrl' => $authenticationUrl); }
public function __invoke() { // we use \Zend\ServiceManager\ServiceManager instead of the \Zend\Mvc\Controller\PluginManager witch returned from the pugin /* @var $serviceLocator \Zend\ServiceManager\ServiceManager */ $serviceLocator = $this->getServiceLocator()->getServiceLocator(); // config array $config = $serviceLocator->get('config')['modules']['Grid\\GoogleAnalytics']; // create view model of the box $view = new ViewModel(); $view->setTemplate('grid/google-analytics/admin-dashboard-plugin/empty'); $view->setVariable('config', $config); if (isset($config['googleApi']['clientId']) && isset($config['googleApi']['clientSecret']) && isset($config['googleApi']['accessToken']) && isset($config['dashboardChart']['profileId']) && isset($config['dashboardChart']['enabled']) && $config['dashboardChart']['enabled']) { $api = new GoogleApi(); $api->getClient()->setClientId($config['googleApi']['clientId']); $api->getClient()->setClientSecret($config['googleApi']['clientSecret']); $api->getClient()->setAccessToken($config['googleApi']['accessToken']); $report = $api->getVisits($config['dashboardChart']['profileId'], date('Y-m-d', time() - 7 * 24 * 3600), date('Y-m-d', time() - 1 * 24 * 3600)); $view->setTemplate('grid/google-analytics/admin-dashboard-plugin/chart'); $view->setVariable('report', $report); } return $view; }
public function callbackAction() { /* @var $params \Zend\Mvc\Controller\Plugin\Params */ $params = $this->params(); $session = $this->getSessionContainer(self::SESSION_NAMESPACE); $viewModel = new ViewModel(); $viewModel->setTerminal(true); try { $api = new GoogleApi(); $api->getClient()->setClientId($session[self::SESSION_KEY_CLIENT_ID]); $api->getClient()->setClientSecret($session[self::SESSION_KEY_CLIENT_SECRET]); $callbackCode = $params->fromQuery('code'); $api->getClient()->authenticate($callbackCode); $accessToken = $api->getClient()->getAccessToken(); $avalilableProfiles = $api->getAvalilableProfiles($session[self::SESSION_KEY_TRACKING_ID]); $viewModel->setVariable('accessToken', $accessToken); $viewModel->setVariable('avalilableProfiles', $avalilableProfiles); } catch (\Exception $e) { $message = $e->getMessage(); if (preg_match('/^Error calling GET .+ \\(400\\) invalid accountId: .+$/', $message)) { $message = 'Invalid accountId.'; } $viewModel->setVariable('errorMessage', $message); } return $viewModel; /* @var $session array */ // $session = $this->getSessionContainer(self::SESSION_NAMESPACE); /* $api = new Api($session[self::SESSION_KEY_CLIENT_ID], $session[self::SESSION_KEY_CLIENT_SECRET]); $api->authenticate($callbackCode); if ($api->getAccessToken()) { $session[$this->getTokenKey()] = $api->getAccessToken(); } return $this->redirectToRefresh();*/ /* @var $session array */ // $session = $this->getSessionContainer(self::SESSION_NAMESPACE); /*$api = new Api($session[self::SESSION_KEY_CLIENT_ID], $session[self::SESSION_KEY_CLIENT_SECRET]); $api->setAccessToken($session[$this->getTokenKey()]); $profiles = $api->getProfiles($session[self::SESSION_KEY_ANALYITICS_ID]); */ //$viewModel->setVariable('profiles', $profiles); }