Skip to content

Commit

Permalink
refactor(events): fixed revenue value not passed error for data360 ac…
Browse files Browse the repository at this point in the history
…counts
  • Loading branch information
Saksham Gupta authored and rohitesh-wingify committed Jul 14, 2023
1 parent 7ad0938 commit cf0cb6c
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 41 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ 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.55.0] - 2023-07-14

### Changed

- Modified the revenueProp logic to read from eventProperties for Data360 enabled accounts
- Updated the code to check for existence of eventProperties and access the revenueProp property accordingly

## [1.50.0] - 2023-06-27

### Changed
Expand Down
2 changes: 1 addition & 1 deletion src/Constants/Segments.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Segments
const OPERAND_OR = 'or';
const CUSTOM_VARIABLE = 'custom_variable';
const USER = 'user';

/**
* REGEX CONSTANTS
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Bucketer.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public static function isUserPartofCampaign($bucketVal, $percentTraffic)
public static function getMultiplier($traffic, $disableLogs = false)
{
$multiplier = 0;
if($traffic <= 0) {
if ($traffic <= 0) {
LoggerService::log(
Logger::ERROR,
'Something went wrong. Traffic is ' . $traffic . ' and error is division by zero',
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/ImpressionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ImpressionBuilder
/**
* sdk version for api hit
*/
const SDK_VERSION = '1.50.0';
const SDK_VERSION = '1.55.0';
/**
* sdk langauge for api hit
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Utils/OperandEvaluator.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ public static function evaluateCustomVariableOperand($operand, $customVariables)
}
} elseif ($operandType == SegmentConstants::OPERAND_GREATER_THAN) {
$result = $isCustomVariabbleNumeric ? (float)$operandValue < (float)$customVariable : false;
} elseif ($operandType == SegmentConstants::OPERAND_LESS_THAN ) {
} elseif ($operandType == SegmentConstants::OPERAND_LESS_THAN) {
$result = $isCustomVariabbleNumeric ? (float)$operandValue > (float)$customVariable : false;
} elseif ($operandType == SegmentConstants::OPERAND_GREATER_THAN_EQUAL_TO ) {
} elseif ($operandType == SegmentConstants::OPERAND_GREATER_THAN_EQUAL_TO) {
$result = $isCustomVariabbleNumeric ? (float)$operandValue <= (float)$customVariable : false;
} elseif ($operandType == SegmentConstants::OPERAND_LESS_THAN_EQUAL_TO ) {
} elseif ($operandType == SegmentConstants::OPERAND_LESS_THAN_EQUAL_TO) {
$result = $isCustomVariabbleNumeric ? (float)$operandValue >= (float)$customVariable : false;
} else {
$result = $customVariable === $operandValue;
Expand Down
85 changes: 55 additions & 30 deletions src/VWO.php
Original file line number Diff line number Diff line change
Expand Up @@ -551,41 +551,66 @@ public function track($campaignKey = '', $userId = '', $goalIdentifier = '', arr

$goal = CommonUtil::getGoalFromGoals($campaign['goals'], $goalIdentifier);
$goalId = isset($goal['id']) ? $goal['id'] : 0;
$mca = isset($goal['mca']) ? $goal['mca'] : NULL;

$mca = isset($goal['mca']) ? $goal['mca'] : null;
if ($goalId && isset($bucketInfo['id']) && $bucketInfo['id'] > 0) {
if ($goal['type'] == "REVENUE_TRACKING") {

if ($this->isEventArchEnabled()) {
$doesRevenuePropExist = false;
if (!$revenueValue) {
$doesRevenuePropExist = false;

foreach ($eventProperties as $eventProp => $eventValue) {
if ($eventProp == $goal['revenueProp']) {
if (isset($goal['revenueProp'])) {
$doesRevenuePropExist = true;
break;
}
}

if (!$doesRevenuePropExist) {
if (is_null($revenueValue)) {
LoggerService::log(
Logger::ERROR,
'TRACK_API_EVENTS_REVENUE_NOT_PASSED',
[
'{goalIdentifier}' => $goalIdentifier,
'{campaignKey}' => $campaign['key'],
'{userId}' => $userId
],
self::CLASSNAME
);
$result[$campaign['key']] = null;
continue;
//If it's a metric of type - value of an event property and calculation logic is first Value (mca doesn't exist)
if (!isset($mca)) {
/*
In this case it is expected that goal will have revenueProp
Error should be logged if eventProperties is not Defined ` OR ` eventProperties does not have revenueProp key
*/
if (!isset($eventProperties) || !array_key_exists($goal['revenueProp'], $eventProperties)) {
LoggerService::log(
Logger::ERROR,
'TRACK_API_REVENUE_NOT_PASSED_FOR_REVENUE_GOAL',
[
'{goalIdentifier}' => $goalIdentifier,
'{campaignKey}' => $campaign['key'],
'{userId}' => $userId
],
self::CLASSNAME
);
$result[$campaign['key']] = null;
continue;
}
} else {
$revProp = $goal['revenueProp'];
$eventProperties[$revProp] = $revenueValue;
/*
here mca == -1 so there could only be 2 scenarios,
1. If revenueProp is defined then eventProperties should have revenueProp key
2. if revenueProp is not defined then it's a metric of type - Number of times an event has been triggered.
*/
if ($doesRevenuePropExist) {
// Error should be logged if eventProperties is not Defined ` OR ` eventProperties does not have revenueProp key
if (!isset($eventProperties) || !array_key_exists($goal['revenueProp'], $eventProperties)) {
LoggerService::log(
Logger::ERROR,
'TRACK_API_REVENUE_NOT_PASSED_FOR_REVENUE_GOAL',
[
'{goalIdentifier}' => $goalIdentifier,
'{campaignKey}' => $campaign['key'],
'{userId}' => $userId
],
self::CLASSNAME
);
$result[$campaign['key']] = null;
continue;
}
}
}
} else {
$revProp = $goal['revenueProp'];
$eventProperties[$revProp] = $revenueValue;
}
} elseif (is_null($revenueValue)) {
} elseif (is_null($revenueValue)) {
LoggerService::log(
Logger::ERROR,
'TRACK_API_REVENUE_NOT_PASSED_FOR_REVENUE_GOAL',
Expand All @@ -598,7 +623,7 @@ public function track($campaignKey = '', $userId = '', $goalIdentifier = '', arr
);
$result[$campaign['key']] = null;
continue;
}
}
}

