Skip to content

Commit

Permalink
refactor: update client api
Browse files Browse the repository at this point in the history
  • Loading branch information
franklinkim committed Mar 12, 2024
1 parent 202a816 commit 2c68979
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
40 changes: 24 additions & 16 deletions measurementprotocol/v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
type (
Client struct {
l *zap.Logger
url string
path string
host string
cookies []string
trackingID string
measurementID string
Expand All @@ -35,6 +36,12 @@ func ClientWithHTTPClient(v *http.Client) ClientOption {
}
}

func ClientWithPath(v string) ClientOption {
return func(o *Client) {
o.path = v
}
}

func ClientWithCookies(v ...string) ClientOption {
return func(o *Client) {
o.cookies = append(o.cookies, v...)
Expand All @@ -51,10 +58,11 @@ func ClientWithMiddlewares(v ...ClientMiddleware) ClientOption {
// ~ Constructor
// ------------------------------------------------------------------------------------------------

func NewClient(l *zap.Logger, url, trackingID string, opts ...ClientOption) *Client {
func NewClient(l *zap.Logger, host, trackingID string, opts ...ClientOption) *Client {
inst := &Client{
l: l,
url: url,
host: host,
path: "/g/collect",
cookies: []string{"gtm_auth", "gtm_debug", "gtm_preview"},
trackingID: trackingID,
protocolVersion: "2",
Expand Down Expand Up @@ -87,32 +95,32 @@ func (c *Client) HTTPClient() *http.Client {
// ~ Public methods
// ------------------------------------------------------------------------------------------------

func (c *Client) Send(r *http.Request, event *Event) error {
next := c.SendRaw
for _, middleware := range c.middlewares {
next = middleware(next)
}
return next(r, event)
}

func (c *Client) SendType(r *http.Request, event Marshler) error {
func (c *Client) Send(r *http.Request, event Marshler) error {
e, err := event.MarshalMPv2()
if err != nil {
return err
}
return c.Send(r, e)
return c.SendEvent(r, e)
}

func (c *Client) SendEvent(r *http.Request, event *Event) error {
next := c.SendRawEvent
for _, middleware := range c.middlewares {
next = middleware(next)
}
return next(r, event)
}

func (c *Client) SendRaw(r *http.Request, event *Event) error {
func (c *Client) SendRawEvent(r *http.Request, event *Event) error {
values, body, err := Encode(event)
if err != nil {
return errors.Wrap(err, "failed to marshall event")
return errors.Wrap(err, "failed to encode event")
}

req, err := http.NewRequestWithContext(
r.Context(),
http.MethodPost,
fmt.Sprintf("%s?%s", c.url, EncodeValues(values)),
fmt.Sprintf("%s%s?%s", c.host, c.path, EncodeValues(values)),
body,
)
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions measurementprotocol/v2/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func MiddlewareTrackingID(v string) ClientMiddleware {
return func(next ClientHandler) ClientHandler {
return func(r *http.Request, event *Event) error {
event.TrackingID = &v
return nil
return next(r, event)
}
}
}
Expand All @@ -27,7 +27,7 @@ func MiddlewarProtocolVersion(v string) ClientMiddleware {
return func(next ClientHandler) ClientHandler {
return func(r *http.Request, event *Event) error {
event.ProtocolVersion = &v
return nil
return next(r, event)
}
}
}
Expand All @@ -36,7 +36,7 @@ func MiddlewarIgnoreReferrer(v string) ClientMiddleware {
return func(next ClientHandler) ClientHandler {
return func(r *http.Request, event *Event) error {
event.IgnoreReferrer = &v
return nil
return next(r, event)
}
}
}
Expand All @@ -47,7 +47,7 @@ func MiddlewarDebug(next ClientHandler) ClientHandler {
if value, _ := r.Cookie("gtm_debug"); value != nil {
event.IsDebug = &v
}
return nil
return next(r, event)
}
}

Expand All @@ -57,7 +57,7 @@ func MiddlewarClientID(next ClientHandler) ClientHandler {
clientID := strings.TrimPrefix(value.Value, "GA1.1.")
event.ClientID = &clientID
}
return nil
return next(r, event)
}
}

Expand All @@ -69,6 +69,6 @@ func MiddlewarDocument(next ClientHandler) ClientHandler {
event.DocumentLocation = &referrer.Path
event.DocumentHostname = &referrer.Host
}
return nil
return next(r, event)
}
}

0 comments on commit 2c68979

Please sign in to comment.