https://github.com/matoous/visigo
Unique site visits counter in Go
https://github.com/matoous/visigo
golang hyperloglog middleware unique-visitors
Last synced: 7 months ago
JSON representation
Unique site visits counter in Go
- Host: GitHub
- URL: https://github.com/matoous/visigo
- Owner: matoous
- License: mit
- Created: 2017-09-15T08:33:55.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-12-15T02:34:45.000Z (over 2 years ago)
- Last Synced: 2025-03-20T01:41:12.332Z (about 1 year ago)
- Topics: golang, hyperloglog, middleware, unique-visitors
- Language: Go
- Homepage:
- Size: 21.5 KB
- Stars: 19
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Visigo
[](https://github.com/matoous/visigo/actions)
[](https://github.com/matoous/visigo/actions)
[](https://godoc.org/github.com/matoous/visigo)
[](https://goreportcard.com/report/github.com/matoous/visigo)
[](https://github.com/matoous/visigo/issues)
[](https://github.com/matoous/visigo/LICENSE)
**Visigo** is http middleware for page unique visits counting. It uses HyperLogLog as
a counter, so it's pretty fast.
**Warning:** Visigo stores HyperLogLog++ in *map*, so this implementation
should be used only on smaller sites.
## HyperLogLog++
[HyperLogLog++ paper](http://research.google.com/pubs/pub40671.html)
[Google article about HyperLogLog++](https://research.neustar.biz/2013/01/24/hyperloglog-googles-take-on-engineering-hll/)
From [Wikipedia](https://en.wikipedia.org/wiki/HyperLogLog)
> HyperLogLog is an algorithm for the count-distinct problem, approximating the number of distinct elements in a multiset.
Calculating the exact cardinality of a multiset requires an amount of memory proportional to the cardinality, which is impractical for very large data sets. Probabilistic cardinality estimators, such as the HyperLogLog algorithm, use significantly less memory than this, at the cost of obtaining only an approximation of the cardinality. The HyperLogLog algorithm is able to estimate cardinalities of > 109 with a typical accuracy of 2%, using 1.5 kB of memory.
HyperLogLog is an extension of the earlier LogLog algorithm, itself deriving from the 1984 Flajolet–Martin algorithm.
## Install
Via go get tool
``` bash
$ go get github.com/matoous/visigo
```
## Usage
``` go
package main
import (
"fmt"
"net/http"
"github.com/matoous/visigo"
)
func main() {
http.Handle("/", visigo.Counter(http.HandlerFunc(final)))
http.Handle("/total", visigo.Counter(http.HandlerFunc(total)))
http.ListenAndServe(":3000", nil)
}
func final(w http.ResponseWriter, r *http.Request) {
count, _ := visigo.Visits(r)
response := fmt.Sprintf("This page was viewed by %d unique visitors", count)
w.Write([]byte(response))
}
func total(w http.ResponseWriter, r *http.Request) {
count, _ := visigo.TotalVisits()
response := fmt.Sprintf("This website had %d unique visitors in total", count)
w.Write([]byte(response))
}
```
## Testing
``` bash
$ go test ./...
```
## Notice
If you use **Visigo** on your site or in your project, please let me know!
If you have any issues, just feel free and open it in this repository, thanks!
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.