if (isset($goalIdentifier)) {
Expand All @@ -615,7 +640,7 @@ public function track($campaignKey = '', $userId = '', $goalIdentifier = '', arr
if (!empty($this->_userStorageObj)) {
$this->variationDecider->userStorageSet($this->_userStorageObj, $userId, $campaign['key'], $bucketInfo, $bucketInfo['goalIdentifier']);
}
} else if ($mca != -1) {
} elseif ($mca != -1) {
LoggerService::log(
Logger::INFO,
'CAMPAIGN_GOAL_ALREADY_TRACKED',
Expand All @@ -632,7 +657,7 @@ public function track($campaignKey = '', $userId = '', $goalIdentifier = '', arr
}

if ($this->isEventArchEnabled()) {
if ($goal['type'] == "REVENUE_TRACKING" && !in_array($goal['revenueProp'], $revenueProps)) {
if ($goal['type'] == "REVENUE_TRACKING" && isset($goal['revenueProp']) && !in_array($goal['revenueProp'], $revenueProps)) {
$revenueProps[] = $goal['revenueProp'];
}
$metricMap[$campaign['id']] = $goal["id"];
Expand Down Expand Up @@ -701,6 +726,7 @@ public function track($campaignKey = '', $userId = '', $goalIdentifier = '', arr
$metricMap,
$eventProperties
);

$eventArchResponse = $this->eventDispatcher->sendEventRequest($parameters, $payload);
if ($eventArchResponse) {
LoggerService::log(
Expand Down Expand Up @@ -999,8 +1025,7 @@ public function getSDKKey()

private function isEligibleToSendImpressionToVWO()
{
return (
empty($this->_userStorageObj) ||
return (empty($this->_userStorageObj) ||
!$this->variationDecider->hasStoredVariation
);
}
Expand Down
5 changes: 3 additions & 2 deletions tests/Core/MutuallyExclusiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,9 @@ public function testWinnerCampaignIsNotTheCalledCampaignAfterRandomWeightageDist
$winners = 0;
for ($i = 0; $i < $iterations; $i++) {
$variation = $vwoInstance->getVariationName($campaignKey, 'George');
if($variation != null)
$winners = $winners+1;
if ($variation != null) {
$winners = $winners + 1;
}
}

$actualRatio = $winners / $iterations;
Expand Down
57 changes: 55 additions & 2 deletions tests/VWOTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ protected function setUp()
$this->settings7 = Settings7::setup();
$this->settings8 = Settings8::setup();
$this->settings9 = Settings9::setup();
$this->settingsFileEventProperties = SettingsFileEventProperties::setup();
$segmentEvaluatorJson = new SegmentEvaluatorJson();
$results = new VariationResults();

Expand Down Expand Up @@ -786,14 +787,22 @@ public function testTrackApiWithEventArch()
$this->vwoInstance = TestUtil::instantiateSdk($settingsFile, ['isDevelopmentMode' => 1]);
$campaignKey = 'DEV_TEST_' . $devtest;
$options = [];
//$options['eventProperties']
$eventProperties = [
'numberProperty' => 100,
'textProperty' => 'abc',
'booleanProperty' => true
];

foreach ($this->users as $userId) {
foreach ($settingsFile['campaigns'] as $index => $campaign) {
if ($campaign['key'] == $campaignKey) {
$goalName = $campaign['goals'][0]['identifier'];
if ($campaign['goals'][0]['type'] == 'REVENUE_TRACKING') {
$options['revenueValue'] = 10;
$settingsFile['campaigns'][$index]['goals'][0]['revenueProp'] = 'dummyRevenueProperty';
$options = [
'eventProperties' => $eventProperties
];
$settingsFile['campaigns'][$index]['goals'][0]['revenueProp'] = 'numberProperty';
$this->vwoInstance = TestUtil::instantiateSdk($settingsFile, ['isDevelopmentMode' => 1]);
}
break;
Expand Down Expand Up @@ -995,4 +1004,48 @@ public function testPushApiForMultipleCustomDimension()
$this->assertEquals(true, $response[$tagKey]);
}
}
public function testWhenRevenueValueNotPassedInTheGoal(){

$settingsFileEventProperties = SettingsFileEventProperties::setUp();
$campaignKey = $settingsFileEventProperties['campaigns'][0]['key'];
$vwoInstance = TestUtil::instantiateSdk($settingsFileEventProperties);
$response = $vwoInstance->track($campaignKey,'Abby','Track3');
$this->assertEquals(false, $response);
}

public function testWhenRevenueValueIsNotPassedForMetricOfTypeNumberOfTimesEventIsTriggered(){
$settingsFileEventProperties = SettingsFileEventProperties::setUp();
$campaignKey = $settingsFileEventProperties['campaigns'][0]['key'];
$vwoInstance = TestUtil::instantiateSdk($settingsFileEventProperties);
$response = $vwoInstance->track($campaignKey,'Abby','Track4');
$this->assertEquals(true, $response);
}

public function testIfEventPropertiesIsPassedInsteadOfRevenueValue(){
$settingsFileEventProperties = SettingsFileEventProperties::setUp();
$campaignKey = $settingsFileEventProperties['campaigns'][0]['key'];
$vwoInstance = TestUtil::instantiateSdk($settingsFileEventProperties);
$eventProperties = [
'abcd' => 100
];
$options = [
'eventProperties' => $eventProperties
];
$response = $vwoInstance->track($campaignKey,'Abby','Track3',$options);
$this->assertEquals(true, $response);
}

public function testIfEventPropertiesDoNotHaveRevenuePropAndItIsPassedInsteadOfRevenueValue(){
$settingsFileEventProperties = SettingsFileEventProperties::setUp();
$campaignKey = $settingsFileEventProperties['campaigns'][0]['key'];
$vwoInstance = TestUtil::instantiateSdk($settingsFileEventProperties);
$eventProperties = [
'abcde' => 100
];
$options = [
'eventProperties' => $eventProperties
];
$response = $vwoInstance->track($campaignKey,'Abby','Track3',$options);
$this->assertEquals(false, $response);
}
}
81 changes: 81 additions & 0 deletions tests/assets/SettingsFileEventProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?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 SettingsFileEventProperties
{
public static function setup()
{
return [
'sdkKey' => 'loremipsum1234567',
'campaigns' => [
[
'goals' => [
[
'identifier' => 'Track1',
'id' => 216,
'type' => 'CUSTOM_GOAL'
],
[
'identifier' => 'Track2',
'id' => 217,
'type' => 'CUSTOM_GOAL'
],
[
'identifier' => 'Track3',
'id' => 218,
'type' => 'REVENUE_TRACKING',
'revenueProp' => 'abcd'
],
[
'identifier' => 'Track4',
'id' => 219,
'type' => 'REVENUE_TRACKING',
'mca' => -1
]
],
'variations' => [
[
'id' => 1,
'name' => 'Control',
'changes' => [],
'weight' => 10
],
[
'id' => 2,
'name' => 'Variation-1',
'changes' => [],
'weight' => 90
]
],
'id' => 233,
'name' => "Track",
'percentTraffic' => 100,
'key' => 'Track',
'status' => 'RUNNING',
'type' => 'VISUAL_AB',
'isForcedVariationEnabled'=> false
]
],
'accountId' => 123456,
'version' => 1,
'isEventArchEnabled' => true
];
}
}
3 changes: 2 additions & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@
require 'assets/FRWhitelistingSettings.php';
require 'assets/VariationResults.php';
require 'TestUtil.php';
require 'assets/SettingsFileBucketing.php';
require 'assets/SettingsFileBucketing.php';
require 'assets/SettingsFileEventProperties.php';

0 comments on commit cf0cb6c

Please sign in to comment.