GtmHelper = {}; GtmHelper.products = []; GtmHelper.userData = null; GtmHelper.addProduct = function(name, price, id, itemCategory, quantity = 1) { GtmHelper.products.push({ name, price, id, itemCategory, quantity }); }; GtmHelper.addUserData = function(email, phone, name, surname, city, zip, country) { GtmHelper.userData = { email, phone, name, surname, city, zip, country }; }; GtmHelper.render = function(action = 'add_to_cart', transactionId = '', isNewCustomer = false, tax = 0) { const items = []; let totalPrice = 0; GtmHelper.products.forEach(function(product, i) { items.push({ 'item_id': product.id, 'item_name': product.name, 'price': parseFloat(product.price), 'quantity': product.quantity, 'index': i, 'item_category': product.itemCategory }); totalPrice = totalPrice + (parseFloat(product.price) * product.quantity); }); const result = { 'event': action, 'ecommerce': { 'currency': 'CZK', 'value': parseFloat(totalPrice), 'items': items } }; if (action === 'pending_purchase' && GtmHelper.userData) { const user = GtmHelper.userData; result.user_data = { email_address : user.email, phone_number: user.phone, address: { first_name: user.name, last_name: user.surname, city: user.city, postal_code: user.zip, country: user.country } } result.transaction_id = transactionId; result.new_customer = isNewCustomer; result.tax = parseFloat(tax); result.shipping = 0.0; } return result; } GtmHelper.addWebhostingToCart = function(name, price, id) { dataLayer.push({ ecommerce: null }); GtmHelper.addProduct(`Webhosting: ${name}`, price, id, 'Webhosting'); const gtmEvent = GtmHelper.render(); dataLayer.push(gtmEvent); console.log(dataLayer); } GtmHelper.addMailToCart = function(name, price, id) { dataLayer.push({ ecommerce: null }); GtmHelper.addProduct(name, price, id, 'Mailhosting'); const gtmEvent = GtmHelper.render(); dataLayer.push(gtmEvent); }