Creating list of items for JavaScript

Can anyone help me with this issue?

I’m using Google Analytics eCommerce tracking using dataLayer push via Google Tag Manager events.

Pushing a single item to the data layer using Javascript Toolbox is fine, but how can it do this for a number of items that make up an order. This works for a single item:

window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
‘event’:‘begin_checkout’,
‘ecommerce’:{
‘items’:[
{
‘item_name’:’[name]’,
‘item_id’:’[id]’,
‘price’:’[price]’,
‘item_brand’:’[brand]’,
‘item_category’:’[cat1’,
‘item_category_2’:’[cat2]’,
‘item_category_3’:’[cat3]’,
‘item_category_4’:’[cat4]’,
‘item_variant’:’[variant]’,
‘item_list_name’:’[list_name]’,
‘item_list_id’:’[list_id]’,
‘index’:[1],
‘quantity’:’[quantity]’
}]
}}
)

An order can contain several items, but I want to be able to capture all the items in order, not just one.

Thanks

Euan

Put you a for loop in there big dawg

1 Like

I don’t want to run the event multiple times, just add several items to the event, so begin_checkout only fires once, but I could have 1 item or 10 items and they’d all be captured as part of the event.

Hope I’m making sense, I’m a beginner at joining code like this to Bubble’s no code functions.

You most certainly will need a for loop

Can anyone help building the for loop? I’ve tried researching it but it’s beyond my current capability/ familiarity with JavaScript.

@doug.burden is right, you’ll need a for loop to process the list.

As for the code, you can do what i do. Ask ChatGPT for the code.

Or @ihsanzainal84, consult MDN and know the answer for yourself.

3 Likes