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

184
Visualizações
react-ga4 sends view multiple times

I´ve set up a google analytics account and and created a new property. Took the tracking id out and then added with react-ga4 like this.

For example on my Album ItemPage

const ItemPage = () => {
    const {user} = useContext(AuthContext);
    let { item } = useParams();
    const [album, setAlbum] = useState({});


    useEffect(() => {
        const getAlbum = async () => {
            try {
                const res = await axios.get("/albums/find/" + item, {
                });
                setAlbum(res.data);
            } catch (err) {
                console.log(err);
            }
        };

        getAlbum();

    }, []);


    let location = useLocation();
    ReactGA.initialize("G-XXXXXXXXX");
    ReactGA.send({hitType: "pageview", page: location.pathname})

    return (
        <>
        <div className={"itemPage"} style={{background: "linear-gradient(to bottom," + color + " -130%,black 90%), url(https://preview.redd.it/o0aq46bb2w111.jpg?width=640&crop=smart&auto=webp&s=6a9216cd0c400677ad95dc66d7cc10e0854aa980)"}}>
            <Navbar/>
            <Cover/>
            <Footer style={{marginTop: "10%"}}/>
        </div>
            </>
    );
}

export default ItemPage;

it sends the correct path and album title to google analytics but ONE click sends SEVEN views. Is there a way to let it max out at 1 time?

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

0

You need to move the ReactGA code into your useEffect block so that it only runs once when the page is loaded:

const ItemPage = () => {
  const { user } = useContext(AuthContext);
  let { item } = useParams();
  const [album, setAlbum] = useState({});

  useEffect(() => {
    const getAlbum = async () => {
      try {
        const res = await axios.get("/albums/find/" + item, {});
        setAlbum(res.data);
      } catch (err) {
        console.log(err);
      }
    };

    getAlbum();
    let location = useLocation();
    ReactGA.initialize("G-XXXXXXXXX");
    ReactGA.send({ hitType: "pageview", page: location.pathname });
  }, []);

  return (
    <>
      <div
        className={"itemPage"}
        style={{
          background:
            "linear-gradient(to bottom," +
            color +
            " -130%,black 90%), url(https://preview.redd.it/o0aq46bb2w111.jpg?width=640&crop=smart&auto=webp&s=6a9216cd0c400677ad95dc66d7cc10e0854aa980)",
        }}
      >
        <Navbar />
        <Cover />
        <Footer style={{ marginTop: "10%" }} />
      </div>
    </>
  );
};
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