skip trcking robots and tracking paths
This commit is contained in:
@@ -2,6 +2,7 @@ package tracking
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"knet.sk/src/util"
|
"knet.sk/src/util"
|
||||||
@@ -34,10 +35,33 @@ type PageStat struct {
|
|||||||
|
|
||||||
var ts *TrackingService
|
var ts *TrackingService
|
||||||
|
|
||||||
|
var ignorePaths = []string{
|
||||||
|
"/tracking",
|
||||||
|
"/metrics",
|
||||||
|
}
|
||||||
|
|
||||||
|
var ignoreUserAgents = []string{
|
||||||
|
"Prometheus",
|
||||||
|
"UptimeRobot",
|
||||||
|
}
|
||||||
|
|
||||||
func Echo(next echo.HandlerFunc) echo.HandlerFunc {
|
func Echo(next echo.HandlerFunc) echo.HandlerFunc {
|
||||||
ts = StartTrackingService()
|
ts = StartTrackingService()
|
||||||
return func(c echo.Context) error {
|
return func(c echo.Context) error {
|
||||||
fmt.Printf("Tracking visit: %s %s\n", c.RealIP(), c.Request().URL.Path)
|
fmt.Printf("Tracking visit: %s %s\n", c.RealIP(), c.Request().URL.Path)
|
||||||
|
// Check if the path is in the ignore list
|
||||||
|
for _, path := range ignorePaths {
|
||||||
|
if c.Request().URL.Path == path {
|
||||||
|
return next(c) // Skip tracking for ignored paths
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the user agent is in the ignore list
|
||||||
|
for _, ua := range ignoreUserAgents {
|
||||||
|
if strings.Contains(c.Request().UserAgent(), ua) {
|
||||||
|
return next(c) // Skip tracking for ignored user agents
|
||||||
|
}
|
||||||
|
}
|
||||||
go ts.AddVisit(c)
|
go ts.AddVisit(c)
|
||||||
return next(c)
|
return next(c)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user