woocommerce/includes/tracks/class-wc-site-tracking.php

111 lines
3.0 KiB
PHP
Raw Normal View History

2019-02-01 01:26:38 +00:00
<?php
/**
* Nosara Tracks for WooCommerce
*
* @package WooCommerce\Tracks
*/
defined( 'ABSPATH' ) || exit;
/**
* This class adds actions to track usage of WooCommerce.
*/
class WC_Site_Tracking {
/**
* Check if tracking is enabled.
*
* @return bool
*/
public static function is_tracking_enabled() {
/**
* Don't track users who haven't opted-in to tracking or if a filter
* has been applied to turn it off.
*/
if ( ! apply_filters( 'woocommerce_apply_user_tracking', true ) ) {
return false;
}
// Check if tracking is actively being opted into.
$is_obw_opting_in = isset( $_POST['wc_tracker_checkbox'] ) && 'yes' === sanitize_text_field( $_POST['wc_tracker_checkbox'] ); // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification, WordPress.Security.ValidatedSanitizedInput
if ( 'yes' !== get_option( 'woocommerce_allow_tracking' ) && ! $is_obw_opting_in ) {
2019-02-01 01:26:38 +00:00
return false;
}
if ( ! class_exists( 'WC_Tracks' ) ) {
return false;
}
return true;
}
2019-02-01 01:26:38 +00:00
/**
* Add scripts required to record events from javascript.
*/
public static function enqueue_scripts() {
// Add w.js to the page.
wp_enqueue_script( 'woo-tracks', 'https://stats.wp.com/w.js', array(), gmdate( 'YW' ), true );
// Expose tracking via a function in the wcTracks global namespace.
wc_enqueue_js(
"
window.wcTracks = window.wcTracks || {};
window.wcTracks.recordEvent = function( name, properties ) {
var eventName = '" . WC_Tracks::PREFIX . "' + name;
var eventProperties = properties || {};
eventProperties.url = '" . home_url() . "'
2019-02-21 18:46:58 +00:00
eventProperties.products_count = '" . WC_Tracks::get_products_count() . "'
eventProperties.orders_gross = '" . WC_Tracks::get_total_revenue() . "'
2019-02-01 01:26:38 +00:00
window._tkq = window._tkq || [];
window._tkq.push( [ 'recordEvent', eventName, eventProperties ] );
}
"
);
}
2019-02-01 01:26:38 +00:00
/**
* Init tracking.
*/
public static function init() {
2019-02-01 01:26:38 +00:00
2019-02-01 01:26:38 +00:00
if ( ! self::is_tracking_enabled() ) {
2019-02-01 01:26:38 +00:00
// Define window.wcTracks.recordEvent in case there is an attempt to use it when tracking is turned off.
wc_enqueue_js(
'
window.wcTracks = window.wcTracks || {};
window.wcTracks.recordEvent = function() {};
'
);
2019-02-01 01:26:38 +00:00
return;
}
2019-02-01 01:26:38 +00:00
self::enqueue_scripts();
include_once WC_ABSPATH . 'includes/tracks/events/class-wc-admin-setup-wizard-tracking.php';
include_once WC_ABSPATH . 'includes/tracks/events/class-wc-extensions-tracking.php';
include_once WC_ABSPATH . 'includes/tracks/events/class-wc-importer-tracking.php';
include_once WC_ABSPATH . 'includes/tracks/events/class-wc-products-tracking.php';
include_once WC_ABSPATH . 'includes/tracks/events/class-wc-orders-tracking.php';
$tracking_classes = array(
'WC_Admin_Setup_Wizard_Tracking',
'WC_Extensions_Tracking',
'WC_Importer_Tracking',
'WC_Products_Tracking',
'WC_Orders_Tracking',
);
foreach ( $tracking_classes as $tracking_class ) {
$init_method = array( $tracking_class, 'init' );
if ( is_callable( $init_method ) ) {
call_user_func( $init_method );
}
}
2019-02-01 01:26:38 +00:00
}
}