Skip to content

Commit

Permalink
fix(userStorageMAB): check userStorage is configured if campaign has …
Browse files Browse the repository at this point in the history
…MAB enabled
  • Loading branch information
rohitesh-wingify committed Sep 28, 2023
1 parent f59da98 commit 6b99696
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 3 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.60.0] - 2023-09-28

### Added

- Check for user storage if campaign has MAB enabled

## [1.58.0] - 2023-08-11

### Added
Expand Down
4 changes: 2 additions & 2 deletions 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.58.0';
const SDK_VERSION = '1.60.0';
/**
* sdk langauge for api hit
*/
Expand Down Expand Up @@ -212,7 +212,7 @@ public static function getEventBasePayload($configObj, $userId, $eventName, $usa

$properties = [
"d" => [
"msgId" => $uuid . "-" . time(),
"msgId" => $uuid . "-" . CommonUtil::getCurrentUnixTimestampInMillis(),
"visId" => $uuid,
"sessionId" => time(),
"event" => [
Expand Down
21 changes: 21 additions & 0 deletions src/VWO.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,13 @@ public function isFeatureEnabled($campaignKey, $userId, $options = [])
}
// get campaigns
$campaign = ValidationsUtil::getCampaignFromCampaignKey($campaignKey, $this->settings, self::$apiName);
// Check if MAB enabled, if yes, then userStorage must be defined
if ($campaign != null && isset($campaign['isMAB']) && $campaign['isMAB']) {
if ($this->_userStorageObj == null) {
LoggerService::log(Logger::ERROR, 'This campaign: ' .$campaignKey. ' has MAB configured. Please configure User Storage to proceed.', [], self::CLASSNAME);
return null;
}
}
if ($campaign == null) {
return null;
}
Expand Down Expand Up @@ -532,6 +539,13 @@ public function track($campaignKey = '', $userId = '', $goalIdentifier = '', arr
}

foreach ($campaigns as $campaign) {
// Check if MAB enabled, if yes, then userStorage must be defined
if ($campaign != null && isset($campaign['isMAB']) && $campaign['isMAB']) {
if ($this->_userStorageObj == null) {
LoggerService::log(Logger::ERROR, 'This campaign: ' .$campaignKey. ' has MAB configured. Please configure User Storage to proceed.', [], self::CLASSNAME);
return null;
}
}
try {
if ($campaign['type'] == CampaignTypes::FEATURE_ROLLOUT) {
LoggerService::log(
Expand Down Expand Up @@ -809,6 +823,13 @@ private function getVariation($campaignKey, $userId, $options, $trackVisitor, $a
$bucketInfo = null;
try {
$campaign = ValidationsUtil::getCampaignFromCampaignKey($campaignKey, $this->settings, $apiName);
// Check if MAB enabled, if yes, then userStorage must be defined
if ($campaign != null && isset($campaign['isMAB']) && $campaign['isMAB']) {
if ($this->_userStorageObj == null) {
LoggerService::log(Logger::ERROR, 'This campaign: ' .$campaignKey. ' has MAB configured. Please configure User Storage to proceed.', [], self::CLASSNAME);
return null;
}
}
if ($campaign !== null) {
if (($campaign['type'] == CampaignTypes::FEATURE_ROLLOUT) || ($campaign['type'] == CampaignTypes::FEATURE_TEST && $trackVisitor == 1)) {
LoggerService::log(
Expand Down
15 changes: 15 additions & 0 deletions tests/VWOTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ protected function setUp()
$this->settings8 = Settings8::setup();
$this->settings9 = Settings9::setup();
$this->settingsFileEventProperties = SettingsFileEventProperties::setup();
$this->MABTrueSettingsFile = MABTrueSettingsFile::setup();
$segmentEvaluatorJson = new SegmentEvaluatorJson();
$results = new VariationResults();

Expand Down Expand Up @@ -1048,4 +1049,18 @@ public function testIfEventPropertiesDoNotHaveRevenuePropAndItIsPassedInsteadOfR
$response = $vwoInstance->track($campaignKey,'Abby','Track3',$options);
$this->assertEquals(false, $response);
}

public function testWhenMABTrueButNoUserStorage(){
$campaignKey = $this->MABTrueSettingsFile['campaigns'][0]['key'];
$vwoInstance = TestUtil::instantiateSdk($this->MABTrueSettingsFile, ['isDevelopmentMode' => 1]);
$variation = $vwoInstance->activate($campaignKey, 'George');
$this->assertEquals($variation, null);
}

public function testWhenMABTrueWithUserStorageEnabled(){
$campaignKey = $this->MABTrueSettingsFile['campaigns'][0]['key'];
$vwoInstance = TestUtil::instantiateSdk($this->MABTrueSettingsFile, ['isUserStorage' => 1, 'isDevelopmentMode' => 1]);
$variation = $vwoInstance->activate($campaignKey, 'George');
$this->assertEquals($variation, 'Control');
}
}
63 changes: 63 additions & 0 deletions tests/assets/MABTrueSettingsFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/**
* Copyright 2019-2022 Wingify Software Pvt. Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace vwo;

class MABTrueSettingsFile
{
public static function setup()
{
return [
'sdkKey' => 'loremipsum123456',
'campaigns' => [
[
'goals' => [
[
'identifier' => 'CUSTOM',
'id' => 213,
'type' => 'CUSTOM_GOAL'
]
],
'variations' => [
[
'id' => 1,
'name' => 'Control',
'changes' => [],
'weight' => 50
],
[
'id' => 2,
'name' => 'Variation-1',
'changes' => [],
'weight' => 50
]
],
'id' => 230,
'name' => "Campaign-230",
'percentTraffic' => 50,
'key' => 'DEV_TEST_1',
'status' => 'RUNNING',
'type' => 'VISUAL_AB',
'isMAB' => true
]
],
'accountId' => 123456,
'version' => 1
];
}
}
3 changes: 2 additions & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@
require 'assets/VariationResults.php';
require 'TestUtil.php';
require 'assets/SettingsFileBucketing.php';
require 'assets/SettingsFileEventProperties.php';
require 'assets/SettingsFileEventProperties.php';
require 'assets/MABTrueSettingsFile.php';

0 comments on commit 6b99696

Please sign in to comment.