Advanced Cart Updates

This article explains how to update visitors’ carts in multiple ways.

Christiaan Proper avatar
Written by Christiaan Proper
Updated over a week ago

NOTE: If you use this way of syncing carts with our tracking script, you can’t update carts with the “paq.push method” explained in this article.

The benefit of using this method of updating carts is that you have complete control over adding or removing items from the cart and overwriting or emptying them. The “paq.push” method only allows you to overwrite the cart with each update.

Suppose you use Google Tag Manager and share e-commerce events with your dataLayer. In that case, we recommend you to use this method of sharing cart updates with Datatrics since Google also allows you to add and remove single items from the cart in both Universal Analytics (Enhanced Ecommerce) and GA4 GTM Ecommerce.

You need to ensure that your item variable is in the proper data type for items added/removed from the cart. Below you can find an explanation of each item variable and which data type to use:

Variable

Type

Explanation

{{item_id}}

String

Item unique identifier

{{item_name}}

String

Item name

{{item_category}}

String or Array

Item category. You can also specify an array of up to 5 categories e.g. ["{{categoryname1}}", "{{categoryname2}}", "{{categoryname 3}}"]

{{item_price}}

Decimal

Per item price

{{item_quantity}}

Number

Item quantity (amount added/removed from cart)

Below you can find all possible ways of syncing a visitor’s cart with our tracking script. Each function also comes with an example event.

Add item(s) to the cart

You need to execute the code below for each item added to the cart. If the same item is added multiple times, you can execute it numerous times or immediately update the quantity to the amount added.

datatricsAddCartItem(
"{{item_id}}",
"{{item_name}}",
"{{item_category}}",
{{item_price}},
{{item_quantity}}
);

//Example with actual data

datatricsAddCartItem(
"325791-564a",
"The best book",
"Best sellers",
50.5,
1
);

Remove item(s) from the cart

Only the item_id and amount removed are necessary to share with our tracking script when removing items from the cart.

datatricsRemoveCartItem(
"{{item_id}}",
{{quantity}}
);

//Example with actual data

datatricsRemoveCartItem(
"325791-564a",
1
);

Overwrite the cart

If you’d like to overwrite the full cart (for example, when a visitor is visiting their shopping cart or entering a checkout), you need to implement it like this.

datatricsSetCart([{
id: "{{item_id}}",
name: "{{item_name}}",
category: "{{item_category}}",
price: {{item_price}},
quantity: {{item_quantity}}
},{
id: "{{item_id}}",
name: "{{item_name}}",
category: "{{item_category}}",
price: {{item_price}},
quantity: {{item_quantity}}
},{
id: "{{item_id}}",
name: "{{item_name}}",
category: "{{item_category}}",
price: {{item_price}},
quantity: {{item_quantity}}
}]);

//Example with actual data

datatricsSetCart([{
id: "325791-564a",
name: "The best book",
category: "Best sellers",
price: 50.5,
quantity: 1
},{
id: "518168",
name: "Another book",
category: "Thriller",
price: 25,
quantity: 2
},{
id: "8771649",
name: "Adventure Book 2",
category: ["Adventure Books", "Best sellers"],
price: 15,
quantity: 1
}]);

Empty the cart

When the cart is fully emptied, you can use this function to share this event with our tracking script.

datatricsEmptyCart();
Did this answer your question?