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

339
Views
Dynamically Adding Google Analytics and Data Layer On Accept Cookies

I am trying to add Google Analytics to my site dynamically only after the use clicks "I Accept Cookies".

The script that I have so far is

<script>
  function addScript() {
    var addGoogleAnalytics = document.createElement("script");
    addGoogleAnalytics.setAttribute("src","https://www.googletagmanager.com/gtag/js?id=G-MYIDHERE");
    addGoogleAnalytics.async = "true";
    document.head.appendChild(addGoogleAnalytics);

    var addDataLayer = document.createElement("script");
    var dataLayerData = document.createTextNode("window.dataLayer = window.dataLayer || []; \n function gtag(){dataLayer.push(arguments);} \n gtag('js', new Date()); \n gtag('config', 'G-MYIDHERE');");
    addDataLayer.appendChild(dataLayerData);
    document.head.appendChild(addDataLayer);
  }
</script>

This is triggered using a simple

<a href="#" onclick="addScript();">Click Here</a>

This seems to work fine, but my questions are:

  1. Is there a better way to do this? I am no expert at javascript, so I am a little surprised it works and want to make sure there are no major code/security risks before deploying.

  2. More importantly.. How do I store the "I accept" in a cookie or localStorage so that they do not have to click I accept each time, and if that is set it loads the script automatically without the onclick function?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

So the problem with the above was that the code showed the cookie notice on every page.

<script>
var cookies = localStorage.getItem("cookiesAccepted");
// this checks if cookies local storage was accepted before
if(cookies == 'Yes') {

//if they were it creates the analytics tag

    var addGoogleAnalytics = document.createElement("script");
    addGoogleAnalytics.setAttribute("src","https://www.googletagmanager.com/gtag/js?id=G-MYIDHERE");
    addGoogleAnalytics.async = "true";
    document.head.appendChild(addGoogleAnalytics);

    var addDataLayer = document.createElement("script");
    var dataLayerData = document.createTextNode("window.dataLayer = window.dataLayer || []; \n function gtag(){dataLayer.push(arguments);} \n gtag('js', new Date()); \n gtag('config', 'G-MYIDHERE');");
    addDataLayer.appendChild(dataLayerData);
    document.head.appendChild(addDataLayer);
}

//if cookies were not set to yes, it waits for the cookie banner click function.

function addScript() {
    localStorage.setItem('cookieAccepted', 'Yes');
    var addGoogleAnalytics = document.createElement("script");
    addGoogleAnalytics.setAttribute("src","https://www.googletagmanager.com/gtag/js?id=G-MYIDHERE");
    addGoogleAnalytics.async = "true";
    document.head.appendChild(addGoogleAnalytics);

    var addDataLayer = document.createElement("script");
    var dataLayerData = document.createTextNode("window.dataLayer = window.dataLayer || []; \n function gtag(){dataLayer.push(arguments);} \n gtag('js', new Date()); \n gtag('config', 'G-MYIDHERE');");
    addDataLayer.appendChild(dataLayerData);
    document.head.appendChild(addDataLayer);
  }
</script>

This code above seems to work. If the user already accepted cookies it adds the Google Analytics tag without prompting the user. If they have not accepted the prompt is used to trigger the addscript function on accept.

about 4 years ago · Juan Pablo Isaza Report
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!