use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class EventBuilder method createConversionEvent.
public LogEvent createConversionEvent(@Nonnull ProjectConfig projectConfig, @Nonnull Map<Experiment, Variation> experimentVariationMap, @Nonnull String userId, @Nonnull String eventId, @Nonnull String eventName, @Nonnull Map<String, String> attributes, @Nonnull Map<String, ?> eventTags) {
if (experimentVariationMap.isEmpty()) {
return null;
}
ArrayList<Decision> decisions = new ArrayList<Decision>();
for (Map.Entry<Experiment, Variation> entry : experimentVariationMap.entrySet()) {
Decision decision = new Decision(entry.getKey().getLayerId(), entry.getKey().getId(), entry.getValue().getId(), false);
decisions.add(decision);
}
EventType eventType = projectConfig.getEventNameMapping().get(eventName);
Event conversionEvent = new Event(System.currentTimeMillis(), UUID.randomUUID().toString(), eventType.getId(), eventType.getKey(), null, EventTagUtils.getRevenueValue(eventTags), eventTags, eventType.getKey(), EventTagUtils.getNumericValue(eventTags));
Snapshot snapshot = new Snapshot(decisions, Arrays.asList(conversionEvent));
Visitor visitor = new Visitor(userId, null, buildAttributeList(projectConfig, attributes), Arrays.asList(snapshot));
List<Visitor> visitors = Arrays.asList(visitor);
EventBatch eventBatch = new EventBatch(clientEngine.getClientEngineValue(), clientVersion, projectConfig.getAccountId(), visitors, projectConfig.getAnonymizeIP(), projectConfig.getProjectId(), projectConfig.getRevision());
String payload = this.serializer.serialize(eventBatch);
return new LogEvent(LogEvent.RequestMethod.POST, EVENT_ENDPOINT, Collections.<String, String>emptyMap(), payload);
}
use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class OptimizelyTest method getVariationWithExperimentKeyForced.
/**
* Verify that the {@link Optimizely#getVariation(String, String, Map<String, String>)} call
* uses forced variation to force the user into the second variation. The mock bucket returns
* the first variation. Then remove the forced variation and confirm that the forced variation is null.
*/
@Test
public void getVariationWithExperimentKeyForced() throws Exception {
Experiment activatedExperiment = validProjectConfig.getExperiments().get(0);
Variation forcedVariation = activatedExperiment.getVariations().get(1);
Variation bucketedVariation = activatedExperiment.getVariations().get(0);
EventBuilder mockEventBuilder = mock(EventBuilder.class);
Optimizely optimizely = Optimizely.builder(validDatafile, mockEventHandler).withBucketing(mockBucketer).withEventBuilder(mockEventBuilder).withConfig(validProjectConfig).withErrorHandler(mockErrorHandler).build();
optimizely.setForcedVariation(activatedExperiment.getKey(), testUserId, forcedVariation.getKey());
Map<String, String> testUserAttributes = new HashMap<String, String>();
if (datafileVersion >= 4) {
testUserAttributes.put(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_VALUE);
} else {
testUserAttributes.put("browser_type", "chrome");
}
testUserAttributes.put(testBucketingIdKey, testBucketingId);
Map<String, String> testParams = new HashMap<String, String>();
testParams.put("test", "params");
LogEvent logEventToDispatch = new LogEvent(RequestMethod.GET, "test_url", testParams, "");
when(mockEventBuilder.createImpressionEvent(eq(validProjectConfig), eq(activatedExperiment), eq(forcedVariation), eq(testUserId), eq(testUserAttributes))).thenReturn(logEventToDispatch);
when(mockBucketer.bucket(activatedExperiment, testBucketingId)).thenReturn(bucketedVariation);
// activate the experiment
Variation actualVariation = optimizely.getVariation(activatedExperiment.getKey(), testUserId, testUserAttributes);
assertThat(actualVariation, is(forcedVariation));
optimizely.setForcedVariation(activatedExperiment.getKey(), testUserId, null);
assertEquals(optimizely.getForcedVariation(activatedExperiment.getKey(), testUserId), null);
actualVariation = optimizely.getVariation(activatedExperiment.getKey(), testUserId, testUserAttributes);
assertThat(actualVariation, is(bucketedVariation));
}
use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class ActivateNotificationListener method notify.
/**
* Base notify called with var args. This method parses the parameters and calls the abstract method.
* @param args - variable argument list based on the type of notification.
*/
@Override
public final void notify(Object... args) {
assert (args[0] instanceof Experiment);
Experiment experiment = (Experiment) args[0];
assert (args[1] instanceof String);
String userId = (String) args[1];
assert (args[2] instanceof java.util.Map);
Map<String, String> attributes = (Map<String, String>) args[2];
assert (args[3] instanceof Variation);
Variation variation = (Variation) args[3];
assert (args[4] instanceof LogEvent);
LogEvent logEvent = (LogEvent) args[4];
onActivate(experiment, userId, attributes, variation, logEvent);
}
use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class BucketerTest method bucketToMultipleVariations.
/**
* Given an experiment with 4 variations, verify that bucket values are correctly mapped to the associated range.
*/
@Test
public void bucketToMultipleVariations() throws Exception {
List<String> audienceIds = Collections.emptyList();
// create an experiment with 4 variations using ranges: [0 -> 999, 1000 -> 4999, 5000 -> 5999, 6000 -> 9999]
List<Variation> variations = Arrays.asList(new Variation("1", "var1"), new Variation("2", "var2"), new Variation("3", "var3"), new Variation("4", "var4"));
List<TrafficAllocation> trafficAllocations = Arrays.asList(new TrafficAllocation("1", 1000), new TrafficAllocation("2", 5000), new TrafficAllocation("3", 6000), new TrafficAllocation("4", 10000));
Experiment experiment = new Experiment("1234", "exp_key", "Running", "1", audienceIds, variations, Collections.<String, String>emptyMap(), trafficAllocations, "");
final AtomicInteger bucketValue = new AtomicInteger();
Bucketer algorithm = mockBucketAlgorithm(bucketValue);
// verify bucketing to the first variation
bucketValue.set(0);
assertThat(algorithm.bucket(experiment, "user1"), is(variations.get(0)));
bucketValue.set(500);
assertThat(algorithm.bucket(experiment, "user2"), is(variations.get(0)));
bucketValue.set(999);
assertThat(algorithm.bucket(experiment, "user3"), is(variations.get(0)));
// verify the second variation
bucketValue.set(1000);
assertThat(algorithm.bucket(experiment, "user4"), is(variations.get(1)));
bucketValue.set(4000);
assertThat(algorithm.bucket(experiment, "user5"), is(variations.get(1)));
bucketValue.set(4999);
assertThat(algorithm.bucket(experiment, "user6"), is(variations.get(1)));
// ...and the rest
bucketValue.set(5100);
assertThat(algorithm.bucket(experiment, "user7"), is(variations.get(2)));
bucketValue.set(6500);
assertThat(algorithm.bucket(experiment, "user8"), is(variations.get(3)));
}
use of com.optimizely.ab.config.Variation in project java-sdk by optimizely.
the class DecisionServiceTest method getVariationForcedPrecedesAudienceEval.
/**
* Verify that {@link DecisionService#getVariation(Experiment, String, Map)}
* gives precedence to forced variation bucketing over audience evaluation.
*/
@Test
public void getVariationForcedPrecedesAudienceEval() throws Exception {
Bucketer bucketer = spy(new Bucketer(validProjectConfig));
DecisionService decisionService = spy(new DecisionService(bucketer, mockErrorHandler, validProjectConfig, null));
Experiment experiment = validProjectConfig.getExperiments().get(0);
Variation expectedVariation = experiment.getVariations().get(1);
// user excluded without audiences and whitelisting
assertNull(decisionService.getVariation(experiment, genericUserId, Collections.<String, String>emptyMap()));
logbackVerifier.expectMessage(Level.INFO, "User \"" + genericUserId + "\" does not meet conditions to be in experiment \"etag1\".");
// set the runtimeForcedVariation
validProjectConfig.setForcedVariation(experiment.getKey(), genericUserId, expectedVariation.getKey());
// no attributes provided for a experiment that has an audience
assertThat(decisionService.getVariation(experiment, genericUserId, Collections.<String, String>emptyMap()), is(expectedVariation));
verify(decisionService, never()).getStoredVariation(eq(experiment), any(UserProfile.class));
assertEquals(validProjectConfig.setForcedVariation(experiment.getKey(), genericUserId, null), true);
assertNull(validProjectConfig.getForcedVariation(experiment.getKey(), genericUserId));
}
Aggregations