Skip to content

Commit

Permalink
Disable analytics by default unless cookie is set if cookie_consent s…
Browse files Browse the repository at this point in the history
…pecified
  • Loading branch information
jonaharagon committed Dec 12, 2024
1 parent 0364534 commit 7dc7b29
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ umami [<matcher>] {
- `.html`
- `.php`
- **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_consent** is the name of a cookie, this plugin will run **only** if the cookie's value is `true`. 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.
Expand Down
13 changes: 11 additions & 2 deletions umami.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,16 +309,25 @@ func (p *Umami) GetAllowed(r *http.Request) int {
if len(p.CookieConsent) != 0 {
if p.CookieConsent[0].Behavior == "path_only" {
cookie, err := r.Cookie(p.CookieConsent[0].Name)
if err == nil && cookie != nil && cookie.Value == "false" {
if err == nil && cookie != nil && cookie.Value == "true" {
p.logger.Debug("Cookie allows analytics")
return 1
} else {
p.logger.Debug("Cookie does not allow analytics, sending path only")
return 2
}
} else if p.CookieConsent[0].Behavior == "disable_all" {
cookie, err := r.Cookie(p.CookieConsent[0].Name)
if err == nil && cookie != nil && cookie.Value == "false" {
if err == nil && cookie != nil && cookie.Value == "true" {
p.logger.Debug("Cookie allows analytics")
return 1
} else {
p.logger.Debug("Cookie does not allow analytics, sending no analytics")
return 0
}
}
}
p.logger.Debug("Cookie check disabled, sending all analytics")
return 1
}

Expand Down

0 comments on commit 7dc7b29

Please sign in to comment.