Skip to content

Instantly share code, notes, and snippets.

@acanza
Last active January 29, 2018 10:01
Show Gist options
  • Select an option

  • Save acanza/fc2f72e8da37564694bf2ffcf8435ef3 to your computer and use it in GitHub Desktop.

Select an option

Save acanza/fc2f72e8da37564694bf2ffcf8435ef3 to your computer and use it in GitHub Desktop.
Inserta el pixel de Facebook + evento de conversión de compra
// Inserta el pixel de Facebook en todas las páginas y añade el evento de conversión de compra para medir las compras finalizadas
if ( in_array( 'woocommerce/woocommerce.php', get_option( 'active_plugins' ) ) && version_compare( WC()->version , '3.0.0', '>' ) ){
add_action( 'wp_head', 'fb_pixel_purchases_traking' );
function fb_pixel_purchases_traking() {
$fb_pixel_id = '<FB_PIXEL_ID>'; // Debes reemplazar el texto <FB_PIXEL_ID> por tu código identificador del pixel manteniendo las comillas
ob_start()
?>
<!-- Start FB Tracking -->
<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', <?php echo '\''. $fb_pixel_id .'\'';?>);
fbq('track', 'PageView');
</script>
<noscript>
<img height="1" width="1" border="0" alt="" style="display:none" src="https://www.facebook.com/tr?id=<?php echo $fb_pixel_id;?>&amp;ev=PageView&amp;noscript=1" />
</noscript>
<!-- END FB Tracking -->
<?php
if ( is_wc_endpoint_url( get_option( 'woocommerce_checkout_order_received_endpoint' ) ) && isset( $_GET[ 'key' ] ) ) {
$order = new WC_Order( wc_get_order_id_by_order_key( $_GET[ 'key' ] ) );
$order_total = $order->get_total();
?>
<script>
fbq('track', 'Purchase', {
value: <?php echo '\''. $order_total .'\''; ?>,
currency: <?php echo '\''. get_woocommerce_currency() .'\''; ?>
});
</script>
<?php
}
$fb_pixel_script = ob_get_contents();
ob_end_clean();
echo $fb_pixel_script;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment