mirror of
https://github.com/foomo/sesamy-go.git
synced 2025-10-16 12:35:43 +00:00
Introduced `GTagMiddlewarWithoutCancel` and `MPv2MiddlewarWithoutCancel` to allow handling of requests with contexts where cancellation signals are ignored. This improves flexibility for specific use cases where cancel propagation is not desired.
71 lines
1.9 KiB
Go
71 lines
1.9 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/foomo/sesamy-go/pkg/encoding/mpv2"
|
|
"github.com/foomo/sesamy-go/pkg/session"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func MPv2MiddlewarSessionID(measurementID string) MPv2Middleware {
|
|
measurementID = strings.Split(measurementID, "-")[1]
|
|
return func(next MPv2Handler) MPv2Handler {
|
|
return func(r *http.Request, payload *mpv2.Payload[any]) error {
|
|
if payload.SessionID == "" {
|
|
value, err := session.ParseGASessionID(r, measurementID)
|
|
if err != nil && !errors.Is(err, http.ErrNoCookie) {
|
|
return errors.Wrap(err, "failed to parse client cookie")
|
|
}
|
|
payload.SessionID = value
|
|
}
|
|
return next(r, payload)
|
|
}
|
|
}
|
|
}
|
|
|
|
func MPv2MiddlewarClientID(next MPv2Handler) MPv2Handler {
|
|
return func(r *http.Request, payload *mpv2.Payload[any]) error {
|
|
if payload.ClientID == "" {
|
|
value, err := session.ParseGAClientID(r)
|
|
if err != nil && !errors.Is(err, http.ErrNoCookie) {
|
|
return errors.Wrap(err, "failed to parse client cookie")
|
|
}
|
|
payload.ClientID = value
|
|
}
|
|
return next(r, payload)
|
|
}
|
|
}
|
|
|
|
func MPv2MiddlewarDebugMode(next MPv2Handler) MPv2Handler {
|
|
return func(r *http.Request, payload *mpv2.Payload[any]) error {
|
|
if !payload.DebugMode {
|
|
payload.DebugMode = session.IsGTMDebug(r)
|
|
}
|
|
return next(r, payload)
|
|
}
|
|
}
|
|
|
|
func MPv2MiddlewarWithoutCancel(next MPv2Handler) MPv2Handler {
|
|
return func(r *http.Request, payload *mpv2.Payload[any]) error {
|
|
return next(r.WithContext(context.WithoutCancel(r.Context())), payload)
|
|
}
|
|
}
|
|
|
|
func MPv2MiddlewareUserID(cookieName string) MPv2Middleware {
|
|
return func(next MPv2Handler) MPv2Handler {
|
|
return func(r *http.Request, payload *mpv2.Payload[any]) error {
|
|
if payload.UserID == "" {
|
|
value, err := r.Cookie(cookieName)
|
|
if err != nil && !errors.Is(err, http.ErrNoCookie) {
|
|
return err
|
|
}
|
|
payload.UserID = value.Value
|
|
}
|
|
return next(r, payload)
|
|
}
|
|
}
|
|
}
|