Collect
pkg/collect. High-level server that fronts GA4 traffic and forwards it to a tagging server.
Constructor
go
func New(l *zap.Logger, opts ...Option) (*Collect, error)Options
go
WithTagging(url string) // downstream tagging server URL
WithTaggingClient(c *http.Client) // override HTTP client
WithGTagHTTPMiddlewares(v ...gtaghttp.Middleware) // /g/collect middleware chain
WithMPv2HTTPMiddlewares(v ...mpv2http.Middleware) // /mp/collect middleware chainMethods
go
func (c *Collect) GTagHTTPHandler(w http.ResponseWriter, r *http.Request)
func (c *Collect) MPv2HTTPHandler(w http.ResponseWriter, r *http.Request)Both have signature http.HandlerFunc so they mount on any router.
Behavior
For each request:
- Decode payload using the per-protocol
Handler(...)— invalid requests get an HTTP error and never reach middleware. - Run the configured middleware chain.
- Re-encode the payload and
POSTit to${taggingURL}${path}, where path is/g/collectfor GTag and/mp/collectfor MPv2. Inboundr.Headeris cloned to the upstream request. - Map upstream non-200 responses to
500 Internal Server Erroron the inbound side.
Defaults
taggingClientdefaults tohttp.DefaultClient(override withWithTaggingClient).- No middlewares by default. The chain is purely opt-in via the
With…Middlewaresoptions.
Example
go
c, err := collect.New(l,
collect.WithTagging("https://sgtm.example.com"),
collect.WithMPv2HTTPMiddlewares(
mpv2http.MiddlewareLogger,
mpv2http.MiddlewareClientID,
mpv2http.MiddlewareIPOverride,
mpv2http.MiddlewareUserAgent,
mpv2http.MiddlewarePageLocation,
mpv2http.MiddlewareEngagementTime,
mpv2http.MiddlewareWithTimeout(2 * time.Second),
),
)
http.HandleFunc("/g/collect", c.GTagHTTPHandler)
http.HandleFunc("/mp/collect", c.MPv2HTTPHandler)