Нет описания

class-jetpack-google-amp-analytics.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * Add support for Google Analytics e-commerce events for AMP pages.
  4. *
  5. * @package automattic/jetpack
  6. */
  7. /**
  8. * Bail if accessed directly
  9. */
  10. if ( ! defined( 'ABSPATH' ) ) {
  11. exit;
  12. }
  13. /**
  14. * Jetpack_Google_AMP_Analytics class.
  15. */
  16. class Jetpack_Google_AMP_Analytics {
  17. /**
  18. * Constructor method.
  19. */
  20. public function __construct() {
  21. $this->maybe_load_hooks();
  22. }
  23. /**
  24. * Maybe load the hooks.
  25. * Checks if its AMP request, if WooCommerce is available, if there's tracking code and in tracking is enabled.
  26. */
  27. public function maybe_load_hooks() {
  28. if ( ! class_exists( 'Jetpack_AMP_Support' ) || ! Jetpack_AMP_Support::is_amp_request() ) {
  29. return;
  30. }
  31. if ( ! class_exists( 'WooCommerce' ) ) {
  32. return;
  33. }
  34. if ( ! Jetpack_Google_Analytics_Options::has_tracking_code() ) {
  35. return;
  36. }
  37. if ( ! Jetpack_Google_Analytics_Options::track_add_to_cart_is_enabled() ) {
  38. return;
  39. }
  40. add_action( 'woocommerce_add_to_cart', array( $this, 'amp_add_to_cart' ), 10, 6 );
  41. add_action( 'woocommerce_thankyou', array( $this, 'amp_after_purchase' ), 10, 1 );
  42. add_action( 'wp_footer', array( $this, 'amp_send_ga_events' ) );
  43. }
  44. /**
  45. * Generate a GA event when adding an item to the cart.
  46. *
  47. * @param string $cart_item_key Cart item key.
  48. * @param string $product_id Product ID.
  49. * @param int $quantity Product quantity.
  50. * @param int $variation_id Product variation ID.
  51. * @param object $variation Product variation.
  52. * @param object $cart_item_data Cart item data.
  53. */
  54. public function amp_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
  55. $product = wc_get_product( $product_id );
  56. if ( $product ) {
  57. $product_sku = Jetpack_Google_Analytics_Utils::get_product_sku_or_id( $product );
  58. $product_name = $product->get_name();
  59. $events = WC()->session->get( 'wc_ga_events' );
  60. $events[] = array(
  61. 'type' => 'add',
  62. 'ga_params' => array(
  63. 'pa' => 'add',
  64. 'pr1id' => sanitize_text_field( $product_sku ),
  65. 'pr1nm' => sanitize_text_field( $product_name ),
  66. 'pr1qt' => absint( $quantity ),
  67. ),
  68. );
  69. WC()->session->set( 'wc_ga_events', $events );
  70. }
  71. }
  72. /**
  73. * Generate a GA event when removing an item to the cart.
  74. *
  75. * @param int $order_id The Order ID.
  76. */
  77. public function amp_after_purchase( $order_id ) {
  78. $events = WC()->session->get( 'wc_ga_events' );
  79. $order = wc_get_order( $order_id );
  80. $order_total = $order->get_total();
  81. $order_tax = $order->get_total_tax();
  82. $i = 1;
  83. $event = array(
  84. 'type' => 'purchase',
  85. 'ga_params' => array(
  86. 'pa' => 'purchase',
  87. 'ti' => absint( $order_id ),
  88. 'tr' => (float) $order_total,
  89. 'tt' => (float) $order_tax,
  90. ),
  91. );
  92. foreach ( $order->get_items() as $item ) {
  93. $product = $item->get_product();
  94. if ( $product ) {
  95. $event['ga_params'][ 'pr' . $i . 'id' ] = sanitize_text_field( Jetpack_Google_Analytics_Utils::get_product_sku_or_id( $product ) );
  96. $event['ga_params'][ 'pr' . $i . 'nm' ] = sanitize_text_field( $item->get_name() );
  97. $event['ga_params'][ 'pr' . $i . 'qt' ] = absint( $item->get_quantity() );
  98. $i++;
  99. }
  100. }
  101. $events[] = $event;
  102. WC()->session->set( 'wc_ga_events', $events );
  103. }
  104. /**
  105. * Send the stored events to GA.
  106. */
  107. public function amp_send_ga_events() {
  108. if ( 'GET' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) {
  109. return;
  110. }
  111. $events = WC()->session->get( 'wc_ga_events' );
  112. if ( ! is_array( $events ) ) {
  113. return;
  114. }
  115. foreach ( $events as $event ) {
  116. ?>
  117. <amp-analytics type='googleanalytics'>
  118. <script type='application/json'>
  119. {
  120. "vars": {
  121. "account": "<?php echo esc_html( Jetpack_Google_Analytics_Options::get_tracking_code() ); ?>"
  122. },
  123. "triggers": {
  124. "trackPageview": {
  125. "on": "visible",
  126. "request": "pageview",
  127. "extraUrlParams": <?php echo wp_json_encode( $event['ga_params'] ); ?>
  128. }
  129. }
  130. }
  131. </script>
  132. </amp-analytics>
  133. <?php
  134. array_shift( $events );
  135. }
  136. WC()->session->set( 'wc_ga_events', $events );
  137. }
  138. }