class-jetpack-google-amp-analytics.php
3 years ago
wp-google-analytics-legacy.php
3 years ago
wp-google-analytics-options.php
4 years ago
wp-google-analytics-universal.php
3 years ago
wp-google-analytics-utils.php
3 years ago
wp-google-analytics-universal.php
471 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * Jetpack_Google_Analytics_Universal hooks and and enqueues support for analytics.js |
| 4 | * https://developers.google.com/analytics/devguides/collection/analyticsjs/ |
| 5 | * https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce |
| 6 | * |
| 7 | * @author allendav |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * Bail if accessed directly |
| 12 | */ |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Jetpack_Google_Analytics_Universal main class. |
| 19 | */ |
| 20 | class Jetpack_Google_Analytics_Universal { |
| 21 | /** |
| 22 | * Jetpack_Google_Analytics_Universal constructor. |
| 23 | */ |
| 24 | public function __construct() { |
| 25 | add_filter( 'jetpack_wga_universal_commands', array( $this, 'maybe_anonymize_ip' ) ); |
| 26 | add_filter( 'jetpack_wga_universal_commands', array( $this, 'maybe_track_purchases' ) ); |
| 27 | |
| 28 | add_action( 'wp_head', array( $this, 'wp_head' ), 999999 ); |
| 29 | |
| 30 | add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'add_to_cart' ) ); |
| 31 | add_action( 'wp_footer', array( $this, 'loop_add_to_cart' ) ); |
| 32 | add_action( 'woocommerce_after_cart', array( $this, 'remove_from_cart' ) ); |
| 33 | add_action( 'woocommerce_after_mini_cart', array( $this, 'remove_from_cart' ) ); |
| 34 | add_filter( 'woocommerce_cart_item_remove_link', array( $this, 'remove_from_cart_attributes' ), 10, 2 ); |
| 35 | add_action( 'woocommerce_after_shop_loop_item', array( $this, 'listing_impression' ) ); |
| 36 | add_action( 'woocommerce_after_shop_loop_item', array( $this, 'listing_click' ) ); |
| 37 | add_action( 'woocommerce_after_single_product', array( $this, 'product_detail' ) ); |
| 38 | add_action( 'woocommerce_after_checkout_form', array( $this, 'checkout_process' ) ); |
| 39 | |
| 40 | // we need to send a pageview command last - so we use priority 24 to add |
| 41 | // this command's JavaScript just before wc_print_js is called (pri 25) |
| 42 | add_action( 'wp_footer', array( $this, 'send_pageview_in_footer' ), 24 ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Hook for the `wp_head` action to output the analytics code. |
| 47 | */ |
| 48 | public function wp_head() { |
| 49 | $tracking_code = Jetpack_Google_Analytics_Options::get_tracking_code(); |
| 50 | if ( empty( $tracking_code ) ) { |
| 51 | echo "<!-- No tracking ID configured for Jetpack Google Analytics -->\r\n"; |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | // If we're in the admin_area or DNT is honored and enabled, return without inserting code. |
| 56 | if ( |
| 57 | is_admin() |
| 58 | || Jetpack_Google_Analytics_Utils::is_dnt_enabled() |
| 59 | ) { |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | if ( class_exists( Jetpack_AMP_Support::class ) && Jetpack_AMP_Support::is_amp_request() ) { |
| 64 | // For Reader mode — legacy. |
| 65 | add_filter( 'amp_post_template_analytics', 'Jetpack_Google_Analytics::amp_analytics_entries', 1000 ); |
| 66 | // For Standard and Transitional modes. |
| 67 | add_filter( 'amp_analytics_entries', 'Jetpack_Google_Analytics::amp_analytics_entries', 1000 ); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Allow for additional elements to be added to the universal Google Analytics queue (ga) array |
| 73 | * |
| 74 | * @since 5.6.0 |
| 75 | * |
| 76 | * @param array $custom_vars Array of universal Google Analytics queue elements |
| 77 | */ |
| 78 | $universal_commands = apply_filters( 'jetpack_wga_universal_commands', array() ); |
| 79 | |
| 80 | // phpcs:disable WordPress.WP.EnqueuedResources.NonEnqueuedScript -- Script is added to wp_head. |
| 81 | $async_code = " |
| 82 | <!-- Jetpack Google Analytics --> |
| 83 | <script> |
| 84 | window.ga = window.ga || function(){ ( ga.q = ga.q || [] ).push( arguments ) }; ga.l=+new Date; |
| 85 | ga( 'create', '%tracking_id%', 'auto' ); |
| 86 | ga( 'require', 'ec' ); |
| 87 | %universal_commands% |
| 88 | </script> |
| 89 | <script async src='https://www.google-analytics.com/analytics.js'></script> |
| 90 | <!-- End Jetpack Google Analytics --> |
| 91 | "; // phpcs:enable WordPress.WP.EnqueuedResources.NonEnqueuedScript |
| 92 | $async_code = str_replace( '%tracking_id%', $tracking_code, $async_code ); |
| 93 | |
| 94 | $universal_commands_string = implode( "\r\n", $universal_commands ); |
| 95 | $async_code = str_replace( '%universal_commands%', $universal_commands_string, $async_code ); |
| 96 | |
| 97 | echo "$async_code\r\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Check if the 'anonymize_ip' option should be added to the universal Google Analytics queue (ga) commands. |
| 102 | * |
| 103 | * @param array $command_array Array of commands. |
| 104 | * @return array `$command_array` with the additional command conditionally added. |
| 105 | */ |
| 106 | public function maybe_anonymize_ip( $command_array ) { |
| 107 | if ( Jetpack_Google_Analytics_Options::anonymize_ip_is_enabled() ) { |
| 108 | array_push( $command_array, "ga( 'set', 'anonymizeIp', true );" ); |
| 109 | } |
| 110 | |
| 111 | return $command_array; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Process purchase tracking options for the universal Google Analytics queue (ga) commands. |
| 116 | * |
| 117 | * May also update post meta to indicate the order has been tracked. |
| 118 | * |
| 119 | * @param array $command_array Array of commands. |
| 120 | * @return array `$command_array` with additional commands conditionally added. |
| 121 | */ |
| 122 | public function maybe_track_purchases( $command_array ) { |
| 123 | global $wp; |
| 124 | |
| 125 | if ( ! Jetpack_Google_Analytics_Options::track_purchases_is_enabled() ) { |
| 126 | return $command_array; |
| 127 | } |
| 128 | |
| 129 | if ( ! class_exists( 'WooCommerce' ) ) { |
| 130 | return $command_array; |
| 131 | } |
| 132 | |
| 133 | $minimum_woocommerce_active = class_exists( 'WooCommerce' ) && version_compare( WC_VERSION, '3.0', '>=' ); |
| 134 | if ( ! $minimum_woocommerce_active ) { |
| 135 | return $command_array; |
| 136 | } |
| 137 | |
| 138 | if ( ! is_order_received_page() ) { |
| 139 | return $command_array; |
| 140 | } |
| 141 | |
| 142 | $order_id = isset( $wp->query_vars['order-received'] ) ? $wp->query_vars['order-received'] : 0; |
| 143 | if ( 0 === (int) $order_id ) { |
| 144 | return $command_array; |
| 145 | } |
| 146 | |
| 147 | // A 1 indicates we've already tracked this order - don't do it again |
| 148 | if ( 1 === (int) get_post_meta( $order_id, '_ga_tracked', true ) ) { |
| 149 | return $command_array; |
| 150 | } |
| 151 | |
| 152 | $order = new WC_Order( $order_id ); |
| 153 | $order_currency = $order->get_currency(); |
| 154 | $command = "ga( 'set', '&cu', '" . esc_js( $order_currency ) . "' );"; |
| 155 | array_push( $command_array, $command ); |
| 156 | |
| 157 | // Order items |
| 158 | if ( $order->get_items() ) { |
| 159 | foreach ( $order->get_items() as $item ) { |
| 160 | $product = $order->get_product_from_item( $item ); |
| 161 | $product_sku_or_id = Jetpack_Google_Analytics_Utils::get_product_sku_or_id( $product ); |
| 162 | |
| 163 | $item_details = array( |
| 164 | 'id' => $product_sku_or_id, |
| 165 | 'name' => $item['name'], |
| 166 | 'category' => Jetpack_Google_Analytics_Utils::get_product_categories_concatenated( $product ), |
| 167 | 'price' => $order->get_item_total( $item ), |
| 168 | 'quantity' => $item['qty'], |
| 169 | ); |
| 170 | $command = "ga( 'ec:addProduct', " . wp_json_encode( $item_details ) . ' );'; |
| 171 | array_push( $command_array, $command ); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // Order summary |
| 176 | $summary = array( |
| 177 | 'id' => $order->get_order_number(), |
| 178 | 'affiliation' => get_bloginfo( 'name' ), |
| 179 | 'revenue' => $order->get_total(), |
| 180 | 'tax' => $order->get_total_tax(), |
| 181 | 'shipping' => $order->get_total_shipping(), |
| 182 | ); |
| 183 | $command = "ga( 'ec:setAction', 'purchase', " . wp_json_encode( $summary ) . ' );'; |
| 184 | array_push( $command_array, $command ); |
| 185 | |
| 186 | update_post_meta( $order_id, '_ga_tracked', 1 ); |
| 187 | |
| 188 | return $command_array; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Enqueue add-to-cart click tracking script, if enabled. |
| 193 | */ |
| 194 | public function add_to_cart() { |
| 195 | if ( ! Jetpack_Google_Analytics_Options::track_add_to_cart_is_enabled() ) { |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | if ( ! is_single() ) { |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | global $product; |
| 204 | |
| 205 | $product_sku_or_id = Jetpack_Google_Analytics_Utils::get_product_sku_or_id( $product ); |
| 206 | $selector = '.single_add_to_cart_button'; |
| 207 | |
| 208 | wc_enqueue_js( |
| 209 | "$( '" . esc_js( $selector ) . "' ).click( function() { |
| 210 | var productDetails = { |
| 211 | 'id': '" . esc_js( $product_sku_or_id ) . "', |
| 212 | 'name' : '" . esc_js( $product->get_title() ) . "', |
| 213 | 'quantity': $( 'input.qty' ).val() ? $( 'input.qty' ).val() : '1', |
| 214 | }; |
| 215 | ga( 'ec:addProduct', productDetails ); |
| 216 | ga( 'ec:setAction', 'add' ); |
| 217 | ga( 'send', 'event', 'UX', 'click', 'add to cart' ); |
| 218 | } );" |
| 219 | ); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Enqueue add-to-cart click tracking script for looped product views, if enabled. |
| 224 | */ |
| 225 | public function loop_add_to_cart() { |
| 226 | if ( ! Jetpack_Google_Analytics_Options::track_add_to_cart_is_enabled() ) { |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | if ( ! class_exists( 'WooCommerce' ) ) { |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | $minimum_woocommerce_active = class_exists( 'WooCommerce' ) && version_compare( WC_VERSION, '3.0', '>=' ); |
| 235 | if ( ! $minimum_woocommerce_active ) { |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | $selector = '.add_to_cart_button:not(.product_type_variable, .product_type_grouped)'; |
| 240 | |
| 241 | wc_enqueue_js( |
| 242 | "$( '" . esc_js( $selector ) . "' ).click( function() { |
| 243 | var productSku = $( this ).data( 'product_sku' ); |
| 244 | var productID = $( this ).data( 'product_id' ); |
| 245 | var productDetails = { |
| 246 | 'id': productSku ? productSku : '#' + productID, |
| 247 | 'quantity': $( this ).data( 'quantity' ), |
| 248 | }; |
| 249 | ga( 'ec:addProduct', productDetails ); |
| 250 | ga( 'ec:setAction', 'add' ); |
| 251 | ga( 'send', 'event', 'UX', 'click', 'add to cart' ); |
| 252 | } );" |
| 253 | ); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Enqueue remove-from-cart click tracking script, if enabled. |
| 258 | */ |
| 259 | public function remove_from_cart() { |
| 260 | if ( ! Jetpack_Google_Analytics_Options::enhanced_ecommerce_tracking_is_enabled() ) { |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | if ( ! Jetpack_Google_Analytics_Options::track_remove_from_cart_is_enabled() ) { |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | // We listen at div.woocommerce because the cart 'form' contents get forcibly |
| 269 | // updated and subsequent removals from cart would then not have this click |
| 270 | // handler attached |
| 271 | wc_enqueue_js( |
| 272 | "$( 'div.woocommerce' ).on( 'click', 'a.remove', function() { |
| 273 | var productSku = $( this ).data( 'product_sku' ); |
| 274 | var productID = $( this ).data( 'product_id' ); |
| 275 | var quantity = $( this ).parent().parent().find( '.qty' ).val() |
| 276 | var productDetails = { |
| 277 | 'id': productSku ? productSku : '#' + productID, |
| 278 | 'quantity': quantity ? quantity : '1', |
| 279 | }; |
| 280 | ga( 'ec:addProduct', productDetails ); |
| 281 | ga( 'ec:setAction', 'remove' ); |
| 282 | ga( 'send', 'event', 'UX', 'click', 'remove from cart' ); |
| 283 | } );" |
| 284 | ); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Adds the product ID and SKU to the remove product link (for use by remove_from_cart above) if not present |
| 289 | * |
| 290 | * @param string $url Full HTML a tag of the link to remove an item from the cart. |
| 291 | * @param string $key Unique Key ID for a cart item. |
| 292 | */ |
| 293 | public function remove_from_cart_attributes( $url, $key ) { |
| 294 | if ( false !== strpos( $url, 'data-product_id' ) ) { |
| 295 | return $url; |
| 296 | } |
| 297 | |
| 298 | $item = WC()->cart->get_cart_item( $key ); |
| 299 | $product = $item['data']; |
| 300 | |
| 301 | $new_attributes = sprintf( |
| 302 | '" data-product_id="%1$s" data-product_sku="%2$s">', |
| 303 | esc_attr( $product->get_id() ), |
| 304 | esc_attr( $product->get_sku() ) |
| 305 | ); |
| 306 | |
| 307 | $url = str_replace( '">', $new_attributes, $url ); |
| 308 | return $url; |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Enqueue listing impression tracking script, if enabled. |
| 313 | */ |
| 314 | public function listing_impression() { |
| 315 | if ( ! Jetpack_Google_Analytics_Options::enhanced_ecommerce_tracking_is_enabled() ) { |
| 316 | return; |
| 317 | } |
| 318 | |
| 319 | if ( ! Jetpack_Google_Analytics_Options::track_product_impressions_is_enabled() ) { |
| 320 | return; |
| 321 | } |
| 322 | |
| 323 | if ( isset( $_GET['s'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- No site actions, just GA options being set. |
| 324 | $list = 'Search Results'; |
| 325 | } else { |
| 326 | $list = 'Product List'; |
| 327 | } |
| 328 | |
| 329 | global $product, $woocommerce_loop; |
| 330 | $product_sku_or_id = Jetpack_Google_Analytics_Utils::get_product_sku_or_id( $product ); |
| 331 | |
| 332 | $item_details = array( |
| 333 | 'id' => $product_sku_or_id, |
| 334 | 'name' => $product->get_title(), |
| 335 | 'category' => Jetpack_Google_Analytics_Utils::get_product_categories_concatenated( $product ), |
| 336 | 'list' => $list, |
| 337 | 'position' => $woocommerce_loop['loop'], |
| 338 | ); |
| 339 | wc_enqueue_js( "ga( 'ec:addImpression', " . wp_json_encode( $item_details ) . ' );' ); |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Enqueue listing click tracking script, if enabled. |
| 344 | */ |
| 345 | public function listing_click() { |
| 346 | if ( ! Jetpack_Google_Analytics_Options::enhanced_ecommerce_tracking_is_enabled() ) { |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | if ( ! Jetpack_Google_Analytics_Options::track_product_clicks_is_enabled() ) { |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | if ( isset( $_GET['s'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- No site actions, just GA options being set. |
| 355 | $list = 'Search Results'; |
| 356 | } else { |
| 357 | $list = 'Product List'; |
| 358 | } |
| 359 | |
| 360 | global $product, $woocommerce_loop; |
| 361 | $product_sku_or_id = Jetpack_Google_Analytics_Utils::get_product_sku_or_id( $product ); |
| 362 | |
| 363 | $selector = '.products .post-' . esc_js( $product->get_id() ) . ' a'; |
| 364 | |
| 365 | $item_details = array( |
| 366 | 'id' => $product_sku_or_id, |
| 367 | 'name' => $product->get_title(), |
| 368 | 'category' => Jetpack_Google_Analytics_Utils::get_product_categories_concatenated( $product ), |
| 369 | 'position' => $woocommerce_loop['loop'], |
| 370 | ); |
| 371 | |
| 372 | wc_enqueue_js( |
| 373 | "$( '" . esc_js( $selector ) . "' ).click( function() { |
| 374 | if ( true === $( this ).hasClass( 'add_to_cart_button' ) ) { |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | ga( 'ec:addProduct', " . wp_json_encode( $item_details ) . " ); |
| 379 | ga( 'ec:setAction', 'click', { list: '" . esc_js( $list ) . "' } ); |
| 380 | ga( 'send', 'event', 'UX', 'click', { list: '" . esc_js( $list ) . "' } ); |
| 381 | } );" |
| 382 | ); |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Enqueue product detail view tracking script, if enabled. |
| 387 | */ |
| 388 | public function product_detail() { |
| 389 | if ( ! Jetpack_Google_Analytics_Options::enhanced_ecommerce_tracking_is_enabled() ) { |
| 390 | return; |
| 391 | } |
| 392 | |
| 393 | if ( ! Jetpack_Google_Analytics_Options::track_product_detail_view_is_enabled() ) { |
| 394 | return; |
| 395 | } |
| 396 | |
| 397 | global $product; |
| 398 | $product_sku_or_id = Jetpack_Google_Analytics_Utils::get_product_sku_or_id( $product ); |
| 399 | |
| 400 | $item_details = array( |
| 401 | 'id' => $product_sku_or_id, |
| 402 | 'name' => $product->get_title(), |
| 403 | 'category' => Jetpack_Google_Analytics_Utils::get_product_categories_concatenated( $product ), |
| 404 | 'price' => $product->get_price(), |
| 405 | ); |
| 406 | wc_enqueue_js( |
| 407 | "ga( 'ec:addProduct', " . wp_json_encode( $item_details ) . ' );' . |
| 408 | "ga( 'ec:setAction', 'detail' );" |
| 409 | ); |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Enqueue post-checkout tracking script, if enabled. |
| 414 | */ |
| 415 | public function checkout_process() { |
| 416 | if ( ! Jetpack_Google_Analytics_Options::enhanced_ecommerce_tracking_is_enabled() ) { |
| 417 | return; |
| 418 | } |
| 419 | |
| 420 | if ( ! Jetpack_Google_Analytics_Options::track_checkout_started_is_enabled() ) { |
| 421 | return; |
| 422 | } |
| 423 | |
| 424 | $universal_commands = array(); |
| 425 | $cart = WC()->cart->get_cart(); |
| 426 | |
| 427 | foreach ( $cart as $cart_item_key => $cart_item ) { |
| 428 | /** |
| 429 | * This filter is already documented in woocommerce/templates/cart/cart.php |
| 430 | */ |
| 431 | $product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key ); |
| 432 | $product_sku_or_id = Jetpack_Google_Analytics_Utils::get_product_sku_or_id( $product ); |
| 433 | |
| 434 | $item_details = array( |
| 435 | 'id' => $product_sku_or_id, |
| 436 | 'name' => $product->get_title(), |
| 437 | 'category' => Jetpack_Google_Analytics_Utils::get_product_categories_concatenated( $product ), |
| 438 | 'price' => $product->get_price(), |
| 439 | 'quantity' => $cart_item['quantity'], |
| 440 | ); |
| 441 | |
| 442 | array_push( $universal_commands, "ga( 'ec:addProduct', " . wp_json_encode( $item_details ) . ' );' ); |
| 443 | } |
| 444 | |
| 445 | array_push( $universal_commands, "ga( 'ec:setAction','checkout' );" ); |
| 446 | |
| 447 | wc_enqueue_js( implode( "\r\n", $universal_commands ) ); |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * Enqueue pageview event in footer of all pages. |
| 452 | * |
| 453 | * Action hook added with later priority to come after all of the above tracking. |
| 454 | */ |
| 455 | public function send_pageview_in_footer() { |
| 456 | if ( ! Jetpack_Google_Analytics_Options::has_tracking_code() ) { |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | if ( is_admin() ) { |
| 461 | return; |
| 462 | } |
| 463 | |
| 464 | if ( ! class_exists( 'WooCommerce' ) ) { |
| 465 | return; |
| 466 | } |
| 467 | |
| 468 | wc_enqueue_js( "ga( 'send', 'pageview' );" ); |
| 469 | } |
| 470 | } |
| 471 |