Skip to main content

Add to Cart

Track when users add products to their shopping cart. This enables abandoned cart campaigns.

When to use​

Trigger this event when the user clicks an Add to Cart button.

Implementation​

<script type="text/javascript">
// First, ensure product data is set (usually from product_view)
window._edrone = window._edrone || {};
_edrone.product_ids = '12345';
_edrone.product_titles = encodeURIComponent('Nike Air Max 90');
_edrone.product_images = encodeURIComponent('https://yourstore.com/images/nike-air-max.jpg');
_edrone.product_urls = encodeURIComponent('https://yourstore.com/products/nike-air-max-90');
_edrone.product_category_ids = '1~2~3';
_edrone.product_category_names = encodeURIComponent('Shoes~Sneakers~Nike');

// Attach to add-to-cart button
const addToCartButton = document.querySelector('#add-to-cart');
if (addToCartButton) {
addToCartButton.addEventListener('click', function() {
_edrone.action_type = 'add_to_cart';
_edrone.init();
});
}
</script>

Required fields​

FieldDescription
action_typeadd_to_cart
product_idsProduct ID being added
product_titlesProduct name
product_imagesProduct image URL
product_urlsProduct page URL
product_category_idsCategory IDs
product_category_namesCategory names
Re-using product data

If you've already set product data for product_view on the same page, you only need to change the action_type and call _edrone.init().

What does _edrone.init() do?

The _edrone.init() function sends all the data stored in the _edrone object to the Trace API. Call it after setting all required fields. On page load, it's called automatically by the edrone script - you only need to call it manually for user-triggered events like add-to-cart or subscribe.

Multiple add-to-cart buttons​

For pages with multiple products (like category pages):

document.querySelectorAll('.add-to-cart-btn').forEach(function(button) {
button.addEventListener('click', function() {
const productData = JSON.parse(this.dataset.product);

_edrone.action_type = 'add_to_cart';
_edrone.product_ids = productData.id;
_edrone.product_titles = encodeURIComponent(productData.title);
_edrone.product_images = encodeURIComponent(productData.image);
_edrone.product_urls = encodeURIComponent(productData.url);
_edrone.product_category_ids = productData.categoryIds;
_edrone.product_category_names = encodeURIComponent(productData.categoryNames);
_edrone.init();
});
});