<script>
// Set the PRODUCT_NAME and dynamicValue variables using Shopify's Liquid code
{% if product.title %}
window.PRODUCT_NAME = {{ product.title | escape }};
window.dynamicValue = {{ product.price | divided_by: 100 }}; // Example: Convert price to a numeric value
{% else %}
window.PRODUCT_NAME = 'Default Product Name'; // Provide a default name
window.dynamicValue = 0; // Provide a default value
{% endif %}
</script>
<script>
window.addEventListener('load', function(event) {
var addToCartButtons = document.querySelectorAll('.add-to-cart-button'); // Replace with your actual selector
addToCartButtons.forEach(function(button) {
button.addEventListener('click', function(clickEvent) {
var dynamicProductName = window.PRODUCT_NAME;
var eventName = 'added to cart: ' + dynamicProductName;
// Calculate the dynamic value based on your logic
var dynamicValue = window.dynamicValue;
// Track the event with the dynamic product name and value
fathom.trackEvent(eventName, {
_value: dynamicValue
});
});
});
});
</script>