Skip to content

Commit

Permalink
Add event tracking for AddToCart
Browse files Browse the repository at this point in the history
  • Loading branch information
Antti Kuosmanen committed Nov 30, 2015
1 parent 6803e78 commit 7eb53d7
Showing 1 changed file with 48 additions and 3 deletions.
51 changes: 48 additions & 3 deletions inc/class-wc-integration-fb-conversion-tracking.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php


class WC_Integration_Facebook_Conversion_Tracking extends WC_Integration {
/**
* Constructor, sets up all the actions
*/
public function __construct() {
$this->id = 'wc-fb-conversion-tracking';
$this->method_title = __( 'Facebook', 'wc-fb-conversion-tracking' );
Expand All @@ -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' ) );

}

/**
Expand All @@ -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
*/
Expand All @@ -64,6 +90,25 @@ public function fb_tracking_pixel() {
src="https://www.facebook.com/tr?id=<?php echo esc_html( $this->fbid ); ?>&ev=PageView&noscript=1"
/></noscript>
<!-- End Facebook Pixel Code -->
<?php
}

/**
* Output inline javascript to bind an fbq event to the $.click() event of a selector
*/
public function fb_track_event( $name, $selector, $params = array() ) {
if( !$this->fbid ) {
return;
}
?>
<script>
(function($) {
$('<?php echo esc_js( $selector ); ?>').click(function() {
fbq('track', '<?php echo esc_js( $name ); ?>');
});
console.log('Facebook Tracking Enabled for:', $('<?php echo esc_js( $selector ); ?>'));
})(jQuery);
</script>
<?php
}
}
Expand Down

0 comments on commit 7eb53d7

Please sign in to comment.