From ec1dd0d77be2224334ccb91a275836400fde508d Mon Sep 17 00:00:00 2001 From: Rohitesh Dutta Date: Tue, 21 Nov 2023 22:01:54 +0530 Subject: [PATCH] fix(MEG): Issues with Advanced implementation of MEG --- CHANGELOG.md | 6 +++++ composer.json | 2 +- src/Core/VariationDecider.php | 33 ++++++++++++---------------- src/Utils/ImpressionBuilder.php | 2 +- tests/Core/MutuallyExclusiveTest.php | 8 ++++--- 5 files changed, 27 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c43aac9..b83bfb1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.66.0] - 2023-11-21 + +### Fixed + +- Fixed issue with Advanced implementation of MEG + ## [1.65.0] - 2023-11-09 ### Fixed diff --git a/composer.json b/composer.json index 031ee65..decdfca 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "description": "VWO server side sdk", "require": { "monolog/monolog": "^1.0 || ^2.0", - "ramsey/uuid": "^3.8 || ^4.4", + "ramsey/uuid": "^3.8 || ^4.0", "justinrainbow/json-schema": "^5.2", "vwo/vwo-sdk-log-messages": ">=0.10.0" }, diff --git a/src/Core/VariationDecider.php b/src/Core/VariationDecider.php index 8d808d7..2bd6217 100644 --- a/src/Core/VariationDecider.php +++ b/src/Core/VariationDecider.php @@ -487,35 +487,30 @@ private static function findWinnerCampaign($userId, $eligibleCampaigns, $megAlgo // If winnerCampaign not found through Priority, then go for weighted Random distribution and for that, // Store the list of campaigns (participatingCampaigns) out of eligibleCampaigns and their corresponding weights which are present in weightage distribution array (wt) in 2 different lists if ($winnerCampaign == null) { - $weights = array(); $partipatingCampaignList = array(); for ($i = 0; $i < count($eligibleCampaigns); $i++) { $campaignId = $eligibleCampaigns[$i]['id']; if (isset($wt[$campaignId])) { - $weights[] = $wt[$campaignId]; + // Add weight to the campaign + $eligibleCampaigns[$i]['weight'] = $wt[$campaignId]; + $partipatingCampaignList[] = $eligibleCampaigns[$i]; } } - /* - * Finding winner campaign using weighted random distribution : - 1. Calculate the sum of all weights - 2. Generate a random number between 0 and the weight sum: - 3. Iterate over the weights array and subtract each weight from the random number until the random number becomes negative. The corresponding ith value is the required value - 4. Set the ith campaign as WinnerCampaign + /* Finding winner campaign using weighted Distibution : + 1. Re-distribute the traffic by assigning range values for each camapign in particaptingCampaignList + 2. Calculate bucket value for the given userId and groupId + 3. Get the winnerCampaign by checking the Start and End Bucket Allocations of each campaign */ - $weightSum = array_sum($weights); - $randomNumber = rand(1, $weightSum); - - $sum = 0; - for ($i = 0; $i < count($weights); $i++) { - $sum += $weights[$i]; - if ($randomNumber < $sum) { - $winnerCampaign = $partipatingCampaignList[$i]; - break; - } - } + + //Allocate new range for campaigns + $eligibleCampaigns = Bucketer::addRangesToCampaigns($partipatingCampaignList); + //Now retrieve the campaign from the modified_campaign_for_whitelisting + list($bucketVal, $hashValue) = Bucketer::getBucketVal($userId, [], false, true); + + $winnerCampaign = Bucketer::getCampaignUsingRange($bucketVal, $eligibleCampaigns); } return $winnerCampaign; diff --git a/src/Utils/ImpressionBuilder.php b/src/Utils/ImpressionBuilder.php index 5f12894..04a9d7b 100644 --- a/src/Utils/ImpressionBuilder.php +++ b/src/Utils/ImpressionBuilder.php @@ -31,7 +31,7 @@ class ImpressionBuilder /** * sdk version for api hit */ - const SDK_VERSION = '1.65.0'; + const SDK_VERSION = '1.66.0'; /** * sdk langauge for api hit */ diff --git a/tests/Core/MutuallyExclusiveTest.php b/tests/Core/MutuallyExclusiveTest.php index 2897530..0310a73 100644 --- a/tests/Core/MutuallyExclusiveTest.php +++ b/tests/Core/MutuallyExclusiveTest.php @@ -429,7 +429,8 @@ public function testWinnerCampaignIsNotTheCalledCampaignAfterRandomWeightageDist $winners = 0; for ($i = 0; $i < $iterations; $i++) { - $variation = $vwoInstance->getVariationName($campaignKey, 'George'); + $userId = "user" . $i; + $variation = $vwoInstance->getVariationName($campaignKey, $userId); if ($variation != null) { $winners = $winners + 1; } @@ -464,8 +465,9 @@ public function testWinnerCampaignFoundThroughWeightage() $winners = 0; for ($i = 0; $i < $iterations; $i++) { - $variation = $vwoInstance->getVariationName($campaignKey, 'George'); - $winners = $variation == 'Control' ? $winners + 1 : $winners; + $userId = "user" . $i; + $variation = $vwoInstance->getVariationName($campaignKey, $userId); + $winners = $variation != null ? $winners + 1 : $winners; } $actualRatio = $winners / $iterations;