Yoast_Google_Analytics

Google Analytics class.

Defined (1)

The class is defined in the following location(s).

/admin/class-google-analytics.php  
  1. class Yoast_Google_Analytics { 
  2.  
  3. /** 
  4. * @var string 
  5. */ 
  6. private $option_name = 'yst_ga_api'; 
  7.  
  8. /** 
  9. * @var array|mixed 
  10. */ 
  11. private $options = array(); 
  12.  
  13. /** 
  14. * @var null|Yoast_Google_Analytics 
  15. */ 
  16. private static $instance = null; 
  17.  
  18. /** 
  19. * @var The api client object holder 
  20. */ 
  21. private $client; 
  22.  
  23. /** 
  24. * Singleton 
  25. */ 
  26. protected function __construct() { 
  27.  
  28. if ( is_null( self::$instance ) ) { 
  29. self::$instance = $this; 
  30.  
  31. $this->options = $this->get_options(); 
  32.  
  33. // Setting the client 
  34. $this->set_client(); 
  35.  
  36. /** 
  37. * Getting the instance object 
  38. * This method will return the instance of itself, if instance not exists, becauses of it's called for the first 
  39. * time, the instance will be created. 
  40. * @return null|Yoast_Google_Analytics 
  41. */ 
  42. public static function get_instance() { 
  43. if ( is_null( self::$instance ) ) { 
  44. self::$instance = new self(); 
  45.  
  46. return self::$instance; 
  47.  
  48.  
  49. /** 
  50. * Check if something went wrong with API calls to Google Analytics 
  51. */ 
  52. public function check_for_ga_issues() { 
  53.  
  54. $last_run = get_option( 'yst_ga_last_wp_run' ); 
  55. $has_failed = get_option( 'yst_ga_api_call_fail', false ); 
  56.  
  57. // Show error, something went wrong 
  58. if ( $has_failed && ( $last_run === false || Yoast_GA_Utils::hours_between( strtotime( $last_run ), time() ) >= 48 ) ) { 
  59. $notice_type = 'warning_fetching_data'; 
  60.  
  61. // Authentication has been successful, so there will be an access token 
  62. if ( ! $this->client->getAccessToken() ) { 
  63. $notice_type .= '_authenticate'; 
  64.  
  65. add_action( 'admin_notices', array( 'Yoast_Google_Analytics_Notice', $notice_type ) ); 
  66.  
  67.  
  68. /** 
  69. * Wrapper for authenticate the client. If authentication code is send it will get and check an access token. 
  70. * @param mixed $authentication_code 
  71. * @return boolean 
  72. */ 
  73. public function authenticate( $authentication_code = null ) { 
  74. // When authentication again we should clean up some stuff 
  75. $this->api_cleanup(); 
  76.  
  77. return $this->client->authenticate_client( $authentication_code ); 
  78.  
  79. /** 
  80. * Getting the analytics profiles 
  81. * Doing the request to the Google analytics API and if there is a response, parses this response and return its 
  82. * array 
  83. * @return array 
  84. */ 
  85. public function get_profiles() { 
  86. $accounts = $this->format_profile_call( 
  87. $this->do_request( 'https://www.googleapis.com/analytics/v3/management/accountSummaries' ) 
  88. ); 
  89.  
  90. if ( is_array( $accounts ) ) { 
  91. $this->save_profile_response( $accounts ); 
  92.  
  93. return $accounts; 
  94.  
  95. return array(); 
  96.  
  97. /** 
  98. * Doing request to Google Analytics 
  99. * This method will do a request to google and get the response code and body from content 
  100. * @param string $target_request_url 
  101. * @return array|null 
  102. */ 
  103. public function do_request( $target_request_url ) { 
  104.  
  105. $response = $this->client->do_request( $target_request_url ); 
  106.  
  107. if ( ! empty( $response ) ) { 
  108. return array( 
  109. 'response' => array( 'code' => $this->client->get_http_response_code() ),  
  110. 'body' => json_decode( $response->getResponseBody(), true ),  
  111. ); 
  112.  
  113.  
  114.  
  115. /** 
  116. * Check if client has a refresh token 
  117. * @return bool 
  118. */ 
  119. public function has_refresh_token() { 
  120. return $this->client->is_authenticated(); 
  121.  
  122. /** 
  123. * Getting the options bases on $this->option_name from the database 
  124. * @return mixed 
  125. */ 
  126. public function get_options() { 
  127. return get_option( $this->option_name ); 
  128.  
  129. /** 
  130. * Checks whether we'll ever be able to reach Google. 
  131. * @return bool 
  132. */ 
  133. public function check_google_access_from_wp() { 
  134. $can_access_google = true; 
  135. if ( defined( 'WP_HTTP_BLOCK_EXTERNAL' ) && WP_HTTP_BLOCK_EXTERNAL ) { 
  136. $can_access_google = false; 
  137. if ( defined( 'WP_ACCESSIBLE_HOSTS' ) ) { 
  138. // Better to use the internal WP logic from this point forward. 
  139. $can_access_google = $this->test_connection_to_google(); 
  140.  
  141. return $can_access_google; 
  142.  
  143. /** 
  144. * Check if we can access Google Apis from this server by making a dummy connection 
  145. */ 
  146. public function check_google_access() { 
  147. return $this->test_connection_to_google(); 
  148.  
  149. /** 
  150. * Updating the options based on $this->option_name and the internal property $this->options 
  151. */ 
  152. protected function update_options() { 
  153. update_option( $this->option_name, $this->options ); 
  154.  
  155. /** 
  156. * Setting the client 
  157. * The filter is a hook to override the configuration/ 
  158. */ 
  159. protected function set_client() { 
  160. // See https://developers.google.com/identity/protocols/OAuth2InstalledApp#formingtheurl for more details about these fields. 
  161. $config = array( 
  162. 'application_name' => 'Google Analytics by Yoast',  
  163. 'client_id' => '346753076522-21smrc6aq0hq8oij8001s57dfoo8igf5.apps.googleusercontent.com',  
  164. 'client_secret' => '5oWaEGFgp-bSrY6vWBmdPfIF',  
  165. 'redirect_uri' => 'urn:ietf:wg:oauth:2.0:oob',  
  166. 'scopes' => array( 'https://www.googleapis.com/auth/analytics.readonly' ),  
  167. ); 
  168.  
  169. $config = apply_filters( 'yst-ga-filter-ga-config', $config ); 
  170.  
  171. $this->client = new Yoast_Api_Google_Client( $config, 'yoast-ga', '' ); 
  172.  
  173. /** 
  174. * Gets an authentication URL 
  175. * @return mixed 
  176. */ 
  177. public function create_auth_url() { 
  178. return $this->client->createAuthUrl(); 
  179.  
  180. /** 
  181. * Saving profile response in options 
  182. * @param array $accounts 
  183. */ 
  184. protected function save_profile_response( $accounts ) { 
  185. $this->options['ga_api_response_accounts'] = $accounts; 
  186.  
  187. $this->update_options(); 
  188.  
  189. /** 
  190. * Test a connection to Google 
  191. * @return bool 
  192. */ 
  193. private function test_connection_to_google() { 
  194. $wp_http = new WP_Http(); 
  195. if ( $wp_http->block_request( 'https://www.googleapis.com/analytics/v3/management/accountSummaries' ) === false ) { 
  196. return true; 
  197.  
  198. return false; 
  199.  
  200. /** 
  201. * Format the accounts request 
  202. * @param array $response 
  203. * @return mixed 
  204. */ 
  205. private function format_profile_call( $response ) { 
  206.  
  207. if ( isset( $response['response']['code'] ) && $response['response']['code'] == 200 ) { 
  208. if ( ! empty( $response['body']['items'] ) && is_array( $response['body']['items'] ) ) { 
  209. $accounts = array(); 
  210.  
  211. foreach ( $response['body']['items'] as $item ) { 
  212. // Check if webProperties is set 
  213. if ( isset( $item['webProperties'] ) ) { 
  214. $profiles = array(); 
  215.  
  216. foreach ( $item['webProperties'] as $property_key => $property ) { 
  217. $profiles[ $property_key ] = array( 
  218. 'id' => $property['id'],  
  219. 'name' => $property['name'],  
  220. 'items' => array(),  
  221. ); 
  222.  
  223. // Check if profiles is set 
  224. if ( isset( $property['profiles'] ) ) { 
  225. foreach ( $property['profiles'] as $key => $profile ) { 
  226. $profiles[ $property_key ]['items'][ $key ] = array_merge( 
  227. $profile,  
  228. array( 
  229. 'name' => $profile['name'] . ' (' . $property['id'] . ')',  
  230. 'ua_code' => $property['id'],  
  231. ); 
  232.  
  233. $accounts[ $item['id'] ] = array( 
  234. 'id' => $item['id'],  
  235. 'ua_code' => $property['id'],  
  236. 'parent_name' => $item['name'],  
  237. 'items' => $profiles,  
  238. ); 
  239.  
  240.  
  241. return $accounts; 
  242.  
  243. return false; 
  244.  
  245. /** 
  246. * Doing some clean up when this method is called 
  247. */ 
  248. private function api_cleanup() { 
  249. delete_option( 'yst_ga_api_call_fail' ); 
  250.