ABC Test

node v18.11.0
version: 4.0.0
endpointsharetweet
const crypto = require('crypto'); function generateABCValue(userId, featureName, ratio, segment, segmentsArray) { if (ratio > 1 || ratio < 0) { throw new Error("Ratio must be between 0 and 1"); } const hasSegment = segmentsArray.includes(segment); if(!hasSegment) return 'C'; if(ratio === 0) return 'A'; if(ratio === 1) return 'B'; // Create a hash from the userId, featureName const hash = crypto.createHash('sha1'); hash.update(userId + featureName); const hashedValue = hash.digest('hex'); // Convert the last 2 characters of the hash into an integer and normalize to 0-1 range const intValue = parseInt(hashedValue.slice(-2), 16); const normalizedValue = intValue / 255; // console.log({intValue, normalizedValue}) // Check if the segment is in the segmentsArray if (!hasSegment) return 'C'; // Use the ratio to determine the group return normalizedValue <= ratio ? 'B' : 'A'; } console.log('Groups: ' + generateABCValue("62432877", "ab_test_bnpl_ph", 0.5, 'S23', ['S4', 'S3', 'C1']));
Loading…

no comments

    sign in to comment