|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Jetpack_Google_Analytics_Universal hooks and and enqueues support for analytics.js |
|
5
|
|
|
* https://developers.google.com/analytics/devguides/collection/analyticsjs/ |
|
6
|
|
|
* |
|
7
|
|
|
* @author allendav |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Bail if accessed directly |
|
12
|
|
|
*/ |
|
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
14
|
|
|
exit; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
class Jetpack_Google_Analytics_Universal { |
|
18
|
|
View Code Duplication |
public function __construct() { |
|
19
|
|
|
add_filter( 'jetpack_wga_universal_commands', array( $this, 'maybe_anonymize_ip' ) ); |
|
20
|
|
|
add_filter( 'jetpack_wga_universal_commands', array( $this, 'maybe_track_purchases' ) ); |
|
21
|
|
|
|
|
22
|
|
|
add_action( 'wp_head', array( $this, 'wp_head' ), 999999 ); |
|
23
|
|
|
|
|
24
|
|
|
//TODO add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'add_to_cart' ) ); |
|
|
|
|
|
|
25
|
|
|
//TODO add_action( 'wp_footer', array( $this, 'loop_add_to_cart' ) ); |
|
|
|
|
|
|
26
|
|
|
//TODO add_action( 'woocommerce_after_cart', array( $this, 'remove_from_cart' ) ); |
|
|
|
|
|
|
27
|
|
|
//TODO add_action( 'woocommerce_after_mini_cart', array( $this, 'remove_from_cart' ) ); |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
//TODO add_filter( 'woocommerce_cart_item_remove_link', array( $this, 'remove_from_cart_attributes' ), 10, 2 ); |
|
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
//TODO add_action( 'woocommerce_after_shop_loop_item', array( $this, 'listing_impression' ) ); |
|
|
|
|
|
|
32
|
|
|
//TODO add_action( 'woocommerce_after_shop_loop_item', array( $this, 'listing_click' ) ); |
|
|
|
|
|
|
33
|
|
|
//TODO add_action( 'woocommerce_after_single_product', array( $this, 'product_detail' ) ); |
|
|
|
|
|
|
34
|
|
|
//TODO add_action( 'woocommerce_after_checkout_form', array( $this, 'checkout_process' ) ); |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
add_action( 'wp_footer', array( $this, 'wp_footer' ) ); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function wp_head() { |
|
40
|
|
|
$tracking_code = Jetpack_Google_Analytics_Options::get_tracking_code(); |
|
41
|
|
|
if ( empty( $tracking_code ) ) { |
|
42
|
|
|
echo "<!-- No tracking ID configured for Jetpack Google Analytics -->\r\n"; |
|
43
|
|
|
return; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// If we're in the admin_area, return without inserting code. |
|
47
|
|
|
if ( is_admin() ) { |
|
48
|
|
|
return; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Allow for additional elements to be added to the universal Google Analytics queue (ga) array |
|
53
|
|
|
* |
|
54
|
|
|
* @since 5.6.0 |
|
55
|
|
|
* |
|
56
|
|
|
* @param array $custom_vars Array of universal Google Analytics queue elements |
|
57
|
|
|
*/ |
|
58
|
|
|
$universal_commands = apply_filters( 'jetpack_wga_universal_commands', array() ); |
|
59
|
|
|
|
|
60
|
|
|
$async_code = " |
|
61
|
|
|
<!-- Jetpack Google Analytics --> |
|
62
|
|
|
<script> |
|
63
|
|
|
window.ga = window.ga || function(){ ( ga.q = ga.q || [] ).push( arguments ) }; ga.l=+new Date; |
|
64
|
|
|
ga( 'create', '%tracking_id%', 'auto' ); |
|
65
|
|
|
%universal_commands% |
|
66
|
|
|
</script> |
|
67
|
|
|
<script async src='https://www.google-analytics.com/analytics.js'></script> |
|
68
|
|
|
<!-- End Jetpack Google Analytics --> |
|
69
|
|
|
"; |
|
70
|
|
|
$async_code = str_replace( '%tracking_id%', $tracking_code, $async_code ); |
|
71
|
|
|
|
|
72
|
|
|
$universal_commands_string = implode( "\r\n", $universal_commands ); |
|
73
|
|
|
$async_code = str_replace( '%universal_commands%', $universal_commands_string, $async_code ); |
|
74
|
|
|
|
|
75
|
|
|
echo "$async_code\r\n"; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function maybe_anonymize_ip( $command_array ) { |
|
79
|
|
|
if ( Jetpack_Google_Analytics_Options::anonymize_ip_is_enabled() ) { |
|
80
|
|
|
array_push( $command_array, "ga( 'set', 'anonymizeIp', true );" ); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $command_array; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function maybe_track_purchases( $command_array ) { |
|
87
|
|
|
global $wp; |
|
88
|
|
|
|
|
89
|
|
|
if ( ! Jetpack_Google_Analytics_Options::track_purchases_is_enabled() ) { |
|
90
|
|
|
return $command_array; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if ( ! class_exists( 'WooCommerce' ) ) { |
|
94
|
|
|
return $command_array; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$minimum_woocommerce_active = class_exists( 'WooCommerce' ) && version_compare( WC_VERSION, '3.0', '>=' ); |
|
98
|
|
|
if ( ! $minimum_woocommerce_active ) { |
|
99
|
|
|
return $command_array; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if ( ! is_order_received_page() ) { |
|
103
|
|
|
return $command_array; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$order_id = isset( $wp->query_vars['order-received'] ) ? $wp->query_vars['order-received'] : 0; |
|
107
|
|
|
if ( 0 == $order_id ) { |
|
108
|
|
|
return $command_array; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
// A 1 indicates we've already tracked this order - don't do it again |
|
112
|
|
|
if ( 1 == get_post_meta( $order_id, '_ga_tracked', true ) ) { |
|
113
|
|
|
return $command_array; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$order = new WC_Order( $order_id ); |
|
117
|
|
|
$order_currency = $order->get_currency(); |
|
118
|
|
|
$command = "ga( 'set', '&cu', '" . esc_js( $order_currency ) . "' );"; |
|
119
|
|
|
array_push( $command_array, $command ); |
|
120
|
|
|
|
|
121
|
|
|
// Order items |
|
122
|
|
|
if ( $order->get_items() ) { |
|
123
|
|
|
foreach ( $order->get_items() as $item ) { |
|
124
|
|
|
$product = $order->get_product_from_item( $item ); |
|
125
|
|
|
$sku_or_id = $product->get_sku() ? $product->get_sku() : $product->get_id(); |
|
126
|
|
|
|
|
127
|
|
|
$item_details = array( |
|
128
|
|
|
'id' => $sku_or_id, |
|
129
|
|
|
'name' => $item['name'], |
|
130
|
|
|
'category' => Jetpack_Google_Analytics_Utils::get_product_categories_concatenated( $product ), |
|
131
|
|
|
'price' => $order->get_item_total( $item ), |
|
132
|
|
|
'quantity' => $item['qty'], |
|
133
|
|
|
); |
|
134
|
|
|
$command = "ga( 'ec:addProduct', " . wp_json_encode( $item_details ) . " );"; |
|
135
|
|
|
array_push( $command_array, $command ); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
// Order summary |
|
140
|
|
|
$summary = array( |
|
141
|
|
|
'id' => $order->get_order_number(), |
|
142
|
|
|
'affiliation' => get_bloginfo( 'name' ), |
|
143
|
|
|
'revenue' => $order->get_total(), |
|
144
|
|
|
'tax' => $order->get_total_tax(), |
|
145
|
|
|
'shipping' => $order->get_total_shipping() |
|
146
|
|
|
); |
|
147
|
|
|
$command = "ga( 'ec:setAction', 'purchase', " . wp_json_encode( $summary ) . " );"; |
|
148
|
|
|
array_push( $command_array, $command ); |
|
149
|
|
|
|
|
150
|
|
|
update_post_meta( $order_id, '_ga_tracked', 1 ); |
|
151
|
|
|
|
|
152
|
|
|
return $command_array; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function wp_footer() { |
|
156
|
|
|
if ( ! Jetpack_Google_Analytics_Options::has_tracking_code() ) { |
|
157
|
|
|
return; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
if ( is_admin() ) { |
|
161
|
|
|
return; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
$async_code = " |
|
165
|
|
|
<!-- Jetpack Google Analytics --> |
|
166
|
|
|
<script> |
|
167
|
|
|
ga( 'send', 'pageview' ); |
|
168
|
|
|
</script> |
|
169
|
|
|
<!-- End Jetpack Google Analytics --> |
|
170
|
|
|
"; |
|
171
|
|
|
|
|
172
|
|
|
echo "$async_code\r\n"; |
|
173
|
|
|
} |
|
174
|
|
|
} |