Learn business growth with Google Analytics 4 Forums Google Analytics 4 Correcting the Href Path for React GA in a React App

  • Correcting the Href Path for React GA in a React App

    Posted by Ava on 29 August 2022 at 12:30 pm

    Hey there! I ran into a small issue with my react app project where I’m using Google analytics. Here’s what my code looks like:

    `javascript
    import ReactGA from “react-ga4”;
    import TagManager from “react-gtm-module”;

    useEffect(() => {
    ReactGA.initialize(“G-xxxxx…”);
    ReactGA.send(“pageview”);
    TagManager.initialize({
    gtmId: “GTM-x”xxxx…
    });
    }, []);

    <a
    href={${window.location.protocol}//${domain}}
    target=”_blank”
    rel=”noreferrer noopener”>
    {${window.location.protocol}//${domain}}

    `

    But when I browse, the link href appears like this:

    `html
    https://test.com
    `

    I can’t figure out why it happens. Hoping you might help me fixing it. Ideally, I want it like this:

    `html
    https://test.com
    `

    Elijah replied 11 months, 2 weeks ago 2 Members · 1 Reply
  • 1 Reply
  • Elijah

    Member
    24 April 2023 at 1:38 am

    Certainly! Your issue seems to be arising from some settings related to Google Analytics in your project. These parameters – gl and _ga – are probably being added because cross domain tracking is currently enabled in your Analytics settings.

    Cross domain tracking is a Google Analytics feature that tracks a user navigating across multiple domains as a single session, which is useful when your website spans across multiple domains. The parameters added to the URL enable this tracking.

    If you don’t need this feature, you can adjust your Google Analytics settings to remove the parameters. Simply find the cross domain tracking section in your settings and modify it according to your needs. This should solve the issue. Have a look at the GA4 Cross Domain Tracking Document for more details (pictured below). Hope this helps!

    (Here’s an image showing where to find the settings: ![cross domain settings](https://i.stack.imgur.com/F7lAj.png))

Log in to reply.