request_uri = 'https://www.googleapis.com/analytics/v3/management'; $this->request_headers['authorization'] = sprintf( 'Bearer %s', is_string( $access_token ) ? $access_token : '' ); } /* listing account summarries */ public function list_account_summaries($account_summaries) { $list_account_summaries = array(); if ( isset( $account_summaries->items ) ) { $list_account_summaries = (array) $account_summaries->items; } return $list_account_summaries; } /* list all views for webproperty */ public function list_views($views) { $profiles = array(); if ( isset( $views->items ) ) { $profiles = (array) $views->items; } return $profiles; } /* get account summaries */ public function get_account_summaries() { $this->reset_response(); $i = 0; while(1) { $response = wp_safe_remote_request( 'https://www.googleapis.com/analytics/v3/management/accountSummaries' , $this->get_request_args() ); if (!empty($response) && is_array($response)){ if(isset($response['response']['code']) && ((int)$response['response']['code'] < 300)){ break; } } if($i > 1) { break; } $i++; } try { $response = $this->handle_response( $response ); } catch ( Exception $e ) { error_log($e->getMessage(),0); } return $response; } /* get g4 account summaries */ public function get_g4_account_summaries() { $this->reset_response(); $i = 0; while(1) { $response = wp_safe_remote_request( 'https://analyticsadmin.googleapis.com/v1alpha/accountSummaries' , $this->get_request_args() ); if (!empty($response) && is_array($response)){ if(isset($response['response']['code']) && ((int)$response['response']['code'] < 300)){ break; } } if($i > 1) { break; } $i++; } try { $response = $this->handle_response( $response ); } catch ( Exception $e ) { error_log($e->getMessage(),0); } return $response; } /* get data webstreams */ public function get_web_data_streams($property_name){ $this->reset_response(); $i = 0; while(1) { $response = wp_safe_remote_request( "https://analyticsadmin.googleapis.com/v1alpha/{$property_name}/webDataStreams" , $this->get_request_args() ); if (!empty($response) && is_array($response)){ if(isset($response['response']['code']) && ((int)$response['response']['code'] < 300)){ break; } } if($i > 1) { break; } $i++; } try { $response = $this->handle_response( $response ); } catch ( Exception $e ) { error_log($e->getMessage(),0); } return $response; } /* get property views summaries */ public function get_property_views($account_id,$property_id) { $this->reset_response(); if(isset($property_id) && !empty($property_id)){ $i = 0; while(1) { $response = wp_safe_remote_request( "https://www.googleapis.com/analytics/v3/management/accounts/{$account_id}/webproperties/{$property_id}/profiles?fields=items(id,name,internalWebPropertyId)" , $this->get_request_args() ); if (!empty($response) && is_array($response)){ if(isset($response['response']['code']) && ((int)$response['response']['code'] < 300)){ break; } } if($i > 1) { break; } $i++; } try { $response = $this->handle_response( $response ); } catch ( Exception $e ) { error_log($e->getMessage(),0); } return $response; } return; } /* handle response received from api request */ protected function handle_response( $response ) { if ( is_wp_error( $response ) ) { throw new Exception( $response->get_error_message() ); } $this->response_code = wp_remote_retrieve_response_code( $response ); $this->response_message = wp_remote_retrieve_response_message( $response ); $this->raw_response_body = wp_remote_retrieve_body( $response ); $response_headers = wp_remote_retrieve_headers( $response ); if($this->response_code == 200){ $this->response = json_decode ($this->raw_response_body); }else{ $this->response =''; throw new Exception($this->response_message); } return $this->response; } /* getting request args for api requests */ protected function get_request_args() { $args = array( 'method' => 'GET', 'timeout' => MINUTE_IN_SECONDS, 'redirection' => 0, 'httpversion' => '1.0', 'sslverify' => true, 'blocking' => true, 'user-agent' => $this->get_request_user_agent(), 'headers' => $this->request_headers, 'body' => '', 'cookies' => array(), ); return $args; } /* request user agent */ protected function get_request_user_agent() { if(function_exists(wc_get_user_agent())){ $user_agent = htmlentities( wc_get_user_agent(), ENT_QUOTES, 'UTF-8' ); }else{ $user_agent = sprintf( '%s/%s (WooCommerce/%s; WordPress/%s)', 'WoocommerceAnalytics', AWCA_VERSION, WC_VERSION, $GLOBALS['wp_version'] ); } return $user_agent; } /* get views settings info */ public function get_profiles( $account_id, $property_id ) { $this->reset_response(); $i = 0; while(1) { $response = wp_safe_remote_request( "https://www.googleapis.com/analytics/v3/management/accounts/{$account_id}/webproperties/{$property_id}/profiles?fields=items(id,internalWebPropertyId,name,websiteUrl,currency,timezone,eCommerceTracking,enhancedECommerceTracking)" , $this->get_request_args() ); if (!empty($response) && is_array($response)){ if(isset($response['response']['code']) && ((int)$response['response']['code'] < 300)){ break; } } if($i > 1) { break; } $i++; } try { $response = $this->handle_response( $response ); } catch ( Exception $e ) { error_log($e->getMessage(),0); } return $response; } /* resetting response variables */ protected function reset_response() { $this->response_code = null; $this->response_message = null; $this->raw_response_body = null; $this->response = null; } }