Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!