/**
  * Save the Targets settings
  */
 public function save()
 {
     try {
         $this->verify_request(WooCommerce_Grow_Helpers::get_field('_wpnonce', $_REQUEST), 'woocommerce-grow-targets');
         $is_calculate_growth = null !== WooCommerce_Grow_Helpers::get_field('calculate_growth', $_POST) ? true : false;
         // Calculate and Growth settings
         if ($is_calculate_growth) {
             $initial_revenue_number = WooCommerce_Grow_Helpers::get_field('initial_revenue_number', $_POST);
             $initial_orders_number = WooCommerce_Grow_Helpers::get_field('initial_orders_number', $_POST);
             $initial_sessions_number = WooCommerce_Grow_Helpers::get_field('initial_sessions_number', $_POST);
             $initial_cr_number = WooCommerce_Grow_Helpers::get_field('initial_cr_number', $_POST);
             $initial_aov_number = WooCommerce_Grow_Helpers::get_field('initial_aov_number', $_POST);
             // Call to GA to get sessions for the last month
             $ga = WooCommerce_Grow_Google_Analytics::get_instance();
             $month = date('m', strtotime('first day of previous month'));
             $year = date('Y');
             list($start_date, $end_date) = WooCommerce_Grow_Helpers::get_first_and_last_of_the_month($month, $year);
             $initial_sessions = $ga->get_sessions_for_month($month, $year);
             $filters = array('date_min' => $start_date, 'date_max' => $end_date);
             $reports = WooCommerce_Grow_Helpers::setup_wc_reports($filters);
             $initial_revenue = WooCommerce_Grow_Helpers::get_wc_total_sales($reports);
             $initial_orders = WooCommerce_Grow_Helpers::get_wc_total_orders($reports);
             WooCommerce_Grow_Helpers::add_debug_log('Revenue: ' . $initial_revenue);
             WooCommerce_Grow_Helpers::add_debug_log('Orders: ' . $initial_orders);
             $initial_cr = WooCommerce_Grow_Helpers::calculate_cr($initial_orders, $initial_sessions);
             $initial_aov = WooCommerce_Grow_Helpers::calculate_aov($initial_revenue, $initial_orders);
             $growth_rate = WooCommerce_Grow_Helpers::get_field('growth_rate', $_POST);
             // Save the initial options
             WooCommerce_Grow_Helpers::update_option('initial_revenue_number', $initial_revenue);
             WooCommerce_Grow_Helpers::update_option('initial_orders_number', $initial_orders);
             WooCommerce_Grow_Helpers::update_option('initial_sessions_number', $initial_sessions);
             WooCommerce_Grow_Helpers::update_option('initial_cr_number', $initial_cr);
             WooCommerce_Grow_Helpers::update_option('initial_aov_number', $initial_aov);
             WooCommerce_Grow_Helpers::update_option('growth_rate', $growth_rate);
             $months = WooCommerce_Grow_Helpers::get_twelve_months_ahead();
             foreach ($months as $month) {
                 $target_sessions = WooCommerce_Grow_Helpers::calculate_growth($initial_sessions, $growth_rate);
                 $target_cr = WooCommerce_Grow_Helpers::calculate_growth($initial_cr, $growth_rate);
                 $target_aov = WooCommerce_Grow_Helpers::calculate_growth($initial_aov, $growth_rate);
                 $targets['sessions_percentage'][$month['year']][$month['month']] = ceil($target_sessions);
                 $targets['cr_percentage'][$month['year']][$month['month']] = $target_cr;
                 $targets['aov_percentage'][$month['year']][$month['month']] = $target_aov;
                 $targets['revenue_percentage'][$month['year']][$month['month']] = wc_format_decimal($target_sessions * $target_cr * $target_aov, 2);
                 $targets['orders_percentage'][$month['year']][$month['month']] = ceil($target_sessions * $target_cr);
             }
             WooCommerce_Grow_Helpers::update_option('monthly_targets', $targets);
         }
     } catch (Exception $e) {
         WC_Admin_Settings::add_error($e->getMessage());
     }
 }
    /**
     * Check, if the analytics account is authenticated and has an access token.
     * Authenticate it, if it is not.
     *
     * TODO: have the access token in a transient and check if the token expired before continuing
     *
     * @return bool
     */
    function is_app_authenticated()
    {
        $access_token = WooCommerce_Grow_Helpers::get_option('access_token');
        // We have an access token already
        if (!empty($access_token)) {
            try {
                $this->client->setAccessToken($access_token);
            } catch (Exception $e) {
                echo sprintf(__('Error: Unable to set Google Analytics access token. Error Code: %s. Error Message: %s  ', 'woocommerce-grow'), $e->getCode(), $e->getMessage());
                return false;
            }
        } else {
            $settings = get_option('woocommerce_woocommerce_grow_settings', array());
            $auth_code = WooCommerce_Grow_Helpers::get_field('authorization_token', $settings);
            if (empty($auth_code)) {
                // TODO: needs behavior
                return false;
            }
            try {
                // The authenticate method gets an access token, sets the access token
                // and returns the access token all at once.
                $access_token = $this->client->authenticate($auth_code);
                $this->client->setAccessToken($access_token);
                WooCommerce_Grow_Helpers::update_option('access_token', $access_token);
            } catch (Exception $e) {
                echo sprintf(__('Google Analytics was unable to authenticate you.
						Please refresh and try again. If the problem persists, please obtain a new authorizations token.
						Error Code: %s. Error Message: %s  ', 'woocommerce-grow'), $e->getCode(), $e->getMessage());
                return false;
            }
        }
        return true;
    }