ga_gtag_enabled ) {
return WC_Google_Gtag_JS::get_instance( $options );
}
return WC_Google_Analytics_JS::get_instance( $options );
}
/**
* Constructor
* Init and hook in the integration.
*/
public function __construct() {
$this->id = 'google_analytics';
$this->method_title = __( 'Google Analytics', 'woocommerce-google-analytics-integration' );
$this->method_description = __( 'Google Analytics is a free service offered by Google that generates detailed statistics about the visitors to a website.', 'woocommerce-google-analytics-integration' );
$this->dismissed_info_banner = get_option( 'woocommerce_dismissed_info_banner' );
// Load the settings
$this->init_form_fields();
$this->init_settings();
$constructor = $this->init_options();
// Contains snippets/JS tracking code
include_once( 'class-wc-abstract-google-analytics-js.php' );
include_once( 'class-wc-google-analytics-js.php' );
include_once( 'class-wc-google-gtag-js.php' );
$this->get_tracking_instance( $constructor );
// Display an info banner on how to configure WooCommerce
if ( is_admin() ) {
include_once( 'class-wc-google-analytics-info-banner.php' );
WC_Google_Analytics_Info_Banner::get_instance( $this->dismissed_info_banner, $this->ga_id );
}
// Admin Options
add_filter( 'woocommerce_tracker_data', array( $this, 'track_options' ) );
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_enqueue_scripts', array( $this, 'load_admin_assets') );
// Tracking code
add_action( 'wp_head', array( $this, 'tracking_code_display' ), 999999 );
// Event tracking code
add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'add_to_cart' ) );
add_action( 'wp_footer', array( $this, 'loop_add_to_cart' ) );
add_action( 'woocommerce_after_cart', array( $this, 'remove_from_cart' ) );
add_action( 'woocommerce_after_mini_cart', array( $this, 'remove_from_cart' ) );
add_filter( 'woocommerce_cart_item_remove_link', array( $this, 'remove_from_cart_attributes' ), 10, 2 );
add_action( 'woocommerce_after_shop_loop_item', array( $this, 'listing_impression' ) );
add_action( 'woocommerce_after_shop_loop_item', array( $this, 'listing_click' ) );
add_action( 'woocommerce_after_single_product', array( $this, 'product_detail' ) );
add_action( 'woocommerce_after_checkout_form', array( $this, 'checkout_process' ) );
// utm_nooverride parameter for Google AdWords
add_filter( 'woocommerce_get_return_url', array( $this, 'utm_nooverride' ) );
}
/**
* 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_id',
'ga_set_domain_name',
'ga_gtag_enabled',
'ga_standard_tracking_enabled',
'ga_support_display_advertising',
'ga_support_enhanced_link_attribution',
'ga_use_universal_analytics',
'ga_anonymize_enabled',
'ga_404_tracking_enabled',
'ga_ecommerce_tracking_enabled',
'ga_enhanced_ecommerce_tracking_enabled',
'ga_enhanced_remove_from_cart_enabled',
'ga_enhanced_product_impression_enabled',
'ga_enhanced_product_click_enabled',
'ga_enhanced_checkout_process_enabled',
'ga_enhanced_product_detail_view_enabled',
'ga_event_tracking_enabled',
'ga_linker_cross_domains',
'ga_linker_allow_incoming_enabled',
);
$constructor = array();
foreach ( $options as $option ) {
$constructor[ $option ] = $this->$option = $this->get_option( $option );
}
return $constructor;
}
/**
* Tells WooCommerce which settings to display under the "integration" tab
*/
public function init_form_fields() {
// backwards_compatibility
if ( get_option( 'woocommerce_ga_use_universal_analytics' ) ) {
$ua_default_value = get_option( 'woocommerce_ga_use_universal_analytics' );
} else {
// don't enable for extension updates, only default to enabled on new installs
$ua_default_value = get_option( $this->get_option_key() ) ? 'no' : 'yes';
}
$this->form_fields = array(
'ga_id' => array(
'title' => __( 'Google Analytics Tracking ID', 'woocommerce-google-analytics-integration' ),
'description' => __( 'Log into your Google Analytics account to find your ID. e.g. G-XXXXX or UA-XXXXX-X', 'woocommerce-google-analytics-integration' ),
'type' => 'text',
'placeholder' => 'UA-XXXXX-X',
'default' => get_option( 'woocommerce_ga_id' ) // Backwards compat
),
'ga_set_domain_name' => array(
'title' => __( 'Set Domain Name', 'woocommerce-google-analytics-integration' ),
'description' => sprintf( __( '(Optional) Sets the _setDomainName variable. See here for more information.', 'woocommerce-google-analytics-integration' ), 'https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingSite#multipleDomains' ),
'type' => 'text',
'default' => '',
'class' => 'legacy-setting',
),
'ga_gtag_enabled' => array(
'title' => __( 'Tracking Options', 'woocommerce-google-analytics-integration' ),
'label' => __( 'Use Global Site Tag', 'woocommerce-google-analytics-integration' ),
'description' => sprintf( __( 'The Global Site Tag provides streamlined tagging across Google’s site measurement, conversion tracking, and remarketing products. This must be enabled to use a Google Analytics 4 Measurement ID (e.g., G-XXXXX). See here for more information.', 'woocommerce-google-analytics-integration' ), 'https://support.google.com/analytics/answer/7475631?hl=en' ),
'type' => 'checkbox',
'checkboxgroup' => '',
'default' => get_option( $this->get_option_key() ) ? 'no' : 'yes', // don't enable on updates, only default on new installs
),
'ga_use_universal_analytics' => array(
'label' => __( 'Enable Universal Analytics', 'woocommerce-google-analytics-integration' ),
'description' => sprintf( __( 'Uses Universal Analytics instead of Classic Google Analytics. If you have not previously used Google Analytics on this site, check this box. Otherwise, %sfollow step 1 of the Universal Analytics upgrade guide.%s Enabling this setting will take care of step 2. %sRead more about Universal Analytics%s. Universal Analytics or Global Site Tag must be enabled to enable enhanced eCommerce.', 'woocommerce-google-analytics-integration' ), '', '', '', '' ),
'type' => 'checkbox',
'checkboxgroup' => '',
'default' => $ua_default_value,
'class' => 'legacy-setting',
),
'ga_standard_tracking_enabled' => array(
'label' => __( 'Enable Standard Tracking', 'woocommerce-google-analytics-integration' ),
'description' => __( 'This tracks session data such as demographics, system, etc. You don\'t need to enable this if you are using a 3rd party Google analytics plugin.', 'woocommerce-google-analytics-integration' ),
'type' => 'checkbox',
'checkboxgroup' => 'start',
'default' => get_option( 'woocommerce_ga_standard_tracking_enabled' ) ? get_option( 'woocommerce_ga_standard_tracking_enabled' ) : 'no' // Backwards compat
),
'ga_support_display_advertising' => array(
'label' => __( '"Display Advertising" Support', 'woocommerce-google-analytics-integration' ),
'description' => sprintf( __( 'Set the Google Analytics code to support Display Advertising. %sRead more about Display Advertising%s.', 'woocommerce-google-analytics-integration' ), '', '' ),
'type' => 'checkbox',
'checkboxgroup' => '',
'default' => get_option( 'woocommerce_ga_support_display_advertising' ) ? get_option( 'woocommerce_ga_support_display_advertising' ) : 'yes', // Backwards compat
),
'ga_support_enhanced_link_attribution' => array(
'label' => __( 'Use Enhanced Link Attribution', 'woocommerce-google-analytics-integration' ),
'description' => sprintf( __( 'Set the Google Analytics code to support Enhanced Link Attribution. %sRead more about Enhanced Link Attribution%s.', 'woocommerce-google-analytics-integration' ), '', '' ),
'type' => 'checkbox',
'checkboxgroup' => '',
'default' => get_option( 'woocommerce_ga_support_enhanced_link_attribution' ) ? get_option( 'woocommerce_ga_support_enhanced_link_attribution' ) : 'no' // Backwards compat
),
'ga_anonymize_enabled' => array(
'label' => __( 'Anonymize IP addresses', 'woocommerce-google-analytics-integration' ),
'description' => sprintf( __( 'Enabling this option is mandatory in certain countries due to national privacy laws. %sRead more about IP Anonymization%s.', 'woocommerce-google-analytics-integration' ), '', '' ),
'type' => 'checkbox',
'checkboxgroup' => '',
'default' => 'yes'
),
'ga_404_tracking_enabled' => array(
'label' => __( 'Track 404 (Not found) Errors', 'woocommerce-google-analytics-integration' ),
'description' => sprintf( __( 'Enable this to find broken or dead links. An "Event" with category "Error" and action "404 Not Found" will be created in Google Analytics for each incoming pageview to a non-existing page. By setting up a "Custom Goal" for these events within Google Analytics you can find out where broken links originated from (the referrer). %sRead how to set up a goal%s.', 'woocommerce-google-analytics-integration' ), '', '' ),
'type' => 'checkbox',
'checkboxgroup' => '',
'default' => 'yes'
),
'ga_ecommerce_tracking_enabled' => array(
'label' => __( 'Purchase Transactions', 'woocommerce-google-analytics-integration' ),
'description' => __( 'This requires a payment gateway that redirects to the thank you/order received page after payment. Orders paid with gateways which do not do this will not be tracked.', 'woocommerce-google-analytics-integration' ),
'type' => 'checkbox',
'checkboxgroup' => 'start',
'default' => get_option( 'woocommerce_ga_ecommerce_tracking_enabled' ) ? get_option( 'woocommerce_ga_ecommerce_tracking_enabled' ) : 'yes', // Backwards compat
),
'ga_event_tracking_enabled' => array(
'label' => __( 'Add to Cart Events', 'woocommerce-google-analytics-integration' ),
'type' => 'checkbox',
'checkboxgroup' => '',
'default' => 'yes',
),
'ga_linker_cross_domains' => array(
'title' => __( 'Cross Domain Tracking', 'woocommerce-google-analytics-integration' ),
/* translators: Read more link */
'description' => sprintf( __( 'Add a comma separated list of domains for automatic linking. %1$sRead more about Cross Domain Measurement%2$s', 'woocommerce-google-analytics-integration' ), '', '' ),
'type' => 'text',
'placeholder' => 'example.com, example.net',
'default' => '',
),
'ga_linker_allow_incoming_enabled' => array(
'label' => __( 'Accept Incoming Linker Parameters', 'woocommerce-google-analytics-integration' ),
'description' => __( 'Enabling this option will allow incoming linker parameters from other websites.', 'woocommerce-google-analytics-integration' ),
'type' => 'checkbox',
'checkboxgroup' => '',
'default' => 'no',
),
'ga_enhanced_ecommerce_tracking_enabled' => array(
'title' => __( 'Enhanced eCommerce', 'woocommerce-google-analytics-integration' ),
'label' => __( 'Enable Enhanced eCommerce ', 'woocommerce-google-analytics-integration' ),
'description' => sprintf( __( 'Enhanced eCommerce allows you to measure more user interactions with your store, including: product impressions, product detail views, starting the checkout process, adding cart items, and removing cart items. Universal Analytics or Global Site Tag must be enabled for Enhanced eCommerce to work. If using Universal Analytics, turn on Enhanced eCommerce in your Google Analytics dashboard before enabling this setting. See here for more information.', 'woocommerce-google-analytics-integration' ), 'https://support.google.com/analytics/answer/6032539?hl=en' ),
'type' => 'checkbox',
'checkboxgroup' => '',
'default' => 'no',
'class' => 'legacy-setting'
),
// Enhanced eCommerce Sub-Settings
'ga_enhanced_remove_from_cart_enabled' => array(
'label' => __( 'Remove from Cart Events', 'woocommerce-google-analytics-integration' ),
'type' => 'checkbox',
'checkboxgroup' => '',
'default' => 'yes',
'class' => 'enhanced-setting'
),
'ga_enhanced_product_impression_enabled' => array(
'label' => __( 'Product Impressions from Listing Pages', 'woocommerce-google-analytics-integration' ),
'type' => 'checkbox',
'checkboxgroup' => '',
'default' => 'yes',
'class' => 'enhanced-setting'
),
'ga_enhanced_product_click_enabled' => array(
'label' => __( 'Product Clicks from Listing Pages', 'woocommerce-google-analytics-integration' ),
'type' => 'checkbox',
'checkboxgroup' => '',
'default' => 'yes',
'class' => 'enhanced-setting'
),
'ga_enhanced_product_detail_view_enabled' => array(
'label' => __( 'Product Detail Views', 'woocommerce-google-analytics-integration' ),
'type' => 'checkbox',
'checkboxgroup' => '',
'default' => 'yes',
'class' => 'enhanced-setting'
),
'ga_enhanced_checkout_process_enabled' => array(
'label' => __( 'Checkout Process Initiated', 'woocommerce-google-analytics-integration' ),
'type' => 'checkbox',
'checkboxgroup' => '',
'default' => 'yes',
'class' => 'enhanced-setting'
),
);
}
/**
* Shows some additional help text after saving the Google Analytics settings
*/
function show_options_info() {
$this->method_description .= "
" . __( 'Please allow Google Analytics 24 hours to start displaying results.', 'woocommerce-google-analytics-integration' ) . "
" . __( 'Please note, for transaction tracking to work properly, you will need to use a payment gateway that redirects the customer back to a WooCommerce order received/thank you page.', 'woocommerce-google-analytics-integration' ) . "