| 1 |
<?php |
| 2 |
/** |
| 3 |
* WooCommerce Event Tracking for VWO |
| 4 |
*/ |
| 5 |
|
| 6 |
if (!defined('ABSPATH')) { |
| 7 |
exit; // Exit if accessed directly |
| 8 |
} |
| 9 |
|
| 10 |
include_once plugin_dir_path(__FILE__) . 'VwoPlugin.php'; |
| 11 |
// Check if WooCommerce is active |
| 12 |
if (!in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) { |
| 13 |
return; |
| 14 |
} |
| 15 |
function vwo_plugin_activate() { |
| 16 |
vwo_track_woocommerce_events(); // Ensure hooks are registered |
| 17 |
} |
| 18 |
register_activation_hook(__FILE__, 'vwo_plugin_activate'); |
| 19 |
register_deactivation_hook(__FILE__, 'vwo_plugin_deactivate'); |
| 20 |
// Hook into WooCommerce events if enabled in VWO settings |
| 21 |
function vwo_track_woocommerce_events() { |
| 22 |
|
| 23 |
add_action('woocommerce_after_single_product', 'vwo_track_product_view'); |
| 24 |
add_action('woocommerce_after_single_product', 'server_vwo_track_product_view'); |
| 25 |
|
| 26 |
add_action('woocommerce_after_add_to_cart_button', 'vwo_track_add_to_cart', 10, 6); |
| 27 |
add_action('woocommerce_add_to_cart', 'server_vwo_track_add_to_cart', 10, 6); |
| 28 |
|
| 29 |
add_action('woocommerce_cart_item_removed', 'server_vwo_track_remove_from_cart', 10, 2); |
| 30 |
add_action('woocommerce_after_cart', 'vwo_track_remove_from_cart', 10, 2); |
| 31 |
|
| 32 |
add_action('woocommerce_thankyou', 'vwo_track_purchase'); |
| 33 |
add_action('woocommerce_thankyou', 'server_vwo_track_purchase', 10, 1); |
| 34 |
} |
| 35 |
|
| 36 |
function vwo_plugin_deactivate() { |
| 37 |
// Remove WooCommerce event tracking hooks |
| 38 |
remove_action('woocommerce_after_single_product', 'vwo_track_product_view'); |
| 39 |
remove_action('woocommerce_after_single_product', 'server_vwo_track_product_view'); |
| 40 |
|
| 41 |
remove_action('woocommerce_after_add_to_cart_button', 'vwo_track_add_to_cart', 10, 6); |
| 42 |
remove_action('woocommerce_add_to_cart', 'server_vwo_track_add_to_cart', 10, 6); |
| 43 |
|
| 44 |
remove_action('woocommerce_cart_item_removed', 'server_vwo_track_remove_from_cart', 10, 2); |
| 45 |
remove_action('woocommerce_after_cart', 'vwo_track_remove_from_cart', 10, 2); |
| 46 |
|
| 47 |
remove_action('woocommerce_before_checkout_form', 'vwo_track_checkout', 10, 6); |
| 48 |
remove_action('woocommerce_checkout_before_customer_details', 'server_vwo_track_checkout', 10, 3); |
| 49 |
|
| 50 |
remove_action('woocommerce_thankyou', 'vwo_track_purchase'); |
| 51 |
remove_action('woocommerce_thankyou', 'server_vwo_track_purchase', 10, 1); |
| 52 |
} |
| 53 |
|
| 54 |
add_action('init', 'vwo_track_woocommerce_events'); |
| 55 |
add_action('wp_footer', function () { |
| 56 |
$server_side_enabled = get_option('vwo_server_side_tracking', false); |
| 57 |
$enable_woocommerce_event_tracking= get_option('enable_woocommerce_event_tracking', false); |
| 58 |
if ($server_side_enabled || !$enable_woocommerce_event_tracking) |
| 59 |
return true; |
| 60 |
$track_add_to_cart = get_option('track_add_to_cart', false); |
| 61 |
if($track_add_to_cart ){ |
| 62 |
wc_enqueue_js(VwoPlugin::render( 'loop' )); |
| 63 |
} |
| 64 |
$track_remove_from_cart = get_option('track_remove_from_cart', false); |
| 65 |
|
| 66 |
if($track_remove_from_cart){ |
| 67 |
wc_enqueue_js(VwoPlugin::render( 'remove-mini' )); |
| 68 |
} |
| 69 |
$track_purchase = get_option('track_purchase', false); |
| 70 |
if ( $track_purchase && is_order_received_page() and $order = get_order() ) { |
| 71 |
wc_enqueue_js(VwoPlugin::render( 'order', get_order_data( $order) )); |
| 72 |
$order->update_meta_data( '_vwo_order_tracked', 1 ); |
| 73 |
$order->save(); |
| 74 |
} |
| 75 |
}); |
| 76 |
|
| 77 |
// Product view event |
| 78 |
function vwo_track_product_view() { |
| 79 |
global $product; |
| 80 |
$server_side_enabled = get_option('vwo_server_side_tracking', false); |
| 81 |
$track_product_view = get_option('track_product_view', false); |
| 82 |
$enable_woocommerce_event_tracking= get_option('enable_woocommerce_event_tracking',false); |
| 83 |
if ($server_side_enabled || !$product || !$enable_woocommerce_event_tracking || !$track_product_view) |
| 84 |
return; |
| 85 |
|
| 86 |
wc_enqueue_js(VwoPlugin::render('product', get_product_data($product->get_id()))); |
| 87 |
} |
| 88 |
|
| 89 |
// Add to cart event |
| 90 |
function vwo_track_add_to_cart() { |
| 91 |
$server_side_enabled = get_option('vwo_server_side_tracking', false); |
| 92 |
$enable_woocommerce_event_tracking= get_option('enable_woocommerce_event_tracking', false); |
| 93 |
$track_add_to_cart = get_option('track_add_to_cart', false); |
| 94 |
if ($server_side_enabled || ! is_single() || !$enable_woocommerce_event_tracking || !$track_add_to_cart) |
| 95 |
return; |
| 96 |
|
| 97 |
global $product; |
| 98 |
wc_enqueue_js( VwoPlugin::render( 'add', get_product_data( $product->get_id() ) ) ); |
| 99 |
} |
| 100 |
|
| 101 |
function vwo_track_remove_from_cart() { |
| 102 |
$server_side_enabled = get_option('vwo_server_side_tracking', false); |
| 103 |
$enable_woocommerce_event_tracking= get_option('enable_woocommerce_event_tracking', false); |
| 104 |
$track_remove_from_cart = get_option('track_remove_from_cart', false); |
| 105 |
if ($server_side_enabled || !$enable_woocommerce_event_tracking || !$track_remove_from_cart) |
| 106 |
return; |
| 107 |
wc_enqueue_js(VwoPlugin::render( 'remove' )); |
| 108 |
} |
| 109 |
|
| 110 |
// Checkout event |
| 111 |
function vwo_track_checkout() { |
| 112 |
$server_side_enabled = get_option('vwo_server_side_tracking', false); |
| 113 |
$enable_woocommerce_event_tracking= get_option('enable_woocommerce_event_tracking', false); |
| 114 |
if ($server_side_enabled || $enable_woocommerce_event_tracking) |
| 115 |
return; |
| 116 |
wc_enqueue_js(VwoPlugin::render( 'checkout' )); |
| 117 |
} |
| 118 |
|
| 119 |
function vwo_track_purchase($order_id) { |
| 120 |
$server_side_enabled = get_option('vwo_server_side_tracking', false); |
| 121 |
$track_purchase = get_option('track_purchase', false); |
| 122 |
$enable_woocommerce_event_tracking= get_option('enable_woocommerce_event_tracking', false); |
| 123 |
if ($server_side_enabled || $enable_woocommerce_event_tracking || !$track_purchase) |
| 124 |
return; |
| 125 |
$order = wc_get_order($order_id); |
| 126 |
wc_enqueue_js(VwoPlugin::render( 'order', get_order_data( $order) )); |
| 127 |
} |
| 128 |
|
| 129 |
function get_product_categories( $product_id ) { |
| 130 |
$categories = []; |
| 131 |
foreach ( get_the_terms( $product_id, 'product_cat' ) as $term ) |
| 132 |
$categories[] = esc_js( $term->name ); |
| 133 |
|
| 134 |
switch ( count( $categories ) ) { |
| 135 |
case 0: return ''; |
| 136 |
case 1: return $categories[ 0 ]; |
| 137 |
default : return array_slice( $categories, 0, 5 ); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
function get_product_data( $product_id, $quantity = 1 ) { |
| 142 |
return ( $product = wc_get_product( $product_id ) ) ? [ |
| 143 |
'variantId'=> esc_js($product->get_sku()), |
| 144 |
'productSku' => esc_js( $product->get_sku() ?: ( '#' . $product->get_id() ) ), |
| 145 |
'productTitle' => esc_js( $product->get_title() ), |
| 146 |
'productId' => esc_js($product->get_id()), |
| 147 |
'productCategory' => get_product_categories( $product->get_id() ), |
| 148 |
'price' => floatval( $product->get_price() ), |
| 149 |
'currency' => get_woocommerce_currency(), |
| 150 |
'quantity' => intval( $quantity ), |
| 151 |
'productUrl' => esc_url(get_permalink($product->get_id())) |
| 152 |
] : []; |
| 153 |
} |
| 154 |
|
| 155 |
function get_order() { |
| 156 |
global $wp; |
| 157 |
|
| 158 |
$order_id = is_numeric( $wp->query_vars[ 'order-received' ] ) ? intval( $wp->query_vars[ 'order-received' ] ) : 0; |
| 159 |
if ( ! $order_id ) return null; |
| 160 |
|
| 161 |
$order = wc_get_order( $order_id ); |
| 162 |
if ( $order and $order->has_status( 'failed' ) ) return null; |
| 163 |
if ( $order and (bool)$order->get_meta( '_vwo_order_tracked' ) ) return null; |
| 164 |
|
| 165 |
return $order; |
| 166 |
} |
| 167 |
|
| 168 |
function get_order_data( $order ) { |
| 169 |
$order_data = [ |
| 170 |
'orderId' => $order->get_id(), |
| 171 |
'totalPrice' => floatval( $order->get_total() ), |
| 172 |
'shippingPrice' => floatval( $order->get_shipping_total() ), |
| 173 |
'totalTax' => floatval( $order->get_total_tax() ), |
| 174 |
'discount' => floatval( $order->get_total_discount() ), |
| 175 |
'currencyCode' => $order->get_currency(), |
| 176 |
'vwoMeta'=> array( |
| 177 |
'source'=> "woocommerce-server", |
| 178 |
) |
| 179 |
]; |
| 180 |
|
| 181 |
// Get products from order |
| 182 |
$productIds = []; |
| 183 |
$productSkus = []; |
| 184 |
$productPrices = []; |
| 185 |
$productQuantities = []; |
| 186 |
|
| 187 |
foreach ( $order->get_items() as $item_id => $item ) { |
| 188 |
$product = $item->get_product(); |
| 189 |
if ( $product ) { |
| 190 |
$productIds[] = $product->get_id(); |
| 191 |
$productSkus[] = $product->get_sku(); |
| 192 |
$productPrices[] = floatval( $item->get_total() ); |
| 193 |
$productQuantities[] = intval( $item->get_quantity() ); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
// Convert arrays to comma-separated strings |
| 198 |
$order_data['productId'] = implode(",", $productIds); |
| 199 |
$order_data['productSku'] = implode(",", $productSkus); |
| 200 |
$order_data['productPrice'] = implode(",", $productPrices); |
| 201 |
$order_data['productQuantity'] = implode(",", $productQuantities); |
| 202 |
|
| 203 |
return $order_data; |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Get visitor ID from VWO or Wingify cookie. |
| 208 |
* |
| 209 |
* @return string |
| 210 |
*/ |
| 211 |
function vwo_get_visitor_id() { |
| 212 |
if ( ! empty( $_COOKIE['_vwo_uuid'] ) ) { |
| 213 |
return sanitize_text_field( wp_unslash( $_COOKIE['_vwo_uuid'] ) ); |
| 214 |
} |
| 215 |
|
| 216 |
if ( ! empty( $_COOKIE['_wingify_uuid'] ) ) { |
| 217 |
return sanitize_text_field( wp_unslash( $_COOKIE['_wingify_uuid'] ) ); |
| 218 |
} |
| 219 |
|
| 220 |
return ''; |
| 221 |
} |
| 222 |
|
| 223 |
function server_vwo_track_product_view() { |
| 224 |
$server_side_enabled = get_option('vwo_server_side_tracking', false); |
| 225 |
$track_product_view = get_option('track_product_view', false); |
| 226 |
if (!$server_side_enabled || !$track_product_view || !is_product()) |
| 227 |
return; |
| 228 |
global $product; |
| 229 |
$vis_id = vwo_get_visitor_id(); |
| 230 |
// if (!$vis_id) return; |
| 231 |
$eventName="woocommerce.productViewed"; |
| 232 |
$categories = get_product_categories( $product->get_id() ); |
| 233 |
|
| 234 |
// Ensure it is an array before using implode |
| 235 |
if (is_array($categories)) { |
| 236 |
$category_list = implode(",", $categories); |
| 237 |
} else { |
| 238 |
$category_list = $categories; // If it's a string, use it directly |
| 239 |
} |
| 240 |
$event_data = array( |
| 241 |
'd' => array( |
| 242 |
'msgId' => $vis_id . '-' . time() . rand(1000, 9999), |
| 243 |
'visId' => $vis_id, |
| 244 |
'event' => array( |
| 245 |
'props' => array( |
| 246 |
'variantId' => $product->get_sku(), |
| 247 |
'productSku' => $product->get_sku(), |
| 248 |
'productId' => $product->get_id(), |
| 249 |
'productTitle' => $product->get_name(), |
| 250 |
'productCategory' => $category_list, |
| 251 |
'currency' => get_woocommerce_currency(), |
| 252 |
'price' => $product->get_price(), |
| 253 |
'quantity' => 1, |
| 254 |
'productUrl' => get_permalink($product->get_id()), |
| 255 |
'page' => array( |
| 256 |
'title' => get_the_title(), |
| 257 |
'url' => get_permalink() |
| 258 |
), |
| 259 |
"vwoMeta"=> array( |
| 260 |
"source"=> "woocommerce-server", |
| 261 |
) |
| 262 |
), |
| 263 |
'name' => $eventName, |
| 264 |
'time' => time() * 1000, |
| 265 |
), |
| 266 |
'sessionId' => time(), |
| 267 |
), |
| 268 |
); |
| 269 |
$resonse=vwo_send_event_to_vwo($eventName, $event_data); |
| 270 |
} |
| 271 |
|
| 272 |
function server_vwo_track_add_to_cart($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data){ |
| 273 |
$server_side_enabled = get_option('vwo_server_side_tracking', false); |
| 274 |
$track_add_to_cart = get_option('track_add_to_cart', false); |
| 275 |
if (!$server_side_enabled || !$track_add_to_cart) |
| 276 |
return; |
| 277 |
$eventName= "woocommerce.addToCart"; |
| 278 |
$vis_id = vwo_get_visitor_id(); |
| 279 |
// if (!$vis_id) return; |
| 280 |
$product_data= get_product_data($product_id); |
| 281 |
$categories = get_product_categories($product_id); |
| 282 |
// Ensure it is an array before using implode |
| 283 |
if (is_array($categories)) { |
| 284 |
$category_list = implode(",", $categories); |
| 285 |
} else { |
| 286 |
$category_list = $categories; // If it's a string, use it directly |
| 287 |
} |
| 288 |
$event_data = array( |
| 289 |
'd' => array( |
| 290 |
'msgId' => $vis_id . '-' . time() . rand(1000, 9999), |
| 291 |
'visId' => $vis_id, |
| 292 |
'event' => array( |
| 293 |
'props' => array( |
| 294 |
'variantId' => $variation_id, |
| 295 |
'productSku' => $product_data['productSku'], |
| 296 |
'productId' => $product_data['productId'], |
| 297 |
'productTitle' => $product_data['productTitle'], |
| 298 |
'productCategory' => $category_list, |
| 299 |
'currency' => get_woocommerce_currency(), |
| 300 |
'price' => $product_data['price'], |
| 301 |
'quantity' => 1, |
| 302 |
'productUrl' => $product_data['productUrl'], |
| 303 |
'page' => array( |
| 304 |
'title' => get_the_title(), |
| 305 |
'url' => get_permalink() |
| 306 |
), |
| 307 |
"vwoMeta"=> array( |
| 308 |
"source"=> "woocommerce-server", |
| 309 |
) |
| 310 |
), |
| 311 |
'name' => $eventName, |
| 312 |
'time' => time() * 1000, |
| 313 |
), |
| 314 |
'sessionId' => time(), |
| 315 |
), |
| 316 |
); |
| 317 |
$response=vwo_send_event_to_vwo($eventName, $event_data); |
| 318 |
} |
| 319 |
|
| 320 |
function server_vwo_track_remove_from_cart($cart_item_key, $cart){ |
| 321 |
$server_side_enabled = get_option('vwo_server_side_tracking', false); |
| 322 |
$track_remove_from_cart = get_option('track_remove_from_cart', false); |
| 323 |
if (!$server_side_enabled || !$track_remove_from_cart) |
| 324 |
return; |
| 325 |
$eventName= "woocommerce.removeFromCart"; |
| 326 |
$vis_id = vwo_get_visitor_id(); |
| 327 |
// if (!$vis_id) return; |
| 328 |
$cart_item = $cart->removed_cart_contents[$cart_item_key]; |
| 329 |
$product_id= $cart_item['product_id']; |
| 330 |
$quantity= $cart_item['quantity']; |
| 331 |
$variation_id= $cart_item['variation_id']; |
| 332 |
$product_data= get_product_data($product_id); |
| 333 |
$event_data = array( |
| 334 |
'd' => array( |
| 335 |
'msgId' => $vis_id . '-' . time() . rand(1000, 9999), |
| 336 |
'visId' => $vis_id, |
| 337 |
'event' => array( |
| 338 |
'props' => array( |
| 339 |
'variantId' => $variation_id, |
| 340 |
'productSku' => $product_data['productSku'], |
| 341 |
'productId' => $product_data['productId'], |
| 342 |
'productTitle' => $product_data['productTitle'], |
| 343 |
'currency' => get_woocommerce_currency(), |
| 344 |
'price' => $product_data['price'], |
| 345 |
'quantity' => $quantity, |
| 346 |
'productUrl' => $product_data['productUrl'], |
| 347 |
'page' => array( |
| 348 |
'title' => get_the_title(), |
| 349 |
'url' => get_permalink() |
| 350 |
), |
| 351 |
"vwoMeta"=> array( |
| 352 |
"source"=> "woocommerce-server", |
| 353 |
) |
| 354 |
), |
| 355 |
'name' => $eventName, |
| 356 |
'time' => time() * 1000, |
| 357 |
), |
| 358 |
'sessionId' => time(), |
| 359 |
), |
| 360 |
); |
| 361 |
return vwo_send_event_to_vwo($eventName, $event_data); |
| 362 |
} |
| 363 |
|
| 364 |
function server_vwo_track_checkout(){ |
| 365 |
|
| 366 |
$track_checkout = get_option('track_checkout', false); |
| 367 |
$server_side_enabled = get_option('vwo_server_side_tracking', false); |
| 368 |
if (!$server_side_enabled || !$track_checkout) |
| 369 |
return; |
| 370 |
$eventName= "woocommerce.checkoutStarted"; |
| 371 |
$vis_id = vwo_get_visitor_id(); |
| 372 |
// if (!$vis_id) return; |
| 373 |
|
| 374 |
$event_data = array( |
| 375 |
'd' => array( |
| 376 |
'msgId' => $vis_id . '-' . time() . rand(1000, 9999), |
| 377 |
'visId' => $vis_id, |
| 378 |
'event' => array( |
| 379 |
'props' => array( |
| 380 |
'page' => array( |
| 381 |
'title' => get_the_title(), |
| 382 |
'url' => get_permalink() |
| 383 |
), |
| 384 |
"vwoMeta"=> array( |
| 385 |
"source"=> "woocommerce-server", |
| 386 |
) |
| 387 |
), |
| 388 |
'name' => $eventName, |
| 389 |
'time' => time() * 1000, |
| 390 |
), |
| 391 |
'sessionId' => time(), |
| 392 |
), |
| 393 |
); |
| 394 |
return vwo_send_event_to_vwo( $eventName, $event_data); |
| 395 |
} |
| 396 |
|
| 397 |
function server_vwo_track_purchase($order_id){ |
| 398 |
$server_side_enabled = get_option('vwo_server_side_tracking', false); |
| 399 |
$track_purchase = get_option('track_purchase', false); |
| 400 |
if (!$server_side_enabled || ! $track_purchase) |
| 401 |
return; |
| 402 |
$eventName= "woocommerce.purchase"; |
| 403 |
$vis_id = vwo_get_visitor_id(); |
| 404 |
// if (!$vis_id) return; |
| 405 |
$order = wc_get_order($order_id); |
| 406 |
$order_data = get_order_data( $order); |
| 407 |
$order_data['page' ]=array( |
| 408 |
'title' => get_the_title(), |
| 409 |
'url' => get_permalink() |
| 410 |
); |
| 411 |
$event_data = array( |
| 412 |
'd' => array( |
| 413 |
'msgId' => $vis_id . '-' . time() . rand(1000, 9999), |
| 414 |
'visId' => $vis_id, |
| 415 |
'event' => array( |
| 416 |
'props' => $order_data, |
| 417 |
'name' => $eventName, |
| 418 |
'time' => time() * 1000, |
| 419 |
), |
| 420 |
'sessionId' => time(), |
| 421 |
), |
| 422 |
); |
| 423 |
$order->update_meta_data( '_vwo_order_tracked', 1 ); |
| 424 |
$order->save(); |
| 425 |
return vwo_send_event_to_vwo($eventName, $event_data); |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
function vwo_send_event_to_vwo($eventName, $eventData) { |
| 430 |
$vwo_account_id = get_option('vwo_id'); |
| 431 |
$vwo_coll_url = get_option('vwo_coll_url'); |
| 432 |
$enable_woocommerce_event_tracking= get_option('enable_woocommerce_event_tracking'); |
| 433 |
if(!$enable_woocommerce_event_tracking){ |
| 434 |
return; |
| 435 |
} |
| 436 |
// Construct the URL based on collUrl availability |
| 437 |
if(!$vwo_coll_url){ |
| 438 |
$vwo_coll_url=vwo_clhf_fetch_and_save_coll_url($vwo_account_id); |
| 439 |
} |
| 440 |
$url = $vwo_coll_url |
| 441 |
? "https://$vwo_coll_url/events/t?en=$eventName&a=$vwo_account_id" |
| 442 |
: "https://dev.visualwebsiteoptimizer.com/events/t?en=$eventName&a=$vwo_account_id"; |
| 443 |
|
| 444 |
// Set User-Agent, default to "vwo-woocommerce-plugin" if not provided |
| 445 |
$user_agent = !empty($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : "vwo-woocommerce-plugin"; |
| 446 |
|
| 447 |
$response = wp_remote_post($url, array( |
| 448 |
'body' => json_encode($eventData), |
| 449 |
'headers' => array( |
| 450 |
'Content-Type' => 'application/json', |
| 451 |
'User-Agent' => $user_agent |
| 452 |
), |
| 453 |
'timeout' => 20, |
| 454 |
)); |
| 455 |
|
| 456 |
return $response; |
| 457 |
} |
| 458 |
function vwo_clhf_fetch_and_save_coll_url($vwo_id) { |
| 459 |
return vwo_clhf_fetch_and_save_account_info($vwo_id); |
| 460 |
} |
| 461 |
|
| 462 |
?> |
| 463 |
|