Install Facebook Pixel Conversion Tracking Without Using a Plugin – WordPress
It’s important to know our target audience, the time they spent on our site, the top term your user landed on your site as well knowing what they actually do, clicking on our site, in that, we can provide a much more user experience.
Let’s get started
Add lists of actions below into your theme’s functions.php
file and you’re good to go.
Track Purchase Event
This will be use to track Purchase event.
add_action( 'woocommerce_thankyou', 'bacs_order_payment_processing_order_status', 10, 1 );
/**
* Triggers Purchase event
*/
function bacs_order_payment_processing_order_status( $order_id ) {
if ( ! $order_id ) {
return;
}
$order = new WC_Order( $order_id );
// FB pixel
$pixel = $product_ids = array();
$content_type = 'product';
// Check for product order items
if( $order->get_items() ) :
foreach( $order->get_items() as $item_key => $item_values ) {
$product = wc_get_product( $item_values['product_id'] );
$product_ids[] = $product->get_id();
if ( $product->get_type() === 'variable' ) {
$content_type = 'product_group';
}
}
$pixel = array(
'content_ids' => $product_ids,
'content_type' => $content_type,
'value' => $order->get_total(),
'currency' => get_woocommerce_currency()
);
// Assuming multiple items are purchased
wc_enqueue_js(
sprintf(
"// WooCommerce Facebook Integration Event Tracking\n".
"fbq( 'track', 'Purchase', %s );",
json_encode( $pixel )
)
);
endif;
}
Track AddToCart Event
This will be use to track AddToCart event.
add_action('woocommerce_after_cart', 'rstheme_inject_add_to_cart_event' );
/**
* Triggers AddToCart for cart page and add_to_cart button clicks
*/
function rstheme_inject_add_to_cart_event() {
$product_ids = array();
$product_ids = rstheme_get_cart_ids( WC()->cart->get_cart() );
$cart_event = array(
'content_ids' => json_encode($product_ids),
'content_type' => 'product',
'value' => WC()->cart->total,
'currency' => get_woocommerce_currency()
);
wc_enqueue_js(
sprintf(
"// WooCommerce Facebook Integration Event Tracking\n".
"fbq( 'track', 'AddToCart', %s );",
json_encode( $cart_event )
)
);
}
// Get lists of cart ids
function rstheme_get_cart_ids( $cart ) {
$product_ids = array();
foreach ( $cart as $item ) {
$product_ids[] = $item['data']->get_id();
}
return $product_ids;
}
Track ViewContent Event
This will be use to track ViewContent event.
add_action( 'woocommerce_after_single_product', 'rstheme_inject_content_view_event' );
/**
* Triggers ContentView for cart page and add_to_cart button clicks
*/
function rstheme_inject_content_view_event() {
$product = wc_get_product( get_the_ID() );
$content_type = 'product';
if (! $product ) {
return;
}
// if product is a variant, fire the pixel with content_type: product_group
if ( $product->get_type() === 'variable' ) {
$content_type = 'product_group';
}
$view_event = array(
'content_name' => $product->get_title(),
'content_ids' => json_encode( $product->get_id() ),
'content_type' => $content_type,
'value' => $product->get_price(),
'currency' => get_woocommerce_currency()
);
wc_enqueue_js(
sprintf(
"// WooCommerce Facebook Integration Event Tracking\n".
"fbq( 'track', 'ViewContent', %s );",
json_encode( $view_event )
)
);
}
That’s it, happy coding ^_^