-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessages.go
368 lines (310 loc) · 7.41 KB
/
messages.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package gamp
import (
"errors"
"fmt"
"io"
"net/url"
"strconv"
"strings"
"time"
)
// Message interface should be implemented by all GA messages
type Message interface {
GetTrackingID() string
SetTrackingID(string)
fmt.Stringer
//
Write(w io.Writer) (n int, err error)
}
func messageToString(m Message) string {
buffer := new(strings.Builder)
if _, err := m.Write(buffer); err != nil {
panic(err.Error())
}
return buffer.String()
}
// Common properties of GA message
type Common struct {
TrackingID string `key:"tid" required:"true"`
ClientID string `key:"cid"`
UserID string `key:"uid"`
UserLanguage string `key:"ul"`
UserAgent string `key:"ua"`
DataSource string `key:"ds"` // https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ds
ApplicationID string `key:"aid"`
}
// GetTrackingID returns tracking ID of GA message
func (c *Common) GetTrackingID() string {
return c.TrackingID
}
// GetTrackingID returns tracking ID of GA message
func (c *Common) SetTrackingID(v string) {
c.TrackingID = v
}
// Event is GA message of 'event' type
type Event struct {
Common
Category string `key:"ec" required:"true"`
Action string `key:"ea" required:"true"`
Label string `key:"el"`
Value uint `key:"ev"`
}
var _ Message = (*Event)(nil)
// Timing is GA message of 'timing' type
type Timing struct {
Common
ServerResponseTime uint
}
var _ Message = (*Timing)(nil)
func (c *Common) write1st(w io.Writer) (n int, err error) {
_n, err := w.Write([]byte("v=1"))
n += _n
if err != nil {
return n, err
}
if c.TrackingID != "" {
_n, err = w.Write([]byte("&tid=" + c.TrackingID))
n += _n
if err != nil {
return n, err
}
}
return
}
func (c *Common) writeRest(w io.Writer) (n int, err error) {
var _n int
if c.ClientID != "" {
_n, err = w.Write([]byte("&cid=" + url.QueryEscape(c.ClientID)))
n += _n
if err != nil {
return n, err
}
}
if c.UserID != "" {
_n, err = w.Write([]byte("&uid=" + url.QueryEscape(c.UserID)))
n += _n
if err != nil {
return n, err
}
}
if c.UserLanguage != "" {
_n, err = w.Write([]byte("&ul=" + c.UserLanguage))
n += _n
if err != nil {
return n, err
}
}
if c.UserAgent != "" {
_n, err = w.Write([]byte("&ua=" + url.QueryEscape(c.UserAgent)))
n += _n
if err != nil {
return n, err
}
}
//if c.ApplicationID != "" {
// _n, err = w.Write([]byte("&aid="+url.QueryEscape(c.ApplicationID)))
// n += _n
// if err != nil {
// return n, err
// }
// _n, err = w.Write([]byte("&an="+url.QueryEscape(c.ApplicationID)))
// n += _n
// if err != nil {
// return n, err
// }
//}
return
}
// NewEvent creates new event
func NewEvent(category, action string, common Common) *Event {
return NewEventWithLabel(category, action, "", common)
}
// NewTiming creates new timing
func NewTiming(serverResponseTime time.Duration) *Timing {
return &Timing{ServerResponseTime: uint(serverResponseTime.Nanoseconds() / int64(time.Millisecond))}
}
// NewEventWithLabel creates new event with label
func NewEventWithLabel(category, action, label string, common Common) *Event {
event := Event{
Category: category,
Action: action,
Label: label,
Common: common,
}
if err := event.Validate(); err != nil {
panic(err.Error())
}
return &event
}
// Validate is checking message for validity
func (e Event) Validate() error {
if e.Category == "" {
return errors.New("Missing required parameter: Category")
}
if e.Action == "" {
return errors.New("Missing required parameter: Action")
}
return nil
}
// Write serializes timing message
func (t Timing) Write(w io.Writer) (n int, err error) {
var _n int
_n, err = t.Common.write1st(w)
if n += _n; err != nil {
return
}
_n, err = w.Write([]byte("&t=timing"))
if n += _n; err != nil {
return n, err
}
_n, err = t.Common.writeRest(w)
if n += _n; err != nil {
return
}
if t.ServerResponseTime != 0 {
_n, err = w.Write([]byte("&srt=" + strconv.FormatUint(uint64(t.ServerResponseTime), 10)))
if n += _n; err != nil {
return n, err
}
}
return n, err
}
// Write serializes event message
func (e Event) Write(w io.Writer) (n int, err error) {
var _n int
_n, err = e.Common.write1st(w)
if n += _n; err != nil {
return
}
_n, err = w.Write([]byte("&t=event"))
if n += _n; err != nil {
return n, err
}
_n, err = e.Common.writeRest(w)
if n += _n; err != nil {
return
}
_n, err = w.Write([]byte("&ec=" + url.QueryEscape(e.Category)))
if n += _n; err != nil {
return n, err
}
_n, err = w.Write([]byte("&ea=" + url.QueryEscape(e.Action)))
if n += _n; err != nil {
return n, err
}
if e.Value != 0 {
_n, err = w.Write([]byte("&ev=" + strconv.FormatUint(uint64(e.Value), 10)))
if n += _n; err != nil {
return n, err
}
}
if e.Label != "" {
_n, err = w.Write([]byte("&el=" + url.QueryEscape(e.Label)))
if n += _n; err != nil {
return n, err
}
}
return n, err
}
// String returns message as URL encoded string
func (e *Event) String() string {
return messageToString(e)
}
// String returns message as URL encoded string
func (t *Timing) String() string {
return messageToString(t)
}
// Pageview message
type Pageview struct {
Common
//DocumentLocation string `key:"dl"`
DocumentHost string `key:"dh"`
DocumentPath string `key:"dp"`
DocumentTitle string `key:"dt"`
}
var _ Message = (*Pageview)(nil)
// NewPageviewWithDocumentHost creates new pageview message with document host value
func NewPageviewWithDocumentHost(documentHost, documentPath, documentTitle string) *Pageview {
return &Pageview{
DocumentHost: documentHost,
DocumentPath: documentPath,
DocumentTitle: documentTitle,
}
}
// String returns message as URL encoded string
func (m *Pageview) String() string {
return messageToString(m)
}
// Write serializes page view message
func (m Pageview) Write(w io.Writer) (n int, err error) {
var _n int
_n, err = m.Common.write1st(w)
if n += _n; err != nil {
return
}
_n, err = w.Write([]byte("&t=pageview"))
if n += _n; err != nil {
return n, err
}
_n, err = m.Common.writeRest(w)
if n += _n; err != nil {
return
}
_n, err = w.Write([]byte("&dh=" + m.DocumentHost)) // We suppose we do not need to encode host
if n += _n; err != nil {
return n, err
}
_n, err = w.Write([]byte("&dp=" + url.QueryEscape(m.DocumentPath)))
if n += _n; err != nil {
return n, err
}
_n, err = w.Write([]byte("&dt=" + url.QueryEscape(m.DocumentTitle)))
if n += _n; err != nil {
return n, err
}
return
}
// Exception message
type Exception struct {
Common
Description string `key:"exd"`
IsFatal bool `key:"exf"`
}
var _ Message = (*Exception)(nil)
// NewException creates new exception message
func NewException(description string, isFatal bool) *Exception {
return &Exception{
Description: description,
IsFatal: isFatal,
}
}
func (m *Exception) String() string {
return messageToString(m)
}
// Write serializes exception message
func (m Exception) Write(w io.Writer) (n int, err error) {
var _n int
_n, err = m.Common.write1st(w)
if n += _n; err != nil {
return
}
_n, err = w.Write([]byte("&t=exception"))
if n += _n; err != nil {
return n, err
}
_n, err = m.Common.writeRest(w)
if n += _n; err != nil {
return
}
_n, err = w.Write([]byte("&exd=" + url.QueryEscape(m.Description))) // We suppose we do not need to encode host
if n += _n; err != nil {
return n, err
}
if m.IsFatal {
_n, err = w.Write([]byte("&exf=1"))
if n += _n; err != nil {
return n, err
}
}
return
}