Seré breve y claro.
Tengo el siguiente código:
<script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'AW-10794286619'); </script> <script> gtag('event', 'conversion', { 'send_to': 'AW-10794286619/ZYX_CI3l2JEDEJv8jpso', 'transaction_id': '' }); </script>Sabiendo que "AW-10794286619" y AW-10794286619/ZYX_CI3l2JEDEJv8jpso son VARIABLES CARGADAS DESDE EL SERVIDOR, ¿cómo puedo cargar esa secuencia de comandos (agregándola en HEADER y ejecutándola) desde javascript y hacer que funcione?
Esto es lo que he intentado:
let googleTag = 'VALUE FROM SERVER' let initiateCheckoutGoogleA let initiateCheckoutGoogleB let initiateCheckoutGoogleC initiateCheckoutGoogleA = document.createElement('script') initiateCheckoutGoogleA.src = `https://www.googletagmanager.com/gtag/js?id=${googleTag.split('/')[0]}` initiateCheckoutGoogleA.async = true document.head.appendChild(initiateCheckoutGoogleA) initiateCheckoutGoogleB = document.createElement('script') initiateCheckoutGoogleB.innerHTML = `window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', ${googleTag.split('/')[0]});` document.head.appendChild(initiateCheckoutGoogleB) initiateCheckoutGoogleC = document.createElement('script') initiateCheckoutGoogleC.innerHTML = `gtag('event', 'conversion', { 'send_to': ${googleTag}});` document.head.appendChild(initiateCheckoutGoogleC)Me da el siguiente error: