1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Jetpack_Google_Analytics_Legacy hooks and enqueues support for ga.js |
5
|
|
|
* https://developers.google.com/analytics/devguides/collection/gajs/ |
6
|
|
|
* |
7
|
|
|
* @author Aaron D. Campbell (original) |
8
|
|
|
* @author allendav |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Bail if accessed directly |
13
|
|
|
*/ |
14
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
15
|
|
|
exit; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
class Jetpack_Google_Analytics_Legacy { |
19
|
|
|
public function __construct() { |
20
|
|
|
add_filter( 'jetpack_wga_classic_custom_vars', array( $this, 'jetpack_wga_classic_anonymize_ip' ) ); |
21
|
|
|
add_filter( 'jetpack_wga_classic_custom_vars', array( $this, 'jetpack_wga_classic_track_purchases' ) ); |
22
|
|
|
add_action( 'wp_footer', array( $this, 'insert_code' ) ); |
23
|
|
|
add_action( 'wp_footer', array( $this, 'jetpack_wga_classic_track_add_to_cart' ) ); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Used to generate a tracking URL |
28
|
|
|
* Called exclusively by insert_code |
29
|
|
|
* |
30
|
|
|
* @param array $track - Must have ['data'] and ['code']. |
31
|
|
|
* @return string - Tracking URL |
32
|
|
|
*/ |
33
|
|
|
private function _get_url( $track ) { |
34
|
|
|
$site_url = ( is_ssl() ? 'https://':'http://' ) . sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ); // Input var okay. |
35
|
|
|
foreach ( $track as $k => $value ) { |
36
|
|
|
if ( strpos( strtolower( $value ), strtolower( $site_url ) ) === 0 ) { |
37
|
|
|
$track[ $k ] = substr( $track[ $k ], strlen( $site_url ) ); |
38
|
|
|
} |
39
|
|
|
if ( 'data' === $k ) { |
40
|
|
|
$track[ $k ] = preg_replace( '/^https?:\/\/|^\/+/i', '', $track[ $k ] ); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// This way we don't lose search data. |
44
|
|
|
if ( 'data' === $k && 'search' === $track['code'] ) { |
45
|
|
|
$track[ $k ] = rawurlencode( $track[ $k ] ); |
46
|
|
|
} else { |
47
|
|
|
$track[ $k ] = preg_replace( '/[^a-z0-9\.\/\+\?=-]+/i', '_', $track[ $k ] ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$track[ $k ] = trim( $track[ $k ], '_' ); |
51
|
|
|
} |
52
|
|
|
$char = ( strpos( $track['data'], '?' ) === false ) ? '?' : '&'; |
53
|
|
|
return str_replace( "'", "\'", "/{$track['code']}/{$track['data']}{$char}referer=" . rawurlencode( isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '' ) ); // Input var okay. |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* This injects the Google Analytics code into the footer of the page. |
58
|
|
|
* Called exclusively by wp_footer action |
59
|
|
|
*/ |
60
|
|
|
public function insert_code() { |
61
|
|
|
$tracking_id = Jetpack_Google_Analytics_Options::get_tracking_code(); |
62
|
|
|
if ( empty( $tracking_id ) ) { |
63
|
|
|
echo "<!-- Your Google Analytics Plugin is missing the tracking ID -->\r\n"; |
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// If we're in the admin_area, return without inserting code. |
68
|
|
|
if ( is_admin() ) { |
69
|
|
|
return; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$custom_vars = array( |
73
|
|
|
"_gaq.push(['_setAccount', '{$tracking_id}']);", |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
$track = array(); |
77
|
|
|
if ( is_404() ) { |
78
|
|
|
// This is a 404 and we are supposed to track them. |
79
|
|
|
$custom_vars[] = "_gaq.push(['_trackEvent', '404', document.location.href, document.referrer]);"; |
80
|
|
|
} elseif ( is_search() ) { |
81
|
|
|
// Set track for searches, if it's a search, and we are supposed to. |
82
|
|
|
$track['data'] = sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ); // Input var okay. |
83
|
|
|
$track['code'] = 'search'; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ( ! empty( $track ) ) { |
87
|
|
|
$track['url'] = $this->_get_url( $track ); |
88
|
|
|
// adjust the code that we output, account for both types of tracking. |
89
|
|
|
$track['url'] = esc_js( str_replace( '&', '&', $track['url'] ) ); |
90
|
|
|
$custom_vars[] = "_gaq.push(['_trackPageview','{$track['url']}']);"; |
91
|
|
|
} else { |
92
|
|
|
$custom_vars[] = "_gaq.push(['_trackPageview']);"; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Allow for additional elements to be added to the classic Google Analytics queue (_gaq) array |
97
|
|
|
* |
98
|
|
|
* @since 5.4.0 |
99
|
|
|
* |
100
|
|
|
* @param array $custom_vars Array of classic Google Analytics queue elements |
101
|
|
|
*/ |
102
|
|
|
$custom_vars = apply_filters( 'jetpack_wga_classic_custom_vars', $custom_vars ); |
103
|
|
|
|
104
|
|
|
// Ref: https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingEcommerce#Example |
105
|
|
|
$async_code = "<!-- Jetpack Google Analytics --> |
106
|
|
|
<script type='text/javascript'> |
107
|
|
|
var _gaq = _gaq || []; |
108
|
|
|
%custom_vars% |
109
|
|
|
|
110
|
|
|
(function() { |
111
|
|
|
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; |
112
|
|
|
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; |
113
|
|
|
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); |
114
|
|
|
})(); |
115
|
|
|
</script>"; |
116
|
|
|
|
117
|
|
|
$custom_vars_string = implode( "\r\n", $custom_vars ); |
118
|
|
|
$async_code = str_replace( '%custom_vars%', $custom_vars_string, $async_code ); |
119
|
|
|
|
120
|
|
|
echo "$async_code\r\n"; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Used to filter in the anonymize IP snippet to the custom vars array for classic analytics |
125
|
|
|
* Ref https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApi_gat#_gat._anonymizelp |
126
|
|
|
* @param array custom vars to be filtered |
127
|
|
|
* @return array possibly updated custom vars |
128
|
|
|
*/ |
129
|
|
|
public function jetpack_wga_classic_anonymize_ip( $custom_vars ) { |
130
|
|
|
if ( Jetpack_Google_Analytics_Options::anonymize_ip_is_enabled() ) { |
131
|
|
|
array_push( $custom_vars, "_gaq.push(['_gat._anonymizeIp']);" ); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $custom_vars; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Used to filter in the order details to the custom vars array for classic analytics |
139
|
|
|
* @param array custom vars to be filtered |
140
|
|
|
* @return array possibly updated custom vars |
141
|
|
|
*/ |
142
|
|
|
public function jetpack_wga_classic_track_purchases( $custom_vars ) { |
143
|
|
|
global $wp; |
144
|
|
|
|
145
|
|
|
if ( ! class_exists( 'WooCommerce' ) ) { |
146
|
|
|
return $custom_vars; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if ( ! Jetpack_Google_Analytics_Options::has_tracking_code() ) { |
150
|
|
|
return; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// Ref: https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingEcommerce#Example |
154
|
|
|
if ( ! Jetpack_Google_Analytics_Options::track_purchases_is_enabled() ) { |
155
|
|
|
return $custom_vars; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$minimum_woocommerce_active = class_exists( 'WooCommerce' ) && version_compare( WC_VERSION, '3.0', '>=' ); |
159
|
|
|
if ( $minimum_woocommerce_active && is_order_received_page() ) { |
160
|
|
|
$order_id = isset( $wp->query_vars['order-received'] ) ? $wp->query_vars['order-received'] : 0; |
161
|
|
|
if ( 0 < $order_id && 1 != get_post_meta( $order_id, '_ga_tracked', true ) ) { |
162
|
|
|
$order = new WC_Order( $order_id ); |
163
|
|
|
|
164
|
|
|
// [ '_add_Trans', '123', 'Site Title', '21.00', '1.00', '5.00', 'Snohomish', 'WA', 'USA' ] |
165
|
|
|
array_push( |
166
|
|
|
$custom_vars, |
167
|
|
|
sprintf( |
168
|
|
|
'_gaq.push( %s );', json_encode( |
169
|
|
|
array( |
170
|
|
|
'_addTrans', |
171
|
|
|
(string) $order->get_order_number(), |
172
|
|
|
get_bloginfo( 'name' ), |
173
|
|
|
(string) $order->get_total(), |
174
|
|
|
(string) $order->get_total_tax(), |
175
|
|
|
(string) $order->get_total_shipping(), |
176
|
|
|
(string) $order->get_billing_city(), |
177
|
|
|
(string) $order->get_billing_state(), |
178
|
|
|
(string) $order->get_billing_country() |
179
|
|
|
) |
180
|
|
|
) |
181
|
|
|
) |
182
|
|
|
); |
183
|
|
|
|
184
|
|
|
// Order items |
185
|
|
|
if ( $order->get_items() ) { |
186
|
|
|
foreach ( $order->get_items() as $item ) { |
187
|
|
|
$product = $order->get_product_from_item( $item ); |
188
|
|
|
$product_sku_or_id = $product->get_sku() ? $product->get_sku() : $product->get_id(); |
189
|
|
|
|
190
|
|
|
array_push( |
191
|
|
|
$custom_vars, |
192
|
|
|
sprintf( |
193
|
|
|
'_gaq.push( %s );', json_encode( |
194
|
|
|
array( |
195
|
|
|
'_addItem', |
196
|
|
|
(string) $order->get_order_number(), |
197
|
|
|
(string) $product_sku_or_id, |
198
|
|
|
$item['name'], |
199
|
|
|
Jetpack_Google_Analytics_Utils::get_product_categories_concatenated( $product ), |
200
|
|
|
(string) $order->get_item_total( $item ), |
201
|
|
|
(string) $item['qty'] |
202
|
|
|
) |
203
|
|
|
) |
204
|
|
|
) |
205
|
|
|
); |
206
|
|
|
} |
207
|
|
|
} // get_items |
208
|
|
|
|
209
|
|
|
// Mark the order as tracked |
210
|
|
|
update_post_meta( $order_id, '_ga_tracked', 1 ); |
211
|
|
|
array_push( $custom_vars, "_gaq.push(['_trackTrans']);" ); |
212
|
|
|
} // order not yet tracked |
213
|
|
|
} // is order received page |
214
|
|
|
|
215
|
|
|
return $custom_vars; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Used to add footer javascript to track user clicking on add-to-cart buttons |
220
|
|
|
* on single views (.single_add_to_cart_button) and list views (.add_to_cart_button) |
221
|
|
|
*/ |
222
|
|
|
public function jetpack_wga_classic_track_add_to_cart() { |
223
|
|
|
if ( ! class_exists( 'WooCommerce' ) ) { |
224
|
|
|
return; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
if ( ! Jetpack_Google_Analytics_Options::has_tracking_code() ) { |
228
|
|
|
return; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
if ( ! Jetpack_Google_Analytics_Options::track_add_to_cart_is_enabled() ) { |
232
|
|
|
return; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
if ( is_product() ) { // product page |
236
|
|
|
global $product; |
237
|
|
|
$product_sku_or_id = $product->get_sku() ? $product->get_sku() : "#" + $product->get_id(); |
238
|
|
|
wc_enqueue_js( |
239
|
|
|
"jQuery( function( $ ) { |
240
|
|
|
$( '.single_add_to_cart_button' ).click( function() { |
241
|
|
|
_gaq.push(['_trackEvent', 'Products', 'Add to Cart', '#" . esc_js( $product_sku_or_id ) . "']); |
242
|
|
|
} ); |
243
|
|
|
} );" |
244
|
|
|
); |
245
|
|
|
} else if ( is_woocommerce() ) { // any other page that uses templates (like product lists, archives, etc) |
246
|
|
|
wc_enqueue_js( |
247
|
|
|
"jQuery( function( $ ) { |
248
|
|
|
$( '.add_to_cart_button:not(.product_type_variable, .product_type_grouped)' ).click( function() { |
249
|
|
|
var label = $( this ).data( 'product_sku' ) ? $( this ).data( 'product_sku' ) : '#' + $( this ).data( 'product_id' ); |
250
|
|
|
_gaq.push(['_trackEvent', 'Products', 'Add to Cart', label]); |
251
|
|
|
} ); |
252
|
|
|
} );" |
253
|
|
|
); |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|