parse hostname from client instead of relying on http header. fixes #79

This commit is contained in:
Danny van Kooten 2018-06-28 10:37:02 +02:00
parent 67308e3348
commit 9b47ee27ef
2 changed files with 11 additions and 14 deletions

View File

@ -68,17 +68,22 @@ function trackPageview() {
return;
}
// get the path or canonical
let path = location.pathname + location.search;
let req = window.location;
// parse path from canonical, if page has one
// parse canonical, if page has one
let canonical = document.querySelector('link[rel="canonical"][href]');
if(canonical) {
let a = document.createElement('a');
a.href = canonical.href;
path = a.pathname;
// use parsed canonical as location object
req = a;
}
// get path and pathname from location or canonical
let path = req.pathname + req.search;
let hostname = req.protocol + "//" + req.hostname;
// if parsing path failed, default to main page
if(!path) {
path = '/';
@ -94,6 +99,7 @@ function trackPageview() {
const d = {
sid: data.sid,
p: path,
h: hostname,
t: document.title,
r: referrer,
u: data.pagesViewed.indexOf(path) == -1 ? 1 : 0,

View File

@ -25,10 +25,6 @@ func shouldCollect(r *http.Request) bool {
return false
}
if r.Referer() == "" {
return false
}
return true
}
@ -79,15 +75,10 @@ func (api *API) NewCollectHandler() http.Handler {
q := r.URL.Query()
now := time.Now()
hostname := parseHostname(r.Referer())
if hostname == "" {
return nil
}
// get pageview details
pageview := &models.Pageview{
SessionID: q.Get("sid"),
Hostname: hostname,
Hostname: parseHostname(q.Get("h")),
Pathname: parsePathname(q.Get("p")),
IsNewVisitor: q.Get("nv") == "1",
IsNewSession: q.Get("ns") == "1",