is_pixel_enabled() ) { return; } $this->pixel = new \WC_Facebookcommerce_Pixel( $user_info ); $this->aam_settings = $aam_settings; $this->tracked_events = array(); // Initialize external JS hooks early so script is enqueued before wp_enqueue_scripts fires. \WC_Facebookcommerce_Pixel::init_external_js_hooks(); $this->param_builder_server_setup(); $this->add_hooks(); $this->cogs_provider = new CostOfGoods(); } public static function get_param_builder() { if ( null === self::$param_builder ) { try { $site_url = get_site_url(); self::$param_builder = new \FacebookAds\ParamBuilder( array( $site_url ) ); self::$param_builder->processRequest( $site_url, $_GET, $_COOKIE, isset( $_SERVER['HTTP_REFERER'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) : null ); } catch ( \Exception $exception ) { Logger::log( 'Error initializing CAPI Parameter Builder: ' . $exception->getMessage(), array(), array( 'should_send_log_to_meta' => true, 'should_save_log_in_woocommerce' => true, 'woocommerce_log_level' => \WC_Log_Levels::ERROR, ) ); } } return self::$param_builder; } /** * Initializes the CAPI server side Parameter Builder and sets cookies as needed. */ public function param_builder_server_setup() { try { if ( ! (bool) apply_filters( 'facebook_for_woocommerce_integration_pixel_enabled', true ) ) { return; } $cookie_to_set = self::get_param_builder()->getCookiesToSet(); if ( ! headers_sent() ) { foreach ( $cookie_to_set as $cookie ) { setcookie( $cookie->name, $cookie->value, time() + $cookie->max_age, '/', $cookie->domain ); } } } catch ( \Exception $exception ) { Logger::log( 'Error setting up server side CAPI Parameter Builder: ' . $exception->getMessage(), array(), array( 'should_send_log_to_meta' => true, 'should_save_log_in_woocommerce' => true, 'woocommerce_log_level' => \WC_Log_Levels::ERROR, ) ); } } /** * Determines whether the Pixel should be enabled. * * @since 2.2.0 * * @return bool */ private function is_pixel_enabled() { if ( null === $this->is_pixel_enabled ) { /** * Filters whether the Pixel should be enabled. * * @param bool $enabled default true */ $this->is_pixel_enabled = (bool) apply_filters( 'facebook_for_woocommerce_integration_pixel_enabled', true ); } return $this->is_pixel_enabled; } /** * Add events tracker hooks. * * @since 2.2.0 */ private function add_hooks() { // inject Pixel add_action( 'wp_head', array( $this, 'inject_base_pixel' ) ); add_action( 'wp_footer', array( $this, 'inject_base_pixel_noscript' ) ); // set up CAPI Param Builder libraries add_action( 'wp_enqueue_scripts', array( $this, 'param_builder_client_setup' ) ); // 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' ) ); // ViewCategory events add_action( 'woocommerce_after_shop_loop', array( $this, 'inject_view_category_event' ) ); // Search events add_action( 'pre_get_posts', array( $this, 'inject_search_event' ) ); add_filter( 'woocommerce_redirect_single_search_result', array( $this, 'maybe_add_product_search_event_to_session' ) ); // AddToCart events add_action( 'woocommerce_add_to_cart', array( $this, 'inject_add_to_cart_event' ), 40, 4 ); // AddToCart while AJAX is enabled (Classic WooCommerce) add_action( 'woocommerce_ajax_added_to_cart', array( $this, 'add_filter_for_add_to_cart_fragments' ) ); // AddToCart for WooCommerce Blocks (Store API) if ( did_action( 'woocommerce_blocks_loaded' ) ) { $this->register_store_api_endpoint_data(); } else { add_action( 'woocommerce_blocks_loaded', array( $this, 'register_store_api_endpoint_data' ) ); } // AddToCart while using redirect to cart page if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add', 'no' ) ) { add_action( 'wp_head', array( WC_Facebookcommerce_Utils::class, 'print_deferred_events' ) ); add_action( 'shutdown', array( WC_Facebookcommerce_Utils::class, 'save_deferred_events' ) ); } // InitiateCheckout events add_action( 'woocommerce_after_checkout_form', array( $this, 'inject_initiate_checkout_event' ) ); // InitiateCheckout events for checkout block. add_action( 'woocommerce_blocks_checkout_enqueue_data', array( $this, 'inject_initiate_checkout_event' ) ); // Purchase and Subscribe events add_action( 'woocommerce_new_order', array( $this, 'inject_purchase_event' ), 10 ); add_action( 'woocommerce_process_shop_order_meta', array( $this, 'inject_purchase_event' ), 20 ); add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'inject_purchase_event' ), 30 ); add_action( 'woocommerce_thankyou', array( $this, 'inject_purchase_event' ), 40 ); // Lead events through Contact Form 7 add_action( 'wpcf7_contact_form', array( $this, 'inject_lead_event_hook' ), 11 ); // Flush pending events on shutdown add_action( 'shutdown', array( $this, 'send_pending_events' ) ); } /** * Prints the base JavaScript pixel code * * phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped */ public function inject_base_pixel() { if ( $this->is_pixel_enabled() ) { echo $this->pixel->pixel_base_code(); $this->inject_page_view_event(); } } /** * Prints the base