This repository has been archived by the owner on Feb 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
hits.go
337 lines (294 loc) · 9.66 KB
/
hits.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
package model
import (
"errors"
"fmt"
"net/url"
"time"
)
// HitType express the type of the event
type HitType string
// The different event types
const (
ACTIVATION HitType = "ACTIVATION"
CAMPAIGN HitType = "CAMPAIGN"
SCREEN HitType = "SCREENVIEW"
PAGE HitType = "PAGEVIEW"
EVENT HitType = "EVENT"
ITEM HitType = "ITEM"
TRANSACTION HitType = "TRANSACTION"
BATCH HitType = "BATCH"
)
// EventType express the type of the event
type EventType string
// The different event types
const (
CONTEXT EventType = "CONTEXT"
)
// BaseHit represents the API client informations
type BaseHit struct {
VisitorID string `json:"vid,omitempty"`
EnvironmentID string `json:"cid,omitempty"`
Type HitType `json:"t,omitempty"`
DataSource string `json:"ds,omitempty"`
ProtocolVersion string `json:"v,omitempty"`
UserIP string `json:"uip,omitempty"`
DocumentReferrer string `json:"dr,omitempty"`
ViewportSize string `json:"vp,omitempty"`
ScreenResolution string `json:"sr,omitempty"`
PageTitle string `json:"pt,omitempty"`
DocumentEncoding string `json:"de,omitempty"`
ScreenColorDepth string `json:"sd,omitempty"`
UserLanguage string `json:"ul,omitempty"`
JavaEnabled *bool `json:"je,omitempty"`
FlashVersion string `json:"fl,omitempty"`
QueueTime int64 `json:"qt,omitempty"`
DocumentLocation string `json:"dl,omitempty"`
CurrentSessionTimestamp int64 `json:"cst,omitempty"`
SessionNumber int64 `json:"sn,omitempty"`
CreatedAt time.Time `json:"-"`
}
// SetBaseInfos sets the mandatory information for the hit
func (b *BaseHit) SetBaseInfos(envID string, visitorID string) {
b.EnvironmentID = envID
b.VisitorID = visitorID
b.DataSource = "APP"
b.CreatedAt = time.Now()
}
func (b *BaseHit) validateBase() []error {
errorsList := []error{}
if b.VisitorID == "" {
errorsList = append(errorsList, errors.New("Visitor ID should not by empty"))
}
if b.EnvironmentID == "" {
errorsList = append(errorsList, errors.New("Environment ID should not by empty"))
}
if b.DataSource != "APP" {
errorsList = append(errorsList, errors.New("DataSource should be APP"))
}
switch b.Type {
case
TRANSACTION,
EVENT,
PAGE,
SCREEN,
ITEM,
CAMPAIGN,
BATCH:
break
default:
errorsList = append(errorsList, errors.New("Type is not handled"))
}
isScreenOrPageHit := b.Type == PAGE || b.Type == SCREEN
if isScreenOrPageHit && b.DocumentLocation == "" {
errorsList = append(errorsList, errors.New("Document location must not by empty for this hit PAGE or SCREEN"))
} else if !isScreenOrPageHit && b.DocumentLocation != "" {
errorsList = append(errorsList, errors.New("Document location must be empty for this type of hit"))
}
return errorsList
}
// ComputeQueueTime computes hit queue time
func (b *BaseHit) ComputeQueueTime() {
b.QueueTime = int64((time.Since(b.CreatedAt)).Milliseconds())
}
// PageHit represents a pageview hit for the datacollect
type PageHit struct {
BaseHit
}
// SetBaseInfos sets the mandatory information for the hit
func (b *PageHit) SetBaseInfos(envID string, visitorID string) {
b.BaseHit.SetBaseInfos(envID, visitorID)
b.Type = PAGE
}
// Validate checks that the hit is well formed
func (b *PageHit) Validate() []error {
errorsList := b.validateBase()
// Check url format
_, err := url.ParseRequestURI(b.DocumentLocation)
if err != nil {
errorsList = append(errorsList, errors.New("Document location should be a real url for hit page"))
}
return errorsList
}
// ScreenHit represents a screenview hit for the datacollect
type ScreenHit struct {
BaseHit
}
// SetBaseInfos sets the mandatory information for the hit
func (b *ScreenHit) SetBaseInfos(envID string, visitorID string) {
b.BaseHit.SetBaseInfos(envID, visitorID)
b.Type = SCREEN
}
// Validate checks that the hit is well formed
func (b *ScreenHit) Validate() []error {
return b.validateBase()
}
// EventHit represents an event hit for the datacollect
type EventHit struct {
BaseHit
Action string `json:"ea"`
Category string `json:"ec,omitempty"`
Label string `json:"el,omitempty"`
Value int64 `json:"ev,omitempty"`
}
// SetBaseInfos sets the mandatory information for the hit
func (b *EventHit) SetBaseInfos(envID string, visitorID string) {
b.BaseHit.SetBaseInfos(envID, visitorID)
b.Type = EVENT
}
// Validate checks that the hit is well formed
func (b *EventHit) Validate() []error {
errorsList := b.validateBase()
if b.Action == "" {
errorsList = append(errorsList, errors.New("Event Action should not by empty"))
}
return errorsList
}
// TransactionHit represents a transaction hit for the datacollect
type TransactionHit struct {
BaseHit
TransactionID string `json:"tid"`
Affiliation string `json:"ta"`
Revenue float64 `json:"tr,omitempty"`
Shipping float64 `json:"ts,omitempty"`
Tax float64 `json:"tt,omitempty"`
Currency string `json:"tc,omitempty"`
CouponCode string `json:"tcc,omitempty"`
PaymentMethod string `json:"pm,omitempty"`
ShippingMethod string `json:"sm,omitempty"`
ItemCount int `json:"icn,omitempty"`
}
// SetBaseInfos sets the mandatory information for the hit
func (b *TransactionHit) SetBaseInfos(envID string, visitorID string) {
b.BaseHit.SetBaseInfos(envID, visitorID)
b.Type = TRANSACTION
}
// Validate checks that the hit is well formed
func (b *TransactionHit) Validate() []error {
errorsList := b.validateBase()
if b.TransactionID == "" {
errorsList = append(errorsList, errors.New("Transaction ID should not by empty"))
}
if b.Affiliation == "" {
errorsList = append(errorsList, errors.New("Transaction affiliation should not by empty"))
}
return errorsList
}
// ItemHit represents an item hit for the datacollect
type ItemHit struct {
BaseHit
TransactionID string `json:"tid"`
Name string `json:"in"`
Price float64 `json:"ip,omitempty"`
Quantity int `json:"iq,omitempty"`
Code string `json:"ic,omitempty"`
Category string `json:"iv,omitempty"`
}
// SetBaseInfos sets the mandatory information for the hit
func (b *ItemHit) SetBaseInfos(envID string, visitorID string) {
b.BaseHit.SetBaseInfos(envID, visitorID)
b.Type = ITEM
}
// Validate checks that the hit is well formed
func (b *ItemHit) Validate() []error {
errorsList := b.validateBase()
if b.TransactionID == "" {
errorsList = append(errorsList, errors.New("Item Transaction ID should not by empty"))
}
if b.Name == "" {
errorsList = append(errorsList, errors.New("Item name should not by empty"))
}
if b.Code == "" {
errorsList = append(errorsList, errors.New("Item code should not by empty"))
}
return errorsList
}
// ActivationHit represents an item hit for the datacollect
type ActivationHit struct {
VisitorID string `json:"vid"`
EnvironmentID string `json:"cid"`
VariationGroupID string `json:"caid"`
VariationID string `json:"vaid"`
CreatedAt time.Time `json:"-"`
QueueTime int64 `json:"-"`
}
// SetBaseInfos sets the mandatory information for the hit
func (b *ActivationHit) SetBaseInfos(envID string, visitorID string) {
b.EnvironmentID = envID
b.VisitorID = visitorID
}
// Validate checks that the hit is well formed
func (b *ActivationHit) Validate() []error {
errorsList := []error{}
if b.VisitorID == "" {
errorsList = append(errorsList, errors.New("Visitor ID should not by empty"))
}
if b.EnvironmentID == "" {
errorsList = append(errorsList, errors.New("Environment ID should not by empty"))
}
if b.VariationGroupID == "" {
errorsList = append(errorsList, errors.New("Campaign ID should not by empty"))
}
if b.VariationID == "" {
errorsList = append(errorsList, errors.New("Variation should not by empty"))
}
return errorsList
}
// ComputeQueueTime computes hit queue time
func (b *ActivationHit) ComputeQueueTime() {
b.QueueTime = int64((time.Since(b.CreatedAt)).Seconds())
}
// Event represents a context event hit for Flagship
type Event struct {
VisitorID string `json:"visitorId"`
Type EventType `json:"type"`
Data Context `json:"data"`
CreatedAt time.Time `json:"-"`
QueueTime int64 `json:"-"`
}
// SetBaseInfos sets the mandatory information for the hit
func (b *Event) SetBaseInfos(envID string, visitorID string) {
b.VisitorID = visitorID
}
// Validate checks that the hit is well formed
func (b *Event) Validate() []error {
errorsList := []error{}
if b.VisitorID == "" {
errorsList = append(errorsList, errors.New("Visitor ID should not by empty"))
}
if b.Type != "CONTEXT" {
errorsList = append(errorsList, fmt.Errorf("Type %s, is not handled", b.Type))
}
contextErrs := b.Data.Validate()
errorsList = append(errorsList, contextErrs...)
return errorsList
}
// ComputeQueueTime computes hit queue time
func (b *Event) ComputeQueueTime() {
b.QueueTime = int64((time.Since(b.CreatedAt)).Seconds())
}
// BatchHit represents a batch of hits for the datacollect
type BatchHit struct {
BaseHit
Hits []HitInterface `json:"h"`
}
// SetBaseInfos sets the mandatory information for the hit
func (b *BatchHit) SetBaseInfos(envID string, visitorID string) {
b.BaseHit.SetBaseInfos(envID, visitorID)
b.Type = BATCH
}
// Validate checks that the hit is well formed
func (b *BatchHit) Validate() []error {
return b.validateBase()
}
// AddHit adds a hit to the batch
func (b *BatchHit) AddHit(hit HitInterface) {
b.Hits = append(b.Hits, hit)
}
func createBatchHit(baseHit BaseHit) BatchHit {
bHit := BatchHit{
BaseHit: baseHit,
Hits: []HitInterface{},
}
bHit.SetBaseInfos(bHit.EnvironmentID, bHit.VisitorID)
return bHit
}