display name */ public static function ai_sources() { return array( 'chatgpt.com' => 'ChatGPT', 'chat.openai.com' => 'ChatGPT', 'openai' => 'ChatGPT', 'perplexity' => 'Perplexity', 'gemini.google.com' => 'Gemini', 'bard.google.com' => 'Gemini', 'copilot.microsoft' => 'Copilot', 'bing.com/chat' => 'Copilot', 'claude.ai' => 'Claude', 'anthropic' => 'Claude', 'meta.ai' => 'Meta AI', 'grok.com' => 'Grok', 'x.ai' => 'Grok', 'deepseek' => 'DeepSeek', 'you.com' => 'You.com', 'phind' => 'Phind', ); } public static function init() { add_action( 'template_redirect', array( __CLASS__, 'capture_landing' ), 1 ); add_action( 'woocommerce_checkout_create_order', array( __CLASS__, 'tag_order' ), 20, 1 ); add_action( 'woocommerce_store_api_checkout_update_order_meta', array( __CLASS__, 'tag_order' ), 20, 1 ); } /** * Classify a referrer URL or utm_source string. Returns display name or ''. * * @param string $value Referrer or utm value. */ public static function classify( $value ) { $value = strtolower( (string) $value ); if ( '' === $value ) { return ''; } foreach ( self::ai_sources() as $needle => $name ) { if ( false !== strpos( $value, $needle ) ) { return $name; } } return ''; } public static function capture_landing() { if ( is_admin() || isset( $_COOKIE[ self::COOKIE ] ) ) { return; } $ref = isset( $_SERVER['HTTP_REFERER'] ) ? esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) : ''; $utm = isset( $_GET['utm_source'] ) ? sanitize_text_field( wp_unslash( $_GET['utm_source'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification $src = self::classify( $utm ); if ( '' === $src ) { $src = self::classify( wp_parse_url( $ref, PHP_URL_HOST ) . wp_parse_url( $ref, PHP_URL_PATH ) ); } if ( '' !== $src && ! headers_sent() ) { setcookie( self::COOKIE, $src, time() + 30 * DAY_IN_SECONDS, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN, is_ssl(), true ); } } /** * @param WC_Order $order Order being created. */ public static function tag_order( $order ) { if ( ! $order instanceof WC_Order || $order->get_meta( '_agentrue_source' ) ) { return; } if ( isset( $_COOKIE[ self::COOKIE ] ) ) { $src = sanitize_text_field( wp_unslash( $_COOKIE[ self::COOKIE ] ) ); if ( in_array( $src, array_values( self::ai_sources() ), true ) ) { $order->update_meta_data( '_agentrue_source', 'ai_referral' ); $order->update_meta_data( '_agentrue_assistant', $src ); } } } /** * Return the AI assistant behind an order, or '' if none. * * @param WC_Order $order Order. */ public static function order_assistant( $order ) { $src = $order->get_meta( '_agentrue_source' ); $assistant = $order->get_meta( '_agentrue_assistant' ); if ( $assistant ) { return $assistant; } foreach ( array( '_wc_order_attribution_utm_source', '_wc_order_attribution_referrer', '_wc_order_attribution_session_entry' ) as $key ) { $name = self::classify( $order->get_meta( $key ) ); if ( $name ) { return $name; } } return ''; } /** * Aggregate report for the last N days. * * @param int $days Days back. */ public static function report( $days = 90 ) { $orders = wc_get_orders( array( 'limit' => 3000, 'date_created' => '>' . ( time() - $days * DAY_IN_SECONDS ), 'status' => array( 'wc-processing', 'wc-completed', 'wc-on-hold' ), 'type' => 'shop_order', ) ); $report = array( 'days' => $days, 'orders_total' => 0, 'revenue_total' => 0.0, 'orders_ai' => 0, 'revenue_ai' => 0.0, 'by_assistant' => array(), 'recent' => array(), 'currency' => get_woocommerce_currency(), ); foreach ( $orders as $order ) { $total = (float) $order->get_total(); $report['orders_total']++; $report['revenue_total'] += $total; $name = self::order_assistant( $order ); if ( '' === $name ) { continue; } $report['orders_ai']++; $report['revenue_ai'] += $total; if ( ! isset( $report['by_assistant'][ $name ] ) ) { $report['by_assistant'][ $name ] = array( 'orders' => 0, 'revenue' => 0.0 ); } $report['by_assistant'][ $name ]['orders']++; $report['by_assistant'][ $name ]['revenue'] += $total; if ( count( $report['recent'] ) < 15 ) { $report['recent'][] = array( 'id' => $order->get_id(), 'date' => $order->get_date_created() ? $order->get_date_created()->date_i18n( get_option( 'date_format' ) ) : '', 'total' => $total, 'assistant' => $name, ); } } uasort( $report['by_assistant'], function ( $a, $b ) { return $b['revenue'] <=> $a['revenue']; } ); return $report; } }