feat: add session id

This commit is contained in:
Kevin Franklin Kim 2024-03-12 09:03:26 +01:00
parent 14ce26b622
commit 131c7f3f87
No known key found for this signature in database
2 changed files with 16 additions and 1 deletions

View File

@ -78,6 +78,7 @@ func NewClient(l *zap.Logger, host, trackingID string, opts ...ClientOption) *Cl
MiddlewarProtocolVersion("2"),
MiddlewarDebug,
MiddlewarClientID,
MiddlewarSessionID(inst.measurementID),
MiddlewarDocument,
)
return inst

View File

@ -61,12 +61,26 @@ func MiddlewarClientID(next ClientHandler) ClientHandler {
}
}
func MiddlewarSessionID(measurementID string) ClientMiddleware {
return func(next ClientHandler) ClientHandler {
return func(r *http.Request, event *Event) error {
if value, _ := r.Cookie("_ga_" + measurementID); value != nil {
if value := strings.Split(strings.TrimPrefix(value.Value, "GA1.1."), "."); len(value) > 0 {
event.SessionID = &value[0]
}
}
return next(r, event)
}
}
}
func MiddlewarDocument(next ClientHandler) ClientHandler {
return func(r *http.Request, event *Event) error {
if referrer, err := url.Parse(r.Referer()); err != nil {
return err
} else {
event.DocumentLocation = &referrer.Path
location := referrer.RequestURI()
event.DocumentLocation = &location
event.DocumentHostname = &referrer.Host
}
return next(r, event)