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

282
Visualizações
Mixpanel using Google Tag Manager and React JS

I am trying to implement Mixpanel with my React App using Google Tag Manager. I have successfully integrated it using GTM but I have trouble with mixpanel always being undefined on reload. I am using CDN for it.

If I go onto some other page and come back to the page where I am using Mixpanel, there's no error. So I diagnosed that as it is an asynchronous operation it takes some time to load mixpanel in my app. So I tried using the useEffect hook and useState but it failed to solve my problem.

Below is my code:

const [mixpanelLoad, setMixpanelLoad] = useState(false);

  useEffect(()=>{
    console.log('IN USEEFFECT');
    console.log(mixpanel);
    if (mixpanel) {
    // It never comes here on first reload
      setMixpanelLoad(true);
      console.log('MIXPANEL LOADED');
    }
    if (mixpanelLoad) {
    // thus it never does the tracking 
      console.log('DOING TRACKING');
      mixpanel.track('Track Bill Page');
    }
  }, [mixpanelLoad]);

On first reload it always says that mixpanel is undefined.

I am using CDN so I don't have to use any mixpanel library.

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

0

Certain methods/features can only be used after the Mixpanel library has finished loading. init() has a loaded function available to handle this automatically -

mixpanel.init('YOUR PROJECT TOKEN', {
    loaded: function(mixpanel) {
        distinct_id = mixpanel.get_distinct_id();
    }
});
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