init_awca_dash_defaults(); } $property_views = $this->get_analytics_property_views(); if ( is_array( $property_views ) && !empty($property_views) ) { if ( !isset( $awca_dash_settings['report_view'] ) || !in_array( $awca_dash_settings['report_view'], $property_views ) ) { $awca_dash_settings['report_view'] = $property_views[0]->id; } } else { $awca_dash_settings['report_view'] = false; } if ( $awca_dash_settings['report_frame'] == 'Yesterday' ) { $awca_dash_settings['report_to'] = date( 'Y-m-d', strtotime( '-1 day' ) ); $awca_dash_settings['report_from'] = date( 'Y-m-d', strtotime( '-1 day' ) ); } elseif ( $awca_dash_settings['report_frame'] == 'Last 7 days' ) { $awca_dash_settings['report_to'] = date( 'Y-m-d', strtotime( '-1 day' ) ); $awca_dash_settings['report_from'] = date( 'Y-m-d', strtotime( '-8 day' ) ); } elseif ( $awca_dash_settings['report_frame'] == 'Today' ) { $awca_dash_settings['report_to'] = date( 'Y-m-d', strtotime( 'now' ) ); $awca_dash_settings['report_from'] = date( 'Y-m-d', strtotime( 'now' ) ); } elseif ( $awca_dash_settings['report_frame'] == 'Current Year' ) { $awca_dash_settings['report_to'] = date( 'Y-m-d', strtotime( 'now' ) ); $awca_dash_settings['report_from'] = date( 'Y' ) . '-01-01'; } elseif ( $awca_dash_settings['report_frame'] == 'Custom Range' ) { if ( isset( $awca_dash_settings['report_to'] ) && isset( $awca_dash_settings['report_from'] ) ) { } else { $awca_dash_settings['report_to'] = date( 'Y-m-d', strtotime( '-1 day' ) ); $awca_dash_settings['report_from'] = date( 'Y-m-d', strtotime( '-31 day' ) ); } } else { $awca_dash_settings['report_to'] = date( 'Y-m-d', strtotime( '-1 day' ) ); $awca_dash_settings['report_from'] = date( 'Y-m-d', strtotime( '-31 day' ) ); } return $awca_dash_settings; } /* publishing stat data on dashboard */ public function publish_stat_data( $stats_data, $stats_array, $currency_symbol ) { if ( is_array( $stats_data ) && !empty($stats_data) ) { echo '
' ; foreach ( $stats_data as $stat_name => $stat ) { if ( !empty($stats_array[$stat_name][2]) ) { if ( $stats_array[$stat_name][3] ) { if ( $stats_array[$stat_name][2] == 'money' ) { $stat_value = $currency_symbol . $stat; } else { $stat_value = $stats_array[$stat_name][2] . $stat; } } else { $stat_value = $stat . $stats_array[$stat_name][2]; } } else { $stat_value = $stat; } echo '
' . $stats_array[$stat_name][0] . '

' . $stat_value . '

' . $stats_array[$stat_name][1] . '

' ; } echo '
' ; } } /* publishing simple bar chart */ public function publish_simple_bar_chart( $location, $title, $xtitle, $ytitle, $chart_data ) { ?>
get_current_dash_settings(); $currency_symbol = get_woocommerce_currency_symbol(); if ( !empty($awca_dash_settings['report_view']) ) { $new_api = AWCA_Auth::get_instance()->get_google_report_api(); $stats_data = $new_api->get_dashboard_data( $awca_dash_settings['report_view'], $awca_dash_settings['report_from'], $awca_dash_settings['report_to'], $tab_id ); $array_name = 'awca_report_chart_data_' . $tab_id; $chart_data_array = AWCA_Settings::get_instance()->{$array_name}; } else { $new_api = AWCA_Auth::get_instance()->get_google_analytics_data_api(); $stats_data = $new_api->get_dashboard_data( $awca_dash_settings['report_from'], $awca_dash_settings['report_to'], $tab_id ); $array_name = 'awca_report_chart_data_ga4_' . $tab_id; $chart_data_array = AWCA_Settings::get_instance()->{$array_name}; } $i = 0; foreach ( $chart_data_array as $chart_name => $chart_parameters ) { if ( $chart_name == 'stats' ) { if ( !empty($awca_dash_settings['report_view']) ) { $array_name = 'awca_dash_stats_data_' . $tab_id; $stats_array = AWCA_Settings::get_instance()->{$array_name}; } else { $array_name = 'awca_dash_stats_data_ga4_' . $tab_id; $stats_array = AWCA_Settings::get_instance()->{$array_name}; } $this->publish_stat_data( $stats_data[$i], $stats_array, $currency_symbol ); } else { if ( $chart_parameters[0] == 'bar' ) { echo '
' ; if ( is_array( $stats_data[$i] ) ) { $stats_data[$i] = array_reverse( $stats_data[$i] ); } else { $stats_data[$i] = array(); } $this->publish_simple_bar_chart( "chartdiv" . $i . $tab_id, $chart_parameters[1], $chart_parameters[2], $chart_parameters[3], $stats_data[$i] ); echo '
' ; } elseif ( $chart_parameters[0] == 'pie' ) { echo '
' ; if ( is_array( $stats_data[$i] ) ) { $stats_data[$i] = array_reverse( $stats_data[$i] ); } else { $stats_data[$i] = array(); } $this->publish_simple_pie_chart( "chartdiv" . $i . $tab_id, $chart_parameters[1], $stats_data[$i] ); echo '
' ; } } $i++; } } /* revoke google analytics access from website */ public function web_revoke_access() { check_ajax_referer( 'awca-revoke-access', 'security' ); $url = $this->get_access_token_revoke_url(); $response = wp_remote_get( $url ); if ( is_wp_error( $response ) ) { error_log( sprintf( 'Could not revoke access token: %s', json_encode( $response->errors ) ), 0 ); } delete_option( 'awca_access_token' ); delete_option( 'awca_refresh_token' ); delete_option( 'awca_auth_settings' ); wp_die(); } /* Adding localize script */ public function load_local_script() { wp_register_script( 'awca_js', AWCA_URL . 'assests/js/awca.js', array( 'jquery' ), null, true ); wp_localize_script( 'awca_js', 'awca_js_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'auth_url' => $this->get_link_url(), 'revoke_access_nonce' => wp_create_nonce( 'awca-revoke-access' ), 'un_link_nonce' => wp_create_nonce( 'awca-un-link' ), 'tab_update_nonce' => wp_create_nonce( 'awca-tab-update' ), 'current_tab_id' => get_option( 'awca_current_tab_id', 'audience' ), ) ); if ( isset( $_GET['page'] ) && $_GET['page'] == 'awca_pro_plugin_options' ) { wp_enqueue_script( 'awca_js' ); } } /* parsing access token */ private function parse_access_token( $json_token = '' ) { $token = [ 'access_token' => '', 'expires_in' => 0, 'created' => current_time( 'timestamp', true ), ]; if ( is_string( $json_token ) && '' !== $json_token ) { $token = wp_parse_args( (array) json_decode( $json_token ), $token ); } return (object) $token; } /*get access token from database */ public function get_access_token() { return get_option( 'awca_access_token', null ); } /* get revoke url for revoking access token */ public function get_access_token_revoke_url() { $revoke_url = null; $token = $this->parse_access_token( $this->get_access_token() )->access_token; if ( isset( $token ) && !empty($token) ) { $revoke_url = self::PROXY_URL . '/auth/revoke?token2=' . base64_encode( $token ); } return $revoke_url; } /* get linking url for linking Google Analytics and Website */ public function get_link_url() { return self::PROXY_URL . '/auth?callback=' . urlencode( $this->get_callback_url() ); } /* get call back url for completing rest work of linking */ public function get_callback_url() { return get_home_url( null, 'wc-api/woocommerce-analytics/auth' ); } /* get call back url for get refreshed token */ public function get_refresh_callback_url() { return get_home_url( null, 'wc-api/woocommerce-analytics/refresh' ); } /* authentication function */ public function authenticate() { if ( isset( $_GET['access_token'] ) ) { if ( !isset( $_REQUEST['access_token'] ) || empty($_REQUEST['access_token']) ) { echo '' ; wp_die( 'some error occured please try again after some time or use manual connect method' ); } $json_token = sanitize_text_field( base64_decode( $_REQUEST['access_token'], true ) ); $token = json_decode( $json_token, true ); if ( !$token ) { echo '' ; wp_die( 'some error occured please try again after some time or use manual connect method' ); } //if we receive require data from token then save value if ( isset( $token['refresh_token'] ) && isset( $token['access_token'] ) ) { update_option( 'awca_access_token', $json_token ); update_option( 'awca_refresh_token', $token['refresh_token'] ); } echo '' ; wp_die(); } } /* getting tracking id and account id information */ private function get_ga_property_part( $key ) { $auth_settings = get_option( 'awca_auth_settings' ); $property = $auth_settings['property_id']; if ( !$property ) { return; } $pieces = explode( '|', $property ); if ( !isset( $pieces[$key] ) ) { return; } return $pieces[$key]; } /* getting account id */ public function get_ga_account_id() { return $this->get_ga_property_part( 0 ); } /* getting property id */ public function get_ga_property_id() { return $this->get_ga_property_part( 1 ); } /* getting list of ga4 properties */ public function get_analytics_g4_properties() { $g4_account_summary = array(); if ( $this->get_access_token() ) { $api = $this->get_google_management_api(); $g4_account_summary_object = $api->get_g4_account_summaries(); if ( !empty($g4_account_summary_object) ) { $g4_account_summary = $g4_account_summary_object->accountSummaries; } else { try { $token = $this->refresh_access_token(); } catch ( Exception $e ) { error_log( $e->getMessage() ); $token = $this->parse_access_token(); } } } return $g4_account_summary; } /* getting list of properties */ public function get_analytics_properties() { $list_account_summaries = array(); if ( $this->get_access_token() ) { $api = $this->get_google_management_api(); $account_summary_object = $api->get_account_summaries(); if ( !empty($account_summary_object) ) { $list_account_summaries = $api->list_account_summaries( $account_summary_object ); } else { try { $token = $this->refresh_access_token(); } catch ( Exception $e ) { error_log( $e->getMessage() ); $token = $this->parse_access_token(); } } } return $list_account_summaries; } /* getting list of views for property */ public function get_analytics_property_views() { $list_property_views = array(); $account_id = $this->get_ga_account_id(); $property_id = $this->get_ga_property_id(); if ( $this->get_access_token() && !empty($property_id) && strpos( $property_id, 'UA' ) !== false && !empty($account_id) ) { $api = $this->get_google_management_api(); $property_view_summary = $api->get_property_views( $account_id, $property_id ); $list_property_views = $api->list_views( $property_view_summary ); } return $list_property_views; } /* getting management api */ public function get_google_management_api() { require_once AWCA_DIR . 'inc/api/awca-google-management-api.php'; $token = $this->parse_access_token( $this->get_access_token() ); // refresh token if it's expired if ( $this->is_access_token_expired( $token ) ) { try { $token = $this->refresh_access_token(); } catch ( Exception $e ) { error_log( $e->getMessage() ); $token = $this->parse_access_token(); } } return $this->management_api = new AWCA_Google_Management_API( $token->access_token ); } /* getting report api */ public function get_google_report_api() { require_once AWCA_DIR . 'inc/api/awca-google-report-api.php'; $token = $this->parse_access_token( $this->get_access_token() ); // refresh token if it's expired if ( $this->is_access_token_expired( $token ) ) { try { $token = $this->refresh_access_token(); } catch ( Exception $e ) { error_log( $e->getMessage() ); $token = $this->parse_access_token(); } } return $this->report_api = new AWCA_Google_Report_API( $token->access_token ); } /* getting report api */ public function get_google_analytics_data_api() { require_once AWCA_DIR . 'inc/api/awca-google-analytics-data-api.php'; $token = $this->parse_access_token( $this->get_access_token() ); // refresh token if it's expired if ( $this->is_access_token_expired( $token ) ) { try { $token = $this->refresh_access_token(); } catch ( Exception $e ) { error_log( $e->getMessage() ); $token = $this->parse_access_token(); } } return $this->report_api = new AWCA_Google_Analytics_Data_API( $token->access_token ); } /* refeshing access token */ private function refresh_access_token() { if ( !$this->get_refresh_token() ) { throw new Exception( 'Could not refresh access token: refresh token not available.' ); } $refresh_url = $this->get_access_token_refresh_url(); $response = wp_remote_get( $refresh_url, array( 'timeout' => MINUTE_IN_SECONDS, ) ); if ( $response instanceof \WP_Error ) { $updated = update_option( 'awca_access_token', false ); throw new Exception( sprintf( 'Could not refresh access token: %s', json_encode( $response->errors ) ) ); return false; } if ( !$response || empty($response['body']) ) { $updated = update_option( 'awca_access_token', false ); throw new Exception( 'Could not refresh access token: response was empty.' ); return false; } if ( isset( $response['response']['code'] ) && 500 === (int) $response['response']['code'] ) { $updated = update_option( 'awca_access_token', false ); throw new Exception( 'Could not refresh access token: a server error occurred.' ); return false; } // try to decode and sanitizing the token $json_token = sanitize_text_field( base64_decode( $response['body'], true ) ); if ( !json_decode( $json_token, true ) || stripos( $json_token, 'error' ) !== false ) { $updated = update_option( 'awca_access_token', false ); throw new Exception( 'Could not refresh access token: returned token was invalid.' ); return false; } $updated = update_option( 'awca_access_token', $json_token ); return $this->parse_access_token( $json_token ); } /* getting refresh token url */ public function get_access_token_refresh_url() { $refresh_url = null; if ( $refresh_token = $this->get_refresh_token() ) { $refresh_url = self::PROXY_URL . '/auth/refresh?token=' . base64_encode( $refresh_token ) . '&callback=' . urlencode( $this->get_refresh_callback_url() ); } return $refresh_url; } /* getting refresh token */ private function get_refresh_token() { return get_option( 'awca_refresh_token', null ); } /* checking access token is expired or not */ private function is_access_token_expired( $token ) { $expired = !(is_object( $token ) && $token->created && $token->expires_in); if ( !$expired ) { $time_now = current_time( 'timestamp', true ); $time_expires = max( 0, (int) $token->created + (int) $token->expires_in ); $expired = $time_expires <= $time_now; } return $expired; } /* getting configuration erros between analytics and website */ public function get_awca_config_status() { $awca_config_status = array(); if ( $this->get_access_token() && get_option( 'awca_auth_settings' ) ) { $property_id = $this->get_ga_property_id(); $account_id = $this->get_ga_account_id(); if ( $account_id && $property_id ) { $api = $this->get_google_management_api(); $views = $api->get_profiles( $account_id, $property_id ); $profiles = $api->list_views( $views ); $error = 0; if ( !empty($profiles) && is_array( $profiles ) ) { foreach ( $profiles as $profile ) { $internal_error = 0; if ( !isset( $profile->id, $profile->internalWebPropertyId, $profile->name ) ) { continue; } $profile_id = $profile->id; $property_internal_id = $profile->internalWebPropertyId; if ( empty($profile->eCommerceTracking) ) { $error++; $internal_error++; } if ( isset( $profile->currency ) && $profile->currency !== get_woocommerce_currency() ) { $error++; $internal_error++; } if ( isset( $profile->websiteUrl ) && $profile->websiteUrl !== get_site_url() ) { $error++; $internal_error++; } $profile->internal_error = $internal_error; } $awca_config_status['account_id'] = $account_id; $awca_config_status['propertly_id'] = $property_id; $awca_config_status['profiles'] = $profiles; $awca_config_status['error_count'] = $error; } } } return $awca_config_status; } }