Cloudfront function implement CloudWatch Evidently to do A/B testing

56 Views Asked by At

I have cloudfront function which redirects to different cloudfront based on cookies or based on percentage allocation.

Following is the code

function handler(event) {
    const request = event.request;
    const headers = request.headers;
    const host = headers.host.value;
    
    const broadbandRewrite = 'broadbandRewrite-abtest';
    const broadbandExisting = 'broadbandExisting-abtest';
    const pathBroadbandRewrite = 'https://xxxx1.cloudfront.net/index.html';
    
    if (request.cookies) {
        for (var key in request.cookies) {
            if (key == broadbandRewrite) {
                console.log('Broadband rewrite cookie found');
                let response = {
                    statusCode: 302,
                    statusDescription: 'Found',
                    headers:
                        { "location": { "value": pathBroadbandRewrite } }
                    }
                return response;                
            } else if (key == broadbandExisting)
            {
                return request;
            }
        }
    }

    console.log('Broadband rewrite cookie NOT found. Allocated based on percentage');
    
    let randomAllocation = Math.random();
    console.log('random allocation is: '+ randomAllocation);
    if (randomAllocation < 0.25) {
        console.log('Broadband rewrite allocated ');
        let response = {
            statusCode: 302,
            statusDescription: 'Found',
            headers:
                { "location": { "value": pathBroadbandRewrite } }
            }
        return response;
    }
        
    console.log('Broadband existing allocated ');
    
    return request;
}

Question: Is it possible to implement "CloudWatch Evidently" to do percent allocation in Cloudfront function

0

There are 0 best solutions below