当产品被添加到购物车时,WooCommerce添加facebook像素

时间:2019-07-17 09:31:38

标签: ajax events woocommerce cart facebook-pixel

我已将我的facebook像素添加到我的网上商店,如下所示:

function facebookpixel(){
?>
<script>
(function($) {

<!-- Begin Facebook Pixel Code -->

!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',
'https://connect.facebook.net/en_US/fbevents.js');
fbq('init', ‘PIXELCODE’); 
fbq('track', 'PageView');

<?php if (is_page (3548) ) { ?>
        fbq('track', 'Purchase', {
            value: '0,00',
            currency: 'EUR'
        });
<?php } ?>

<?php if (is_single (array(1377, 1375, 1376, 1374, 1373, 1372, 1371) ) ) { ?>
        fbq('track', 'ViewContent');
<?php } ?>

})(jQuery);

</script>

<noscript>
    <img height="1" width="1" src="https://www.facebook.com/tr?id=PIXELCODE&ev=PageView&noscript=1" />
</noscript>

<!-- End Facebook Pixel Code -->

<?php
}
//Load script in head
add_action('wp_head', 'facebookpixel');

到目前为止一切正常,但是我正在AJAX中添加到WooCommerce中的购物车。将产品fbq('track', 'AddToCart')添加到购物车时,如何仅添加AJAX

基本解决方案:

我发现了如何做到这一点:

$(document.body).on('added_to_cart', function() {
   fbq('track', 'AddToCart');
   console.log('EVENT: added_to_cart');
});

此功能确实需要包装在

$(document).ready(function() {
});

0 个答案:

没有答案