forked from GetStream/stream-go2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalytics_types.go
192 lines (163 loc) · 5.88 KB
/
analytics_types.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package stream
import "time"
// EventFeature is a single analytics event feature, a pair of group and
// value strings.
type EventFeature struct {
Group string `json:"group"`
Value string `json:"value"`
}
// NewEventFeature returns a new EventFeature from the given group and value
// params.
func NewEventFeature(group, value string) EventFeature {
return EventFeature{
Group: group,
Value: value,
}
}
// UserData represents an analytics event user data field, which can either
// be a single string/integer representing the user's ID, or a dictionary
// made of an ID (string or integer) and a string alias.
// For example NewUserData().Int(123).Alias("john") will result in a dictionary
// like {"user_data":{"id": 123, "alias": "john"}}, while NewUserData().String("bob") will
// result in {"user_data": "bob"}.
type UserData struct {
id interface{}
alias string
}
// NewUserData initializes an empty UserData type, which must be populated
// using the String, Int, and/or Alias methods.
func NewUserData() *UserData {
return &UserData{}
}
// String sets the ID as the given string.
func (d *UserData) String(id string) *UserData {
d.id = id
return d
}
// Int sets the ID as the given integer.
func (d *UserData) Int(id int) *UserData {
d.id = id
return d
}
// Alias sets the alias as the given string.
func (d *UserData) Alias(alias string) *UserData {
d.alias = alias
return d
}
func (d *UserData) value() interface{} {
if d.alias == "" {
return d.id
}
return map[string]interface{}{
"id": d.id,
"alias": d.alias,
}
}
// EngagementEvent represent an analytics engagement event. It must be populated
// with the available methods, or custom data can be arbitrarily added to it
// manually as key(string),value(interface{}) pairs.
type EngagementEvent map[string]interface{}
// WithLabel sets the event's label field to the given string.
func (e EngagementEvent) WithLabel(label string) EngagementEvent {
e["label"] = label
return e
}
// WithUserData sets the event's user_data field to the given UserData's value.
func (e EngagementEvent) WithUserData(userData *UserData) EngagementEvent {
e["user_data"] = userData.value()
return e
}
// WithForeignID sets the event's content field to the given foreign ID. If the
// content payload must be an object, use the WithContent method.
func (e EngagementEvent) WithForeignID(foreignID string) EngagementEvent {
e["content"] = foreignID
return e
}
// WithContent sets the event's content field to the given content map, and also
// sets the foreign_id field of such object to the given foreign ID string.
// If just the foreign ID is required to be sent, use the WithForeignID method.
func (e EngagementEvent) WithContent(foreignID string, content map[string]interface{}) EngagementEvent {
if content != nil {
content["foreign_id"] = foreignID
}
e["content"] = content
return e
}
// WithFeedID sets the event's feed_id field to the given string.
func (e EngagementEvent) WithFeedID(feedID string) EngagementEvent {
e["feed_id"] = feedID
return e
}
// WithLocation sets the event's location field to the given string.
func (e EngagementEvent) WithLocation(location string) EngagementEvent {
e["location"] = location
return e
}
// WithPosition sets the event's position field to the given int.
func (e EngagementEvent) WithPosition(position int) EngagementEvent {
e["position"] = position
return e
}
// WithFeatures sets the event's features field to the given list of EventFeatures.
func (e EngagementEvent) WithFeatures(features ...EventFeature) EngagementEvent {
e["features"] = features
return e
}
// WithBoost sets the event's boost field to the given int.
func (e EngagementEvent) WithBoost(boost int) EngagementEvent {
e["boost"] = boost
return e
}
// WithTrackedAt sets the event's tracked_at field to the given time.Time.
func (e EngagementEvent) WithTrackedAt(trackedAt time.Time) EngagementEvent {
e["tracked_at"] = trackedAt.Format(time.RFC3339)
return e
}
// ImpressionEventsData represents the payload of an arbitrary number of impression events.
// It must be populated with the available methods, or custom data can be arbitrarily
// added to it manually as key(string),value(interface{}) pairs.
type ImpressionEventsData map[string]interface{}
// WithForeignIDs sets the content_list field to the given list of strings.
func (d ImpressionEventsData) WithForeignIDs(foreignIDs ...string) ImpressionEventsData {
d["content_list"] = foreignIDs
return d
}
// AddForeignIDs adds the given foreign ID strings to the content_list field, creating
// it if it doesn't exist.
func (d ImpressionEventsData) AddForeignIDs(foreignIDs ...string) ImpressionEventsData {
list, ok := d["content_list"].([]string)
if !ok {
return d.WithForeignIDs(foreignIDs...)
}
return d.WithForeignIDs(append(list, foreignIDs...)...)
}
// WithUserData sets the user_data field to the given UserData value.
func (d ImpressionEventsData) WithUserData(userData *UserData) ImpressionEventsData {
d["user_data"] = userData.value()
return d
}
// WithFeedID sets the feed_id field to the given string.
func (d ImpressionEventsData) WithFeedID(feedID string) ImpressionEventsData {
d["feed_id"] = feedID
return d
}
// WithLocation sets the location field to the given string.
func (d ImpressionEventsData) WithLocation(location string) ImpressionEventsData {
d["location"] = location
return d
}
// WithPosition sets the position field to the given int.
func (d ImpressionEventsData) WithPosition(position int) ImpressionEventsData {
d["position"] = position
return d
}
// WithFeatures sets the features field to the given list of EventFeatures.
func (d ImpressionEventsData) WithFeatures(features ...EventFeature) ImpressionEventsData {
d["features"] = features
return d
}
// WithTrackedAt sets the tracked_at field to the given time.Time.
func (d ImpressionEventsData) WithTrackedAt(trackedAt time.Time) ImpressionEventsData {
d["tracked_at"] = trackedAt.Format(time.RFC3339)
return d
}