diff --git a/GoogleAnalyticsAPI.class.php b/GoogleAnalyticsAPI.class.php index 2762e44..d5ffa1d 100644 --- a/GoogleAnalyticsAPI.class.php +++ b/GoogleAnalyticsAPI.class.php @@ -38,6 +38,7 @@ */ class GoogleAnalyticsAPI { + const REALTIME_API_URL = 'https://www.googleapis.com/analytics/v3/data/realtime'; const API_URL = 'https://www.googleapis.com/analytics/v3/data/ga'; const WEBPROPERTIES_URL = 'https://www.googleapis.com/analytics/v3/management/accounts/~all/webproperties'; const PROFILES_URL = 'https://www.googleapis.com/analytics/v3/management/accounts/~all/webproperties/~all/profiles'; @@ -133,6 +134,18 @@ public function returnObjects($bool) { public function query($params=array()) { return $this->_query($params); } + + /** + * Query the RealTime Google Analytics API + * + * @access public + * @param array $params (default: array()) Query parameters + * @return array data + */ + public function query_realtime($params = array()) + { + return $this->_query_realtime($params); + } /** @@ -374,6 +387,15 @@ public function getReferralTraffic($params=array()) { return $this->_query($_params); } + + public function getRealTimeReport($params = array()) + { + $defaults = array( + 'metrics' => 'rt:activeVisitors', + ); + $_params = array_merge($defaults, $params); + return $this->_query_realtime($_params); + } protected function _query($params=array()){ @@ -387,6 +409,18 @@ protected function _query($params=array()){ return json_decode($data, $this->assoc); } + + protected function _query_realtime($params = array()) + { + if (!$this->accessToken || !$this->accountId) + { + throw new Exception('You must provide the accessToken and an accountId'); + } + $_params = array_merge($this->defaultQueryParams, array('access_token' => $this->accessToken, 'ids' => $this->accountId)); + $queryParams = array_merge($_params, $params); + $data = Http::curl(self::REALTIME_API_URL, $queryParams); + return json_decode($data, $this->assoc); + } }