// Configuration Variables const wickedConfig = { GADS: { send_to: 'AW-XXXXXXXX/AAAA-BBBBBB-CCCC', // replace with your send_to }, includeTaxAndShipping: false, // set true if you want tax & shipping included defaultCurrency: 'USD', //update if currency is not USD }; const accountId = wickedConfig.GADS.send_to.split('/')[0]; // Load Google Tag Manager script const script = document.createElement('script'); script.setAttribute( 'src', `https://www.googletagmanager.com/gtag/js?id=${accountId}` ); script.setAttribute('async', ''); document.head.appendChild(script); console.log('GTag added'); window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', accountId, { allow_enhanced_conversions: true }); // Utility: Get total or subtotal const getTotal = (checkoutData, includeTotal) => { try { const totalValue = checkoutData?.totalPrice?.amount || 0; const subTotal = checkoutData?.subtotalPrice?.amount || 0; return includeTotal ? totalValue : subTotal; } catch (error) { console.error('getTotal error:', error); return 0; } }; // Handle checkout_completed event analytics.subscribe('checkout_completed', async (event) => { gtag('event', 'conversion', { send_to: wickedConfig.GADS.send_to, value: getTotal(event.data.checkout, wickedConfig.includeTaxAndShipping), currency: event.data.checkout.currencyCode || wickedConfig.defaultCurrency, transaction_id: event.data.checkout.order.id, }); });