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
- JavaScript
- cURL
- Python
- PHP
<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>
curl -X POST https://api.edrone.me/trace \
-d "app_id=YOUR_APP_ID" \
-d "action_type=add_to_cart" \
-d "product_ids=12345" \
-d "product_titles=Nike%20Air%20Max%2090" \
-d "product_images=https%3A%2F%2Fyourstore.com%2Fimages%2Fnike-air-max.jpg" \
-d "product_urls=https%3A%2F%2Fyourstore.com%2Fproducts%2Fnike-air-max-90" \
-d "product_category_ids=1~2~3" \
-d "product_category_names=Shoes~Sneakers~Nike" \
-d "sender_type=server"
import requests
from urllib.parse import quote
response = requests.post('https://api.edrone.me/trace', data={
'app_id': 'YOUR_APP_ID',
'action_type': 'add_to_cart',
'product_ids': '12345',
'product_titles': quote('Nike Air Max 90'),
'product_images': quote('https://yourstore.com/images/nike-air-max.jpg'),
'product_urls': quote('https://yourstore.com/products/nike-air-max-90'),
'product_category_ids': '1~2~3',
'product_category_names': quote('Shoes~Sneakers~Nike'),
'sender_type': 'server'
})
<?php
$data = http_build_query([
'app_id' => 'YOUR_APP_ID',
'action_type' => 'add_to_cart',
'product_ids' => '12345',
'product_titles' => 'Nike Air Max 90',
'product_images' => 'https://yourstore.com/images/nike-air-max.jpg',
'product_urls' => 'https://yourstore.com/products/nike-air-max-90',
'product_category_ids' => '1~2~3',
'product_category_names' => 'Shoes~Sneakers~Nike',
'sender_type' => 'server'
]);
$ch = curl_init('https://api.edrone.me/trace');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
Required fields
| Field | Description |
|---|---|
action_type | add_to_cart |
product_ids | Product ID being added |
product_titles | Product name |
product_images | Product image URL |
product_urls | Product page URL |
product_category_ids | Category IDs |
product_category_names | Category 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();
});
});