is_pixel_enabled() ) { return; } $this->pixel = new \WC_Facebookcommerce_Pixel( $user_info ); $this->aam_settings = $aam_settings; $this->tracked_events = []; $this->add_hooks(); } /** * 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', [ $this, 'inject_base_pixel' ] ); add_action( 'wp_footer', [ $this, 'inject_base_pixel_noscript' ] ); // ViewContent for individual products add_action( 'woocommerce_after_single_product', [ $this, 'inject_view_content_event' ] ); add_action( 'woocommerce_after_single_product', [ $this, 'maybe_inject_search_event' ] ); add_action( 'woocommerce_after_shop_loop', [ $this, 'inject_view_category_event' ] ); add_action( 'pre_get_posts', [ $this, 'inject_search_event' ] ); add_filter( 'woocommerce_redirect_single_search_result', [ $this, 'maybe_add_product_search_event_to_session' ] ); // AddToCart events add_action( 'woocommerce_add_to_cart', [ $this, 'inject_add_to_cart_event' ], 40, 4 ); // AddToCart while AJAX is enabled add_action( 'woocommerce_ajax_added_to_cart', [ $this, 'add_filter_for_add_to_cart_fragments' ] ); // AddToCart while using redirect to cart page if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) { add_filter( 'woocommerce_add_to_cart_redirect', [ $this, 'set_last_product_added_to_cart_upon_redirect' ], 10, 2 ); add_action( 'woocommerce_ajax_added_to_cart', [ $this, 'set_last_product_added_to_cart_upon_ajax_redirect' ] ); add_action( 'woocommerce_after_cart', [ $this, 'inject_add_to_cart_redirect_event' ], 10, 2 ); } // InitiateCheckout events add_action( 'woocommerce_after_checkout_form', [ $this, 'inject_initiate_checkout_event' ] ); // Purchase and Subscribe events add_action( 'woocommerce_checkout_update_order_meta', [ $this, 'inject_purchase_event' ] ); add_action( 'woocommerce_thankyou', [ $this, 'inject_purchase_event' ], 40 ); // TODO move this in some 3rd party plugin integrations handler at some point {FN 2020-03-20} add_action( 'wpcf7_contact_form', [ $this, 'inject_lead_event_hook' ], 11 ); } /** * Adds filter hooks. * * @internal * * @deprecated since 2.2.0 */ public function apply_filters() { wc_deprecated_function( __METHOD__, '2.2.0' ); } /** * Prints the base JavaScript pixel code. */ public function inject_base_pixel() { if ( $this->is_pixel_enabled() ) { // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped echo $this->pixel->pixel_base_code(); } } /** * Prints the base