diff --git a/inc/class-wc-integration-fb-conversion-tracking.php b/inc/class-wc-integration-fb-conversion-tracking.php index f46de90..80159c1 100644 --- a/inc/class-wc-integration-fb-conversion-tracking.php +++ b/inc/class-wc-integration-fb-conversion-tracking.php @@ -1,7 +1,9 @@ id = 'wc-fb-conversion-tracking'; $this->method_title = __( 'Facebook', 'wc-fb-conversion-tracking' ); @@ -13,12 +15,18 @@ public function __construct() { // load our preset tracking id from options $this->fbid = $this->get_option( 'fbid', false ); + $this->event_addtocart = $this->get_option( 'fb_event_addtocart', true ); // add WooCommerce settings tab page - add_action( 'woocommerce_update_options_integration_' . $this->id, array( $this, 'process_admin_options' ) ); + add_action( 'woocommerce_update_options_integration_' . $this->id, array( &$this, 'process_admin_options' ) ); // add the tracking pixel to all pages in the frontend - add_action( 'wp_head', array( $this, 'fb_tracking_pixel') ); + add_action( 'wp_head', array( &$this, 'fb_tracking_pixel') ); + + // add to cart event + add_action( 'woocommerce_after_add_to_cart_button', array( &$this, 'add_to_cart' ) ); + add_action( 'woocommerce_pagination', array( &$this, 'loop_add_to_cart' ) ); + } /** @@ -41,6 +49,24 @@ public function init_form_fields() { )); } + /** + * Event tracking for product page add to cart + */ + public function add_to_cart() { + if( $this->event_addtocart ) { + $this->fb_track_event( 'AddToCart', '.button.alt' ); + } + } + + /** + * Event tracking for loop add to cart + */ + public function loop_add_to_cart() { + if( $this->event_addtocart ) { + $this->fb_track_event( 'AddToCart', '.button.add_to_cart_button' ); + } + } + /** * Print the tracking pixel to wp_head */ @@ -64,6 +90,25 @@ public function fb_tracking_pixel() { src="https://www.facebook.com/tr?id=fbid ); ?>&ev=PageView&noscript=1" /> +fbid ) { + return; + } +?> +