Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove options remapping, use settings directly. #360

Merged
merged 4 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions includes/class-wc-abstract-google-analytics-js.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ abstract class WC_Abstract_Google_Analytics_JS {
/** @var WC_Abstract_Google_Analytics_JS $instance Class Instance */
protected static $instance;

/** @var array $options Inherited Analytics options */
protected static $options;
/** @var array $settings Inherited Analytics settings */
protected static $settings;

/** @var string Developer ID */
public const DEVELOPER_ID = 'dOGY3NW';
Expand Down Expand Up @@ -103,14 +103,14 @@ function( $order_id ) {
}

/**
* Return one of our options
* Return one of our settings
*
* @param string $option Key/name for the option.
* @param string $setting Key/name for the setting.
*
* @return string|null Value of the option or null if not found
* @return string|null Value of the setting or null if not found
*/
protected static function get( $option ): ?string {
return self::$options[ $option ] ?? null;
protected static function get( $setting ): ?string {
return self::$settings[ $setting ] ?? null;
}

/**
Expand Down Expand Up @@ -337,8 +337,8 @@ abstract public function append_script_data( string $type, $data ): void;
/**
* Get the class instance
*
* @param array $options Options
* @param array $settings Settings
* @return WC_Abstract_Google_Analytics_JS
*/
abstract public static function get_instance( $options = array() ): WC_Abstract_Google_Analytics_JS;
abstract public static function get_instance( $settings = array() ): WC_Abstract_Google_Analytics_JS;
}
112 changes: 17 additions & 95 deletions includes/class-wc-google-analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,6 @@
*/
class WC_Google_Analytics extends WC_Integration {

/** @var string $ga_id Google Analytics Tracking ID */
public $ga_id;

/** @var string $ga_standard_tracking_enabled Is standard tracking enabled (yes|no) */
public $ga_standard_tracking_enabled;

/** @var string $ga_support_display_advertising Supports display advertising (yes|no) */
public $ga_support_display_advertising;

/** @var string $ga_support_enhanced_link_attribution Use enhanced link attribution (yes|no) */
public $ga_support_enhanced_link_attribution;

/** @var string $ga_anonymize_enabled Anonymize IP addresses (yes|no) */
public $ga_anonymize_enabled;

/** @var string $ga_404_tracking_enabled Track 404 errors (yes|no) */
public $ga_404_tracking_enabled;

/** @var string $ga_ecommerce_tracking_enabled Purchase transactions (yes|no) */
public $ga_ecommerce_tracking_enabled;

/** @var string $ga_enhanced_remove_from_cart_enabled Track remove from cart events (yes|no) */
public $ga_enhanced_remove_from_cart_enabled;

/** @var string $ga_enhanced_product_impression_enabled Track product impressions (yes|no) */
public $ga_enhanced_product_impression_enabled;

/** @var string $ga_enhanced_product_click_enabled Track product clicks (yes|no) */
public $ga_enhanced_product_click_enabled;

/** @var string $ga_enhanced_checkout_process_enabled Track checkout initiated (yes|no) */
public $ga_enhanced_checkout_process_enabled;

/** @var string $ga_enhanced_product_detail_view_enabled Track product detail views (yes|no) */
public $ga_enhanced_product_detail_view_enabled;

/** @var string $ga_event_tracking_enabled Track add to cart events (yes|no) */
public $ga_event_tracking_enabled;

/** @var string $ga_linker_cross_domains Domains for automatic linking */
public $ga_linker_cross_domains;

/** @var string $ga_linker_allow_incoming_enabled Accept incoming linker (yes|no) */
public $ga_linker_allow_incoming_enabled;

/**
* Defines the script handles that should be async.
*/
Expand All @@ -69,11 +24,10 @@ class WC_Google_Analytics extends WC_Integration {
/**
* Returns the proper class based on Gtag settings.
*
* @param array $options Options
* @return WC_Abstract_Google_Analytics_JS
*/
protected function get_tracking_instance( $options = array() ) {
return WC_Google_Gtag_JS::get_instance( $options );
protected function get_tracking_instance() {
return WC_Google_Gtag_JS::get_instance( $this->settings );
}

/**
Expand All @@ -88,19 +42,18 @@ public function __construct() {
// Load the settings
$this->init_form_fields();
$this->init_settings();
$constructor = $this->init_options();

add_action( 'admin_notices', array( $this, 'universal_analytics_upgrade_notice' ) );

// Contains snippets/JS tracking code
include_once 'class-wc-abstract-google-analytics-js.php';
include_once 'class-wc-google-gtag-js.php';
$this->get_tracking_instance( $constructor );
$this->get_tracking_instance();

// Display a task on "Things to do next section"
add_action( 'init', array( $this, 'add_wc_setup_task' ), 20 );
// Admin Options
add_filter( 'woocommerce_tracker_data', array( $this, 'track_options' ) );
add_filter( 'woocommerce_tracker_data', array( $this, 'track_settings' ) );
add_action( 'woocommerce_update_options_integration_google_analytics', array( $this, 'process_admin_options' ) );
add_action( 'woocommerce_update_options_integration_google_analytics', array( $this, 'show_options_info' ) );
add_action( 'admin_init', array( $this, 'privacy_policy' ) );
Expand Down Expand Up @@ -135,38 +88,6 @@ public function universal_analytics_upgrade_notice() {
}
}

/**
* Loads all of our options for this plugin (stored as properties as well)
*
* @return array An array of options that can be passed to other classes
*/
public function init_options() {
$options = array(
'ga_product_identifier' => 'product_sku',
'ga_id' => null,
'ga_standard_tracking_enabled' => null,
'ga_support_display_advertising' => null,
'ga_support_enhanced_link_attribution' => null,
'ga_anonymize_enabled' => null,
'ga_404_tracking_enabled' => null,
'ga_enhanced_remove_from_cart_enabled' => null,
'ga_enhanced_product_impression_enabled' => null,
'ga_enhanced_product_click_enabled' => null,
'ga_enhanced_checkout_process_enabled' => null,
'ga_enhanced_product_detail_view_enabled' => null,
'ga_event_tracking_enabled' => null,
'ga_linker_cross_domains' => null,
'ga_linker_allow_incoming_enabled' => null,
);

$constructor = array();
foreach ( $options as $option => $default ) {
$constructor[ $option ] = $this->$option = $this->get_option( $option, $default );
}

return $constructor;
}

/**
* Tells WooCommerce which settings to display under the "integration" tab
*/
Expand Down Expand Up @@ -315,22 +236,23 @@ public function show_options_info() {
* @param array $data Current WC tracker data.
* @return array Updated WC Tracker data.
*/
public function track_options( $data ) {
public function track_settings( $data ) {
$settings = $this->settings;
$data['wc-google-analytics'] = array(
'standard_tracking_enabled' => $this->ga_standard_tracking_enabled,
'support_display_advertising' => $this->ga_support_display_advertising,
'support_enhanced_link_attribution' => $this->ga_support_enhanced_link_attribution,
'anonymize_enabled' => $this->ga_anonymize_enabled,
'ga_404_tracking_enabled' => $this->ga_404_tracking_enabled,
'ecommerce_tracking_enabled' => $this->ga_ecommerce_tracking_enabled,
'event_tracking_enabled' => $this->ga_event_tracking_enabled,
'standard_tracking_enabled' => $settings['ga_standard_tracking_enabled'],
'support_display_advertising' => $settings['ga_support_display_advertising'],
'support_enhanced_link_attribution' => $settings['ga_support_enhanced_link_attribution'],
'anonymize_enabled' => $settings['ga_anonymize_enabled'],
'ga_404_tracking_enabled' => $settings['ga_404_tracking_enabled'],
'ecommerce_tracking_enabled' => $settings['ga_ecommerce_tracking_enabled'],
'event_tracking_enabled' => $settings['ga_event_tracking_enabled'],
'plugin_version' => WC_GOOGLE_ANALYTICS_INTEGRATION_VERSION,
'linker_allow_incoming_enabled' => empty( $this->ga_linker_allow_incoming_enabled ) ? 'no' : 'yes',
'linker_cross_domains' => $this->ga_linker_cross_domains,
'linker_allow_incoming_enabled' => empty( $settings['ga_linker_allow_incoming_enabled'] ) ? 'no' : 'yes',
'linker_cross_domains' => $settings['ga_linker_cross_domains'],
);

// ID prefix, blank, or X for unknown
$prefix = strstr( strtoupper( $this->ga_id ), '-', true );
$prefix = strstr( strtoupper( $settings['ga_id'] ), '-', true );
if ( in_array( $prefix, array( 'UA', 'G', 'GT' ), true ) || empty( $prefix ) ) {
$data['wc-google-analytics']['ga_id'] = $prefix;
} else {
Expand Down Expand Up @@ -419,7 +341,7 @@ protected function enqueue_ecommerce_tracking_code( $order_id ) {
* @return bool True if tracking for a certain setting is disabled
*/
private function disable_tracking( $type ) {
return is_admin() || current_user_can( 'manage_options' ) || ( ! $this->ga_id ) || 'no' === $type || apply_filters( 'woocommerce_ga_disable_tracking', false, $type );
return is_admin() || current_user_can( 'manage_options' ) || ( ! $this->settings['ga_id'] ) || 'no' === $type || apply_filters( 'woocommerce_ga_disable_tracking', false, $type );
}

/**
Expand Down
18 changes: 9 additions & 9 deletions includes/class-wc-google-gtag-js.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ class WC_Google_Gtag_JS extends WC_Abstract_Google_Analytics_JS {

/**
* Constructor
* Takes our options from the parent class so we can later use them in the JS snippets
* Takes our settings from the parent class so we can later use them in the JS snippets
*
* @param array $options Options
* @param array $settings Settings
*/
public function __construct( $options = array() ) {
public function __construct( $settings = array() ) {
parent::__construct();
self::$options = $options;
self::$settings = $settings;

$this->load_analytics_config();
$this->map_actions();
Expand Down Expand Up @@ -199,8 +199,8 @@ public static function get_enabled_events(): array {
'begin_checkout' => 'ga_enhanced_checkout_process_enabled',
);

foreach( $settings as $event => $option_name ) {
if ( 'yes' === self::get( $option_name ) ) {
foreach( $settings as $event => $setting_name ) {
if ( 'yes' === self::get( $setting_name ) ) {
$events[] = $event;
}
}
Expand All @@ -211,12 +211,12 @@ public static function get_enabled_events(): array {
/**
* Get the class instance
*
* @param array $options Options
* @param array $settings Settings
* @return WC_Abstract_Google_Analytics_JS
*/
public static function get_instance( $options = array() ): WC_Abstract_Google_Analytics_JS {
public static function get_instance( $settings = array() ): WC_Abstract_Google_Analytics_JS {
if ( null === self::$instance ) {
self::$instance = new self( $options );
self::$instance = new self( $settings );
}

return self::$instance;
Expand Down
Loading