Skip to content

Commit

Permalink
fix(sdk): naming convention and README updated
Browse files Browse the repository at this point in the history
  • Loading branch information
griffinmarc committed May 5, 2020
1 parent c994768 commit b64d9c5
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 168 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ 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.7.2] - 2020-05-06
### Changed
- Fix variable naming and update `README.md` file

## [1.7.1] - 2020-04-29
### Changed
- Use `bucketinfo` correctly in `VWO.php` which was causing weird issues related to decision making in Feature Rollout and Feature Test.
Expand Down
163 changes: 53 additions & 110 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

[![Latest Stable Version](https://img.shields.io/packagist/v/vwo/vwo-php-sdk.svg)](https://packagist.org/packages/vwo/vwo-php-sdk) [![Build Status](http://img.shields.io/travis/wingify/vwo-php-sdk/master.svg?style=flat)](http://travis-ci.org/wingify/vwo-php-sdk) [![Coverage Status](https://coveralls.io/repos/github/wingify/vwo-php-sdk/badge.svg?branch=master)](https://coveralls.io/github/wingify/vwo-php-sdk?branch=master)[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](http://www.apache.org/licenses/LICENSE-2.0)

VWO server side sdk helps in integrating you integrating the vwo features in backend.
Using the sdk you can fetch the campaigns , variations and goals which you have configured
in vwo app. Sdk will automatically calculate the variation that should be assigned to the user.
One can also send the goal track data to vwo app to check the conversions on the vwo dashborad.

This open source library provides you server-side testing capabilities.

## Requirements
* PHP 5.6 or later

> PHP >= 5.6
## Installation

Expand All @@ -25,45 +22,38 @@ composer require vwo/vwo-php-sdk

```php
<?php

require_once('vendor/autoload.php');
require_once('userStorage.php'); // Optional :if you are using UserStorage service feature
require_once('customLogger.php');// Optional :if you are using custom logging feature


use vwo\VWO;


$accountId = 123456;
$sdkKey = 'PROJECT_ENVIRONMENT_KEY';
$accountId = REPLACE_WITH_VWO_ACCOUNT_ID; // eg: 123456
$sdkKey = 'REPLACE_WITH_TOKEN'; // eg: can be found in VWO app - FullStack project
$campaignKey = 'CAMPAIGN_UNIQUE_TEST_KEY';
$userId = 'USER_IDENTIFIER';
$goalIdentifier = 'CAMPAIGN_GOAL_IDENTIFIER';

$userId = 'USER_ID';
$goalIdentifier = 'REPLACE_WITH_CAMPAIGN_GOAL_IDENTIFIER';

// to fetch the settings i.e campaigns, variations and goals
$settingsFile=VWO::getSettingsFile($accountId, $sdkKey);
$settingsFile = VWO::getSettingsFile($accountId, $sdkKey);

$config=['settingsFile' => $settingsFile,
'isDevelopmentMode' => 0, // optional: 1 to enable the dev mode
'logging' => new CustomLogger(), // optional
'userStorageService' => new userStorageService() // optional
$sdkConfig = [
'settingsFile' => $settingsFile,
'isDevelopmentMode' => 0, // optional: 1 to enable the dev mode
'logging' => new CustomLogger(), // optional
'userStorageService' => new userStorageService() // optional
];

$vwoClient = new VWO($config);
$vwoClient = new VWO($sdkConfig);

// to get the variation name along with add a visitor hit to vwo app stats
$varient=$vwoClient->activate($campaignKey, $userId, $options);


// to get the variation name
$varient=$vwoClient->getVariation($campaignKey, $userId, $options);

$variation = $vwoClient->activate($campaignKey, $userId, $options);
// Or, to get the variation name
$variation = $vwoClient->getVariation($campaignKey, $userId, $options);

// add code here to use variation
//...


/**
*send the track api hit to the vwo app stats to increase conversions
* $revenue is optional send in case if there is any revenue inside $options
Expand All @@ -72,39 +62,42 @@ $varient=$vwoClient->getVariation($campaignKey, $userId, $options);
$vwoClient->track($campaignKey, $userId, $goalIdentifier, $options);
```

**Code for UserStorage service**
**Code for implementing User Storage Service**

```php
<?php
require_once('vendor/autoload.php');
use vwo\Utils\UserStorageInterface;
Class UserStorage implements UserStorageInterface{

/**
* @param $userId
* @param $campaignKey
* @return string
*/
public function get($userId, $campaignKey){
return[
'userId' => $userId,
'campaignKey' => $campaignKey,
'variationName' => 'Variation-2'
];
}

/**
* @param $campaignUserMapping
* @return bool
*/
public function set($campaignUserMapping){
// S...code to tore in DB/storage system
return True;
}
/**
* @param $userId
* @param $campaignKey
*
* @return array
*/
public function get($userId, $campaignKey) {
// search in DB/Storage system
$variation = $db->fetch($userId, $campaignKey); // replace with your implementation

return[
'userId' => $userId,
'campaignKey' => $campaignKey,
'variationName' => $variation
];
}

/**
* @param $campaignUserMapping
* @return bool - could be changed
*/
public function set($campaignUserMapping) {
// S...code to store in DB/storage system
return True;
}
}
```

**Code for customLogger file**
**Code for implementing Custom Logger**

```php
<?php
Expand All @@ -115,73 +108,23 @@ use vwo\Logger\LoggerInterface;
* Class CustomLogger
*/
Class CustomLogger implements LoggerInterface{

/**
* @param $message
* @param $level
* @return string
*/
public function addLog($message, $level){
//do code for writing logs to your files/databases
//throw new Exception('my test');
//return $x;

}
/**
* @param $message
* @param $level
*
* @return
*/
public function addLog($message, $level){
// use $level and log $message to either print or store them for later use
}

}
```

## Code Snippets

**Use the code below to fetch settings**

```php
// to fetch the settings i.e campaigns, variations and goals
$settingsFile=VWO::getSettings($accountId, $sdkKey);
```

**Use the code below to create a vwo instance**

```php
$config=['settingsFile'=> $settingsFile,
'isDevelopmentMode'=> 0, // optional: 1 to enable the dev mode
'logger'=> new CustomLogger(), // optional
'userStorageService'=> new userStorageService() // optional
];

$vwoClient = new VWO($config);
```

**Use the code below to both activate campaign for a user and fetch variation name**

```php
// to get the variation name along with add a visitor hit to vwo app stats
$varient=$vwoClient->activate($campaignKey, $userId, $options);
```

**Use the code below to get variation name**

```php
// to get the variation name along with add a visitor hit to vwo app stats
$varient=$vwoClient->getVariation($campaignKey, $userId, $options);
```

**Use the code below to track**

```php
/**
*send the track api hit to the vwo app stats to increase conversions
* $revenue is optional send in case if there is any revenue inside $options
*/

$vwoClient->track($campaignKey, $userId, $goalIdentifier, $options);
```

## Documentation

Refer [Official VWO Documentation](https://developers.vwo.com/reference#server-side-introduction)


## Third-party Resources and Credits

Refer [third-party-attributions.txt](https://github.com/wingify/vwo-php-sdk/blob/master/third-party-attributions.txt)
Expand Down
42 changes: 21 additions & 21 deletions src/Constants/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Constants
/**
* sdk version for api hit
*/
const SDK_VERSION = '1.7.1';
const SDK_VERSION = '1.7.2';
/**
* sdk langauge for api hit
*/
Expand Down Expand Up @@ -71,15 +71,15 @@ class Constants
'CUSTOM_LOGGER_USED' => '({file}): Custom logger used',
'SDK_INITIALIZED' => '({file}): SDK properly initialzed',
'SETTINGS_FILE_PROCESSED' => '({file}): Settings file processed',
'NO_STORED_VARIATION' => '({file}): No stored variation for UserId:{userId} for Campaign:{campaignTestKey} found in UserStorageService',
'NO_STORED_VARIATION' => '({file}): No stored variation for UserId:{userId} for Campaign:{campaignKey} found in UserStorageService',
'NO_USER_STORAGE_SERVICE_GET' => '({file}): No UserStorageService to get for stored data',
'NO_USER_STORAGE_SERVICE_SET' => '({file}): No UserStorageService to set data',
'GETTING_STORED_VARIATION' => '({file}): Got stored variation for UserId:{userId} of Campaign:{campaignTestKey} as Variation: {variationName}, found in UserStorageService',
'CHECK_USER_ELIGIBILITY_FOR_CAMPAIGN' => '({file}): campaign:{campaignTestKey} having traffic allocation:{trafficAllocation} assigned value:{trafficAllocation} to userId:{userId}',
'GETTING_STORED_VARIATION' => '({file}): Got stored variation for UserId:{userId} of Campaign:{campaignKey} as Variation: {variationName}, found in UserStorageService',
'CHECK_USER_ELIGIBILITY_FOR_CAMPAIGN' => '({file}): campaign:{campaignKey} having traffic allocation:{trafficAllocation} assigned value:{trafficAllocation} to userId:{userId}',
'USER_HASH_BUCKET_VALUE' => '({file}): userId:{userId} having hash:{hashValue} got bucketValue:{bucketValue}',
'VARIATION_HASH_BUCKET_VALUE' => '({file}): userId:{userId} for campaign:{campaignTestKey} having percent traffic:{percentTraffic} got hash-value:{hashValue} and bucket value:{bucketValue}',
'GOT_VARIATION_FOR_USER' => '({file}): userId:{userId} for campaign:{campaignTestKey} got variationName:{variationName} inside method:{method}',
'USER_NOT_PART_OF_CAMPAIGN' => '({file}): userId:{userId} for campaign:{campaignTestKey} did not become part of campaign, method:{method}',
'VARIATION_HASH_BUCKET_VALUE' => '({file}): userId:{userId} for campaign:{campaignKey} having percent traffic:{percentTraffic} got hash-value:{hashValue} and bucket value:{bucketValue}',
'GOT_VARIATION_FOR_USER' => '({file}): userId:{userId} for campaign:{campaignKey} got variationName:{variationName} inside method:{method}',
'USER_NOT_PART_OF_CAMPAIGN' => '({file}): userId:{userId} for campaign:{campaignKey} did not become part of campaign, method:{method}',
'UUID_FOR_USER' => '({file}): Uuid generated for userId:{userid} and accountId:{accountId} is {desiredUuid}',
'FEATURE_FLAG_NOT_LINKED' => '({file}): Feature:{featureKey} is not linked to any running campaigns',
'USER_HASH_BUCKET_VALUE' => '({file}): User ID:{userId} having hash:{hashValue} got bucketValue:{bucketValue}',
Expand All @@ -91,19 +91,19 @@ class Constants
*/

const INFO_MESSAGES = [
'VARIATION_RANGE_ALLOCATION' => '({file}): Campaign:{campaignTestKey} having variations:{variationName} with weight:{variationWeight} got range as: ( {start} - {end} ))',
'VARIATION_ALLOCATED' => '({file}): UserId:{userId} of Campaign:{campaignTestKey} got variation: {variationName}',
'VARIATION_RANGE_ALLOCATION' => '({file}): Campaign:{campaignKey} having variations:{variationName} with weight:{variationWeight} got range as: ( {start} - {end} ))',
'VARIATION_ALLOCATED' => '({file}): UserId:{userId} of Campaign:{campaignKey} got variation: {variationName}',
'GETTING_UP_USER_STORAGE_SERVICE' => '({file}): Getting data into UserStorageService for userId:{userId} successful',
'SETTING_DATA_USER_STORAGE_SERVICE' => '({file}): Setting data into UserStorageService for userId:{userId} successful',
'GOT_STORED_VARIATION' => '({file}): Got stored variation:{variationName} of campaign:{campaignTestKey} for userId:{userId} from UserStorageService',
'NO_VARIATION_ALLOCATED' => '({file}): UserId:{userId} of Campaign:{campaignTestKey} did not get any variation',
'GOT_STORED_VARIATION' => '({file}): Got stored variation:{variationName} of campaign:{campaignKey} for userId:{userId} from UserStorageService',
'NO_VARIATION_ALLOCATED' => '({file}): UserId:{userId} of Campaign:{campaignKey} did not get any variation',
'USER_ELIGIBILITY_FOR_CAMPAIGN' => '({file}): Is userId:{userId} part of campaign? {isUserPart}',
'AUDIENCE_CONDITION_NOT_MET' => '({file}): userId:{userId} does not become part of campaign because of not meeting audience conditions',
'GOT_VARIATION_FOR_USER' => '({file}): userId:{userId} for campaign:{campaignTestKey} got variationName:{variationName}',
'USER_GOT_NO_VARIATION' => '({file}): userId:{userId} for campaign:{campaignTestKey} did not allot any variation',
'GOT_VARIATION_FOR_USER' => '({file}): userId:{userId} for campaign:{campaignKey} got variationName:{variationName}',
'USER_GOT_NO_VARIATION' => '({file}): userId:{userId} for campaign:{campaignKey} did not allot any variation',
'IMPRESSION_SUCCESS' => '({file}): Event sent to VWO - {endPoint} having main keys: accountId:{accountId}, userId:{userId}, campaignId:{campaignId} and variationId:{variationId}',
'IMPRESSION_SUCCESS_PUSH' => '({file}): Event sent to VWO - {endPoint} having main keys: accountId:{accountId}, userId:{userId} and tags:{tags}',
'INVALID_VARIATION_KEY' => '({file}): Variation was not assigned to userId:{userId} for campaign:{campaignTestKey}',
'INVALID_VARIATION_KEY' => '({file}): Variation was not assigned to userId:{userId} for campaign:{campaignKey}',
'API_CALLED' => '({file}): API: {api} called for userid : {userId}',
'IMPRESSION_FOR_TRACK_USER' => '({file}): impression built for track-user - {properties}',
'IMPRESSION_FOR_TRACK_GOAL' => '({file}): impression built for track-goal - {properties}',
Expand Down Expand Up @@ -133,23 +133,23 @@ class Constants
const ERROR_MESSAGE = [
'INVALID_CONFIGURATION' => '({file}): SDK configuration or account settings or both is/are not valid.',
'SETTINGS_FILE_CORRUPTED' => '({file}): Settings file is corrupted. Please contact VWO Support for help.',
'ACTIVATE_API_MISSING_PARAMS' => '({file}): "activate" API got bad parameters. It expects campaignTestKey(String) as first and userId(String/Number) as second argument',
'ACTIVATE_API_MISSING_PARAMS' => '({file}): "activate" API got bad parameters. It expects campaignKey(String) as first and userId(String/Number) as second argument',
'ACTIVATE_API_CONFIG_CORRUPTED' => '({file}): "activate" API has corrupted configuration',
'GET_VARIATION_API_MISSING_PARAMS' => '({file}): "getVariation" API got bad parameters. It expects campaignTestKey(String) as first and userId(String/Number) as second argument',
'GET_VARIATION_API_MISSING_PARAMS' => '({file}): "getVariation" API got bad parameters. It expects campaignKey(String) as first and userId(String/Number) as second argument',
'GET_VARIATION_API_CONFIG_CORRUPTED' => '({file}): "getVariation" API has corrupted configuration',
'TRACK_API_MISSING_PARAMS' => '({file}): "track" API got bad parameters. It expects campaignTestKey(String) as first, userId(String/Number) as second and goalIdentifier (string) as third argument. options is revenueValue(Float/Number/String) and is required for revenue goal only.',
'TRACK_API_MISSING_PARAMS' => '({file}): "track" API got bad parameters. It expects campaignKey(String) as first, userId(String/Number) as second and goalIdentifier (string) as third argument. options is revenueValue(Float/Number/String) and is required for revenue goal only.',
'TRACK_API_CONFIG_CORRUPTED' => '({file}): "track" API has corrupted configuration',
'TRACK_API_GOAL_NOT_FOUND' => '({file}): Goal not found for campaign:{campaignTestKey} and userId:{userId}',
'TRACK_API_VARIATION_NOT_FOUND' => '({file}): Variation not found for campaign:{campaignTestKey} and userId:{userId}',
'CAMPAIGN_NOT_RUNNING' => '({file}): Campaign:{campaignTestKey} is not RUNNING. Please verify from VWO App',
'TRACK_API_GOAL_NOT_FOUND' => '({file}): Goal not found for campaign:{campaignKey} and userId:{userId}',
'TRACK_API_VARIATION_NOT_FOUND' => '({file}): Variation not found for campaign:{campaignKey} and userId:{userId}',
'CAMPAIGN_NOT_RUNNING' => '({file}): Campaign:{campaignKey} is not RUNNING. Please verify from VWO App',
'GET_USER_STORAGE_SERVICE_FAILED' => '({file}): Getting data from UserStorageService failed for userId:{userId}',
'SET_USER_STORAGE_SERVICE_FAILED' => '({file}): Setting data into UserStorageService failed for userId:{userId}',
'INVALID_CAMPAIGN' => '({file}): Invalid campaign passed to {method} of this file',
'IMPRESSION_FAILED' => '({file}): Event could not be sent to VWO - {endPoint}',
'USERID_KEY_CORRUPTED' => '({file}): userId parameter value - {userId} is corrupted',
'FEATURE_KEY_CORRUPTED' => '({file}): featureKey parameter value - {featureKey} is corrupted',
'CUSTOM_LOGGER_MISCONFIGURED' => '({file}): Custom logger is provided but seems to have misconfigured. Please check the API Docs. Using default logger.',
'MISSING_GOAL_REVENUE' => 'Revenue value should be passed for revenue goal {goalIdentifier} for campaign {campaignTestKey} and userId {userId}',
'MISSING_GOAL_REVENUE' => 'Revenue value should be passed for revenue goal {goalIdentifier} for campaign {campaignKey} and userId {userId}',
'TAG_KEY_LENGTH_ERROR' => '({file}): Length of tagKey:{tagKey} for userID:{userId} can not be greater than 255',
'TAG_VALUE_LENGTH_ERROR' => '({file}): Length of tagValue:{tagValue} for userID:{userId} can not be greater than 255',
'INVALID_USER_ID' => '({file}): Invalid userId:{userId} passed to {method} of this file',
Expand Down
6 changes: 3 additions & 3 deletions src/Core/Bucketer.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static function getBucket($userId, $campaign)
// if bucketing to be done
$bucketVal = self::getBucketVal($userId, self::$MAX_CAMPAIGN_TRAFFIC);
if (!self::isUserPartofCampaign($bucketVal, $campaign['percentTraffic'])) {
\vwo\VWO::addLog(Logger::DEBUG, Constants::DEBUG_MESSAGES['USER_NOT_PART_OF_CAMPAIGN'], ['{userId}' => $userId, '{method}' => 'getBucket', '{campaignTestKey}' => $campaign['key']], self::$CLASSNAME);
\vwo\VWO::addLog(Logger::DEBUG, Constants::DEBUG_MESSAGES['USER_NOT_PART_OF_CAMPAIGN'], ['{userId}' => $userId, '{method}' => 'getBucket', '{campaignKey}' => $campaign['key']], self::$CLASSNAME);
return null;
}
$multiplier = self::getMultiplier($campaign['percentTraffic']);
Expand All @@ -89,10 +89,10 @@ public static function getBucket($userId, $campaign)
$variation = self::variationUsingRange($rangeForVariations, $campaign['variations']);
\vwo\VWO::addLog(Logger::DEBUG, Constants::DEBUG_MESSAGES['VARIATION_HASH_BUCKET_VALUE'], ['{userId}' => $userId,'{bucketValue}' => $rangeForVariations, '{percentTraffic}' => $campaign['percentTraffic'], '{campaignKey}' => $campaign['key']], self::$CLASSNAME);
if ($variation !== null) {
\vwo\VWO::addLog(Logger::INFO, Constants::INFO_MESSAGES['GOT_VARIATION_FOR_USER'], ['{variationName}' => $variation['name'], '{userId}' => $userId, '{method}' => 'getBucket', '{campaignTestKey}' => $campaign['key']], self::$CLASSNAME);
\vwo\VWO::addLog(Logger::INFO, Constants::INFO_MESSAGES['GOT_VARIATION_FOR_USER'], ['{variationName}' => $variation['name'], '{userId}' => $userId, '{method}' => 'getBucket', '{campaignKey}' => $campaign['key']], self::$CLASSNAME);
return $variation;
}
\vwo\VWO::addLog(Logger::INFO, Constants::INFO_MESSAGES['NO_VARIATION_ALLOCATED'], ['{userId}' => $userId, '{campaignTestKey}' => $campaign['key']], self::$CLASSNAME);
\vwo\VWO::addLog(Logger::INFO, Constants::INFO_MESSAGES['NO_VARIATION_ALLOCATED'], ['{userId}' => $userId, '{campaignKey}' => $campaign['key']], self::$CLASSNAME);
return null;
}

Expand Down
Loading

0 comments on commit b64d9c5

Please sign in to comment.