user3178479
user3178479

Reputation: 99

fix react GA href path

I have a simple react app project with google analytics:

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}`}
</a>

But this link in browser have a href like this:

<a href="https://test.com&amp;_gl=1*l68v33*_ga*ABRVALTRA.*_ga_1RTDSMNASHGFDTJSLJSNG.." target="_blank" rel="noreferrer noopener">https://test.com</a>

Why is this happening and how can i fix it?

p.s: This is mast be: <a href="https://test.com" target="_blank" rel="noreferrer noopener">https://test.com</a>

Upvotes: 0

Views: 263

Answers (2)

user3178479
user3178479

Reputation: 99

I changed the link to the button:

          <Button
            variant="link"
            onClick={handleClickOnLink}>
            {`${window.location.protocol}//${domain}`}
          </Button>

And:

const href = `${window.location.protocol}//${domain}`;

  const handleClickOnLink = (e) => {
    e.preventDefault();
    window.open(href, "_blank");
  };

This solved my problem

Upvotes: 0

darrelltw
darrelltw

Reputation: 2372

Can you try to check the setting in your Google Analytics Setting?

You might have configured the cross domain tracking from there and GA will add the gl and _ga parameter in the url href.

GA4 Cross Domain Tracking Document enter image description here

Upvotes: 1

Related Questions