JSFiddle - React, Tailwind, and code Playground

by joshuatz

JavaScript

(function($){
	$(document).ready(function(){
		$("a[href*='/cart/add']").on("click",function(evt){
			var linkToFollow = evt.currentTarget.href;
			var productId = /[?&]id=(\d+)/.exec(linkToFollow)[1];
      var contentType = 'product';
      if (/[?&]productid=(\d+)/.test(linkToFollow)){
      	productId = /[?&]productid=(\d+)/.exec(linkToFollow)[1];
        contentType = 'product_group';
      }
			var productPrice = $(evt.currentTarget).closest("div[data-title*='column'][class*='col']").find("div.elHeadline").filter(function(){
				return (/discount/i.test(this.innerText) !== true);
			}).find("div:contains('$')")[0].innerText.replace("$","");

			// Stop immediate link follow
			evt.preventDefault();

			// Fallback in case firing fails
			setTimeout(function(){
				document.location.href = linkToFollow;
			},1000);

			// Fire FB pixel, add to cart, with product ID
			fbq('trackSingle', '471660933166709', 'AddToCart', {
				value: productPrice,
				currency: 'USD',
				content_ids: [productId],
				content_type: contentType
			});

			// Follow link now that FB has fired
			document.location.href = linkToFollow;
		});
	});
})(jQuery);