2
2 Comments

Using the Referer Header to know where your page's visits come from

I don't use any analytics packages. I just write my own. Until recently, I had no real idea where visits to forky.io come from. I just guessed it was a couple blogs and my producthunt page.

I recently found that tracking the referer header is pretty great. The referer header contains the site the user is coming from. If they clicked a link from some page to your page then your webserver will receive a referer header in the requests (unless they disabled it)

Google analytics will do this for you, but opted to use my own coding to track things.

So how I did it was, when they load app paths (that serve index.html) from my webserver, it saves the referer header in the database.

e.GET("/", func(ctx echo.Context) error {
	if ctx.Request().Referer() != "" {
		go measureReferer(ctx.Request().Referer())
	}
	return appHandler(ctx)
})

measureReferer(referer string) {
  // save to database  
}

Then in Grafana I added a panel that shows my referrers saved in the database.

-- a bunch of sql only relevant to me
SELECT
  ts as time,
  val #>> '{metricPrimaryType}' as n,
  val #>> '{metricPrimaryValue}' as v
FROM
  bridge_snaps
WHERE
  bucket = 'analytics' AND
  val #>> '{metricPrimaryType}' = 'forky.referer' AND val #>> '{metricPrimaryValue}' not like '%//forky.io%' AND
  $__unixEpochFilter(ts)
ORDER BY time DESC

Now I can see that my visits are coming from some blogs... several search engines, and my product hunt post.

It's also useful to check your load balancer's logs, which sometimes saves headers. I use Google Cloud and exported all the logs that had referer headers in them.

By the way, it doesn't work 100% of the time. It depends on the referer policy https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy

Huzzah.
Good luck all.

on December 12, 2022
  1. 1

    Note: The header name "referer" is actually a misspelling of the word "referrer"

  2. 1

    Go to https://forky.io to increase my dopamine today, and make some nice diagrams. :)