public function actionGetPageView($mediaId, $key) { $listMedia = array_filter(explode('|', $mediaId)); $listKey = array_filter(explode('|', $key)); $listIdAndKey = array_combine($listMedia, $listKey); $googleAnalytics = GARequest::install(); $last_time_haschange = []; header('Content-Type: text/event-stream'); header('Cache-Control: no-cache'); while (1) { foreach ($listIdAndKey as $id => $key) { try { $result = $googleAnalytics->getGAData($id, $key); if (empty($result) || empty($result['timestamp'])) { GAUtils::streamError($result, false, $id); } elseif (!isset($last_time_haschange[$id]) || $result['timestamp'] != $last_time_haschange[$id]) { $last_time_haschange[$id] = $result['timestamp']; GAUtils::streamHasData($result['value'], $id); } else { GAUtils::streamPing($id); } } catch (\yii\base\Exception $exc) { $error = ['errno' => $exc->getMessage()]; GAUtils::streamError($error, false, $id); } } ob_flush(); flush(); sleep(2); } }
public static function getCookie($getNew = false) { //try get from cache $cookie = \Yii::$app->memCache->get(GACookie::KEY_COOKIE); if (empty($cookie) || $getNew) { GAUtils::streamWarn('Can not get new cookie from memcache'); //cookie empty, try get new cook and store in cache $cookie = self::getNewCookie(); if (empty($cookie) || isset($cookie['error']) && $cookie['error'] !== false || empty($cookie['cookie'])) { if (isset($_GET['debug'])) { throw new \yii\base\Exception('Can not get new cookie'); } else { GAUtils::streamError('Can not get new cookie'); } } else { $cookie = $cookie['cookie']; \Yii::$app->memCache->set(GACookie::KEY_COOKIE, $cookie); } } return $cookie; }