/**
 * Google eCommerce Analytics integration for WooCommerce.
 *
 * @param int $order_id current order id.
 *
 * @return bool
 */
function dorzki_google_analytics_ecommerce( $order_id ) {

	// Exit if already submitted the data to google.
	if ( get_post_meta( $order_id, '_ga_ec_tracked', true ) ) {
		return false;
	}

	// Get current order by id.
	$order = wc_get_order( $order_id );

	// If order has failed for some reason, don't send the data.
	if ( $order->has_status( 'failed' ) ) {
		return false;
	}

	// Build transaction object.
	$ga_code = "<script>" . PHP_EOL;

	$ga_code .= " ga( 'ecommerce:addTransaction', {
    'id': '{$order_id}',
    'affiliation': '" . esc_js( get_bloginfo( 'name' ) ) . "',
    'revenue': '" . esc_js( $order->get_total() ) . "',
    'shipping': '" . esc_js( $order->get_shipping_total() ) . "',
    'tax': '" . esc_js( $order->get_total_tax() ) . "',
    'currency': '" . esc_js( $order->get_currency() ) . "'
  } );" . PHP_EOL . PHP_EOL;

	// Build items object.
	foreach ( $order->get_items() as $item ) {

		$_product    = $item->get_product();
		$product_cat = '';

		// Get product category.
		$variation_data = $_product->is_type( 'variation' ) ? wc_get_product_variation_attributes( $_product->get_id() ) : '';
		$categories     = get_the_terms( $_product->get_id(), 'product_cat' );

		// If the current product is a variation, retrieve the parent product.
		if ( is_array( $variation_data ) && ! empty( $variation_data ) ) {

            $product_id = version_compare( WC_VERSION, '3.0', '<' ) ? $_product->parent->id : $_product->get_parent_id();

			$parent_product = wc_get_product( $product_id );

			$categories = get_the_terms( $parent_product->get_id(), 'product_cat' );

		}

		if ( ! empty( $categories ) ) {

			$product_cat = $categories[0]->name;

		}

		$ga_code .= " ga( 'ecommerce:addItem', {
	      'id': '{$order_id}',
	      'name': '" . esc_js( $item['name'] ) . "',
	      'sku': '" . esc_js( $_product->get_sku() ? $_product->get_sku() : $_product->get_id() ) . "',
	      'category': '" . esc_js( $product_cat ) . "',
	      'price': '" . esc_js( $order->get_item_total( $item ) ) . "',
	      'quantity': '" . esc_js( $item['qty'] ) . "'
	    } );" . PHP_EOL . PHP_EOL;

	}

	$ga_code .= " ga( 'ecommerce:send' );" . PHP_EOL;
	$ga_code .= "</script>";

	// Mark that the data was sent.
	update_post_meta( $order_id, '_ga_ec_tracked', true );

	echo $ga_code;

}

add_action( 'woocommerce_thankyou', 'dorzki_google_analytics_ecommerce' );

Credit: dorzki.co.il

How to use it?

Usually adding functionality to a WordPress site is by creating a plugin.
However you can simply copy the code and paste it in your child theme’s functions.php file at the end of the file, just before ?>.

For JavaScript code you will need to add them to your theme’s main JavaScript file.

Having issues with the code?

If the code doesn’t work or you get an error please let us know in the comments section.
We will do our best to fix it as-soon-as-possible and update the code on this page.

Comments

  1. Samuel

    Great!
    Thank you! 🙏

    Reply

Leave a Reply