Skip to content

Instantly share code, notes, and snippets.

@coccoinomane
Last active December 2, 2017 08:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coccoinomane/9bce191924692bbe7b6a1c59513ccb7a to your computer and use it in GitHub Desktop.
Save coccoinomane/9bce191924692bbe7b6a1c59513ccb7a to your computer and use it in GitHub Desktop.
<?php
/**
* Send an event to GA whenever a user is about to initiate payment.
*
* The event is fired not just after the user clicks on the "Place Order"
* button, but when WooCommerce is actually going to redirect the user
* to the Payment Gateway, using the woocommerce_checkout_order_processed
* action.
*
* Furthermore, additional information is passed with the event:
* - The order ID as the event label, in the format order_id_<ID>.
* - The order's total price as the event value.
*
* Requires the plugin 'WooCommerce Google Analytics Pro' by SkyVerge.
* With respect to the standard 'placed order' event from the plugin,
* the event sent by this function includes the order's value.
*
* Linked to the woocommerce_checkout_order_processed action.
*
* You can find the latest version of this snippet at:
* https://gist.github.com/coccoinomane/9bce191924692bbe7b6a1c59513ccb7a
*/
function omai_send_placed_order_event( $order_id ) {
/* Will be the event action in GA */
$eventName = "placed_order_custom";
/* Event value will be the price of the cart items; no shipping, no discount */
$eventValue = wc_get_order( $order_id )->get_total();
/* Event details; for more information, see the track_event() function in
class-wc-google-analytics-pro-measurement-protocol-api-request.php */
$properties = array(
'eventCategory' => "Checkout",
'eventLabel' => "order_id_" . $order_id,
'eventValue' => $eventValue,
'checkoutStep' => null,
);
// error_log( "About to send '$eventName' event" );
// error_log( "Event value = " . $eventValue );
/* Send the event to GA using the measurement protocol */
wc_google_analytics_pro()->get_integration()->custom_event( $eventName, $properties );
// error_log( "Event sent" );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment