Untitled

 avatar
unknown
plain_text
13 days ago
819 B
7
Indexable
document.querySelectorAll('.add_to_cart_button, .single_add_to_cart_button').forEach(function(button) {
  button.addEventListener('click', function(event) {
    let productId = this.getAttribute('data-product_id');
    if (!productId) {
      const productInput = document.querySelector('input[name="product_id"]');
      if (productInput) {
        productId = productInput.value;
      }
    }
    let productName = '';
    const ariaLabel = this.getAttribute('aria-label');
    if (ariaLabel) {
      productName = ariaLabel.replace('Dodaj do koszyka: ', '').replace(/["„”]/g, '');
    }
    dataLayer.push({
      'event': 'add_to_cart',
      'ecommerce': {
        'add': {
          'products': [{
            'id': productId,
            'name': productName
          }]
        }
      }
    });
  });
});
Editor is loading...
Leave a Comment