How We Track Client Profitability Using Facebook Pixel – And How You Can Too

Posted Friday, October 18, 2024

By: Mike Hudgins

How We Track Client Profitability Using Facebook Pixel – And How You Can Too

How We Track Client Profitability Using Facebook Pixel – And How You Can Too

At Impakt Results, our success is tied directly to our clients' profits. It’s not just a catchy phrase—our entire business model depends on making sure our clients are seeing positive returns on their investment. To ensure this, we track profitability on each transaction in real-time using a custom event on Shopify's checkout page.

Here’s how we approached this solution, and how you can apply it to your own business for better insights into your profit margins.

Why Profit Tracking Matters for Business

Most businesses focus on tracking revenue—knowing how much you made is essential. But revenue doesn’t tell the whole story. Profitability is what drives long-term success, and if you're not tracking how much profit you're making, you're flying blind.

In our case, because our business model relies on our clients making money, we needed to track our profit margins on every sale. If our clients aren't seeing positive returns, we aren't truly succeeding. That's why we developed a unique solution: tracking profit via the Facebook Pixel.

The Problem

Facebook Ads Manager gives you tools to track purchases, but those standard events (like the "Purchase" event) only tell you the total revenue from a sale. They don’t give you insight into your actual profit. That's where our challenge began—we needed a way to calculate and track profitability, not just revenue, with each sale.

With Shopify, we had access to cost data on each product (via custom properties), but there was no direct way to pass this profit calculation to Facebook’s Pixel using a standard event. That’s when we got creative.

The Solution: Profit Tracking with a Custom Event

We knew we could use a custom Facebook Pixel event to track profitability directly from the checkout page using the legacy checkout settings area. By adding a small amount of JavaScript to this area, we could calculate the total profit of each sale and send that data back to Facebook using the Pixel.

Here’s the code we used:

