/** * Settings * * @return null */ public function actionSettings() { craft()->analytics_plugin->requireDependencies(); craft()->analytics_oauth->requireOauth(); $plugin = craft()->plugins->getPlugin('analytics'); $variables = array('provider' => false, 'account' => false, 'token' => false, 'error' => false); $provider = craft()->oauth->getProvider('google'); if ($provider && $provider->isConfigured()) { $token = craft()->analytics_oauth->getToken(); if ($token) { try { $account = craft()->analytics_cache->get(['getAccount', $token]); if (!$account) { $account = $provider->getAccount($token); craft()->analytics_cache->set(['getAccount', $token], $account); } $propertiesOpts = $this->getPropertiesOpts(); if ($account) { AnalyticsPlugin::log("Account:\r\n" . print_r($account, true), LogLevel::Info); $variables['account'] = $account; $variables['propertiesOpts'] = $propertiesOpts; $variables['settings'] = $plugin->getSettings(); } } catch (\Exception $e) { if (method_exists($e, 'getResponse')) { AnalyticsPlugin::log("Couldn’t get account: " . $e->getResponse(), LogLevel::Error); } else { AnalyticsPlugin::log("Couldn’t get account: " . $e->getMessage(), LogLevel::Error); } $variables['error'] = $e->getMessage(); } } $variables['token'] = $token; $variables['provider'] = $provider; } $this->renderTemplate('analytics/settings', $variables); }
/** * Connect * * @return null */ public function actionConnect() { // referer $referer = craft()->httpSession->get('analytics.referer'); if (!$referer) { $referer = craft()->request->getUrlReferrer(); craft()->httpSession->add('analytics.referer', $referer); AnalyticsPlugin::log('OAuth Connect Referer: ' . $referer, LogLevel::Info); } // connect if ($response = craft()->oauth->connect(array('plugin' => 'analytics', 'provider' => $this->handle, 'scope' => $this->scope, 'authorizationOptions' => $this->authorizationOptions))) { if ($response['success']) { // token $token = $response['token']; // save token craft()->analytics_oauth->saveToken($token); if ($token) { AnalyticsPlugin::log('Token: ' . "\r\n" . print_r($token->getAttributes(), true), LogLevel::Info); } else { AnalyticsPlugin::log('Couldn’t get token', LogLevel::Error); } // session notice craft()->userSession->setNotice(Craft::t("Connected to Google Analytics.")); } else { // session error craft()->userSession->setError(Craft::t($response['errorMsg'])); } } else { // session error craft()->userSession->setError(Craft::t("Couldn’t connect")); } // OAuth Step 5 // redirect craft()->httpSession->remove('analytics.referer'); $this->redirect($referer); }
/** * Send */ public function send() { try { $this->tracking->send(); } catch (\Exception $e) { AnalyticsPlugin::log('Couldn’t send tracking: ' . $e->getMessage(), LogLevel::Error); } }
/** * Is Configured */ public function isConfigured() { // check dependencies $pluginDependencies = $this->getPluginDependencies(); if (count($pluginDependencies) > 0) { return false; } // check provider and token $provider = craft()->oauth->getProvider('google'); if ($provider) { // token $token = craft()->analytics_oauth->getToken(); if (!$token) { return false; } } else { return false; } // check if profile id is set up $plugin = craft()->plugins->getPlugin('analytics'); $settings = $plugin->getSettings(); $profileId = $settings['profileId']; if (!$profileId) { AnalyticsPlugin::log('Analytics profileId not found', LogLevel::Info, true); return false; } return true; }