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

153
Visualizações
Reliably fetching the Google Analytics Tracking ID from another script

I need to retrieve the Google Analytics Tracking ID via JavaScript for use in another script. Something like:

var ga_id = (typeof ga !== 'undefined') ? ga.getAll()[0].get('trackingId') : 'Unable to retrieve GA id'

My Analytics is loaded using Tag manager, so even though I'm running my own JavaScript quite late in the page, its not detecting the presence of the ga object. ( I can access it without a problem via the developer tools console.)

How can I ensure that ga has loaded before trying to access its properties?

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

0

It's there on window loaded and you can see it in the GTM preview like so:

enter image description here

I have this CJS in that var (used your code):

enter image description here

Although this is ample in my case, yours may differ, so what you can do is the following:

  1. Make a custom html tag that runs on pageload and deploys a closure with a timeout in it.
  2. In the timeout callback push a dataLayer event.
  3. Now move the tag that you want to get the value of the property id for to the dataLayer event trigger (custom event).

This is not very elegant, but it'll work. Again, for normal implementations, Window Loaded should be ample.

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