Skip to content

Commit

Permalink
fix(MEG): Issues with Advanced implementation of MEG
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitesh-wingify committed Nov 21, 2023
1 parent 129c836 commit ec1dd0d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 24 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
33 changes: 14 additions & 19 deletions src/Core/VariationDecider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/ImpressionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
8 changes: 5 additions & 3 deletions tests/Core/MutuallyExclusiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit ec1dd0d

Please sign in to comment.