Search in sources :

Example 21 with Experiment

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"));
}
Also used : ProjectConfig(com.optimizely.ab.config.ProjectConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Experiment(com.optimizely.ab.config.Experiment) Test(org.junit.Test) ExhaustiveTest(com.optimizely.ab.categories.ExhaustiveTest)

Example 22 with Experiment

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);
}
Also used : ProjectConfig(com.optimizely.ab.config.ProjectConfig) EventType(com.optimizely.ab.config.EventType) HashMap(java.util.HashMap) LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) Variation(com.optimizely.ab.config.Variation) ConfigParseException(com.optimizely.ab.config.parser.ConfigParseException)

Example 23 with Experiment

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);
}
Also used : ProjectConfig(com.optimizely.ab.config.ProjectConfig) Experiment(com.optimizely.ab.config.Experiment) Nullable(javax.annotation.Nullable)

Example 24 with Experiment

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);
}
Also used : Group(com.optimizely.ab.config.Group) TrafficAllocation(com.optimizely.ab.config.TrafficAllocation) Experiment(com.optimizely.ab.config.Experiment) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayList(java.util.ArrayList) List(java.util.List) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 25 with Experiment

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);
}
Also used : Experiment(com.optimizely.ab.config.Experiment) JsonNode(com.fasterxml.jackson.databind.JsonNode) TrafficAllocation(com.optimizely.ab.config.TrafficAllocation) ArrayList(java.util.ArrayList) List(java.util.List) Variation(com.optimizely.ab.config.Variation) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

Experiment (com.optimizely.ab.config.Experiment)137 Test (org.junit.Test)113 Variation (com.optimizely.ab.config.Variation)91 Matchers.anyString (org.mockito.Matchers.anyString)58 LogEvent (com.optimizely.ab.event.LogEvent)47 HashMap (java.util.HashMap)42 EventType (com.optimizely.ab.config.EventType)28 EventBuilder (com.optimizely.ab.event.internal.EventBuilder)27 ProjectConfig (com.optimizely.ab.config.ProjectConfig)22 Map (java.util.Map)18 Attribute (com.optimizely.ab.config.Attribute)17 ImmutableMap (com.google.common.collect.ImmutableMap)15 EventBuilderTest.createExperimentVariationMap (com.optimizely.ab.event.internal.EventBuilderTest.createExperimentVariationMap)15 ArrayList (java.util.ArrayList)14 Rollout (com.optimizely.ab.config.Rollout)13 EventBatch (com.optimizely.ab.event.internal.payload.EventBatch)12 TrafficAllocation (com.optimizely.ab.config.TrafficAllocation)11 ExperimentUtils.isUserInExperiment (com.optimizely.ab.internal.ExperimentUtils.isUserInExperiment)10 Bucketer (com.optimizely.ab.bucketing.Bucketer)9 ExhaustiveTest (com.optimizely.ab.categories.ExhaustiveTest)9