<p><p><p><p><p dir="ltr">{% if first_time_accessed %}</p></p></p></p></p> <p><p><p><p><p dir="ltr"><script></p></p></p></p></p> <p><p><p><p><p dir="ltr">!function(f,b,e,v,n,t,s)</p></p></p></p></p> <p><p><p><p><p dir="ltr">{if(f.fbq)return;n=f.fbq=function(){n.callMethod?</p></p></p></p></p> <p><p><p><p><p dir="ltr">n.callMethod.apply(n,arguments):n.queue.push(arguments)};</p></p></p></p></p> <p><p><p><p><p dir="ltr">if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';</p></p></p></p></p> <p><p><p><p><p dir="ltr">n.queue=[];t=b.createElement(e);t.async=!0;</p></p></p></p></p> <p><p><p><p><p dir="ltr">t.src=v;s=b.getElementsByTagName(e)[0];</p></p></p></p></p> <p><p><p><p><p dir="ltr">s.parentNode.insertBefore(t,s)}(window, document,'script',</p></p></p></p></p> <p><p><p><p><p dir="ltr">'<a href="https://connect.facebook.net/en_US/fbevents.js">https://connect.facebook.net/e...</a>');</p></p></p></p></p> <p><p><p><p><p dir="ltr">fbq('init', 'YOUR_PIXEL_ID_HERE');</p></p></p></p></p> <p><p><p><p><p dir="ltr">window.addEventListener("load", () => {</p></p></p></p></p> <p><p><p><p><p dir="ltr"> const lineItems = Shopify.checkout.line_items;</p></p></p></p></p> <p><p><p><p><p dir="ltr"> const subtotal = Shopify.checkout.total_price;</p></p></p></p></p> <p><p><p><p><p dir="ltr"> </p></p></p></p></p> <p><p><p><p><p dir="ltr"> let totalCost = 0;</p></p></p></p></p> <p><p><p><p><p dir="ltr"> // Loop through all line items</p></p></p></p></p> <p><p><p><p><p dir="ltr"> lineItems.forEach(item => {</p></p></p></p></p> <p><p><p><p><p dir="ltr"> const itemCost = parseFloat(item.properties.fbpf) || 0; // Assuming 'fbpf' is the cost property</p></p></p></p></p> <p><p><p><p><p dir="ltr"> const quantity = item.quantity;</p></p></p></p></p> <p><p><p><p><p dir="ltr"> totalCost += itemCost * quantity;</p></p></p></p></p> <p><p><p><p><p dir="ltr"> });</p></p></p></p></p> <p><p><p><p><p dir="ltr"> // Calculate the total profit, assuming 10% for overhead costs</p></p></p></p></p> <p><p><p><p><p dir="ltr"> const totalProfit = (subtotal - totalCost - (subtotal/10)).toFixed(2);</p></p></p></p></p> <p><p><p><p><p dir="ltr"> // Send the profit data to Facebook</p></p></p></p></p> <p><p><p><p><p dir="ltr"> fbq('trackCustom', 'custom_profit', {</p></p></p></p></p> <p><p><p><p><p dir="ltr"> value: parseFloat(totalProfit) </p></p></p></p></p> <p><p><p><p><p dir="ltr"> });</p></p></p></p></p> <p><p><p><p><p dir="ltr"> </p></p></p></p></p> <p><p><p><p><p dir="ltr">});</p></p></p></p></p> <p><p><p><p><p dir="ltr"></script></p></p></p></p></p> <p><p><p><p><p dir="ltr">{% endif %}</p></p></p></p></p>


Breaking Down the Code

Let’s take a closer look at how this solution works:

  1. Initialize the Facebook Pixel: We start by initializing the Facebook Pixel in the same way you would for any standard pixel setup.

  2. Listening for Page Load: We use JavaScript's window.addEventListener to wait for the checkout page to fully load before executing the rest of the code.

  3. Accessing the Shopify Checkout Data: Using Shopify.checkout.line_items, we grab the details of the products the customer has purchased. This includes the quantity and a custom property (fbpf) that we use to store the cost of each item.

  4. Calculating the Total Cost: We loop through all the line items, multiplying the cost of each item by the quantity purchased, and sum up the total cost of goods sold (COGS).

  5. Profit Calculation: We then subtract the total cost of the items from the subtotal to calculate the profit. Additionally, we subtract 10% from the subtotal to account for overhead (this can be customized based on your own business model).

  6. Tracking the Custom Event: Finally, we send the profit value as a custom event (custom_profit) to Facebook using the fbq('trackCustom') method.

Why This Is a Game-Changer for Profitability Tracking

By using this method, we aren’t just guessing if we’re profitable on ad spend—we know. This gives us the ability to optimize our campaigns not just based on revenue, but on the actual profit margin we’re generating from each sale.

This kind of insight is invaluable for scaling campaigns. We can now track whether certain products or customer segments are more profitable, allowing us to allocate our ad spend where it’s truly driving value.

How You Can Implement This in Your Store

If you’re running a Shopify store, you can easily implement this profit tracking system yourself by following these steps:

  1. Store Product Costs in a Custom Property: You’ll need to store the cost of goods sold (COGS) in a custom product property like fbpf (or any other key of your choice). This can be done when adding products to your store. You can use any Metafield app to do this.

  2. Add the Custom Code to Your Checkout Settings: Shopify allows you to add custom JavaScript to your checkout process. You can use the code I provided above, making sure to replace YOUR_PIXEL_ID_HERE with your actual Facebook Pixel ID.

  3. Test the Pixel Firing: Use the Facebook Pixel Helper Chrome extension to verify that your custom_profit event is firing correctly on the order confirmation page. To test this, temporarily remove the {% if first_time_accessed %} and {% endif %}. Once you do that, you can click on any order and click “more” -> “view order status page”

  4. Track and Optimize Your Campaigns: Once this custom event is being tracked in Facebook Ads Manager, you can start using it to optimize your campaigns for profit, not just revenue. This is where the real magic happens—allocating ad spend where it’s most profitable, based on real-time data. The custom pixel event currently will not show up in ads manager unless your campaign is optimized for the custom event.

Conclusion

At Impakt Resultsdfg, we don’t succeed unless our clients succeed. Our business is built around making sure our clients are profitable, and this custom profit-tracking solution is a powerful example of how we ensure that.

By implementing custom profit tracking, you're not just looking at how much money is coming in—you're making sure that you're growing your business in a sustainable and profitable way. If you’re ready to take your profit tracking to the next level, this solution is an excellent way to start!



Ready to become a conversion machine?

Book a free call