This article is a how-to disable Facebook Pixel tracking on WordPress based on user role. This is not built in functionality for the Facebook for WordPress plugin, so if youâre like me, youâre struggling to find a solution to enable Pixel tracking for some users and disable it for others.
My application is for a client, On The Rox Fabrication, which is an ecommerce store (WooCommerce) selling parts for the Mahindra Roxor. I was building Facebook ad campaigns for them, but our tracking was getting skewed because of âDealersâ, re-sellers of On The Rox parts that use the website to place their orders.
If a Dealer had seen an ad, and then went in to place an order, that was being tracked by Facebook. This is a problem because Dealers arenât making purchases based on our ads â theyâre just ordering what their customers have ordered. Basically, if a Dealer sees an ad, it has no bearing on whether or not theyâre going to make a purchase.
It became clear pretty quickly this was going to skew our ad campaign metrics, as the users operating the Dealer accounts were putting in big orders and had also seen some of our Facebook ads. We were seeing absurd ROAs that were not representative of how our campaign was actually working.
So, I set out to find a way to disable the Pixel tracking for anyone on the site with the âDealerâ role. Note that this method will work for any role, itâs just that âDealerâ is the role I was specifically targeting in this case.
I also disabled tracking for the âAdministratorâ role because sometimes I would fill up my cart with products to test shipping rates, for example, and that was also skewing Facebookâs metrics. This method will show you how to disable Facebook Pixel tracking on WordPress (WooCommerce) for as many roles as you want.
Iâll also note that this isnât a âbest practicesâ solution. This is a quick fix to the problem that can easily be replicated when the Facebook for WooCommerce plugin gets updated and overwrites the custom code.
Okay, so how do we do this?
Install Facebook for WooCommerce
If you havenât done this already, youâll need to install the plugin âFacebook for WooCommerceâ.
Access the Installed Plugin Files
Youâll need a file manager to access the installed plugin files. (FileZilla works well and is free.) Youâll also need a text editor. Notepad will work fine, but I can also recommend Notepad++.
Next, you will need to navigate to your WordPress installation directory, then:
wp-content -> plugins -> facebook-for-woocommerce
Thatâs the directory for the Facebook for WooCommerce plugin. In there youâll find a bunch of files. The one you want to open and edit is called facebook-commerce-events-tracker.php
In that file, near the top between the comments and first if statement, youâll copy in a block of code:
//limit facebook pixel based on user role (dealer, admin) $GLOBALS['enable_FB_tracking'] = true; if(function_exists('wp_get_current_user')) { $current_user = wp_get_current_user(); if(in_array('administrator',(array)$current_user->roles)) { $GLOBALS['enable_FB_tracking'] = false; //echo ""; } if(in_array('dealer',(array)$current_user->roles)) { $GLOBALS['enable_FB_tracking'] = false; //echo ""; } }
What weâre doing here is setting a global variable to enable or disable tracking of commerce events. By default it will be set to true and operate as normal, and if the user role matches âdealerâ or âadministratorâ we will switch the global variable to false.
Further down in the __construct() function we will enclose the function in an if statement based on our global variable.
public function __construct( $user_info ) { $this->pixel = new WC_Facebookcommerce_Pixel( $user_info ); $enable_FB_tracking = $GLOBALS['enable_FB_tracking']; if($enable_FB_tracking) { //Unedited original code, encapsuled in our if statement add_action( 'wp_head', array( $this, 'apply_filters' ) ); // Pixel Tracking Hooks add_action( 'wp_head', array( $this, 'inject_base_pixel' ) ); add_action( 'wp_footer', array( $this, 'inject_base_pixel_noscript' ) ); // ViewContent for individual products add_action( 'woocommerce_after_single_product', [ $this, 'inject_view_content_event' ] ); add_action( 'woocommerce_after_shop_loop', array( $this, 'inject_view_category_event' ) ); add_action( 'pre_get_posts', array( $this, 'inject_search_event' ) ); // 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_thankyou', [ $this, 'inject_purchase_event' ], 40 ); add_action( 'woocommerce_payment_complete', [ $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' ], self::FB_PRIORITY_LOW ); } }
Basically, only if the variable is set to true will any of the commerce tracking stuff happen.
Testing
To test your changes and make sure they are working correctly, right click on the open webpage of your site and click âview source.â Then use Ctrl+F to search for âfbqâ which is part of the Facebook Pixel tracking script. If it doesnât show up anywhere, and youâre logged in with a user role that should be disabling it, itâs working!
Youâll want to test logged out as well to ensure that it is still tracking properly for all other user roles. That process is the same, search for âfbqâ in the page source and you should see it throughout the page.
Download
You can also download my edited file and make simpler changes yourself. Simply change the user role name from âdealerâ to whatever role you would like to disable Facebook Pixel tracking for.
- Facebook for WooCommerce 1.11.3 â facebook-commerce-events-tracker.txt
- Facebook for WooCommerce 1.11.1 â facebook-commerce-events-tracker.txt
Simply edit this file and then re-upload to your plugin installation directory, then test to make sure it works properly.
All Done
Thatâs it! Once youâve made those changes to the file and uploaded them to the server, the Facebook Pixel will stop tracking ecommerce events for the specified user roles. To be clear, the user has to be logged into their account, and that account has to be tagged with the correct role for this to all work properly.
Itâs also worth noting that when you update the plugin, it will overwrite these custom changes. That means youâll have to re-change the file. However, after the first time that should become very easy.
Hopefully someday the Facebook for WooCommerce plugin will come with a built-in user role option. For now, this workaround gets the job done. I couldnât find anyone else who had this problem or had done this, so I decided to write this article to share with anyone else having the same issue.
If youâre having trouble or donât feel comfortable editing the code yourself, feel free to contact us and I can personally set this up for you on your WooCommerce site. To do that, request a quote, email me at [email protected] or give us a call at (406) 500-6338.