0

I'm wondering what the standard way is to avoid executing some code when a user refreshes the page. We have a unique ID (order number) so we can keep it server side, there's no need to set a cookie.

On our WooCommerce 'Thank You' page we call the Facebook Pixel 'Purchase' event. However some mobile device users are opening their browser the next day and the page is requested again which adds a duplicate pixel event, skewing our data.

I experimented with transients, but the below code resulted in no events firing. I installed the Transient Manager plugin and cannot see anything set, but I understand WPEngine does have some customisations which may be changing how things work though I'm not across the full details.

<?php 
$transient_id = 'fbpixel-' . $order->get_order_number();
if ( $order->get_subtotal() > 0 && ( false === ( $pixel_fired = get_transient( $transient_id ) ) ) ) : 
    set_transient( $transient_id, $order->get_order_number(), 3 * DAY_IN_SECONDS );
?>
<script>
    fbq('track', 'Purchase', {currency: "AUD", value: <?php echo ($order->get_total() - $order->get_total_shipping()); ?> });
</script>
<?php endif; ?>

My understanding also is that transients may be deleted at any time for no reason even before the expiration time, so they may not be an appropriate solution.

1

You can use order post meta to check order already there or not. try below code. let me know if this helps you.

$order_id = $order->get_id();
$fbpixel = get_post_meta($order_id, 'fbpixel-'.$order->get_order_number(),true);
if ( $order->get_subtotal() > 0 && ( empty( $fbpixel ) ) ) : 
  update_post_meta($order_id, 'fbpixel-'.$order->get_order_number(),1);
  ?>
  <script>
      fbq('track', 'Purchase', {currency: "AUD", value: <?php echo ($order->get_total() - $order->get_total_shipping()); ?> });
  </script>
<?php endif;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.