public function actionCron() { $client_id = '746431818850-sddh6at9g6o18hbmi82rh13tu2mi3pbg.apps.googleusercontent.com'; $client_secret = 'mz1lOoqB9oslvGZVhoT6KS-V'; $redirect_uri = 'http://insol.madeforpets.com.ua/cron'; $account_id = 'ga:114815562'; session_start(); $_SESSION['oauth_access_token'] = 'ya29.jAK-QzSp8fb_ou3CxnTp2SY_WEZ5aQF5JjxNmu9kpan_C0roaG6I1nFVGlOQJXL94MAU'; $ga = new GoogleAnalyticsAPI(); $ga->auth->setClientId($client_id); $ga->auth->setClientSecret($client_secret); $ga->auth->setRedirectUri($redirect_uri); if (isset($_GET['force_oauth'])) { $_SESSION['oauth_access_token'] = null; } if (!isset($_SESSION['oauth_access_token']) && !isset($_GET['code'])) { // Go get the url of the authentication page, redirect the client and go get that token! $url = $ga->auth->buildAuthUrl(); header("Location: " . $url); } if (!isset($_SESSION['oauth_access_token']) && isset($_GET['code'])) { $auth = $ga->auth->getAccessToken($_GET['code']); if ($auth['http_code'] == 200) { $accessToken = $auth['access_token']; $refreshToken = $auth['refresh_token']; $tokenExpires = $auth['expires_in']; $tokenCreated = time(); // For simplicity of the example we only store the accessToken // If it expires use the refreshToken to get a fresh one $_SESSION['oauth_access_token'] = $accessToken; } else { die("Sorry, something wend wrong retrieving the oAuth tokens"); } } if (!empty($_SESSION['oauth_access_token'])) { $ga->setAccessToken($_SESSION['oauth_access_token']); $ga->setAccountId($account_id); // Set the default params. For example the start/end dates and max-results $defaults = array('start-date' => '2016-01-01', 'end-date' => 'today'); $ga->setDefaultQueryParams($defaults); $params = array('metrics' => 'ga:pageviews,ga:sessionDuration', 'dimensions' => 'ga:pagePath,ga:date,ga:fullReferrer', 'sort' => '-ga:date'); $visits = $ga->query($params); } $data = $visits['rows']; if (isset($data)) { // проверка на пустоту массива $mass = $data; for ($i = 0; $i < count($mass); $i++) { $result = $this->find_($mass[$i][0]); if (!($result === false)) { $id = $this->find_id($mass[$i][0]); if ($id != '') { $models = Campaigns::Get_info($id); $mass[$i][] = (int) $id; $mass[$i][] = $models->id_user; } $new[] = $mass[$i]; } } } for ($i = 0; $i < count($new); $i++) { $models = Analytics::Get_info($new[$i][1], $new[$i][5]); if ($models == '') { $create_ = Analytics::Create_($new[$i]); if ($create_ != false) { $create_detail_ = Detail::Create_($new[$i], $create_); } } else { $check_detail = Detail::Get_info($models->id, $new[$i][2]); if ($check_detail == '') { $create_detail_ = Detail::Create_($new[$i], $models->id); } else { $update_detail_ = Detail::Update_($new[$i], $models->id); } } } return $this->render('cron', ['data' => $data]); }
/** * Get data form Ajax request and create or update Analytics model. * @return mixed */ public function actionAjax_request() { if (Yii::$app->request->isAjax) { $data = Yii::$app->request->post(); function find_($value1) { // проверка на нужную ссылку $value = '/campaigns?id='; $result = strpos($value1, $value); return $result; } function find_id($value1) { // получение id $value = '/campaigns?id='; $id = str_replace($value, '', $value1); return $id; } if (isset($data['analytics'])) { // проверка на пустоту массива $mass = $data['analytics']; for ($i = 0; $i < count($mass); $i++) { $result = find_($mass[$i][0]); if (!($result === false)) { $id = find_id($mass[$i][0]); if ($id != '') { $models = Campaigns::Get_info($id); $mass[$i][] = (int) $id; $mass[$i][] = $models->id_user; } $new[] = $mass[$i]; } } } // подготовили массив for ($i = 0; $i < count($new); $i++) { $models = Analytics::Get_info($new[$i][1], $new[$i][5]); if ($models == '') { $create_ = Analytics::Create_($new[$i]); if ($create_ != false) { $create_detail_ = Detail::Create_($new[$i], $create_); } } else { $check_detail = Detail::Get_info($models->id, $new[$i][2]); if ($check_detail == '') { $create_detail_ = Detail::Create_($new[$i], $models->id); } else { $update_detail_ = Detail::Update_($new[$i], $models->id); } } } \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; return ['data' => 'Данные успешно загружены']; } }