1

Background:

I have website which support 2 regions (US default region: mysite.com/contact; AU region: mysite.com/au/contact) which is built with NextJS + React SPA.

I have also 2 google analytics accounts for both regions separately, and using google gtag to send events. https://www.googletagmanager.com/gtag/js?id=UA-XXXXX-REGION-A and https://www.googletagmanager.com/gtag/js?id=UA-XXXXX-REGION-B after switch regions.

I also have internal code logic, where detect the geo location, and automatically redirect the client to the correct region. The way I did for redirect is set value window.location.href to refresh.

Issue Steps:

  1. AU clients search on google;
  2. google gives a link of mysite.com/contact (US link); and client clicks on the link
  3. landed to the website mysite.com/contact, but the website automatically redirect to mysite.com/au/contact

What I can see is that, the referrer is lost after landed on mysite.com/au/contact, and in the google analytic dashboard, it shows it is "direct" source, meaning its traffic is from within the site (mysite.com/contact), but this is not right, it is actually from google organic search

Note

This issue does not happens on US region, when redirect does not happens, so I flag this issue only on the steps with AU region

0

1 Answer 1

3

There are at least two viable things you could do to fix it.

One would be rewriting your redirect to something that would preserve the referrer. I would suggest moving the redirecting logic to backend and using your normal web-server redirections. Backend redirections preserve the original referrer in vast majority of cases. That also would be better for SEO, in conjunction with properly implemented hreflang metadata.

There's also a more hacky way of fixing it. Before redirecting the user, preserve their referrer in a cookie. When a user is redirected and before firing a new pageview to GA, you check if that cookie is set. If it's set, you force-fill the dr parameter of the GA payload (stands for document referrer) with what you have in the cookie, then you delete the cookie and send the pageview.

Keep an eye on your query parameters. A lot of paid sources attribution is done via query parameters of the request, so make sure your redirections, however done, preserve the query params.

9
  • Hi @BNazaruk, just checking the second way, instead of cookie if I simply retrieve the referrer from document.referrer and then just set location.href with xxxxxxxxxxxxx?dr=currentReferrer, would that automatically fix?
    – Xin
    Commented Apr 17, 2022 at 1:00
  • No, to my knowledge, the ga script doesn't take the dr query param from the request page and assume that's a referrer. You, on the other hand, could parse it out from the url, but I think it's best not to pollute the url.
    – BNazaruk
    Commented Apr 17, 2022 at 1:24
  • Thank you. I think you gave me some light, and the query string may do the track, I am reading this: developers.google.com/analytics/devguides/collection/protocol/…
    – Xin
    Commented Apr 17, 2022 at 4:38
  • they mean the query string as a part of the payload of the request to the GA's endpoint. It has completely nothing to do with the query string on your site. And that's how I suggest you setting it in your tracking.
    – BNazaruk
    Commented Apr 17, 2022 at 16:13
  • Thank you. Could I get some code example for this? I am using gtag.
    – Xin
    Commented Apr 18, 2022 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.