I'll be short and clear.
I have the following code:
<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>
Knowing that "AW-10794286619" and AW-10794286619/ZYX_CI3l2JEDEJv8jpso are VARIABLES LOADED FROM SERVER, how can i load that script (appending it on HEADER and running it) from javascript, and get it working?
Here is what i have tried:
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)
Gives me the following error: