How To Disable Facebook for WooCommerce Pixel Tracking Based on WordPress User Role

Article Image

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.

Screenshot of View Source and Search fbq

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.

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 alex@yellowstonedigitalmedia.com or give us a call at (406) 616-3250.