Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

157
Visualizações
Implementing Google Analytics Script AFTER the Visitor Accept Using Body of Page

The aim is simple, run the google tracking code AFTER the user has accepted and if the user do not accept, do not run.

I went with placing the code inside the tag, but I am struggling to understand how to incorporate a "dismiss" option as well.

This is my "work in progress" code as of right now:

<script>
(function() {
    if (!localStorage.getItem('cookieconsent')) {
        document.body.innerHTML += '\
        <div class="cookieconsent" style="position:fixed;padding:20px;left:0;bottom:0;background-color:#000;color:#FFF;text-align:center;width:100%;z-index:99999;">\
            This site uses standard cookies and Google Analytics. By clicking on "I understand", you agree to their use. \
            <a href="#" style="color:#CCCCCC;font-weight:bold;">I Understand</a>\
        </div>\
        ';
        document.querySelector('.cookieconsent a').onclick = function(e) {
            e.preventDefault();
            document.querySelector('.cookieconsent').style.display = 'none';
            localStorage.setItem('cookieconsent', true);
        };
    }
})();
</script>

And from what I understand, I need to place the google tracking after this line:

localStorage.setItem('cookieconsent', true);

But since the script tag is already opened, how do I do that?

Example of the tracking code:

<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXXXXX');
</script>

On top of this, I'm not sure how to add a dismiss option so that the google code does not run. I'm grateful for all the input I can get.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This probably contains the answer to your question Consent Mode (beta):

"Consent Mode (beta) allows you to adjust how your Google tags behave based on the consent status of your users. You can indicate whether consent has been granted for Analytics and Ads cookies. Google's tags will dynamically adapt, only utilizing measurement tools for the specified purposes when consent has been given by the user."

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda