Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

228
Views
Loading google ads tags dynamically using JavaScript

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:

enter image description here

enter image description here

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!