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

174
Visualizações
How can I add Google Tag Manager to chrome extension content-scripts?

I have a chrome extension that activates on some websites and injects some elements in html DOM. Now I want to add Google Tag Manager on the extension.

In the Google Tag Manager documents it's mentioned to add the given function in the page <head> tag. So first I tried to add the script tag in document <head> tag as soon as the extension is loaded:

const loadGTM = () => {
  const scriptTag = document.createElement("script");
  scriptTag.innerHTML = `(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
  new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
  j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
  'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
  })(window,document,'script','dataLayer','GTM-XXXXXXX');`;
  document.head.appendChild(scriptTag);

  const noScriptTag = document.createElement("noscript");
  noScriptTag.innerHTML = `<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXXXX"
  height="0" width="0" style="display:none;visibility:hidden"></iframe>`;
  document.body.prepend(noScriptTag);
}

Then I tried to connect through Google Tag Assistant and it didn't.

After this I tried to run the script instead of adding the script to head tag. So I open a new gtm.js file and added the script in it:

//gtm.js
const accountToken = `GTM-XXXXXXX`;
function integrateGTM() {
  (function (w, d, s, l, i) {
    w[l] = w[l] || [];
    w[l].push({ "gtm.start": new Date().getTime(), event: "gtm.js" });
    var f = d.getElementsByTagName(s)[0],
      j = d.createElement(s),
      dl = l != "dataLayer" ? "&l=" + l : "";
    j.async = true;
    j.src = "https://www.googletagmanager.com/gtm.js?id=" + i + dl;
    f.parentNode.insertBefore(j, f);
  })(window, document, "script", "dataLayer", accountToken);
  const code = `<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=${accountToken}"
  height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>`;
  const bodyScript = document.createElement("noscript");
  bodyScript.innerHTML = code;
  document.body.prepend(bodyScript);
}

integrateGTM();

And again after trying to connect through Tag Assistant it doesn't connect.

So If anybody has any idea or experience in this field I would be glad to get some help. Thank you!

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