1. Home
  2. Developers
  3. Google Analytics Ecommerce Tracking

Google Analytics Ecommerce Tracking

Google Analytics Ecommerce Tracking allows you to measure the number of transactions and revenue that your website generates.

You must have added regular Google Analytics code to the top of your page and you must also have enabled ‘Ecommerce’ on your Google Analytics dashboard.

function _wpi_custom_ga_ecommerce_tracking( $invoice ) {
    if ( ! wpinv_is_success_page() ) {
        return;
    }

    $quantities_enabled = wpinv_item_quantities_enabled();
    $cart_details       = $invoice->get_cart_details();

    $transaction = array(
        'id'            => $invoice->get_number(), // The transaction ID. Required.
        'affiliation'   => wpinv_get_business_name(), // The store or affiliation from which this transaction occurred.
        'revenue'       => wpinv_round_amount( $invoice->get_total() ), // Specifies the total revenue or grand total associated with the transaction.
        'tax'           => wpinv_use_taxes() ? wpinv_round_amount( $invoice->get_tax() ) : '0' // Specifies the total tax of the transaction.
    );

    $items = array();

    foreach ( $cart_details as $key => $cart_item ) {
        $item = array(
            'id'        => $invoice->get_number(), // Transaction ID. Required.
            'name'      => $cart_item['name'], // Product name. Required.
            'sku'       => $cart_item['id'], // Item ID/SKU/code.
            'price'     => isset( $cart_item['custom_price'] ) && $cart_item['custom_price'] !== '' ? wpinv_round_amount( $cart_item['custom_price'] ) : wpinv_round_amount( $cart_item['item_price'] ), // Unit price.
            'currency'  => $invoice->get_currency() // Local currency code.
        );

        if ( $quantities_enabled ) {
            $item['quantity'] = !empty( $cart_item['quantity'] ) ? absint( $cart_item['quantity'] ) : 1; // Quantity.
        }

        $items[] = $item;
    }
?>



Learn more about Google Analytics Ecommerce Tracking

Was this helpful to you? Yes 1 No