From 72c295490f18c208e2699f4453c635e5b9fe6726 Mon Sep 17 00:00:00 2001 From: Laura Lebovic Date: Wed, 16 Apr 2025 16:13:24 +0200 Subject: [PATCH] Add external ID retrieval to Event class and adjust action priorities --- facebook-commerce-events-tracker.php | 6 +++--- includes/Events/Event.php | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/facebook-commerce-events-tracker.php b/facebook-commerce-events-tracker.php index afc1d55b9..ecb9c224d 100644 --- a/facebook-commerce-events-tracker.php +++ b/facebook-commerce-events-tracker.php @@ -98,12 +98,12 @@ private function is_pixel_enabled() { private function add_hooks() { // inject Pixel - add_action( 'wp_head', array( $this, 'inject_base_pixel' ) ); + add_action( 'wp_head', array( $this, 'inject_base_pixel' ), 1 ); add_action( 'wp_footer', array( $this, 'inject_base_pixel_noscript' ) ); // ViewContent for individual products - add_action( 'woocommerce_after_single_product', array( $this, 'inject_view_content_event' ) ); - add_action( 'woocommerce_after_single_product', array( $this, 'maybe_inject_search_event' ) ); + add_action( 'woocommerce_after_single_product', array( $this, 'inject_view_content_event' ), 20 ); + add_action( 'woocommerce_after_single_product', array( $this, 'maybe_inject_search_event' ), 20 ); // ViewCategory events add_action( 'woocommerce_after_shop_loop', array( $this, 'inject_view_category_event' ) ); diff --git a/includes/Events/Event.php b/includes/Events/Event.php index 26c2388f6..6efb17665 100644 --- a/includes/Events/Event.php +++ b/includes/Events/Event.php @@ -116,6 +116,7 @@ protected function prepare_user_data( $data ) { 'client_user_agent' => $this->get_client_user_agent(), 'click_id' => $this->get_click_id(), 'browser_id' => $this->get_browser_id(), + 'external_id' => $this->get_external_id(), ) ); // Country key is not the same in pixel and CAPI events, see: @@ -277,6 +278,29 @@ protected function get_browser_id() { return ! empty( $_COOKIE['_fbp'] ) ? wc_clean( wp_unslash( $_COOKIE['_fbp'] ) ) : ''; } + /** + * Retrieves the external ID based on the session's customer ID. + * + * @since 2.0.0 + * + * @return string The external ID if available, or an empty string otherwise. + */ + protected function get_external_id() + { + // Ensure the WooCommerce session exists and has the get_customer_id method + if (isset(WC()->session) && method_exists(WC()->session, 'get_customer_unique_id')) { + $customer_id = WC()->session->get_customer_unique_id(); + + // Return the customer ID as a string if it exists + if (! empty($customer_id)) { + return strval($customer_id); + } + } + + // Return an empty string if no customer ID is available + return ''; + } + /** * Gets the data.