ihucos / counter.dev

Web Analytics made simple
https://counter.dev
GNU Affero General Public License v3.0
900 stars 40 forks source link

Ability to define referrer or a website #119

Closed syabro closed 1 year ago

syabro commented 1 year ago

I've an electron app and it doesn't have a referrer. How can I use counter.dev?

ihucos commented 1 year ago

I don't know how exactly electron works but I'd imagine that the "Sources" section would be just empty. The browser type tracking might also not make sense. The rest could be useful metrics. So I'd say you can just use it with limiations.

syabro commented 1 year ago

@ihucos I got 400 from your server because of the empty referrer :(

ihucos commented 1 year ago

I see. In the code empty referrers should just be ignored and not raise an error (The err variable is ignored, should be called _ actually for better readability)

https://github.com/ihucos/counter.dev/blob/master/backend/endpoints/track.go#L101

So it must be something else I guess. Are you able to provide me with a minimal example. That would be a curl or similar request that causes the 400 response code.

syabro commented 1 year ago
curl 'https://t.counter.dev/trackpage' \
  -H 'authority: t.counter.dev' \
  -H 'accept: */*' \
  -H 'accept-language: en-GB' \
  -H 'content-type: application/x-www-form-urlencoded;charset=UTF-8' \
  -H 'sec-ch-ua: "Not:A-Brand";v="99", "Chromium";v="112"' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'sec-ch-ua-platform: "macOS"' \
  -H 'sec-fetch-dest: empty' \
  -H 'sec-fetch-mode: no-cors' \
  -H 'sec-fetch-site: cross-site' \
  -H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) presync/0.1.0 Chrome/112.0.5615.87 Electron/24.1.2 Safari/537.36' \
  --data-raw 'id=fc040caa-d114-4925-9cf2-cda6fbfcaacc&page=%2FVolumes%2Fcode%2Fapps%2Fpresync%2Fpresync%2Fclient%2Fdist%2Fmac-arm64%2FPresync.app%2FContents%2FResources%2Fapp.asar%2Fout%2Frenderer%2Findex.html' \
  --compressed

says Origin header can not be empty, not set or "null"%

syabro commented 1 year ago

Also track looks good, but I don't see any of them in the dashboard

curl 'https://t.counter.dev/track?referrer=&screen=2560x1707&id=fc040caa-d114-4925-9cf2-cda6fbfcaacc&utcoffset=7' --compressed
ihucos commented 1 year ago

Sorry for the late reply. I see.

When counter.dev receives a tracking request it takes the domain name that should be tracked from the "Origin" header. There could be two possible solution.

  1. Add in a feature to specify the domain explicitly when including the tracking script as data-domain tag attribute. Pass that to the track request in the tracking script.

  2. Make the electron app behave like more common browsers.

Can we try number two. I never worked with Electron but made ChatGPT to spit out this code to manually add in the required Origin header.

const { app, session } = require('electron');

app.whenReady().then(() => {
  session.defaultSession.webRequest.onBeforeSendHeaders((details, callback) => {
    details.requestHeaders['Origin'] = 'https://your-app-origin.com'; // Set your app's origin here
    callback({ cancel: false, requestHeaders: details.requestHeaders });
  });
});

Would an approach in this direction make sense?

syabro commented 1 year ago

@ihucos thanks! I solved it by the different aproach - loading html + css from the server, so it has a origin

ihucos commented 1 year ago

I see. Thank you very much for the info.