use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class BucketerTest method bucketUserNotInExperiment.
/**
* Verify that {@link Bucketer#bucket(Experiment, String)} doesn't return a variation when a user isn't bucketed
* into the group experiment.
*/
@Test
public void bucketUserNotInExperiment() throws Exception {
final AtomicInteger bucketValue = new AtomicInteger();
Bucketer algorithm = mockBucketAlgorithm(bucketValue);
bucketValue.set(3000);
ProjectConfig projectConfig = validProjectConfigV2();
List<Experiment> groupExperiments = projectConfig.getGroups().get(0).getExperiments();
Experiment groupExperiment = groupExperiments.get(1);
// the user should be bucketed to a different experiment than the one provided, resulting in no variation being
// returned.
logbackVerifier.expectMessage(Level.DEBUG, "Assigned bucket 3000 to user with bucketingId \"blah\" during experiment bucketing.");
logbackVerifier.expectMessage(Level.INFO, "User with bucketingId \"blah\" is not in experiment \"group_etag1\" of group 42");
assertNull(algorithm.bucket(groupExperiment, "blah"));
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class Optimizely method track.
public void track(@Nonnull String eventName, @Nonnull String userId, @Nonnull Map<String, String> attributes, @Nonnull Map<String, ?> eventTags) throws UnknownEventTypeException {
ProjectConfig currentConfig = getProjectConfig();
EventType eventType = getEventTypeOrThrow(currentConfig, eventName);
if (eventType == null) {
// if no matching event type could be found, do not dispatch an event
logger.info("Not tracking event \"{}\" for user \"{}\".", eventName, userId);
return;
}
// determine whether all the given attributes are present in the project config. If not, filter out the unknown
// attributes.
Map<String, String> filteredAttributes = filterAttributes(currentConfig, attributes);
if (eventTags == null) {
logger.warn("Event tags is null when non-null was expected. Defaulting to an empty event tags map.");
eventTags = Collections.<String, String>emptyMap();
}
List<Experiment> experimentsForEvent = projectConfig.getExperimentsForEventKey(eventName);
Map<Experiment, Variation> experimentVariationMap = new HashMap<Experiment, Variation>(experimentsForEvent.size());
for (Experiment experiment : experimentsForEvent) {
if (experiment.isRunning()) {
Variation variation = decisionService.getVariation(experiment, userId, filteredAttributes);
if (variation != null) {
experimentVariationMap.put(experiment, variation);
}
} else {
logger.info("Not tracking event \"{}\" for experiment \"{}\" because experiment has status \"Launched\".", eventType.getKey(), experiment.getKey());
}
}
// create the conversion event request parameters, then dispatch
LogEvent conversionEvent = eventBuilder.createConversionEvent(projectConfig, experimentVariationMap, userId, eventType.getId(), eventType.getKey(), filteredAttributes, eventTags);
if (conversionEvent == null) {
logger.info("There are no valid experiments for event \"{}\" to track.", eventName);
logger.info("Not tracking event \"{}\" for user \"{}\".", eventName, userId);
return;
}
logger.info("Tracking event \"{}\" for user \"{}\".", eventName, userId);
logger.debug("Dispatching conversion event to URL {} with params {} and payload \"{}\".", conversionEvent.getEndpointUrl(), conversionEvent.getRequestParams(), conversionEvent.getBody());
try {
eventHandler.dispatchEvent(conversionEvent);
} catch (Exception e) {
logger.error("Unexpected exception in event dispatcher", e);
}
notificationCenter.sendNotifications(NotificationCenter.NotificationType.Track, eventName, userId, filteredAttributes, eventTags, conversionEvent);
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class Optimizely method activate.
@Nullable
public Variation activate(@Nonnull String experimentKey, @Nonnull String userId, @Nonnull Map<String, String> attributes) throws UnknownExperimentException {
if (!validateUserId(userId)) {
logger.info("Not activating user for experiment \"{}\".", experimentKey);
return null;
}
ProjectConfig currentConfig = getProjectConfig();
Experiment experiment = getExperimentOrThrow(currentConfig, experimentKey);
if (experiment == null) {
// if we're unable to retrieve the associated experiment, return null
logger.info("Not activating user \"{}\" for experiment \"{}\".", userId, experimentKey);
return null;
}
return activate(currentConfig, experiment, userId, attributes);
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class GroupJacksonDeserializer method deserialize.
@Override
public Group deserialize(JsonParser parser, DeserializationContext context) throws IOException {
ObjectMapper mapper = new ObjectMapper();
JsonNode node = parser.getCodec().readTree(parser);
String id = node.get("id").textValue();
String policy = node.get("policy").textValue();
List<TrafficAllocation> trafficAllocations = mapper.readValue(node.get("trafficAllocation").toString(), new TypeReference<List<TrafficAllocation>>() {
});
JsonNode groupExperimentsJson = node.get("experiments");
List<Experiment> groupExperiments = new ArrayList<Experiment>();
if (groupExperimentsJson.isArray()) {
for (JsonNode groupExperimentJson : groupExperimentsJson) {
groupExperiments.add(parseExperiment(groupExperimentJson, id));
}
}
return new Group(id, policy, groupExperiments, trafficAllocations);
}
use of com.optimizely.ab.config.Experiment in project java-sdk by optimizely.
the class GroupJacksonDeserializer method parseExperiment.
private Experiment parseExperiment(JsonNode experimentJson, String groupId) throws IOException {
ObjectMapper mapper = new ObjectMapper();
String id = experimentJson.get("id").textValue();
String key = experimentJson.get("key").textValue();
String status = experimentJson.get("status").textValue();
JsonNode layerIdJson = experimentJson.get("layerId");
String layerId = layerIdJson == null ? null : layerIdJson.textValue();
List<String> audienceIds = mapper.readValue(experimentJson.get("audienceIds").toString(), new TypeReference<List<String>>() {
});
List<Variation> variations = mapper.readValue(experimentJson.get("variations").toString(), new TypeReference<List<Variation>>() {
});
List<TrafficAllocation> trafficAllocations = mapper.readValue(experimentJson.get("trafficAllocation").toString(), new TypeReference<List<TrafficAllocation>>() {
});
Map<String, String> userIdToVariationKeyMap = mapper.readValue(experimentJson.get("forcedVariations").toString(), new TypeReference<Map<String, String>>() {
});
return new Experiment(id, key, status, layerId, audienceIds, variations, userIdToVariationKeyMap, trafficAllocations, groupId);
}
Aggregations