@@ -2,17 +2,23 @@ package event
2
2
3
3
import (
4
4
"errors"
5
+ "fmt"
5
6
"strings"
6
7
"time"
7
8
9
+ "github.com/optimizely/go-sdk/optimizely"
10
+
8
11
guuid "github.com/google/uuid"
9
12
"github.com/optimizely/go-sdk/optimizely/entities"
13
+ "github.com/optimizely/go-sdk/optimizely/logging"
10
14
"github.com/optimizely/go-sdk/optimizely/utils"
11
15
)
12
16
17
+ var efLogger = logging .GetLogger ("EventFactory" )
18
+
13
19
const impressionKey string = "campaign_activated"
14
- const clientKey string = "go-sdk"
15
- const clientVersion string = "1.0.0"
20
+ const clientKey string = optimizely . ClientName
21
+ const clientVersion string = optimizely . Version
16
22
const attributeType = "custom"
17
23
const specialPrefix = "$opt_"
18
24
const botFilteringKey = "$opt_bot_filtering"
@@ -29,27 +35,26 @@ func makeTimestamp() int64 {
29
35
}
30
36
31
37
// CreateEventContext creates and returns EventContext
32
- func CreateEventContext (projectID string , revision string , accountID string , anonymizeIP bool , botFiltering bool , attributeKeyToIDMap map [ string ] string ) Context {
38
+ func CreateEventContext (projectConfig optimizely. ProjectConfig ) Context {
33
39
context := Context {}
34
- context .ProjectID = projectID
35
- context .Revision = revision
36
- context .AccountID = accountID
40
+ context .ProjectID = projectConfig . GetProjectID ()
41
+ context .Revision = projectConfig . GetRevision ()
42
+ context .AccountID = projectConfig . GetAccountID ()
37
43
context .ClientName = clientKey
38
44
context .ClientVersion = clientVersion
39
- context .AnonymizeIP = anonymizeIP
40
- context .BotFiltering = botFiltering
41
- context .attributeKeyToIDMap = attributeKeyToIDMap
45
+ context .AnonymizeIP = projectConfig .GetAnonymizeIP ()
46
+ context .BotFiltering = projectConfig .GetBotFiltering ()
42
47
43
48
return context
44
49
}
45
50
46
- func createImpressionEvent (context Context , experiment entities.Experiment ,
51
+ func createImpressionEvent (projectConfig optimizely. ProjectConfig , experiment entities.Experiment ,
47
52
variation entities.Variation , attributes map [string ]interface {}) ImpressionEvent {
48
53
49
54
impression := ImpressionEvent {}
50
55
impression .Key = impressionKey
51
56
impression .EntityID = experiment .LayerID
52
- impression .Attributes = getEventAttributes (context . attributeKeyToIDMap , attributes , context . BotFiltering )
57
+ impression .Attributes = getEventAttributes (projectConfig , attributes )
53
58
impression .VariationID = variation .ID
54
59
impression .ExperimentID = experiment .ID
55
60
impression .CampaignID = experiment .LayerID
@@ -58,18 +63,18 @@ func createImpressionEvent(context Context, experiment entities.Experiment,
58
63
}
59
64
60
65
// CreateImpressionUserEvent creates and returns ImpressionEvent for user
61
- func CreateImpressionUserEvent (context Context , experiment entities.Experiment ,
66
+ func CreateImpressionUserEvent (projectConfig optimizely. ProjectConfig , experiment entities.Experiment ,
62
67
variation entities.Variation ,
63
68
userContext entities.UserContext ) UserEvent {
64
69
65
- impression := createImpressionEvent (context , experiment , variation , userContext .Attributes )
70
+ impression := createImpressionEvent (projectConfig , experiment , variation , userContext .Attributes )
66
71
67
72
userEvent := UserEvent {}
68
73
userEvent .Timestamp = makeTimestamp ()
69
74
userEvent .VisitorID = userContext .ID
70
75
userEvent .UUID = guuid .New ().String ()
71
76
userEvent .Impression = & impression
72
- userEvent .EventContext = context
77
+ userEvent .EventContext = CreateEventContext ( projectConfig )
73
78
74
79
return userEvent
75
80
}
@@ -94,27 +99,27 @@ func createImpressionVisitor(userEvent UserEvent) Visitor {
94
99
}
95
100
96
101
// create a conversion event
97
- func createConversionEvent (attributeKeyToIDMap map [ string ] string , event entities.Event , attributes map [string ]interface {}, eventTags map [string ]interface {}, botFiltering bool ) ConversionEvent {
102
+ func createConversionEvent (projectConfig optimizely. ProjectConfig , event entities.Event , attributes map [string ]interface {}, eventTags map [string ]interface {}) ConversionEvent {
98
103
conversion := ConversionEvent {}
99
104
100
105
conversion .Key = event .Key
101
106
conversion .EntityID = event .ID
102
107
conversion .Tags = eventTags
103
- conversion .Attributes = getEventAttributes (attributeKeyToIDMap , attributes , botFiltering )
108
+ conversion .Attributes = getEventAttributes (projectConfig , attributes )
104
109
105
110
return conversion
106
111
}
107
112
108
113
// CreateConversionUserEvent creates and returns ConversionEvent for user
109
- func CreateConversionUserEvent (context Context , event entities.Event , userContext entities.UserContext , attributeKeyToIDMap map [ string ] string , eventTags map [string ]interface {}) UserEvent {
114
+ func CreateConversionUserEvent (projectConfig optimizely. ProjectConfig , event entities.Event , userContext entities.UserContext , eventTags map [string ]interface {}) UserEvent {
110
115
111
116
userEvent := UserEvent {}
112
117
userEvent .Timestamp = makeTimestamp ()
113
118
userEvent .VisitorID = userContext .ID
114
119
userEvent .UUID = guuid .New ().String ()
115
120
116
- userEvent .EventContext = context
117
- conversion := createConversionEvent (attributeKeyToIDMap , event , userContext .Attributes , eventTags , context . BotFiltering )
121
+ userEvent .EventContext = CreateEventContext ( projectConfig )
122
+ conversion := createConversionEvent (projectConfig , event , userContext .Attributes , eventTags )
118
123
revenue , err := getRevenueValue (eventTags )
119
124
if err == nil {
120
125
conversion .Revenue = & revenue
@@ -185,49 +190,49 @@ func createVisitor(userEvent UserEvent, attributes []VisitorAttribute,
185
190
// create a batch event with visitor
186
191
func createBatchEvent (userEvent UserEvent , visitor Visitor ) Batch {
187
192
188
-
189
193
eventBatch := Batch {}
190
194
eventBatch .ProjectID = userEvent .EventContext .ProjectID
191
195
eventBatch .Revision = userEvent .EventContext .Revision
192
196
eventBatch .AccountID = userEvent .EventContext .AccountID
193
197
eventBatch .Visitors = []Visitor {visitor }
194
- eventBatch .ClientName = clientKey
195
- eventBatch .ClientVersion = clientVersion
198
+ eventBatch .ClientName = userEvent . EventContext . ClientName
199
+ eventBatch .ClientVersion = userEvent . EventContext . ClientVersion
196
200
eventBatch .AnonymizeIP = userEvent .EventContext .AnonymizeIP
197
201
eventBatch .EnrichDecisions = true
198
202
199
203
return eventBatch
200
204
}
201
205
202
206
// get visitor attributes from user attributes
203
- func getEventAttributes (attributeKeyToIDMap map [ string ] string , attributes map [string ]interface {}, botFiltering bool ) []VisitorAttribute {
207
+ func getEventAttributes (projectConfig optimizely. ProjectConfig , attributes map [string ]interface {}) []VisitorAttribute {
204
208
var eventAttributes = []VisitorAttribute {}
205
209
206
210
for key , value := range attributes {
207
211
if value == nil {
208
212
continue
209
213
}
210
- attribute := VisitorAttribute {}
211
- id := attributeKeyToIDMap [ key ]
212
- if id != "" {
213
- attribute .EntityID = id
214
+ visitorAttribute := VisitorAttribute {}
215
+ attribute , _ := projectConfig . GetAttributeByKey ( key )
216
+ if attribute . ID != "" {
217
+ visitorAttribute .EntityID = attribute . ID
214
218
} else if strings .HasPrefix (key , specialPrefix ) {
215
- attribute .EntityID = key
219
+ visitorAttribute .EntityID = key
216
220
} else {
221
+ efLogger .Debug (fmt .Sprintf ("Unrecognized attribute %s provided. Pruning before sending event to Optimizely." , key ))
217
222
continue
218
223
}
219
- attribute .Value = value
220
- attribute .AttributeType = attributeType
224
+ visitorAttribute .Value = value
225
+ visitorAttribute .AttributeType = attributeType
221
226
222
- eventAttributes = append (eventAttributes , attribute )
227
+ eventAttributes = append (eventAttributes , visitorAttribute )
223
228
}
224
229
225
- attribute := VisitorAttribute {}
226
- attribute .Value = botFiltering
227
- attribute .AttributeType = attributeType
228
- attribute .Key = botFilteringKey
229
- attribute .EntityID = botFilteringKey
230
- eventAttributes = append (eventAttributes , attribute )
230
+ visitorAttribute := VisitorAttribute {}
231
+ visitorAttribute .Value = projectConfig . GetBotFiltering ()
232
+ visitorAttribute .AttributeType = attributeType
233
+ visitorAttribute .Key = botFilteringKey
234
+ visitorAttribute .EntityID = botFilteringKey
235
+ eventAttributes = append (eventAttributes , visitorAttribute )
231
236
232
237
return eventAttributes
233
238
}
0 commit comments