gtm4wp icon indicating copy to clipboard operation
gtm4wp copied to clipboard

gtm4wp.addProductToCartEEC not firing on Wordpress theme

Open engineerized opened this issue 3 years ago • 2 comments

The add to cart event for UA/GA4 won't fire on Wordpress theme twenty twenty one. I have tested other themes and it is the same thing.

The add to cart event fires on product lists but not on the product detail pages.

The code from PDP page looks like this:

<form class="cart" action="/" method="post" enctype="multipart/form-data">
		
			<div class="quantity">
				<label class="screen-reader-text" for="quantity_615b69b500c71">Design Classic quantity</label>
		<input type="number" id="quantity_615b69b500c71" class="input-text qty text" step="1" min="1" max="" name="quantity" value="1" title="Qty" size="4" placeholder="" inputmode="numeric">
			</div>
	
		<button type="submit" name="add-to-cart" value="135" class="single_add_to_cart_button button alt">Add to cart</button>

</form>

And it should work with the following code that is in the plugin:

	// track add to cart events for products on product detail pages
	jQuery( document ).on( 'click', '.single_add_to_cart_button:not(.disabled)', function() {
		var product_form       = jQuery( this ).closest( 'form.cart' );

I added alert('added_to_cart'); just to follow up if the button is working.

Is there any other way to make this work? Thank you in advance.

engineerized avatar Oct 04 '21 20:10 engineerized

Maybe related: https://github.com/duracelltomi/gtm4wp/issues/166

luukskeur avatar Oct 05 '21 12:10 luukskeur

Maybe related: #166

Looked at it but it did not help unfortunately. I tried following just to test;

<script type='text/javascript'>
        jQuery(function($){ $('form.cart').on( 'click', ".single_add_to_cart_button", function(event){
                
                event.preventDefault(); // Disable for testing

                console.log("click");
                alert("click");
            });
        });
</script>

I got the alert to fire on the add to cart button with this code. Somehow the code for plugin do not find the add to cart button with this class;

jQuery( document ).on( 'click', '.single_add_to_cart_button:not(.disabled)', function() {
		var product_form       = jQuery( this ).closest( 'form.cart' );

engineerized avatar Oct 05 '21 13:10 engineerized