From fbb743cb969a9dcdbb68a1c5498fcd58c755bebf Mon Sep 17 00:00:00 2001 From: Kevin Franklin Kim Date: Mon, 4 Mar 2024 15:10:28 +0100 Subject: [PATCH] fix: lint errors --- .golangci.yml | 2 +- integration/watermill/gtm/provider.go | 4 +- integration/watermill/gtm/publisher.go | 71 ++++++++----------- integration/watermill/keel/subscriber.go | 12 ++-- measurementprotocol/getset.go | 2 +- measurementprotocol/v2/client.go | 2 - measurementprotocol/v2/event.go | 14 ++-- .../v2/event/addpaymentinfo.go | 6 +- .../v2/event/addshippinginfo.go | 14 ++-- measurementprotocol/v2/event/addtocart.go | 4 +- measurementprotocol/v2/event/addtowishlist.go | 4 +- measurementprotocol/v2/event/begincheckout.go | 12 ++-- measurementprotocol/v2/event/item.go | 66 ++++++++--------- measurementprotocol/v2/event/purchase.go | 6 +- measurementprotocol/v2/event/refund.go | 6 +- .../v2/event/removefromcart.go | 4 +- measurementprotocol/v2/eventparameter.go | 11 ++- measurementprotocol/v2/eventparamternumber.go | 4 +- measurementprotocol/v2/item.go | 49 +++++++------ measurementprotocol/v2/unmarshal.go | 8 +-- measurementprotocol/v2/unmarshal_test.go | 6 +- 21 files changed, 145 insertions(+), 162 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index f3f5ee7..3ee261a 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -77,7 +77,7 @@ linters: - predeclared # find code that shadows one of Go's predeclared identifiers [fast: true, auto-fix: false] - promlinter # Check Prometheus metrics naming via promlint [fast: true, auto-fix: false] - revive # Fast, configurable, extensible, flexible, and beautiful linter for Go. Drop-in replacement of golint. [fast: false, auto-fix: false] - - tagliatelle # Checks the struct tags. [fast: true, auto-fix: false] + #- tagliatelle # Checks the struct tags. [fast: true, auto-fix: false] - testpackage # linter that makes you use a separate _test package [fast: true, auto-fix: false] - thelper # thelper detects golang test helpers without t.Helper() call and checks the consistency of test helpers [fast: false, auto-fix: false] - unconvert # Remove unnecessary type conversions [fast: false, auto-fix: false] diff --git a/integration/watermill/gtm/provider.go b/integration/watermill/gtm/provider.go index 7e92d95..01a4a71 100644 --- a/integration/watermill/gtm/provider.go +++ b/integration/watermill/gtm/provider.go @@ -1,6 +1,6 @@ package gtm const ( - HeaderUUID = "Message-Uuid" - ProviderName = "gtm" + HeaderUUID = "Message-Uuid" + ProviderName = "gtm" ) diff --git a/integration/watermill/gtm/publisher.go b/integration/watermill/gtm/publisher.go index e43bda1..1707501 100644 --- a/integration/watermill/gtm/publisher.go +++ b/integration/watermill/gtm/publisher.go @@ -1,12 +1,9 @@ package gtm import ( - "bytes" "encoding/json" - "io" "net/http" - "github.com/ThreeDotsLabs/watermill" "github.com/ThreeDotsLabs/watermill/message" mpv2 "github.com/foomo/sesamy/measurementprotocol/v2" "github.com/pkg/errors" @@ -19,7 +16,6 @@ var ( type ( Publisher struct { - //l watermill.LoggerAdapter url string client *http.Client marshalMessageFunc MarshalMessageFunc @@ -77,7 +73,6 @@ func (p *Publisher) Publish(topic string, messages ...*message.Message) error { } for _, msg := range messages { - var event *mpv2.Event if err := json.Unmarshal(msg.Payload, &event); err != nil { return err @@ -88,32 +83,40 @@ func (p *Publisher) Publish(topic string, messages ...*message.Message) error { return err } - u := p.url + "?" + values.Encode() + "&richsstsse" + var richsstsse bool + if values.Has("richsstsse") { + values.Del("richsstsse") + richsstsse = true + } - req, err := http.NewRequestWithContext(msg.Context(), "POST", u, body) + u := p.url + "?" - for s, s2 := range msg.Metadata { - req.Header.Set(s, s2) + if richsstsse { + u += "&richsstsse" } - //req, err := p.marshalMessageFunc(topic, msg) + req, err := http.NewRequestWithContext(msg.Context(), http.MethodPost, u, body) if err != nil { - return errors.Wrapf(err, "cannot marshal message %s", msg.UUID) + return errors.Wrap(err, "failed to create request") } - logFields := watermill.LogFields{ - "uuid": msg.UUID, - "provider": ProviderName, + for s, s2 := range msg.Metadata { + req.Header.Set(s, s2) } - //p.l.Trace("Publishing message", logFields) + // logFields := watermill.LogFields{ + // "uuid": msg.UUID, + // "provider": ProviderName, + // } + + // p.l.Trace("Publishing message", logFields) resp, err := p.client.Do(req) if err != nil { return errors.Wrapf(err, "publishing message %s failed", msg.UUID) } - if err = p.handleResponseBody(resp, logFields); err != nil { + if err = p.handleResponseBody(resp); err != nil { return err } @@ -121,7 +124,7 @@ func (p *Publisher) Publish(topic string, messages ...*message.Message) error { return errors.Wrap(ErrErrorResponse, resp.Status) } - //p.l.Trace("Message published", logFields) + // p.l.Trace("Message published", logFields) } return nil @@ -140,40 +143,26 @@ func (p *Publisher) Close() error { // ~ Private methods // ------------------------------------------------------------------------------------------------ -func (p *Publisher) handleResponseBody(resp *http.Response, logFields watermill.LogFields) error { +func (p *Publisher) handleResponseBody(resp *http.Response) error { defer resp.Body.Close() if resp.StatusCode < http.StatusBadRequest { return nil } - body, err := io.ReadAll(resp.Body) - if err != nil { - return errors.Wrap(err, "could not read response body") - } + // body, err := io.ReadAll(resp.Body) + // if err != nil { + // return errors.Wrap(err, "could not read response body") + // } - logFields = logFields.Add(watermill.LogFields{ - "http_status": resp.StatusCode, - "http_response": string(body), - }) - //p.l.Info("Server responded with error", logFields) + // logFields = logFields.Add(watermill.LogFields{ + // "http_status": resp.StatusCode, + // "http_response": string(body), + // }) + // p.l.Info("Server responded with error", logFields) return nil } // MarshalMessageFunc transforms the message into a HTTP request to be sent to the specified url. type MarshalMessageFunc func(url string, msg *message.Message) (*http.Request, error) - -// DefaultMarshalMessageFunc transforms the message into a HTTP POST request. -// It encodes the UUID and Metadata in request headers. -func DefaultMarshalMessageFunc(url string, msg *message.Message) (*http.Request, error) { - - req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(msg.Payload)) - if err != nil { - return nil, err - } - - req.Header.Set(HeaderUUID, msg.UUID) - - return req, nil -} diff --git a/integration/watermill/keel/subscriber.go b/integration/watermill/keel/subscriber.go index e3c359a..fad5acd 100644 --- a/integration/watermill/keel/subscriber.go +++ b/integration/watermill/keel/subscriber.go @@ -28,11 +28,11 @@ var ( type ( Subscriber struct { - l *zap.Logger - uuidFunc func() string - messages chan *message.Message - middlewares []SubscriberMiddleware - closed bool + l *zap.Logger + uuidFunc func() string + messages chan *message.Message + middlewares []SubscriberMiddleware + closed bool } SubscriberOption func(*Subscriber) SubscriberHandler func(l *zap.Logger, r *http.Request, event *mpv2.Event) error @@ -60,7 +60,7 @@ func SubscriberWithLogger(fields ...zap.Field) SubscriberOption { o.middlewares = append(o.middlewares, func(next SubscriberHandler) SubscriberHandler { return func(l *zap.Logger, r *http.Request, event *mpv2.Event) error { fields = append(fields, zap.String("event_name", mp.GetDefault(event.EventName, "-").String())) - if labeler, ok := log.LabelerFromRequest(r);ok { + if labeler, ok := log.LabelerFromRequest(r); ok { labeler.Add(fields...) } return next(l.With(fields...), r, event) diff --git a/measurementprotocol/getset.go b/measurementprotocol/getset.go index f7c753a..a4ae5e9 100644 --- a/measurementprotocol/getset.go +++ b/measurementprotocol/getset.go @@ -83,7 +83,7 @@ func Get[T any](v *T) T { } func GetDefault[T any](v *T, fallback T) T { - if v == nil { + if v == nil { return fallback } return *v diff --git a/measurementprotocol/v2/client.go b/measurementprotocol/v2/client.go index 8f15d8b..97d20bc 100644 --- a/measurementprotocol/v2/client.go +++ b/measurementprotocol/v2/client.go @@ -57,7 +57,6 @@ func (c *Client) HTTPClient() *http.Client { // ------------------------------------------------------------------------------------------------ func (c *Client) Send(ctx context.Context, event *Event) error { - values, body, err := Marshal(event) if err != nil { return errors.Wrap(err, "failed to marshall event") @@ -90,4 +89,3 @@ func (c *Client) Send(ctx context.Context, event *Event) error { return nil } - diff --git a/measurementprotocol/v2/event.go b/measurementprotocol/v2/event.go index b0261f7..83fe159 100644 --- a/measurementprotocol/v2/event.go +++ b/measurementprotocol/v2/event.go @@ -146,7 +146,7 @@ type Event struct { // Defines a parameter for the current Event // Example: epn.plays_count: 42 EventParameterNumber map[string]string `json:"epn,omitempty" mapstructure:"epn,omitempty"` - // If the current event is set as a conversion on the admin interace the evfent will have this value present + // If the current event is set as a conversion on the admin interacted the evfent will have this value present // Example: 1 IsConversion *string `json:"_c,omitempty" mapstructure:"_c,omitempty"` // External Event @@ -181,7 +181,7 @@ type Event struct { // If the "_ga_THYNGSTER" cookie last session time value is older than 1800 seconds, the current event will have this value present. This will internally create a new "session_start" event on GA4. If this event is also a conversion the value will be "2" if not, will be "1" // Example: 1|2 SessionStart *string `json:"_ss,omitempty" mapstructure:"_ss,omitempty"` - // This seems to be related to the ServerSide hits, it's 0 if the FPLC Cookie is not present and to the current value if it's comming from a Cross Domain linker + // This seems to be related to the ServerSide hits, it's 0 if the FPLC Cookie is not present and to the current value if it's coming from a Cross Domain linker // Example: bVhVicbfiSXaGNxeawKaPlDQc9QXPD6bKcsn36Elden6wZNb7Q5X1iXlkTVP5iP3H3y76cgM3UIgHCaRsYfPoyLGlbiIYMPRjvnUU7KWbdWLagodzxjrlPnvaRZJkw FirstPartyLinkerCookie *string `json:"_fplc,omitempty" mapstructure:"_fplc,omitempty"` // If the current user has a GA4 session cookie, but not a GA (_ga) client id cookie, this parameter will be added to the hit @@ -202,7 +202,7 @@ type Event struct { // Example: JPY Currency *string `json:"cu,omitempty" mapstructure:"cu,omitempty"` // Example: - Items []Item `json:"pr,omitempty" mapstructure:"pr,omitempty"` + Items []*Item `json:"pr,omitempty" mapstructure:"pr,omitempty"` // Promotion Impression/Click Tracking. Promotion Id // Example: summer-offer PromotionID *string `json:"pi,omitempty" mapstructure:"pi,omitempty"` @@ -211,10 +211,10 @@ type Event struct { PromotionName *string `json:"pn,omitempty" mapstructure:"pn,omitempty"` // Promotion Impression/Click Tracking. Creative Name // Example: red-car - CreativeName *string `json:"cn,omitempty" mapstructure:"cn,omitempty"` + // CreativeName *string `json:"cn,omitempty" mapstructure:"cn,omitempty"` // Promotion Impression/Click Tracking. Promotion Slot / Position // Example: slide-3 - CreativeSlot *string `json:"cs,omitempty" mapstructure:"cs,omitempty"` + // CreativeSlot *string `json:"cs,omitempty" mapstructure:"cs,omitempty"` // Google Place ID: Refer to: https://developers.google.com/maps/documentation/places/web-service/place-id . Seems to be inherited from Firebase, not sure about the current use on GA4 // Example: ChIJiyj437sx3YAR9kUWC8QkLzQ LocationID *string `json:"lo,omitempty" mapstructure:"lo,omitempty"` @@ -254,12 +254,10 @@ type Event struct { PrivacySandboxCookieDeprecationLabel *string `json:"pscdl,omitempty" mapstructure:"pscdl,omitempty"` // A timestamp measuring the difference between the moment this parameter gets populated and the moment the navigation started on that particular page. TFD *string `json:"tfd,omitempty" mapstructure:"tfd,omitempty"` - SST *SST `json:"sst,omitempty" mapstructure:"sst,omitempty"` + SST *SST `json:"sst,omitempty" mapstructure:"sst,omitempty"` PAE *string `json:"pae,omitempty" mapstructure:"pae,omitempty"` // --- Unresolved --- Unknown map[string]any `json:"-" mapstructure:",remain"` } - - diff --git a/measurementprotocol/v2/event/addpaymentinfo.go b/measurementprotocol/v2/event/addpaymentinfo.go index eeca20f..ddd2ce0 100644 --- a/measurementprotocol/v2/event/addpaymentinfo.go +++ b/measurementprotocol/v2/event/addpaymentinfo.go @@ -48,14 +48,14 @@ type AddPaymentInfo struct { } func (e *AddPaymentInfo) MPv2() *mpv2.Event { - items := make([]mpv2.Item, len(e.Items)) + items := make([]*mpv2.Item, len(e.Items)) for i, item := range e.Items { - items[i] = *item.MPv2() + items[i] = item.MPv2() } return &mpv2.Event{ Currency: mp.SetString(e.Currency), EventParameter: map[string]string{ - mpv2.EventParameterCoupon.String(): e.Coupon, + mpv2.EventParameterCoupon.String(): e.Coupon, mpv2.EventParameterPaymentType.String(): e.PaymentType, }, EventParameterNumber: map[string]string{ diff --git a/measurementprotocol/v2/event/addshippinginfo.go b/measurementprotocol/v2/event/addshippinginfo.go index cbebd0e..774bc2f 100644 --- a/measurementprotocol/v2/event/addshippinginfo.go +++ b/measurementprotocol/v2/event/addshippinginfo.go @@ -40,22 +40,22 @@ AddShippingInfo https://developers.google.com/tag-platform/gtagjs/reference/even Query: v=2&tid=G-PZ5ELRCR31>m=45je42t1v9177778896z89175355532za220&_p=1709357665402&_dbg=1&gcd=13l3l3l3l1&npa=0&dma_cps=sypham&dma=1&cid=1220643501.1708014725&ul=en-us&sr=3840x1600&_fplc=0&ur=DE-BY&uaa=arm&uab=64&uafvl=Chromium%3B122.0.6261.69%7CNot(A%253ABrand%3B24.0.0.0%7CGoogle%2520Chrome%3B122.0.6261.69&uamb=0&uam=&uap=macOS&uapv=14.3.1&uaw=0&are=1&pscdl=noapi&_eu=IA&sst.uc=DE&sst.etld=google.de&sst.gcsub=region1&sst.gcd=13l3l3l3l1&sst.tft=1709357665402&_s=10&cu=USD&sid=1709529821&sct=9&seg=0&dl=https%3A%2F%2Fsniffer.local.bestbytes.net%2F%3Fgtm_debug%3D1709357665301&dr=https%3A%2F%2Ftagassistant.google.com%2F&dt=Server%20Side%20Tracking%20Prototype%20(codename%3A%20sniffer)&en=add_payment_info&_ss=1&pr1=idSKU_12345~nmStan%20and%20Friends%20Tee~afGoogle%20Store~cpSUMMER_FUN~ds2.22~lp5~brGoogle~caApparel~c2Adult~c3Shirts~c4Crew~c5Short%20sleeve~lirelated_products~lnRelated%20products~vagreen~loChIJIQBpAG2ahYAR_6128GcTUEo~pr10.01~qt3&epn.value=30.03&ep.coupon=SUMMER_FUN&ep.shipping_tier=Ground&tfd=137238301&richsstsse */ type AddShippingInfo struct { - Currency string - Value float64 - Coupon string + Currency string + Value float64 + Coupon string ShippingTier string - Items []*Item + Items []*Item } func (e *AddShippingInfo) MPv2() *mpv2.Event { - items := make([]mpv2.Item, len(e.Items)) + items := make([]*mpv2.Item, len(e.Items)) for i, item := range e.Items { - items[i] = *item.MPv2() + items[i] = item.MPv2() } return &mpv2.Event{ Currency: mp.SetString(e.Currency), EventParameter: map[string]string{ - mpv2.EventParameterCoupon.String(): e.Coupon, + mpv2.EventParameterCoupon.String(): e.Coupon, mpv2.EventParameterShippingTier.String(): e.ShippingTier, }, EventParameterNumber: map[string]string{ diff --git a/measurementprotocol/v2/event/addtocart.go b/measurementprotocol/v2/event/addtocart.go index b6d4d7e..c52c7b5 100644 --- a/measurementprotocol/v2/event/addtocart.go +++ b/measurementprotocol/v2/event/addtocart.go @@ -44,9 +44,9 @@ type AddToCart struct { } func (e *AddToCart) MPv2() *mpv2.Event { - items := make([]mpv2.Item, len(e.Items)) + items := make([]*mpv2.Item, len(e.Items)) for i, item := range e.Items { - items[i] = *item.MPv2() + items[i] = item.MPv2() } return &mpv2.Event{ Currency: mp.SetString(e.Currency), diff --git a/measurementprotocol/v2/event/addtowishlist.go b/measurementprotocol/v2/event/addtowishlist.go index 7ac1253..7af099b 100644 --- a/measurementprotocol/v2/event/addtowishlist.go +++ b/measurementprotocol/v2/event/addtowishlist.go @@ -44,9 +44,9 @@ type AddToWishlist struct { } func (e *AddToWishlist) MPv2() *mpv2.Event { - items := make([]mpv2.Item, len(e.Items)) + items := make([]*mpv2.Item, len(e.Items)) for i, item := range e.Items { - items[i] = *item.MPv2() + items[i] = item.MPv2() } return &mpv2.Event{ Currency: mp.SetString(e.Currency), diff --git a/measurementprotocol/v2/event/begincheckout.go b/measurementprotocol/v2/event/begincheckout.go index c0b48ee..0a24a42 100644 --- a/measurementprotocol/v2/event/begincheckout.go +++ b/measurementprotocol/v2/event/begincheckout.go @@ -39,16 +39,16 @@ BeginCheckout https://developers.google.com/tag-platform/gtagjs/reference/events Query: v=2&tid=G-123456>m=45je42t1v9177778896z89175355532za200&_p=1709325262551&gcd=13l3l3l3l1&npa=0&dma_cps=sypham&dma=1&cid=1220643501.1708014725&ul=en-us&sr=3840x1600&_fplc=0&ur=DE-BY&uaa=arm&uab=64&uafvl=Chromium%3B122.0.6261.69%7CNot(A%253ABrand%3B24.0.0.0%7CGoogle%2520Chrome%3B122.0.6261.69&uamb=0&uam=&uap=macOS&uapv=14.3.1&uaw=0&are=1&pscdl=noapi&_eu=IA&sst.uc=DE&sst.etld=google.de&sst.gcsub=region1&sst.gcd=13l3l3l3l1&sst.tft=1709325262551&_s=5&cu=USD&sid=1709445696&sct=8&seg=0&dl=https%3A%2F%2Fsniffer.local.bestbytes.net%2F&dt=Server%20Side%20Tracking%20Prototype%20(codename%3A%20sniffer)&en=begin_checkout&_ss=1&pr1=idSKU_12345~nmStan%20and%20Friends%20Tee~afGoogle%20Store~cpSUMMER_FUN~ds2.22~lp5~brGoogle~caApparel~c2Adult~c3Shirts~c4Crew~c5Short%20sleeve~lirelated_products~lnRelated%20products~vagreen~loChIJIQBpAG2ahYAR_6128GcTUEo~pr10.01~qt3&epn.value=30.03&ep.coupon=SUMMER_FUN&tfd=120434230&richsstsse */ type BeginCheckout struct { - Currency string - Value float64 - Coupon string - Items []*Item + Currency string + Value float64 + Coupon string + Items []*Item } func (e *BeginCheckout) MPv2() *mpv2.Event { - items := make([]mpv2.Item, len(e.Items)) + items := make([]*mpv2.Item, len(e.Items)) for i, item := range e.Items { - items[i] = *item.MPv2() + items[i] = item.MPv2() } return &mpv2.Event{ Currency: mp.SetString(e.Currency), diff --git a/measurementprotocol/v2/event/item.go b/measurementprotocol/v2/event/item.go index f940b65..0f53170 100644 --- a/measurementprotocol/v2/event/item.go +++ b/measurementprotocol/v2/event/item.go @@ -6,45 +6,45 @@ import ( ) type Item struct { - ID string - Name string - Brand string + ID string + Name string + Brand string CategoryHierarchy1 string CategoryHierarchy2 string CategoryHierarchy3 string CategoryHierarchy4 string CategoryHierarchy5 string - Price string - Quantity string - Variant string - Coupon string - Discount float64 - Index int - ListName string - ListID string - Affiliation string - LocationID string + Price string + Quantity string + Variant string + Coupon string + Discount float64 + Index int + ListName string + ListID string + Affiliation string + LocationID string } func (e *Item) MPv2() *mpv2.Item { - return &mpv2.Item{ - ID: mp.SetString(e.ID), - Name: mp.SetString(e.Name), - Brand: mp.SetString(e.Brand), - CategoryHierarchy1: mp.SetString(e.CategoryHierarchy1), - CategoryHierarchy2: mp.SetString(e.CategoryHierarchy2), - CategoryHierarchy3: mp.SetString(e.CategoryHierarchy3), - CategoryHierarchy4: mp.SetString(e.CategoryHierarchy4), - CategoryHierarchy5: mp.SetString(e.CategoryHierarchy5), - Price: mp.SetString(e.Price), - Quantity: mp.SetString(e.Quantity), - Variant: mp.SetString(e.Variant), - Coupon: mp.SetString(e.Coupon), - Discount: mp.SetFloat64(e.Discount), - ListName: mp.SetString(e.ListName), - ListID: mp.SetString(e.ListID), - ListPosition: mp.SetInt(e.Index), - Affiliation: mp.SetString(e.Affiliation), - LocationID: mp.SetString(e.LocationID), - } + return &mpv2.Item{ + ID: mp.SetString(e.ID), + Name: mp.SetString(e.Name), + Brand: mp.SetString(e.Brand), + CategoryHierarchy1: mp.SetString(e.CategoryHierarchy1), + CategoryHierarchy2: mp.SetString(e.CategoryHierarchy2), + CategoryHierarchy3: mp.SetString(e.CategoryHierarchy3), + CategoryHierarchy4: mp.SetString(e.CategoryHierarchy4), + CategoryHierarchy5: mp.SetString(e.CategoryHierarchy5), + Price: mp.SetString(e.Price), + Quantity: mp.SetString(e.Quantity), + Variant: mp.SetString(e.Variant), + Coupon: mp.SetString(e.Coupon), + Discount: mp.SetFloat64(e.Discount), + ListName: mp.SetString(e.ListName), + ListID: mp.SetString(e.ListID), + ListPosition: mp.SetInt(e.Index), + Affiliation: mp.SetString(e.Affiliation), + LocationID: mp.SetString(e.LocationID), + } } diff --git a/measurementprotocol/v2/event/purchase.go b/measurementprotocol/v2/event/purchase.go index 8e0f8dd..930664c 100644 --- a/measurementprotocol/v2/event/purchase.go +++ b/measurementprotocol/v2/event/purchase.go @@ -48,13 +48,13 @@ type Purchase struct { Coupon string Shipping float64 Tax float64 - Items []Item + Items []*Item } func (e *Purchase) MPv2() *mpv2.Event { - items := make([]mpv2.Item, len(e.Items)) + items := make([]*mpv2.Item, len(e.Items)) for i, item := range e.Items { - items[i] = *item.MPv2() + items[i] = item.MPv2() } return &mpv2.Event{ Currency: mp.SetString(e.Currency), diff --git a/measurementprotocol/v2/event/refund.go b/measurementprotocol/v2/event/refund.go index 19505da..3728041 100644 --- a/measurementprotocol/v2/event/refund.go +++ b/measurementprotocol/v2/event/refund.go @@ -48,13 +48,13 @@ type Refund struct { Coupon string Shipping float64 Tax float64 - Items []Item + Items []*Item } func (e *Refund) MPv2() *mpv2.Event { - items := make([]mpv2.Item, len(e.Items)) + items := make([]*mpv2.Item, len(e.Items)) for i, item := range e.Items { - items[i] = *item.MPv2() + items[i] = item.MPv2() } return &mpv2.Event{ Currency: mp.SetString(e.Currency), diff --git a/measurementprotocol/v2/event/removefromcart.go b/measurementprotocol/v2/event/removefromcart.go index 615dbcc..22122de 100644 --- a/measurementprotocol/v2/event/removefromcart.go +++ b/measurementprotocol/v2/event/removefromcart.go @@ -44,9 +44,9 @@ type RemoveFromCart struct { } func (e *RemoveFromCart) MPv2() *mpv2.Event { - items := make([]mpv2.Item, len(e.Items)) + items := make([]*mpv2.Item, len(e.Items)) for i, item := range e.Items { - items[i] = *item.MPv2() + items[i] = item.MPv2() } return &mpv2.Event{ Currency: mp.SetString(e.Currency), diff --git a/measurementprotocol/v2/eventparameter.go b/measurementprotocol/v2/eventparameter.go index 5b883b8..596847a 100644 --- a/measurementprotocol/v2/eventparameter.go +++ b/measurementprotocol/v2/eventparameter.go @@ -3,15 +3,14 @@ package v2 type EventParameter string const ( - EventParameterMethod EventParameter = "method" - EventParameterCoupon EventParameter = "coupon" - EventParameterPaymentType EventParameter = "payment_type" - EventParameterShippingTier EventParameter = "shipping_tier" + EventParameterMethod EventParameter = "method" + EventParameterCoupon EventParameter = "coupon" + EventParameterPaymentType EventParameter = "payment_type" + EventParameterShippingTier EventParameter = "shipping_tier" EventParameterTransactionID EventParameter = "transaction_id" - EventParameterSearchTerm EventParameter = "search_term" + EventParameterSearchTerm EventParameter = "search_term" ) func (s EventParameter) String() string { return string(s) } - diff --git a/measurementprotocol/v2/eventparamternumber.go b/measurementprotocol/v2/eventparamternumber.go index 85a4166..b7cd282 100644 --- a/measurementprotocol/v2/eventparamternumber.go +++ b/measurementprotocol/v2/eventparamternumber.go @@ -3,9 +3,9 @@ package v2 type EventParameterNumber string const ( - EventParameterNumberValue EventParameterNumber = "value" + EventParameterNumberValue EventParameterNumber = "value" EventParameterNumberShipping EventParameterNumber = "shipping" - EventParameterNumberTax EventParameterNumber = "tax" + EventParameterNumberTax EventParameterNumber = "tax" ) func (s EventParameterNumber) String() string { diff --git a/measurementprotocol/v2/item.go b/measurementprotocol/v2/item.go index 6eef7d3..e025599 100644 --- a/measurementprotocol/v2/item.go +++ b/measurementprotocol/v2/item.go @@ -1,45 +1,48 @@ package v2 -/** - promotion_id: "pi", - promotion_name: "pn", - creative_name: "cn", - creative_slot: "cs", - */ + +/* +* + + promotion_id: "pi", + promotion_name: "pn", + creative_name: "cn", + creative_slot: "cs", +*/ type Item struct { - // Exmaple: 12345 + // Example: 12345 ID *string `json:"id,omitempty" mapstructure:"id,omitempty"` // Example: Stan and Friends Tee Name *string `json:"nm,omitempty" mapstructure:"nm,omitempty"` - // Exmaple: Google + // Example: Google Brand *string `json:"br,omitempty" mapstructure:"br,omitempty"` - // Exmaple: men + // Example: men CategoryHierarchy1 *string `json:"ca,omitempty" mapstructure:"ca,omitempty"` - // Exmaple: t-shirts + // Example: t-shirts CategoryHierarchy2 *string `json:"c2,omitempty" mapstructure:"c2,omitempty"` - // Exmaple: men + // Example: men CategoryHierarchy3 *string `json:"c3,omitempty" mapstructure:"c3,omitempty"` - // Exmaple: men + // Example: men CategoryHierarchy4 *string `json:"c4,omitempty" mapstructure:"c4,omitempty"` - // Exmaple: men + // Example: men CategoryHierarchy5 *string `json:"c5,omitempty" mapstructure:"c5,omitempty"` - // Exmaple: Yellow + // Example: Yellow Variant *string `json:"va,omitempty" mapstructure:"va,omitempty"` - // Exmaple: 123.45 + // Example: 123.45 Price *string `json:"pr,omitempty" mapstructure:"pr,omitempty"` - // Exmaple: 1 + // Example: 1 Quantity *string `json:"qt,omitempty" mapstructure:"qt,omitempty"` - // Exmaple: 50%OFF + // Example: 50%OFF Coupon *string `json:"cp,omitempty" mapstructure:"cp,omitempty"` - // Exmaple: cross-selling: mens + // Example: cross-selling: mens ListName *string `json:"ln,omitempty" mapstructure:"ln,omitempty"` - // Exmaple: 10 + // Example: 10 ListPosition *string `json:"lp,omitempty" mapstructure:"lp,omitempty"` - // Exmaple: id-mens-123 + // Example: id-mens-123 ListID *string `json:"li,omitempty" mapstructure:"li,omitempty"` - // Exmaple: 10.00 + // Example: 10.00 Discount *string `json:"ds,omitempty" mapstructure:"ds,omitempty"` - // Exmaple: Foo Marketplace + // Example: Foo Marketplace Affiliation *string `json:"af,omitempty" mapstructure:"af,omitempty"` // Example: ChIJIQBpAG2ahYAR_6128GcTUEo - LocationID *string `json:"lo,omitempty" mapstructure:"lo,omitempty"` + LocationID *string `json:"lo,omitempty" mapstructure:"lo,omitempty"` } diff --git a/measurementprotocol/v2/unmarshal.go b/measurementprotocol/v2/unmarshal.go index b91c96c..0c36d42 100644 --- a/measurementprotocol/v2/unmarshal.go +++ b/measurementprotocol/v2/unmarshal.go @@ -25,7 +25,6 @@ const ( type Data map[string]any func Marshal(input *Event) (url.Values, io.Reader, error) { - a, err := json.Marshal(input) if err != nil { return nil, nil, err @@ -81,7 +80,6 @@ func Marshal(input *Event) (url.Values, io.Reader, error) { return ret, reader, nil } - func UnmarshalURLValues(input url.Values, output interface{}) error { data := Data{} @@ -147,7 +145,7 @@ func DecodeRegexValue(k string, v []string, r *regexp.Regexp, data Data, key str v = []map[string]any{} } v = append(v, value) - data[key] = value + data[key] = v return true, nil } } @@ -157,7 +155,7 @@ func DecodeRegexValue(k string, v []string, r *regexp.Regexp, data Data, key str // DecodeObjectValue e.g. `idSKU_123456` = map["id"]="SKU_123456" func DecodeObjectValue(s string) (map[string]any, error) { if len(s) == 0 { - return nil, nil + return nil, nil //nolint:nilnil } ret := map[string]any{} for _, part := range strings.Split(s, "~") { @@ -180,7 +178,7 @@ func EncodeObjectValue(s map[string]any) string { keys = append(keys, k) } sort.Strings(keys) - var ret []string + ret := make([]string, 0, len(keys)) for _, k := range keys { ret = append(ret, k+fmt.Sprintf("%s", s[k])) } diff --git a/measurementprotocol/v2/unmarshal_test.go b/measurementprotocol/v2/unmarshal_test.go index a74e8c5..1d3396e 100644 --- a/measurementprotocol/v2/unmarshal_test.go +++ b/measurementprotocol/v2/unmarshal_test.go @@ -10,7 +10,6 @@ import ( "github.com/stretchr/testify/require" ) - func TestUnmarshalURLValues(t *testing.T) { tests := []struct { name string @@ -56,7 +55,7 @@ func TestMarshalURLValues(t *testing.T) { // name: "add_to_cart", // args: "v=2&tid=G-123456>m=45je42s0v9175354889z89175348963za200&_p=1709297934217&_dbg=1&gcd=13l3l3l3l1&npa=0&dma_cps=sypham&dma=1&cid=1220643501.1708014725&ul=en-us&sr=3840x1600&_fplc=0&ur=DE-BY&uaa=arm&uab=64&uafvl=Chromium&uamb=0&uam=&uap=macOS&uapv=14.3.1&uaw=0&are=1&pscdl=noapi&_eu=IA&sst.uc=DE&sst.etld=google.de&sst.gcsub=region1&sst.gcd=13l3l3l3l1&sst.tft=1709297934217&_s=8&cu=USD&sid=1709296380&sct=7&seg=1&dl=https%3A%2F%2Fsniffer.cloud.bestbytes.net%2F%3Fgtm_debug%3D1709297933868&dr=https%3A%2F%2Ftagassistant.google.com%2F&dt=Server%20Side%20Tracking%20Prototype%20(codename%3A%20sniffer)&en=add_to_cart&pr1=idSKU_12345~nmStan%20and%20Friends%20Tee~afGoogle%20Store~cpSUMMER_FUN~ds2.22~lp5~brGoogle~caApparel~c2Adult~c3Shirts~c4Crew~c5Short%20sleeve~lirelated_products~lnRelated%20products~vagreen~loChIJIQBpAG2ahYAR_6128GcTUEo~pr10.01~qt3&epn.value=30.03&tfd=15129&richsstsse", // want: nil, - //}, + // }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -67,14 +66,13 @@ func TestMarshalURLValues(t *testing.T) { if !assert.Empty(t, e.Unknown) { t.Errorf("MarshalURLValues() = %v, want %v", e.Unknown, nil) } - if out, _, err := mpv2.Marshal(e); assert.NoError(t, err){ + if out, _, err := mpv2.Marshal(e); assert.NoError(t, err) { assert.EqualValues(t, u, out) } }) } } - func TestCollect_decodeMapValue(t *testing.T) { tests := []struct { name string