-
Notifications
You must be signed in to change notification settings - Fork 0
/
track.go
60 lines (48 loc) · 1.5 KB
/
track.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//nolint:tagliatelle // datahappy uses camel case
package datahappy
import (
"context"
"time"
"github.com/wearemojo/mojo-public-go/lib/ptr"
)
type RequestType string
const (
RequestTypeTrack RequestType = "track"
RequestTypeUpdate RequestType = "update"
RequestTypeDelete RequestType = "delete"
)
type TrackRequest struct {
ProjectID string `json:"projectId"`
Type RequestType `json:"type"`
AnonymousID string `json:"anonymousId,omitempty"`
UserID string `json:"userId,omitempty"`
Event string `json:"event"`
MessageID string `json:"messageId,omitempty"`
Properties map[string]any `json:"properties,omitempty"`
Context *Context `json:"context,omitempty"`
Integrations *Integrations `json:"integrations,omitempty"`
Timestamp *time.Time `json:"timestamp,omitempty"`
Channel string `json:"channel,omitempty"`
AuthToken string `json:"authToken"`
}
func (c *Client) Track(ctx context.Context, req *TrackRequest) error {
// ensure no mutation of the original request
req = ptr.ShallowCopy(req)
if req.ProjectID == "" {
req.ProjectID = c.ProjectID
}
if req.Type == "" {
req.Type = RequestTypeTrack
}
if req.Context == nil {
req.Context = &Context{}
}
if req.Context.Library == nil {
req.Context = ptr.ShallowCopy(req.Context)
req.Context.Library = library
}
if req.AuthToken == "" {
req.AuthToken = c.AuthToken
}
return c.client.Do(ctx, "POST", "/v1/t", nil, req, nil)
}