Search code examples
javascriptfacebookanalyticsconversion-tracking

How to dynamically update order value of facebook pixel


Facebook states the following: If you have multiple values that you want to track, you can also dynamically update this value by using a server-side variable (ex: you can insert your shopping cart value into the value field of the pixel that uses the checkout pixel).

I am using the following code to track purchases on my order confirmation page:

     <!-- Facebook Pixel Code -->
<script>
!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
document,'script','//connect.facebook.net/en_US/fbevents.js');

fbq('init', '314191338731885');
fbq('track', "PageView");
fbq('track', 'Purchase', {value: '0.00', currency: 'USD'});
</script>
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=314191338731885&ev=PageView&noscript=1"
/></noscript>
    <!-- End Facebook Pixel Code -->  

What does it mean when they say

By using a server-side variable (ex: you can insert your shopping cart value into the value field of the pixel that uses the checkout pixel).


Solution

  • It means use your server side code (PHP, .NET, Java, etc) to modify the HTML/JS sent to the client to include the purchase value you want logged in the

    I.e instead of just outputting fbq('track', 'Purchase', {value: '0.00', currency: 'USD'});

    Set the value based on some information your server side code has access to (for example, the actual purchase value of the order which was jus tplaced)