22
22
use Optimizely \Event \LogEvent ;
23
23
use Optimizely \ProjectConfig ;
24
24
use Optimizely \Utils \EventTagUtils ;
25
+ use Optimizely \Utils \GeneratorUtils ;
25
26
26
27
define ("RESERVED_ATTRIBUTE_KEY_BUCKETING_ID_EVENT_PARAM_KEY " , "optimizely_bucketing_id " );
27
28
@@ -38,14 +39,9 @@ class EventBuilder
38
39
const SDK_VERSION = '1.2.0 ' ;
39
40
40
41
/**
41
- * @var string URL to send impression event to.
42
+ * @var string URL to send event to.
42
43
*/
43
- private static $ IMPRESSION_ENDPOINT = 'https://logx.optimizely.com/log/decision ' ;
44
-
45
- /**
46
- * @var string URL to send conversion event to.
47
- */
48
- private static $ CONVERSION_ENDPOINT = 'https://logx.optimizely.com/log/event ' ;
44
+ private static $ ENDPOINT = 'https://logx.optimizely.com/v1/events ' ;
49
45
50
46
/**
51
47
* @var string HTTP method to be used when making call to log endpoint.
@@ -60,147 +56,151 @@ class EventBuilder
60
56
];
61
57
62
58
/**
63
- * @var array Associative array of parameters to be sent for the event.
64
- */
65
- private $ _eventParams ;
66
-
67
- /**
68
- * Helper function to reset event params.
69
- */
70
- private function resetParams ()
71
- {
72
- $ this ->_eventParams = [];
73
- }
74
-
75
- /**
76
- * @return array Params for the event.
77
- */
78
- private function getParams ()
79
- {
80
- return $ this ->_eventParams ;
81
- }
82
-
83
- /**
84
- * Helper function to set parameters common to impression and conversion event.
59
+ * Helper function to get parameters common to impression and conversion events.
85
60
*
86
61
* @param $config ProjectConfig Configuration for the project.
87
62
* @param $userId string ID of user.
88
63
* @param $attributes array Attributes of the user.
89
64
*/
90
- private function setCommonParams ($ config , $ userId , $ attributes )
65
+ private function getCommonParams ($ config , $ userId , $ attributes )
91
66
{
92
- $ this ->_eventParams [PROJECT_ID ] = $ config ->getProjectId ();
93
- $ this ->_eventParams [ACCOUNT_ID ] = $ config ->getAccountId ();
94
- $ this ->_eventParams [REVISION ] = $ config ->getRevision ();
95
- $ this ->_eventParams [VISITOR_ID ] = $ userId ;
96
- $ this ->_eventParams [CLIENT_ENGINE ] = self ::SDK_TYPE ;
97
- $ this ->_eventParams [CLIENT_VERSION ] = self ::SDK_VERSION ;
98
- $ this ->_eventParams [USER_FEATURES ] = [];
99
- $ this ->_eventParams [IS_GLOBAL_HOLDBACK ] = false ;
100
- $ this ->_eventParams [TIME ] = time ()*1000 ;
101
- if (!isset ($ attributes )) {
102
- $ attributes = [];
103
- }
67
+ $ visitor = [
68
+ SNAPSHOTS => [],
69
+ VISITOR_ID => $ userId ,
70
+ ATTRIBUTES => []
71
+ ];
104
72
105
- forEach ($ attributes as $ attributeKey => $ attributeValue ) {
106
- if ($ attributeValue ) {
73
+ $ commonParams = [
74
+ ACCOUNT_ID => $ config ->getAccountId (),
75
+ PROJECT_ID => $ config ->getProjectId (),
76
+ VISITORS => [$ visitor ],
77
+ REVISION => $ config ->getRevision (),
78
+ CLIENT_ENGINE => self ::SDK_TYPE ,
79
+ CLIENT_VERSION => self ::SDK_VERSION
80
+ ];
81
+
82
+ if (is_null ($ attributes ))
83
+ return $ commonParams ;
84
+
85
+ foreach ($ attributes as $ attributeKey => $ attributeValue ) {
86
+ $ feature = [];
87
+ // Do not discard attribute if value is zero or false
88
+ if (!is_null ($ attributeValue )) {
107
89
// check for reserved attributes
108
90
if (strcmp ($ attributeKey , RESERVED_ATTRIBUTE_KEY_BUCKETING_ID ) == 0 ) {
109
91
// TODO (Alda): the type for bucketing ID attribute may change so that custom
110
92
// attributes are not overloaded
111
- array_push ($ this ->_eventParams [USER_FEATURES ], [
112
- 'name ' => RESERVED_ATTRIBUTE_KEY_BUCKETING_ID_EVENT_PARAM_KEY ,
113
- 'type ' => 'custom ' ,
114
- 'value ' => $ attributeValue ,
115
- 'shouldIndex ' => true
116
- ]);
93
+ $ feature = [
94
+ ENTITY_ID => RESERVED_ATTRIBUTE_KEY_BUCKETING_ID ,
95
+ KEY => RESERVED_ATTRIBUTE_KEY_BUCKETING_ID_EVENT_PARAM_KEY ,
96
+ TYPE => CUSTOM_ATTRIBUTE_FEATURE_TYPE ,
97
+ VALUE => $ attributeValue
98
+ ];
99
+
117
100
} else {
118
101
$ attributeEntity = $ config ->getAttribute ($ attributeKey );
119
102
if (!is_null ($ attributeEntity ->getKey ())) {
120
- array_push ($ this ->_eventParams [USER_FEATURES ], [
121
- 'id ' => $ attributeEntity ->getId (),
122
- 'name ' => $ attributeKey ,
123
- 'type ' => 'custom ' ,
124
- 'value ' => $ attributeValue ,
125
- 'shouldIndex ' => true
126
- ]);
103
+ $ feature = [
104
+ ENTITY_ID => $ attributeEntity ->getId (),
105
+ KEY => $ attributeKey ,
106
+ TYPE => CUSTOM_ATTRIBUTE_FEATURE_TYPE ,
107
+ VALUE => $ attributeValue ,
108
+ ];
127
109
}
128
110
}
129
111
}
112
+
113
+ if (!empty ($ feature ))
114
+ $ commonParams [VISITORS ][0 ][ATTRIBUTES ][] = $ feature ;
130
115
}
116
+
117
+ return $ commonParams ;
131
118
}
132
119
133
120
/**
134
- * Helper function to set parameters specific to impression event.
121
+ * Helper function to get parameters specific to impression event.
135
122
*
136
123
* @param $experiment Experiment Experiment being activated.
137
124
* @param $variationId string
138
125
*/
139
- private function setImpressionParams (Experiment $ experiment , $ variationId )
126
+ private function getImpressionParams (Experiment $ experiment , $ variationId )
140
127
{
141
- $ this ->_eventParams [LAYER_ID ] = $ experiment ->getLayerId ();
142
- $ this ->_eventParams [DECISION ] = [
143
- EXPERIMENT_ID => $ experiment ->getId (),
144
- VARIATION_ID => $ variationId ,
145
- IS_LAYER_HOLDBACK => false
128
+ $ impressionParams = [
129
+ DECISIONS => [
130
+ [
131
+ CAMPAIGN_ID => $ experiment ->getLayerId (),
132
+ EXPERIMENT_ID => $ experiment ->getId (),
133
+ VARIATION_ID => $ variationId
134
+ ]
135
+ ],
136
+
137
+ EVENTS => [
138
+ [
139
+ ENTITY_ID => $ experiment ->getLayerId (),
140
+ TIMESTAMP => time ()*1000 ,
141
+ KEY => ACTIVATE_EVENT_KEY ,
142
+ UUID => GeneratorUtils::getRandomUuid ()
143
+ ]
144
+ ]
145
+
146
146
];
147
+
148
+ return $ impressionParams ;
147
149
}
148
150
149
151
/**
150
- * Helper function to set parameters specific to conversion event.
152
+ * Helper function to get parameters specific to conversion event.
151
153
*
152
154
* @param $config ProjectConfig Configuration for the project.
153
155
* @param $eventKey string Key representing the event.
154
156
* @param $experimentVariationMap array Map of experiment ID to the ID of the variation that the user is bucketed into.
155
157
* @param $userId string ID of user.
156
158
* @param $eventTags array Hash representing metadata associated with the event.
157
159
*/
158
- private function setConversionParams ($ config , $ eventKey , $ experimentVariationMap , $ userId , $ eventTags )
160
+ private function getConversionParams ($ config , $ eventKey , $ experimentVariationMap , $ userId , $ eventTags )
159
161
{
160
- $ this ->_eventParams [EVENT_FEATURES ] = [];
161
- $ this ->_eventParams [EVENT_METRICS ] = [];
162
-
163
- if (!is_null ($ eventTags )) {
164
- forEach ($ eventTags as $ eventTagId => $ eventTagValue ) {
165
- if (is_null ($ eventTagValue )) {
166
- continue ;
167
- }
168
- $ eventFeature = array (
169
- 'name ' => $ eventTagId ,
170
- 'type ' => 'custom ' ,
171
- 'value ' => $ eventTagValue ,
172
- 'shouldIndex ' => false ,
173
- );
174
- array_push ($ this ->_eventParams [EVENT_FEATURES ], $ eventFeature );
175
- }
176
- $ eventValue = EventTagUtils::getRevenueValue ($ eventTags );
177
- if ($ eventValue ) {
178
- $ eventMetric = array (
179
- 'name ' => EventTagUtils::REVENUE_EVENT_METRIC_NAME ,
180
- 'value ' => $ eventValue ,
181
- );
182
- array_push ($ this ->_eventParams [EVENT_METRICS ], $ eventMetric );
183
- }
184
- }
185
162
186
- $ eventEntity = $ config ->getEvent ($ eventKey );
187
- $ this ->_eventParams [EVENT_ID ] = $ eventEntity ->getId ();
188
- $ this ->_eventParams [EVENT_NAME ] = $ eventKey ;
189
-
190
- $ this ->_eventParams [LAYER_STATES ] = [];
191
- forEach ($ experimentVariationMap as $ experimentId => $ variationId ) {
163
+ $ conversionParams = [];
164
+ foreach ($ experimentVariationMap as $ experimentId => $ variationId ){
165
+ $ singleSnapshot = [];
192
166
$ experiment = $ config ->getExperimentFromId ($ experimentId );
193
- array_push ( $ this -> _eventParams [ LAYER_STATES ], [
194
- LAYER_ID => $ experiment -> getLayerId (),
195
- ACTION_TRIGGERED => true ,
196
- REVISION => $ config -> getRevision (),
197
- DECISION => [
167
+ $ eventEntity = $ config -> getEvent ( $ eventKey );
168
+
169
+ $ singleSnapshot [ DECISIONS ] = [
170
+ [
171
+ CAMPAIGN_ID => $ experiment -> getLayerId (),
198
172
EXPERIMENT_ID => $ experimentId ,
199
- VARIATION_ID => $ variationId ,
200
- IS_LAYER_HOLDBACK => false
173
+ VARIATION_ID => $ variationId
174
+ ]
175
+ ];
176
+
177
+ $ singleSnapshot [EVENTS ] = [
178
+ [
179
+ ENTITY_ID => $ eventEntity ->getId (),
180
+ TIMESTAMP => time ()*1000 ,
181
+ UUID => GeneratorUtils::getRandomUuid (),
182
+ KEY => $ eventKey
201
183
]
202
- ]);
184
+ ];
185
+
186
+ if (!is_null ($ eventTags )){
187
+ $ revenue = EventTagUtils::getRevenueValue ($ eventTags );
188
+ if (!is_null ($ revenue )){
189
+ $ singleSnapshot [EVENTS ][0 ][EventTagUtils::REVENUE_EVENT_METRIC_NAME ] = $ revenue ;
190
+ }
191
+
192
+ $ eventValue = EventTagUtils::getNumericValue ($ eventTags );
193
+ if (!is_null ($ eventValue )){
194
+ $ singleSnapshot [EVENTS ][0 ][EventTagUtils::NUMERIC_EVENT_METRIC_NAME ] = $ eventValue ;
195
+ }
196
+
197
+ $ singleSnapshot [EVENTS ][0 ]['tags ' ] = $ eventTags ;
198
+ }
199
+
200
+ $ conversionParams [] = $ singleSnapshot ;
203
201
}
202
+
203
+ return $ conversionParams ;
204
204
}
205
205
206
206
/**
@@ -216,14 +216,15 @@ private function setConversionParams($config, $eventKey, $experimentVariationMap
216
216
*/
217
217
public function createImpressionEvent ($ config , $ experimentKey , $ variationKey , $ userId , $ attributes )
218
218
{
219
- $ this ->resetParams ();
220
- $ this ->setCommonParams ($ config , $ userId , $ attributes );
219
+ $ eventParams = $ this ->getCommonParams ($ config , $ userId , $ attributes );
221
220
222
221
$ experiment = $ config ->getExperimentFromKey ($ experimentKey );
223
222
$ variation = $ config ->getVariationFromKey ($ experimentKey , $ variationKey );
224
- $ this ->setImpressionParams ($ experiment , $ variation ->getId ());
223
+ $ impressionParams = $ this ->getImpressionParams ($ experiment , $ variation ->getId ());
224
+
225
+ $ eventParams [VISITORS ][0 ][SNAPSHOTS ][] = $ impressionParams ;
225
226
226
- return new LogEvent (self ::$ IMPRESSION_ENDPOINT , $ this -> getParams () , self ::$ HTTP_VERB , self ::$ HTTP_HEADERS );
227
+ return new LogEvent (self ::$ ENDPOINT , $ eventParams , self ::$ HTTP_VERB , self ::$ HTTP_HEADERS );
227
228
}
228
229
229
230
/**
@@ -240,10 +241,13 @@ public function createImpressionEvent($config, $experimentKey, $variationKey, $u
240
241
*/
241
242
public function createConversionEvent ($ config , $ eventKey , $ experimentVariationMap , $ userId , $ attributes , $ eventTags )
242
243
{
243
- $ this ->resetParams ();
244
- $ this ->setCommonParams ($ config , $ userId , $ attributes );
245
- $ this ->setConversionParams ($ config , $ eventKey , $ experimentVariationMap , $ userId , $ eventTags );
246
244
247
- return new LogEvent (self ::$ CONVERSION_ENDPOINT , $ this ->getParams (), self ::$ HTTP_VERB , self ::$ HTTP_HEADERS );
245
+ $ eventParams = $ this ->getCommonParams ($ config , $ userId , $ attributes );
246
+ $ conversionParams = $ this ->getConversionParams ($ config , $ eventKey , $ experimentVariationMap , $ userId , $ eventTags );
247
+
248
+ $ eventParams [VISITORS ][0 ][SNAPSHOTS ] = $ conversionParams ;
249
+ return new LogEvent (self ::$ ENDPOINT , $ eventParams , self ::$ HTTP_VERB , self ::$ HTTP_HEADERS );
248
250
}
249
251
}
252
+
253
+
0 commit comments