The addToCart command
Report each product a shopper adds to the cart on your own pages with iadvize('addToCart', { item_id, quantity, price, currency }), on any platform. The fields, how the amount is computed, what is sent, how to check it worked, and its limits.
The agent counts the add-to-carts it makes in conversation on its own. To count the products shoppers add on your own pages too, call the tag once for each cart line they add:
iadvize('addToCart', {
item_id: 'SKU-4821',
item_variant: 'SKU-4821-M-BLUE',
quantity: 2,
price: '39.90',
currency: 'EUR',
})The keys are those of a Google Analytics 4 ecommerce item, so a store that already pushes GA4 add_to_cart events has the values at hand. With these adds, the Sales report compares what shoppers add on your pages with what the agent adds, and a holdout test can run on your site.
On Shopify, the pixel already reports these adds. A second sender for the same adds counts each one twice.
Before you start
- The install snippet runs on the pages where shoppers add to the cart. The command is part of the tag.
- Count add-to-carts on your pages is on in Site settings > Site access (on by default). See The two switches.
Add the call
Call it from the code that runs when a shopper adds a product: your add-to-cart button handler, or the place where your store pushes its GA4 add_to_cart event. Make one call per cart line: an add of three different products is three calls. For example, from a GA4 event you already build:
const event = {
currency: 'EUR',
items: [
{
item_id: 'SKU-4821',
item_variant: 'SKU-4821-M-BLUE',
price: 39.9,
quantity: 2,
},
{ item_id: 'SKU-1077', price: 12.5, quantity: 1 },
],
}
for (const item of event.items) {
iadvize('addToCart', {
item_id: item.item_id,
item_variant: item.item_variant,
quantity: item.quantity,
price: item.price,
currency: event.currency,
})
}You can call it before the tag has finished loading. Like every command, the call is queued and sent once the tag boots.
The fields
| Field | Required | What to pass |
|---|---|---|
item_id | yes | Your product id, as a string of 1 to 512 characters, not only spaces, or as a number. A number is sent as a string. |
quantity | yes | The quantity added by this action, not the quantity now in the cart. A whole number from 1 to 1000, or a string holding one (as GA4 dataLayers often carry it). |
item_variant | no | Your variant id, with the same rules as item_id. Leave it out when the product has no variant. A blank or invalid value is dropped and the add is still sent, with no variant. |
price | no | The unit price, not the line total. A number, or a string of digits with an optional decimal part after a dot ('39.90'). No comma, no currency symbol, no sign. |
currency | no | A three-letter ISO 4217 code, in any case ('EUR', 'eur'). |
eventId | no | Your own id for this add, a string of 1 to 100 characters, not only spaces. Pass it when the same add can trigger the call twice (see Duplicate calls). |
How the amount is computed
The agent computes what the add is worth as price times quantity, then rounds the result once to the currency's minor unit (cents for EUR or USD): price: '39.90' with quantity: 2 in EUR is stored as 7980.
The amount needs both price and currency. When either is missing, or the price is not a valid number, is negative or too large to store, or the currency is not a known ISO code, the add is still counted, with no amount. No warning is written for these cases.
A call the tag refuses
When item_id, quantity or eventId breaks these rules, the tag sends nothing and writes one warning in the browser console, naming the first field it refused:
[iadvize] addToCart ignored: invalid quantityThe call never throws, so a bad value never breaks your page. A bad item_variant is dropped without a warning. The tag does not check price or currency: the agent does, and a bad value only costs the amount, never the add.
Duplicate calls
Without eventId, each call is its own add: two calls for the same line count twice. With an eventId, the tag prefixes it with the shopper's session key, and the agent keeps one add per id on the site. A second call with the same eventId in the same session is dropped. Use an id that is unique for each add in a session, such as your own event id, not the product id: reusing one id for two different adds keeps only the first.
What is sent
One add to the public add-to-cart endpoint of your site, with:
item_idanditem_variant(as strings, the variant only when it is valid), andquantity,priceandcurrencyas you passed them;- the tag's session key, when the browser tab holds one;
- the fact that the tag sent it.
No time is sent: the agent dates the add when it receives it. No cart reference is sent. Nothing that identifies the shopper is sent: no name, email or address.
The tag sends it with the browser's beacon API, so the request survives a navigation right after the call, for example to the cart page. When the browser refuses the beacon, it falls back to a background request that also outlives the page.
The agent then stores the product id, the variant id when there is one, the quantity, the amount and its currency when they are valid, the time it received the add, the device type taken from the browser, the session key and the sender. An add counts as "in a session with a conversation" when the same session key started a conversation on the same site before the add (see Add-to-cart counts).
When a shopper objects
Once the shopper has objected to measurement, the command sends nothing, whether the objection was stored on an earlier page, made in the panel, or set by a privacy call. A privacy objection queued on the same page before the tag loaded stops a queued addToCart too, whatever their order.
The add is then not counted at all. That is intended: a shopper who objected is not measured.
Check that it worked
- Open a product page with your browser's developer tools on the Network tab, and add a product to the cart.
- Look for a request to
/api/public/sites/pk_site_…/cart-adds. It answers204whatever happens, so the request only proves the tag sent it. - Look at the Console for a
[iadvize] addToCart ignoredwarning. - Check that the agent kept it:
lastStorefrontAddAtin Get a site's storefront activity orget_storefront_activityshows the time of the latest add received, and the Carts page of the Sales report counts it.
A test add counts like a real one, and there is no way to delete it.
Limits
- The add goes only if the tag runs on the page. A script blocker, a consent tool that holds the tag, or a page that does not load the snippet means the add is not counted.
- It runs in the shopper's browser, so it can be forged. Anyone can call it, or post to the endpoint, with your public site key. A forged add changes only your own figures, and the amounts are declared by your page, not checked against your catalog.
- The endpoint is rate limited per IP address and per organization. Adds over the limit are dropped.
- The amounts are not revenue. They are the value of what was added to carts. Orders are reported separately, with the transaction command.
The transaction command
Report each completed order from your confirmation page with iadvize('transaction', { orderRef, amount, currency }), on any platform. The fields and their rules, what is sent, how to check it worked, and its limits.
Send orders from a server
Report completed orders to the agent with one HTTP request per order, from your server or order system. The contract of the public orders endpoint, how to send the conversation key, what it ignores and what it never tells you.