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