/** * Getting the instance object * * This method will return the instance of itself, if instance not exists, becauses of it's called for the first * time, the instance will be created. * * @return null|Yoast_Google_Analytics */ public static function get_instance() { if (is_null(self::$instance)) { self::$instance = new self(); } return self::$instance; }
<?php echo Yoast_GA_Admin_Form::create_form('settings'); ?> <input type="hidden" name="return_tab" id="return_tab" value="general" /> <div class="tabwrapper"> <div id="general" class="gatab"> <?php echo '<h2>' . __('General settings', 'google-analytics-for-wordpress') . '</h2>'; echo '<div id="ga-promote">'; $ga_class = Yoast_Google_Analytics::get_instance(); $wp_block_google = $ga_class->check_google_access_from_wp(); $check_google_access = $ga_class->check_google_access(); if ($wp_block_google && $check_google_access) { $profiles = Yoast_GA_Admin_Form::parse_optgroups($yoast_ga_admin->get_profiles()); $auth_url = Yoast_Google_Analytics::get_instance()->create_auth_url(); add_thickbox(); echo '<script>yst_thickbox_heading = "' . __('Paste your Google authentication code', 'google-analytics-for-wordpress') . '";</script>'; echo "<div id='google_ua_code_field'>"; if (count($profiles) == 0) { echo '<div class="ga-form ga-form-input">'; echo '<label class="ga-form ga-form-text-label ga-form-label-left" id="yoast-ga-form-label-text-ga-authwithgoogle">' . __('Google profile', 'google-analytics-for-wordpress') . ':</label>'; echo '<a id="yst_ga_authenticate" class="button" onclick="yst_popupwindow(\'' . $auth_url . '\',500,500);">' . __('Authenticate with your Google account', 'google-analytics-for-wordpress') . '</a>'; echo '</div>'; echo '<div class="ga-form ga-form-input">'; echo '<label class="ga-form ga-form-text-label ga-form-label-left" id="yoast-ga-form-label-text-ga-authwithgoogle">' . __('Current UA-profile', 'google-analytics-for-wordpress') . '</label>'; echo esc_html($yoast_ga_admin->get_tracking_code()); echo '</div>'; } else { echo Yoast_GA_Admin_Form::select(__('Analytics profile', 'google-analytics-for-wordpress'), 'analytics_profile', $profiles, null, false, __('Select a profile', 'google-analytics-for-wordpress')); echo '<div class="ga-form ga-form-input">';
/** * Execute an API call to Google Analytics and store the data in the dashboards data class * * @param string $metric * @param string $start_date 2014-10-16 * @param string $end_date 2014-11-20 * @param string $dimensions ga:date * @param string $storage_name auto * * @return bool */ private function execute_call($metric, $start_date, $end_date, $dimensions = 'ga:date', $storage_name = 'auto') { $dimensions = $this->prepare_dimensions($dimensions, $metric); $params = $this->build_params_for_call($start_date, $end_date, $dimensions, $metric); $storage_type = $this->get_storage_type($dimensions); $response = Yoast_Google_Analytics::get_instance()->do_request('https://www.googleapis.com/analytics/v3/data/ga?' . $params); if (isset($response['response']['code']) && $response['response']['code'] == 200) { // Delete option api_fail because there it's successful now delete_option('yst_ga_api_call_fail'); // Success, set a transient which stores the latest runtime update_option('yst_ga_last_wp_run', date('Y-m-d')); $response = Yoast_Googleanalytics_Reporting::get_instance()->parse_response($response, $storage_type, $start_date, $end_date); } else { // When response is failing, we should count the number of $this->save_api_failure(); return false; } if (strpos('ga:date', $dimensions) !== false) { return $this->handle_response($response, $metric, $dimensions, $start_date, $end_date, 'datelist', $storage_name); } else { return $this->handle_response($response, $metric, $dimensions, $start_date, $end_date, 'table', $storage_name); } }
/** * Checks if there is a callback to get token from Google Analytics API */ private function google_analytics_listener() { $google_auth_code = filter_input(INPUT_POST, 'google_auth_code'); if ($google_auth_code && current_user_can('manage_options') && wp_verify_nonce(filter_input(INPUT_POST, 'yoast_ga_nonce'), 'save_settings')) { self::analytics_api_clean_up(); Yoast_Google_Analytics::get_instance()->authenticate(trim($google_auth_code)); } }
echo sprintf(__('You have not yet finished setting up Google Analytics for Wordpress by Yoast. Please %sadd your Analytics profile here%s to enable tracking.', 'google-analytics-for-wordpress'), '<a href=" ' . admin_url('admin.php?page=yst_ga_settings#top#general') . '">', '</a>'); echo '</p></div>'; } ?> </div> </div> <div id="dimensions" class="wpseotab gatab"> <?php if ($tracking_code !== '') { if (empty($options['analytics_profile'])) { echo '<div class="ga-promote"><p>'; echo sprintf(__('We need you to authenticate with Google Analytics to use this functionality. If you set your UA-code manually, this won\'t work. You can %sauthenticate your Google Analytics profile here%s to enable dashboards.', 'google-analytics-for-wordpress'), '<a href=" ' . admin_url('admin.php?page=yst_ga_settings#top#general') . '">', '</a>'); echo '</p></div>'; } else { if (!Yoast_Google_Analytics::get_instance()->has_refresh_token()) { echo '<div class="ga-promote"><p>'; echo sprintf(__('Because we\'ve switched to a newer version of the Google Analytics API, you\'ll need to re-authenticate with Google Analytics. We\'re sorry for the inconvenience. You can %sre-authenticate your Google Analytics profile here%s.', 'google-analytics-for-wordpress'), '<a href=" ' . admin_url('admin.php?page=yst_ga_settings#top#general') . '">', '</a>'); echo '</p></div>'; } else { ?> <div class="ga-form ga-form-input"> <label class="ga-form ga-form-checkbox-label ga-form-label-left"><?php echo __('Select a dimension', 'google-analytics-for-wordpress'); ?> </label> </div> <select data-rel='toggle_dimensions' id="toggle_dimensions" style="width: 350px"></select> <?php Yoast_GA_Dashboards_Display::get_instance()->display('dimensions');
public function make_request($params, $context = '') { if (!class_exists('Yoast_Google_Analytics') || !defined('GAWP_FILE')) { trigger_error('GATC: ' . __('No requests can be made because Google Analytics Top Content Widget requires the Google Analytics by Yoast plugin to be installed and activated.', 'top-google-posts'), E_USER_WARNING); return; } $path = dirname(GAWP_FILE); $files_to_include = array('Yoast_Google_CacheParser' => '/vendor/yoast/api-libs/google/io/Google_CacheParser.php', 'Yoast_Google_Utils' => '/vendor/yoast/api-libs/google/service/Google_Utils.php', 'Yoast_Google_HttpRequest' => '/vendor/yoast/api-libs/google/io/Google_HttpRequest.php', 'Yoast_Google_IO' => '/vendor/yoast/api-libs/google/io/Google_IO.php', 'Yoast_Google_WPIO' => '/vendor/yoast/api-libs/google/io/Google_WPIO.php', 'Yoast_Google_Auth' => '/vendor/yoast/api-libs/google/auth/Google_Auth.php', 'Yoast_Google_OAuth2' => '/vendor/yoast/api-libs/google/auth/Google_OAuth2.php', 'Yoast_Google_Cache' => '/vendor/yoast/api-libs/google/cache/Google_Cache.php', 'Yoast_Google_WPCache' => '/vendor/yoast/api-libs/google/cache/Google_WPCache.php', 'Yoast_Google_Client' => '/vendor/yoast/api-libs/google/Google_Client.php', 'Yoast_Google_Analytics_Client' => '/vendor/yoast/api-libs/googleanalytics/class-google-analytics-client.php'); if (version_compare(GAWP_VERSION, '5.4.3') >= 0) { unset($files_to_include['Yoast_Google_Analytics_Client']); $files_to_include['Yoast_Api_Google_Client'] = '/vendor/yoast/api-libs/class-api-google-client.php'; } foreach ($files_to_include as $class => $file) { if (!is_admin() || !class_exists($class, true)) { require_once $path . $file; } } $params = apply_filters('gtc_analytics_request_params', $params); if ($context && is_scalar($context)) { $params = apply_filters("gtc_analytics_{$context}_request_params", $params); } $response = Yoast_Google_Analytics::get_instance()->do_request(add_query_arg($params, 'https://www.googleapis.com/analytics/v3/data/ga')); return isset($response['response']['code']) && 200 == $response['response']['code'] ? wp_remote_retrieve_body($response) : false; }
/** * Set the API options */ public function set_options() { $this->options = Yoast_Google_Analytics::get_instance()->get_options(); $this->set_access_token(); }
/** * Checks if there is a callback or reauth to get token from Google Analytics api */ private function connect_with_google_analytics() { if (isset($_REQUEST['ga_oauth_callback'])) { Yoast_Google_Analytics::instance()->authenticate($_REQUEST['oauth_token'], $_REQUEST['oauth_verifier']); wp_redirect(menu_page_url('yst_ga_settings', false)); exit; } if (!empty($_GET['reauth'])) { $authorize_url = Yoast_Google_Analytics::instance()->authenticate(); wp_redirect($authorize_url); exit; } }
public function make_request($params, $context = '') { if (!class_exists('Yoast_Google_Analytics')) { return; } if (!is_admin()) { $path = dirname(GAWP_FILE); $files_to_include = array('Yoast_Google_CacheParser' => '/vendor/yoast/api-libs/google/io/Google_CacheParser.php', 'Yoast_Google_Utils' => '/vendor/yoast/api-libs/google/service/Google_Utils.php', 'Yoast_Google_HttpRequest' => '/vendor/yoast/api-libs/google/io/Google_HttpRequest.php', 'Yoast_Google_IO' => '/vendor/yoast/api-libs/google/io/Google_IO.php', 'Yoast_Google_WPIO' => '/vendor/yoast/api-libs/google/io/Google_WPIO.php', 'Yoast_Google_Auth' => '/vendor/yoast/api-libs/google/auth/Google_Auth.php', 'Yoast_Google_OAuth2' => '/vendor/yoast/api-libs/google/auth/Google_OAuth2.php', 'Yoast_Google_Cache' => '/vendor/yoast/api-libs/google/cache/Google_Cache.php', 'Yoast_Google_WPCache' => '/vendor/yoast/api-libs/google/cache/Google_WPCache.php', 'Yoast_Google_Client' => '/vendor/yoast/api-libs/google/Google_Client.php', 'Yoast_Google_Analytics_Client' => '/vendor/yoast/api-libs/googleanalytics/class-google-analytics-client.php'); if (version_compare(GAWP_VERSION, '5.4.3') >= 0) { unset($files_to_include['Yoast_Google_Analytics_Client']); $files_to_include['Yoast_Api_Google_Client'] = '/vendor/yoast/api-libs/class-api-google-client.php'; } foreach ($files_to_include as $class => $file) { require_once $path . $file; } } $params = apply_filters('gtc_analytics_request_params', $params); if ($context && is_scalar($context)) { $params = apply_filters("gtc_analytics_{$context}_request_params", $params); } $response = Yoast_Google_Analytics::get_instance()->do_request(add_query_arg($params, 'https://www.googleapis.com/analytics/v3/data/ga')); return isset($response['response']['code']) && 200 == $response['response']['code'] ? wp_remote_retrieve_body($response) : false; }
/** * Checks if there is a callback or reauth to get token from Google Analytics api */ private function google_analytics_listener() { if (!empty($_POST['google_auth_code'])) { Yoast_Google_Analytics::get_instance()->authenticate(trim($_POST['google_auth_code'])); } if (!empty($_GET['reauth'])) { delete_option('yst_ga_accounts'); delete_option('yst_ga_response'); Yoast_Google_Analytics::get_instance()->authenticate(); } }
/** * Execute an API call to Google Analytics and store the data in the dashboards data class * * @param $metric * @param $start_date 2014-10-16 * @param $end_date 2014-11-20 * @param $dimensions ga:date * @param $storage_name string * * @return bool */ private function execute_call($metric, $start_date, $end_date, $dimensions = 'ga:date', $storage_name = 'auto') { $dimensions = $this->prepare_dimensions($dimensions, $metric); $params = $this->build_params_for_call($start_date, $end_date, $dimensions, $metric); $storage_type = $this->get_storage_type($dimensions); $response = Yoast_Google_Analytics::get_instance()->do_request('https://www.googleapis.com/analytics/v3/data/ga?' . $params); if (isset($response['response']['code']) && $response['response']['code'] == 200) { $response = Yoast_Googleanalytics_Reporting::get_instance()->parse_response($response, $storage_type, $start_date, $end_date); } else { return false; } if (strpos('ga:date', $dimensions) !== false) { return $this->handle_response($response, $metric, $dimensions, $start_date, $end_date, 'datelist', $storage_name); } else { return $this->handle_response($response, $metric, $dimensions, $start_date, $end_date, 'table', $storage_name); } }
/** * Check if singleton is an instance of Yoast_Google_Analytics * * @covers Yoast_Google_Analytics::instance() */ public function test_instance() { $instance = Yoast_Google_Analytics::get_instance(); $this->assertTrue($instance instanceof Yoast_Google_Analytics); }