Skip to content
This repository has been archived by the owner on Jul 24, 2023. It is now read-only.

Improve handling of referrer #30

Open
bpugh opened this issue Sep 6, 2020 · 0 comments
Open

Improve handling of referrer #30

bpugh opened this issue Sep 6, 2020 · 0 comments

Comments

@bpugh
Copy link

bpugh commented Sep 6, 2020

It looks like right now aside from the configuration options, the referrer is only being set from document.referrer which works when the user gets to the site from an external source but not when they are navigating around the site. Also document.referrer doesn't change when navigating within Gatsby so if a user visits multiple pages they will all show the same external referrer. I don't think this is the behavior we want and it's not how it would behave on a traditional non SPA website.

Fortunately, I think an easy fix is we use the prevLocation parameter of onRouteUpdate. If it's not null then we send that as the referrer otherwise we send document.referrrer. (https://www.gatsbyjs.com/docs/browser-apis/#onRouteUpdate)

Something like:

settings.referrer = () =>
      window.goatcounter?.get_query?.('ref') ??
      window.goatcounter?.get_query?.('utm_source') ??
      prevLocation?.pathname ??
      document.referrer;

I'd be happy to submit a PR but wanted to get your thoughts on it first.

Thanks for this handy plugin!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant