Skip to content

Commit

Permalink
feat: add session id
Browse files Browse the repository at this point in the history
  • Loading branch information
franklinkim committed Mar 12, 2024
1 parent 14ce26b commit 131c7f3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions measurementprotocol/v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion measurementprotocol/v2/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 131c7f3

Please sign in to comment.