Skip to content

Instantly share code, notes, and snippets.

@tienhieuD
Last active April 18, 2019 04:21
Show Gist options
  • Save tienhieuD/61cb55617a94fd9d6070d4685dbc61c8 to your computer and use it in GitHub Desktop.
Save tienhieuD/61cb55617a94fd9d6070d4685dbc61c8 to your computer and use it in GitHub Desktop.

Add to Cart

<script>
$('.product-action-cart, .product__add-to-cart').on('click', function() {
	var product_id = $(this).data('product-id');
    fbq('track', 'AddToCart', {
		content_ids: [product_id.toString()],
        content_type: 'product',
      	// product_catalog_id: '1',
	});  
});
</script>

Initiate Checkout

<script>
  
if (window.location.href.includes('/shop/payment')) {

    var product_ids = [];
    $(".product-name.col-md-9").each(function (index, elem) {
        var id = $(elem).data("product-id");
        if (!!id) {
            product_ids.push(id.toString());
        }
    });

    var price_total = $('span[data-oe-expression="order_total_price"] .oe_currency_value').text();
	price_total = price_total.replace(/\./gi, "")

    fbq('track', 'InitiateCheckout', {
        content_ids: product_ids,
        content_type: 'product',
        value: price_total,
        currency: 'VND'
    });

};
  
</script>

Purchase

<script>
if (window.location.href.includes('/shop/payment')) {
    $('.cart__total-checkout-btn').one('click', function (e) {
        e.preventDefault();

        var product_ids = [];
        $(".product-name.col-md-9").each(function (index, elem) {
            var id = $(elem).data("product-id");
            if (!!id) {
                product_ids.push(id.toString());
            }
        });

        var price_total = $('.cart__total-price span[data-oe-expression="order_total_price"] .oe_currency_value').text();
        price_total = parseFloat(price_total.replace(",", ""));

        fbq('track', 'Purchase', {
            content_ids: product_ids,
            content_type: 'product',
            value: price_total || 0.0,
            currency: 'VND'
        });

        $(this).click();



    });
};
</script>

View Content

<script>
  var product_ids = [];
  
  $(".product-name").each(function(index, elem) {
    var id = $(elem).data("product-id");
    product_ids.push(id.toString());
  });
  
  product_ids = $.unique(product_ids);
  
  fbq('track', 'ViewContent', {      
    content_type : "product",
    content_ids : product_ids,
  });
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment