2
2
createInstance ,
3
3
enums as OptimizelyEnums ,
4
4
} from '@optimizely/optimizely-sdk/dist/optimizely.lite.es' ;
5
- import { getCookie , setHeaderCookie } from './cookies' ;
5
+
6
+ import * as cookie from 'cookie' ;
6
7
7
8
import {
8
9
dispatchEvent ,
@@ -36,8 +37,10 @@ exports.handler = async (event, _context, callback) => {
36
37
}
37
38
38
39
let headers = { } ;
40
+ let cookies = { } ;
39
41
try {
40
42
headers = request . headers ;
43
+ cookies = headers . cookie ;
41
44
} catch ( error ) {
42
45
console . log (
43
46
'[OPTIMIZELY] WARNING: Unable to get headers object from request object. This may be because the event is not a CloudFront event.'
@@ -47,14 +50,14 @@ exports.handler = async (event, _context, callback) => {
47
50
try {
48
51
// 1. User ID: Get the user ID from the cookie if it exists - otherwise generate a new user ID.
49
52
console . log ( '[OPTIMIZELY] Getting user ID...' ) ;
50
- let userId = getCookie ( headers , COOKIE_NAME_OPTIMIZELY_USER_ID ) ;
53
+ let userId = cookies [ COOKIE_NAME_OPTIMIZELY_USER_ID ] || '' ;
51
54
52
- if ( ! userId ) {
55
+ if ( userId === '' ) {
53
56
userId = generateRandomUserId ( ) ;
54
- headers = setHeaderCookie (
55
- headers ,
56
- ` ${ COOKIE_NAME_OPTIMIZELY_USER_ID } = ${ userId } `
57
- ) ;
57
+ headers = {
58
+ ... headers ,
59
+ 'Set-Cookie' : cookie . serialize ( COOKIE_NAME_OPTIMIZELY_USER_ID , userId ) ,
60
+ } ;
58
61
}
59
62
60
63
console . log ( `[OPTIMIZELY] Using User ID: ${ userId } ` ) ;
@@ -77,12 +80,12 @@ exports.handler = async (event, _context, callback) => {
77
80
* Optional event dispatcher. Please uncomment the following line if you want to dispatch an impression event to optimizely logx backend.
78
81
* When enabled, an event is dispatched asynchronously. It does not impact the response time for a particular worker but it may
79
82
* add to the total compute time of the Lambda function and can impact AWS billing.
80
- *
83
+ *
81
84
* The event dispatcher attached below is a sample implementation of fire-and-forget event dispatching
82
85
* in Lambda, however if your needs are more complex, you can implement your own event dispatcher
83
86
* and integrate with Step Functions, SQS, or other services.
84
87
*/
85
- // eventDispatcher: dispatchEvent,
88
+ // eventDispatcher: { dispatchEvent } ,
86
89
} ) ;
87
90
88
91
// 4. Experimentation Logic: Get a specific experiment result for this particular User ID.
@@ -106,7 +109,8 @@ exports.handler = async (event, _context, callback) => {
106
109
// === For a Single Flag === //
107
110
const decision = optimizelyUserContext . decide ( '<YOUR_FLAG_HERE>' ) ; // TODO: Replace with your flag name.
108
111
console . log (
109
- `[OPTIMIZELY] The Flag ${ decision . flagKey } was ${ decision . enabled ? 'Enabled' : 'Not Enabled'
112
+ `[OPTIMIZELY] The Flag ${ decision . flagKey } was ${
113
+ decision . enabled ? 'Enabled' : 'Not Enabled'
110
114
} for the user ${ decision . userContext . getUserId ( ) } `
111
115
) ;
112
116
@@ -134,7 +138,10 @@ exports.handler = async (event, _context, callback) => {
134
138
// 5. Result: Return the result to the caller via appending headers or cookies to the callback function.
135
139
136
140
// 5a. Cookies
137
- // headers = setHeaderCookie(headers, `${COOKIE_NAME_OPTIMIZELY_USER_ID}=${userId}`);
141
+ // headers = {
142
+ // ...headers,
143
+ // 'Set-Cookie': cookie.serialize(COOKIE_NAME_OPTIMIZELY_USER_ID, userId)
144
+ // }
138
145
139
146
// 5b. Headers
140
147
// headers['<YOUR_FLAG_HERE>-decision'] = [
0 commit comments