Plugin Directory

Changeset 3387116


Ignore:
Timestamp:
10/30/2025 01:58:37 PM (5 months ago)
Author:
tracify
Message:

Released Tracify plugin version 3.1.8

Location:
tracify/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • tracify/trunk/classes/tracify_backend.php

    r3364133 r3387116  
    44    <link rel=\"preconnect\" href=\"" . $script_host . "\" />
    55    <link rel=\"preload\" as=\"script\" href=\"" . $script_tag . "\">
    6     <script async src=\"" . $script_tag . "\"></script>"; if (WC()->session !== null && WC()->session->has_session()) { $session_id = $this->calc_group(WC()->session->get_customer_id()); if ($session_id) { echo "<script>window.vids = ['" . $session_id . "'];</script>"; } } } private function calc_group($data) { $digest = hash('sha256', $data, false); $group = substr($digest, 0, strlen($digest) - 5); return hash('sha256', $group, false); } private function transmit_tracify_event($token, $payload) { $header = "Content-Type: application/json\r\n" . "Accept: application/json\r\n" . "tracify-token: " . $token . "\r\n"; $args = array( 'method' => 'POST', 'timeout' => 5, 'headers' => $header, 'body' => json_encode($payload), ); $this->debug_log('Tracify Request: ' . wp_json_encode($args, JSON_PRETTY_PRINT)); $response = wp_remote_post("https://backend.tracify.ai/upload", $args); $this->debug_log('Tracify Response: ' . wp_json_encode($response, JSON_PRETTY_PRINT)); return $response; } public function handle_order_status_change($order_id, $old_status, $new_status) { $this->debug_log(sprintf( 'Order Status Change: Order #%d changed from "%s" to "%s"', $order_id, $old_status, $new_status )); $this->report_order_server_side($order_id); } private function debug_log($message) { if (isset($this->tracify_options['debug-mode']) && $this->tracify_options['debug-mode'] === '1') { $logger = wc_get_logger(); $logger->info($message, array('source' => 'tracify')); } } public function report_order_server_side($order_id) { $token = $this->tracify_options['tracify-token']; $order = wc_get_order($order_id); $csid = $this->tracify_options['csid']; $domain = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]"; $order_id = $order->get_id(); $amount = $order->get_total(); $currency = $order->get_currency(); $amount = number_format($amount, 2, ',', ''); $email = $order->get_billing_email(); $event_infos = array( 'oid' => $order_id, 'amount' => $amount, 'cc' => $currency ); $counter = 0; foreach ($order->get_items() as $item) { $product = $item->get_product(); if ($amount <= 0) { continue; } $counter += 1; $event_infos['ITEM' . $counter] = $product->get_sku(); $event_infos['AMT' . $counter] = number_format($item->get_total(), 2, ',', ''); $event_infos['QTY' . $counter] = $item->get_quantity(); } $voucher_service = new VoucherService(); $coupon_hash = $voucher_service->get_coupons_hash_by_order_id($order_id); if ($coupon_hash) { $shop_coupons = $voucher_service->get_shop_coupons(); foreach ($shop_coupons as $coupon) { $coupon_code = [strtolower($coupon->post_title)]; $encoded_coupon_code = wp_json_encode($coupon_code); $hashed_coupon = md5($encoded_coupon_code); if ($coupon_hash == $hashed_coupon) { $event_infos['discount_codes'] = $coupon->post_title; break; } } } else { $coupon_code = $voucher_service->get_coupon_code($order_id); if ($coupon_code) { $event_infos['discount_codes'] = $coupon_code; } } if (!array_key_exists("csorigin", $event_infos)) { $domain = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]"; $event_infos["csorigin"] = $domain; } $event_infos = array_merge($event_infos, $this->get_conversion_metadata($order)); $entity_data = $this->collect_entity_data($order); $event = [ 'update_order' => true, 'is_returning_customer' => $this->is_returning_customer($order->get_customer_id(), $email), 'interaction_type' => 'purchase', 'loc' => $domain, 'site_id' => $csid, 'event_infos' => $event_infos, 'entity_data' => $this->hash_entity_data($entity_data), 'hive_process_flag' => true, ]; if ($this->tracify_options['debug-mode'] == '1') { $event['unhashed_entity_data'] = $entity_data; } if (WC()->session !== null && WC()->session->has_session()) { $event['visitor_ids'][] = $this->calc_group(WC()->session->get_customer_id()); } $payload = [ 'events' => [$event], 'shop_platform' => 'woocommerce', ]; $this->transmit_tracify_event($token, $payload); } private function get_conversion_metadata($order) { return [ 'conversion_channel' => $this->get_conversion_channel($order), 'conversion_status'=> $this->get_conversion_status($order), 'conversion_tags'=> [], 'payment_source' => $order->get_payment_method(), 'payment_status' => $this->get_payment_status($order), ]; } private function get_conversion_channel($order) { $created_via = $order->get_created_via(); if (in_array($created_via, ['mobile', 'mobile-app'])) { return 'mobile'; } $user_agent = $order->get_customer_user_agent(); if (!empty($user_agent) && $this->is_mobile_user_agent($user_agent)) { return 'mobile'; } return 'web'; } private function is_mobile_user_agent($user_agent) { $mobile_patterns = [ 'Mobile', 'Android', 'iPhone', 'iPad', 'iPod', 'BlackBerry', 'Windows Phone', 'Opera Mini', 'IEMobile', 'Mobile Safari' ]; foreach ($mobile_patterns as $pattern) { if (stripos($user_agent, $pattern) !== false) { return true; } } return false; } private function get_payment_status($order) { if ($order->is_paid()) { return 'paid'; } if ($order->needs_payment()) { return 'pending'; } $order_status = $order->get_status(); switch ($order_status) { case 'failed': return 'failed'; case 'cancelled': return 'cancelled'; case 'refunded': return 'refunded'; case 'processing': case 'completed': return $order->is_paid() ? 'paid' : 'pending'; default: return 'pending'; } } private function get_conversion_status($order) { $order_status = $order->get_status(); if (in_array($order_status, ['cancelled', 'failed'])) { return 'cancelled'; } if ($order_status == 'refunded') { return 'returned'; } if ($order_status == 'completed') { return 'delivered'; } return 'open'; } private function collect_entity_data($order) { $raw_ip = $order->get_customer_ip_address(); $raw_user_agent = $order->get_customer_user_agent(); $email = $order->get_billing_email(); $entity_data = [ $raw_user_agent => 4, ]; $ipService = new IPService(); if ($ipService->isIPv4Address($raw_ip)) { $entity_data[$raw_ip] = 3; $entity_data[$raw_ip . "|" . $raw_user_agent] = 2; } else { $ipv6Parts = $ipService->splitIPv6Address($raw_ip); $networkPrefix = $ipv6Parts[0]; $interfaceIdentifier = $ipv6Parts[1]; $entity_data[$networkPrefix] = 3; $entity_data[$networkPrefix . "|" . $raw_user_agent] = 2; $entity_data[$interfaceIdentifier] = 4; $entity_data[$raw_ip] = 3; $entity_data[$raw_ip . "|" . $raw_user_agent] = 2; } if (!is_null($email)) { $entity_data[$email] = 1; } return $entity_data; } private function hash_entity_data(array $entityData) { $hashedData = []; foreach ($entityData as $key => $typeValue) { $hashedData[$this->calc_group($key)] = $typeValue; } return $hashedData; } private function is_returning_customer(int $customer_id, string $email) { $previous_orders = []; if ($customer_id === 0) { $previous_orders = wc_get_orders([ 'billing_email' => $email, 'orderby' => 'date', 'order' => 'DESC', ]); } else { $previous_orders = wc_get_orders([ 'customer_id' => $customer_id, 'orderby' => 'date', 'order' => 'DESC', ]); } return count($previous_orders) > 1; } public function report_order_client_side($order_id) { $csid = $this->tracify_options['csid']; $order = wc_get_order($order_id); ?>
     6    <script async src=\"" . $script_tag . "\"></script>"; if (WC()->session !== null && WC()->session->has_session()) { $session_id = $this->calc_group(WC()->session->get_customer_id()); if ($session_id) { echo "<script>window.vids = ['" . $session_id . "'];</script>"; } } } private function calc_group($data) { $digest = hash('sha256', $data, false); $group = substr($digest, 0, strlen($digest) - 5); return hash('sha256', $group, false); } private function transmit_tracify_event($token, $payload) { $header = "Content-Type: application/json\r\n" . "Accept: application/json\r\n" . "tracify-token: " . $token . "\r\n"; $args = array( 'method' => 'POST', 'timeout' => 5, 'headers' => $header, 'body' => json_encode($payload), ); $this->debug_log('Tracify Request: ' . wp_json_encode($args, JSON_PRETTY_PRINT)); $response = wp_remote_post("https://backend.tracify.ai/upload", $args); $this->debug_log('Tracify Response: ' . wp_json_encode($response, JSON_PRETTY_PRINT)); return $response; } public function handle_order_status_change($order_id, $old_status, $new_status) { $this->debug_log(sprintf( 'Order Status Change: Order #%d changed from "%s" to "%s"', $order_id, $old_status, $new_status )); $this->report_order_server_side($order_id); } private function debug_log($message) { if (isset($this->tracify_options['debug-mode']) && $this->tracify_options['debug-mode'] === '1') { $logger = wc_get_logger(); $logger->info($message, array('source' => 'tracify')); } } public function report_order_server_side($order_id) { $token = $this->tracify_options['tracify-token']; $order = wc_get_order($order_id); $csid = $this->tracify_options['csid']; $domain = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]"; $order_id = $order->get_id(); $amount = $order->get_total(); $currency = $order->get_currency(); $amount = number_format($amount, 2, ',', ''); $email = $order->get_billing_email(); $created_dt = $order->get_date_created(); $created_ts = $created_dt instanceof WC_DateTime ? $created_dt->getTimestamp() : time(); $created_utc = gmdate('Y-m-d H:i:s', $created_ts); $event_infos = array( 'oid' => $order_id, 'amount' => $amount, 'cc' => $currency, 'datetime' => $created_utc, ); $counter = 0; foreach ($order->get_items() as $item) { $product = $item->get_product(); if ($amount <= 0) { continue; } $counter += 1; $event_infos['ITEM' . $counter] = $product->get_sku(); $event_infos['AMT' . $counter] = number_format($item->get_total(), 2, ',', ''); $event_infos['QTY' . $counter] = $item->get_quantity(); } $voucher_service = new VoucherService(); $coupon_hash = $voucher_service->get_coupons_hash_by_order_id($order_id); if ($coupon_hash) { $shop_coupons = $voucher_service->get_shop_coupons(); foreach ($shop_coupons as $coupon) { $coupon_code = [strtolower($coupon->post_title)]; $encoded_coupon_code = wp_json_encode($coupon_code); $hashed_coupon = md5($encoded_coupon_code); if ($coupon_hash == $hashed_coupon) { $event_infos['discount_codes'] = $coupon->post_title; break; } } } else { $coupon_code = $voucher_service->get_coupon_code($order_id); if ($coupon_code) { $event_infos['discount_codes'] = $coupon_code; } } if (!array_key_exists("csorigin", $event_infos)) { $domain = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]"; $event_infos["csorigin"] = $domain; } $event_infos = array_merge($event_infos, $this->get_conversion_metadata($order)); $entity_data = $this->collect_entity_data($order); $event = [ 'update_order' => true, 'is_returning_customer' => $this->is_returning_customer($order->get_customer_id(), $email), 'interaction_type' => 'purchase', 'loc' => $domain, 'site_id' => $csid, 'event_infos' => $event_infos, 'entity_data' => $this->hash_entity_data($entity_data), 'hive_process_flag' => true, ]; if ($this->tracify_options['debug-mode'] == '1') { $event['unhashed_entity_data'] = $entity_data; } if (WC()->session !== null && WC()->session->has_session()) { $event['visitor_ids'][] = $this->calc_group(WC()->session->get_customer_id()); } $payload = [ 'events' => [$event], 'shop_platform' => 'woocommerce', ]; $this->transmit_tracify_event($token, $payload); } private function get_conversion_metadata($order) { return [ 'conversion_channel' => $this->get_conversion_channel($order), 'conversion_status'=> $this->get_conversion_status($order), 'conversion_tags'=> [], 'payment_source' => $order->get_payment_method(), 'payment_status' => $this->get_payment_status($order), ]; } private function get_conversion_channel($order) { $created_via = $order->get_created_via(); if (in_array($created_via, ['mobile', 'mobile-app'])) { return 'mobile'; } $user_agent = $order->get_customer_user_agent(); if (!empty($user_agent) && $this->is_mobile_user_agent($user_agent)) { return 'mobile'; } return 'web'; } private function is_mobile_user_agent($user_agent) { $mobile_patterns = [ 'Mobile', 'Android', 'iPhone', 'iPad', 'iPod', 'BlackBerry', 'Windows Phone', 'Opera Mini', 'IEMobile', 'Mobile Safari' ]; foreach ($mobile_patterns as $pattern) { if (stripos($user_agent, $pattern) !== false) { return true; } } return false; } private function get_payment_status($order) { if ($order->is_paid()) { return 'paid'; } if ($order->needs_payment()) { return 'pending'; } $order_status = $order->get_status(); switch ($order_status) { case 'failed': return 'failed'; case 'cancelled': return 'cancelled'; case 'refunded': return 'refunded'; case 'processing': case 'completed': return $order->is_paid() ? 'paid' : 'pending'; default: return 'pending'; } } private function get_conversion_status($order) { $order_status = $order->get_status(); if (in_array($order_status, ['cancelled', 'failed'])) { return 'cancelled'; } if ($order_status == 'refunded') { return 'returned'; } if ($order_status == 'completed') { return 'delivered'; } return 'open'; } private function collect_entity_data($order) { $raw_ip = $order->get_customer_ip_address(); $raw_user_agent = $order->get_customer_user_agent(); $email = $order->get_billing_email(); $entity_data = [ $raw_user_agent => 4, ]; $ipService = new IPService(); if ($ipService->isIPv4Address($raw_ip)) { $entity_data[$raw_ip] = 3; $entity_data[$raw_ip . "|" . $raw_user_agent] = 2; } else { $ipv6Parts = $ipService->splitIPv6Address($raw_ip); $networkPrefix = $ipv6Parts[0]; $interfaceIdentifier = $ipv6Parts[1]; $entity_data[$networkPrefix] = 3; $entity_data[$networkPrefix . "|" . $raw_user_agent] = 2; $entity_data[$interfaceIdentifier] = 4; $entity_data[$raw_ip] = 3; $entity_data[$raw_ip . "|" . $raw_user_agent] = 2; } if (!is_null($email)) { $entity_data[$email] = 1; } return $entity_data; } private function hash_entity_data(array $entityData) { $hashedData = []; foreach ($entityData as $key => $typeValue) { $hashedData[$this->calc_group($key)] = $typeValue; } return $hashedData; } private function is_returning_customer(int $customer_id, string $email) { $previous_orders = []; if ($customer_id === 0) { $previous_orders = wc_get_orders([ 'billing_email' => $email, 'orderby' => 'date', 'order' => 'DESC', ]); } else { $previous_orders = wc_get_orders([ 'customer_id' => $customer_id, 'orderby' => 'date', 'order' => 'DESC', ]); } return count($previous_orders) > 1; } public function report_order_client_side($order_id) { $csid = $this->tracify_options['csid']; $order = wc_get_order($order_id); ?>
    77
    88        <script type="text/javascript">
  • tracify/trunk/readme.txt

    r3367669 r3387116  
    92923.1.7
    9393Minor bug fixes
     94
     953.1.8
     96Improve order tracking
  • tracify/trunk/tracify.php

    r3367669 r3387116  
    99 * License:             GPLv3
    1010 * License URI:         https://www.gnu.org/licenses/gpl-3.0.html
    11  * Version:             3.1.7
     11 * Version:             3.1.8
    1212 * Requires at least:   5.0
    1313 * Requires PHP:        7.2
    1414 */
    1515
    16 define('TRACIFY_PLUGIN_VER', '3.1.7');
     16define('TRACIFY_PLUGIN_VER', '3.1.8');
    1717define('TRACIFY_DIR_PATH', plugin_dir_path(__FILE__));
    1818define('TRACIFY_DIR_URL', plugin_dir_url(__FILE__));
Note: See TracChangeset for help on using the changeset viewer.