Skip to content

Commit

Permalink
feat: split into middlewares
Browse files Browse the repository at this point in the history
  • Loading branch information
franklinkim committed Mar 11, 2024
1 parent c1221f8 commit 202a816
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 35 deletions.
44 changes: 9 additions & 35 deletions measurementprotocol/v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"strings"

"github.com/pkg/errors"
"go.uber.org/zap"
Expand All @@ -16,7 +14,6 @@ type (
l *zap.Logger
url string
cookies []string
richsstsse string
trackingID string
measurementID string
protocolVersion string
Expand Down Expand Up @@ -66,6 +63,15 @@ func NewClient(l *zap.Logger, url, trackingID string, opts ...ClientOption) *Cli
for _, opt := range opts {
opt(inst)
}
inst.middlewares = append(inst.middlewares,
MiddlewareRichsstsse,
MiddlewareTrackingID(inst.trackingID),
MiddlewarIgnoreReferrer("1"),
MiddlewarProtocolVersion("2"),
MiddlewarDebug,
MiddlewarClientID,
MiddlewarDocument,
)
return inst
}

Expand All @@ -82,42 +88,10 @@ func (c *Client) HTTPClient() *http.Client {
// ------------------------------------------------------------------------------------------------

func (c *Client) Send(r *http.Request, event *Event) error {
yes := "1"

// set default values
event.TrackingID = &c.trackingID
event.Richsstsse = &c.richsstsse
event.ProtocolVersion = &c.protocolVersion

event.IgnoreReferrer = &yes

{ // set referrer parameter
if referrer, err := url.Parse(r.Referer()); err != nil {
c.l.With(zap.Error(err)).Warn("failed to parse referrer")
} else {
event.DocumentLocation = &referrer.Path
event.DocumentHostname = &referrer.Host
}
}

{ // TODO check
if value, _ := r.Cookie("gtm_debug"); value != nil {
event.IsDebug = &yes
}
}

{ // set client id
if value, _ := r.Cookie("_ga"); value != nil {
clientID := strings.TrimPrefix(value.Value, "GA1.1.")
event.ClientID = &clientID
}
}

next := c.SendRaw
for _, middleware := range c.middlewares {
next = middleware(next)
}

return next(r, event)
}

Expand Down
74 changes: 74 additions & 0 deletions measurementprotocol/v2/middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package v2

import (
"net/http"
"net/url"
"strings"
)

func MiddlewareRichsstsse(next ClientHandler) ClientHandler {
v := ""
return func(r *http.Request, event *Event) error {
event.Richsstsse = &v
return nil
}
}

func MiddlewareTrackingID(v string) ClientMiddleware {
return func(next ClientHandler) ClientHandler {
return func(r *http.Request, event *Event) error {
event.TrackingID = &v
return nil
}
}
}

func MiddlewarProtocolVersion(v string) ClientMiddleware {
return func(next ClientHandler) ClientHandler {
return func(r *http.Request, event *Event) error {
event.ProtocolVersion = &v
return nil
}
}
}

func MiddlewarIgnoreReferrer(v string) ClientMiddleware {
return func(next ClientHandler) ClientHandler {
return func(r *http.Request, event *Event) error {
event.IgnoreReferrer = &v
return nil
}
}
}

func MiddlewarDebug(next ClientHandler) ClientHandler {
v := "1"
return func(r *http.Request, event *Event) error {
if value, _ := r.Cookie("gtm_debug"); value != nil {
event.IsDebug = &v
}
return nil
}
}

func MiddlewarClientID(next ClientHandler) ClientHandler {
return func(r *http.Request, event *Event) error {
if value, _ := r.Cookie("_ga"); value != nil {
clientID := strings.TrimPrefix(value.Value, "GA1.1.")
event.ClientID = &clientID
}
return nil
}
}

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
event.DocumentHostname = &referrer.Host
}
return nil
}
}

0 comments on commit 202a816

Please sign in to comment.