1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
4
|
|
|
exit; |
5
|
|
|
} |
6
|
|
|
|
7
|
|
|
class WC_Stats { |
8
|
|
|
/** |
9
|
|
|
* @var Jetpack |
10
|
|
|
**/ |
11
|
|
|
private $jetpack; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var WC_Stats |
15
|
|
|
**/ |
16
|
|
|
private static $instance = null; |
17
|
|
|
|
18
|
|
|
static function init() { |
19
|
|
|
if ( ! WC_Stats::isActiveStore() ) { |
20
|
|
|
return; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
if ( is_null( self::$instance ) ) { |
24
|
|
|
self::$instance = new WC_Stats(); |
25
|
|
|
} |
26
|
|
|
return self::$instance; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function isActiveStore() { |
30
|
|
|
// Tracking only Site pages |
31
|
|
|
if ( is_admin() ) { |
32
|
|
|
return false; |
33
|
|
|
} |
34
|
|
|
// Make sure Jetpack is installed and active |
35
|
|
|
if ( ! Jetpack::is_active() ) { |
36
|
|
|
return false; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Make sure WooCommerce is installed and active |
41
|
|
|
* |
42
|
|
|
* This action is documented in https://docs.woocommerce.com/document/create-a-plugin |
43
|
|
|
*/ |
44
|
|
|
if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { |
45
|
|
|
return false; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return true; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function __construct() { |
52
|
|
|
$this->jetpack = Jetpack::init(); |
53
|
|
|
// add store analytics for product views and add-to-cart events |
54
|
|
|
add_action( 'wp_enqueue_scripts', array( $this, 'product_and_add_to_cart_events' ), 1 ); |
55
|
|
|
|
56
|
|
|
// add store analytics for purchase events |
57
|
|
|
add_action( 'woocommerce_payment_complete', array( $this, 'handle_purchase_event' ), 10, 1 ); |
58
|
|
|
|
59
|
|
|
// add s.js at the end |
60
|
|
|
add_action( 'wp_enqueue_scripts', array( $this, 'register_s_script' ), 10 ); |
61
|
|
|
|
62
|
|
|
add_action( 'woocommerce_add_to_cart', array( $this, 'capture_add_to_cart_from_product_page' ), 10, 6 ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function get_cart_ids( $result, $item ) { |
66
|
|
|
$comma = strlen( $result ) > 0 ? ',' : ''; |
67
|
|
|
return $result . $comma . $item['product_id']; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function get_cart_quantities( $result, $item ) { |
71
|
|
|
$comma = strlen( $result ) > 0 ? ',' : ''; |
72
|
|
|
return $result . $comma . $item['quantity']; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function get_session_id() { |
76
|
|
|
// NOTE: one session can have multiple id's, this method is not sufficient |
77
|
|
|
$session_handler = new WC_Session_Handler(); |
78
|
|
|
$session = $session_handler->get_session_cookie(); |
79
|
|
|
return $session ? $session[ 0 ] : null; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function register_s_script() { |
83
|
|
|
if ( ! is_admin() ) { |
84
|
|
|
wp_register_script( 'wc_tk_s', 'https://stats.wp.com/s.js', null, null, true ); |
85
|
|
|
wp_enqueue_script( 'wc_tk_s' ); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function product_and_add_to_cart_events() { |
90
|
|
|
$blogid = Jetpack::get_option( 'id' ); |
91
|
|
|
$post_id = get_post()->ID; |
92
|
|
|
|
93
|
|
|
if ( is_product() ) { |
94
|
|
|
wc_enqueue_js( " |
95
|
|
|
window._sta = window._sta || []; |
96
|
|
|
_sta.push( { |
97
|
|
|
_en: 'woocommerce_analytics_product_view', |
98
|
|
|
blog_id: " . $blogid . ", |
99
|
|
|
product_id: " . $post_id . ", |
100
|
|
|
} ); |
101
|
|
|
" ); |
102
|
|
|
} else { |
103
|
|
|
// How to get cart information here? Switch to using cart hash? |
104
|
|
|
wc_enqueue_js( " |
105
|
|
|
window._sta = window._sta || []; |
106
|
|
|
jQuery('body').on('added_to_cart',function(){ |
107
|
|
|
_sta.push( { |
108
|
|
|
_en: 'woocommerce_analytics_product_view', |
109
|
|
|
blog_id: " . $blogid . ", |
110
|
|
|
product_id: " . $post_id . ", |
111
|
|
|
} ); |
112
|
|
|
_sta.push( { |
113
|
|
|
_en: 'woocommerce_analytics_add_to_cart', |
114
|
|
|
blog_id: " . $blogid . ", |
115
|
|
|
cart_id: 'get_cart_id_from_somwhere??', |
116
|
|
|
} ); |
117
|
|
|
}); |
118
|
|
|
" ); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// this is when added from product page post request |
123
|
|
View Code Duplication |
public function capture_add_to_cart_from_product_page() { |
124
|
|
|
$cart = WC()->cart->get_cart(); |
125
|
|
|
wc_enqueue_js( " |
126
|
|
|
jQuery( function( $ ) { |
127
|
|
|
_sta.push( { |
128
|
|
|
_en: 'woocommerce_analytics_add_to_cart', |
129
|
|
|
blog_id: " . $blogid . ", |
|
|
|
|
130
|
|
|
cart_id: " . $this->get_session_id() . ", |
131
|
|
|
cart_products: " . array_reduce( $cart, array( $this, 'get_cart_ids' ), '' ) . ", |
132
|
|
|
cart_quantities: " . array_reduce( $cart, array( $this, 'get_cart_quantities' ), '' ) . " |
133
|
|
|
} ); |
134
|
|
|
} ); |
135
|
|
|
" ); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
View Code Duplication |
public function handle_purchase_event() { |
139
|
|
|
$cart = WC()->cart->get_cart(); |
140
|
|
|
// this one not working yet |
141
|
|
|
wc_enqueue_js( " |
142
|
|
|
window._sta = window._sta || []; |
143
|
|
|
_sta.push( { |
144
|
|
|
_en: 'woocommerce_analytics_purchase', |
145
|
|
|
blog_id: " . Jetpack::get_option( 'id' ) . ", |
146
|
|
|
cart_id: '" . $this->get_session_id() . "', |
147
|
|
|
cart_products: " . array_reduce( $cart, array( $this, 'get_cart_ids' ), '' ) . ", |
148
|
|
|
cart_quantities: " . array_reduce( $cart, array( $this, 'get_cart_quantities' ), '' ) . " |
149
|
|
|
} ); |
150
|
|
|
" ); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
WC_Stats::init(); |
155
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.