Legacy Google Analytics Transaction tracking to GA4 items object

Did you like this post? Share it!

Before the era of Universal Analytics Enhanced Ecommerce, we used old classic or legacy Google Analytics transaction tracking.

Even today, we sometimes meet this tracking on websites. It is always better to ask developer to change object to the new Google Analytics 4 items. Unfortunately, this is not always possible.

Luckily, with easy JavaScript function, it is possible to convert this object.

See the example here (or try on jsfiddle):

 var transactionProducts = [
{
"name":"Product1",
"sku":"111333",
"category":"Path/Path/Path",
"price":113.64,
"quantity":1
},
{
"name":"Product2",
"sku":"111334",
"category":"Path",
"price":77.27,
"quantity":2
}
];
var items = [];

for (var i = 0; i < transactionProducts.length; i++) {

 var category = transactionProducts[i].category;
 var category_helper = category.split("/");
 items.push({
      item_name: transactionProducts[i].name,
      item_id: transactionProducts[i].sku,
      price: String(transactionProducts[i].price),
      item_category: category_helper[0],
      item_category2: category_helper[1] ? category_helper[1] : undefined,
      item_category3: category_helper[2] ? category_helper[2] : undefined,
      item_category4: category_helper[3] ? category_helper[3] : undefined,
      item_category5: category_helper[4] ? category_helper[4] : undefined,  
      quantity: String(transactionProducts[0].quantity)
    });  
   } 
   
    
    
console.log(items);

In case you do not use category path, item_category_2 – 5 may be removed.

This function may be used as a custom JS variable in GTM with little modification.

Subscribe to our newsletter
We send it 4 times a year.
Read more from our specialists
Blog

GA4 settings checklist (for UA migration)

First and foremost, we wouldn’t recommend just migrating your old Universal Analytics to Google Analytics 4. GA4 is the new age of digital analytics, with