Skip to content

Commit

Permalink
Basic device detection
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaharagon committed Apr 2, 2024
1 parent 572a8b7 commit efe4065
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ umami [<matcher>] {
client_ip_header <name>
cookie_consent [<name>]
cookie_resolution [<name>]
device_detection
trusted_ip_header <name>
report_all_resources
debug
Expand All @@ -34,6 +35,7 @@ umami [<matcher>] {
- **client_ip_header** is the name of an HTTP header which will be sent to Umami **alongside** `X-Forwarded-For`, which contains the visitor's IP address.
- **cookie_consent** is the name of a cookie, if that cookie's value is `false` then this plugin will not run. If a name is not set, the default name is `umami_consent`.
- **cookie_resolution** is the name of a cookie whose value should be the user's screen resolution, for example `1920x1080`. It is your responsibility to set this cookie with client-side JavaScript (not provided). If this cookie is not set, device type will just be reported as unknown. If a name is not set, the default name is `umami_resolution`.
- **device_detection** can be enabled to set the sent screen resolution based on `Sec-CH-UA-Mobile`/`Sec-CH-UA-Platform`, for some rudimentary device detection without cookies. If this and `cookie_resolution` are both enabled, a screen resolution set by the cookie will take precedence.
- **trusted_ip_header** is the name of an incoming HTTP request header which contains the visitor's true IP, which will then be sent to Umami via the `X-Forwarded-For`. This may be useful if your Caddy server is behind a reverse proxy.
- **report_all_resources** can be included to report **all** requests to Umami, overriding allowed_extensions. By default, only requests with certain extensions are reported. This may be especially useful when using this module with a matcher.

Expand All @@ -53,8 +55,7 @@ example.com {
website_uuid "4fa2c16a-6c0f-488f-986f-bc26d90c76d1"
allowed_extensions "" .html .htm .php
client_ip_header X-Real-IP
cookie_consent
cookie_resolution screenres
device_detection
}
// ...
Expand Down
33 changes: 25 additions & 8 deletions umami.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Umami struct {
TrustedIPHeader string
CookieConsent string
CookieResolution string
DeviceDetection bool
}

// CaddyModule returns the Caddy module information.
Expand Down Expand Up @@ -110,15 +111,27 @@ func (p Umami) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.
fmt.Printf("Error getting resolution cookie: %v\n", err)
}
}
if cookie != nil {
payload["screen"] = cookie.Value
}
}

// handle if cookie does not exist
if cookie == nil {
cookie = &http.Cookie{
Name: p.CookieResolution,
Value: "",
}
if payload["screen"] == nil && p.DeviceDetection {
mobile := r.Header.Get("Sec-CH-UA-Mobile")
platform := r.Header.Get("Sec-CH-UA-Platform")

if p.DebugLogging {
fmt.Printf("Mobile: %s\n", mobile)
fmt.Printf("Platform: %s\n", platform)
}

if mobile == "?1" {
payload["screen"] = "400x800" // mobile
} else if platform == `"Android"` {
payload["screen"] = "900x600" // tablet
} else if platform == `"Chrome OS"` || platform == `"macOS"` {
payload["screen"] = "1200x800" // laptop
}
payload["screen"] = cookie.Value
}

visitorInfo := map[string]interface{}{
Expand Down Expand Up @@ -189,7 +202,7 @@ func (p *Umami) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
}
p.WebsiteUUID = d.Val()
case "report_all_resources":
p.ReportAllResources = d.Val() == "true"
p.ReportAllResources = true
case "allowed_extensions":
if !d.NextArg() {
return d.ArgErr()
Expand Down Expand Up @@ -222,6 +235,8 @@ func (p *Umami) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
} else {
p.CookieResolution = d.Val()
}
case "device_detection":
p.DeviceDetection = true

default:
return d.Errf("unknown option '%s'", d.Val())
Expand Down Expand Up @@ -252,7 +267,9 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)
if umami.DebugLogging {
fmt.Printf("Event Endpoint: %s\n", umami.EventEndpoint)
fmt.Printf("Website UUID: %s\n", umami.WebsiteUUID)
fmt.Printf("Reporting All Resources: %v\n", umami.ReportAllResources)
fmt.Printf("Allowed Extensions: %v\n", umami.AllowedExtensions)
fmt.Printf("Device Detection: %v\n", umami.DeviceDetection)
}
return umami, nil
}

0 comments on commit efe4065

Please sign in to comment.