Skip to content

Instantly share code, notes, and snippets.

@ceaksan
Last active January 3, 2025 10:14
Show Gist options
  • Save ceaksan/f5862c7127df14d19485d8f83a961877 to your computer and use it in GitHub Desktop.
Save ceaksan/f5862c7127df14d19485d8f83a961877 to your computer and use it in GitHub Desktop.
This code prevents unrealistic quantity values (e.g., thousands or millions) in GA4 reports by dynamically setting min and max limits for product quantity fields. Using Liquid, it assigns values from quantity_rule or defaults to 1 and 10, ensuring valid inputs and maintainable logic.

If you notice unusually large quantity values (e.g., thousands or millions) in item details within your GA4 reports, it might be caused by the lack of a maximum quantity limit. Visitors can manipulate this to trigger exaggerated “Add to Cart” events, distorting your analytics data. To prevent such issues, you can set minimum and maximum quantity limits in your input fields.

This implementation ensures valid input values for product quantity fields in an e-commerce environment by dynamically assigning min and max values using Liquid. The limits are derived from the product’s quantity_rule or default to 1 and 10, respectively, if no rules are defined. By using Liquid’s assign, the logic remains clean, reusable, and maintainable.

Below is an example implementation:

{% assign min_quantity = product.selected_or_first_available_variant.quantity_rule.min | default: 1 %}
{% assign max_quantity = product.selected_or_first_available_variant.quantity_rule.max | default: 10 %}

<input
  class="quantity__input"
  type="number"
  name="quantity"
  id="Quantity-{{ section.id }}"
  data-cart-quantity="{{ cart_qty }}"
  data-min="{{ min_quantity }}"
  min="{{ min_quantity }}"
  data-max="{{ max_quantity }}"
  max="{{ max_quantity }}"
  step="{{ product.selected_or_first_available_variant.quantity_rule.increment | default: 1 }}"
  value="{{ min_quantity }}"
  form="{{ product_form_id }}"
>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment