Search in sources :

Example 51 with LogEvent

use of com.optimizely.ab.event.LogEvent 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);
}
Also used : Visitor(com.optimizely.ab.event.internal.payload.Visitor) EventType(com.optimizely.ab.config.EventType) LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) ArrayList(java.util.ArrayList) Decision(com.optimizely.ab.event.internal.payload.Decision) Snapshot(com.optimizely.ab.event.internal.payload.Snapshot) Event(com.optimizely.ab.event.internal.payload.Event) LogEvent(com.optimizely.ab.event.LogEvent) Variation(com.optimizely.ab.config.Variation) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) Map(java.util.Map)

Example 52 with LogEvent

use of com.optimizely.ab.event.LogEvent 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));
}
Also used : EventBuilder(com.optimizely.ab.event.internal.EventBuilder) HashMap(java.util.HashMap) LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) Matchers.anyString(org.mockito.Matchers.anyString) Variation(com.optimizely.ab.config.Variation) Test(org.junit.Test)

Example 53 with LogEvent

use of com.optimizely.ab.event.LogEvent in project java-sdk by optimizely.

the class EventFactoryTest method createConversionEventAndroidClientEngineClientVersion.

/**
 * Verify that supplying {@link ClientEngineInfo} with a custom client engine and client version results in conversion
 * events being sent with the overriden values.
 */
@Test
public void createConversionEventAndroidClientEngineClientVersion() throws Exception {
    ClientEngineInfo.setClientEngine(EventBatch.ClientEngine.ANDROID_SDK);
    Attribute attribute = validProjectConfig.getAttributes().get(0);
    EventType eventType = validProjectConfig.getEventTypes().get(0);
    Bucketer mockBucketAlgorithm = mock(Bucketer.class);
    for (Experiment experiment : validProjectConfig.getExperiments()) {
        when(mockBucketAlgorithm.bucket(experiment, userId, validProjectConfig)).thenReturn(DecisionResponse.responseNoReasons(experiment.getVariations().get(0)));
    }
    Map<String, String> attributeMap = Collections.singletonMap(attribute.getKey(), "value");
    LogEvent conversionEvent = createConversionEvent(validProjectConfig, userId, eventType.getId(), eventType.getKey(), attributeMap, Collections.emptyMap());
    EventBatch conversion = gson.fromJson(conversionEvent.getBody(), EventBatch.class);
    assertThat(conversion.getClientName(), is(EventBatch.ClientEngine.ANDROID_SDK.getClientEngineValue()));
// assertThat(conversion.getClientVersion(), is("0.0.0"));
}
Also used : ControlAttribute(com.optimizely.ab.internal.ControlAttribute) LogEvent(com.optimizely.ab.event.LogEvent) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) Bucketer(com.optimizely.ab.bucketing.Bucketer) Test(org.junit.Test)

Example 54 with LogEvent

use of com.optimizely.ab.event.LogEvent in project java-sdk by optimizely.

the class EventFactoryTest method createConversionEventIgnoresInvalidAcceptValidValOfValidAttr.

/**
 * Verify that passing through an list of invalid value attribute causes that attribute to be ignored, rather than
 * causing an exception to be thrown and passing only the valid attributes.
 */
@Test
public void createConversionEventIgnoresInvalidAcceptValidValOfValidAttr() {
    assumeTrue(datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()));
    EventType eventType = validProjectConfig.getEventTypes().get(0);
    Attribute validFloatAttribute = validProjectConfig.getAttributes().get(0);
    Attribute invalidFloatAttribute = validProjectConfig.getAttributes().get(1);
    Attribute doubleAttribute = validProjectConfig.getAttributes().get(5);
    Attribute integerAttribute = validProjectConfig.getAttributes().get(4);
    Attribute boolAttribute = validProjectConfig.getAttributes().get(3);
    Attribute emptyAttribute = validProjectConfig.getAttributes().get(6);
    float validFloatValue = 2.1f;
    float invalidFloatValue = (float) (Math.pow(2, 53) + 2000000000);
    double invalidDoubleAttribute = Math.pow(2, 53) + 2;
    long validLongAttribute = 12;
    boolean validBoolAttribute = true;
    Map<String, Object> eventTagMap = new HashMap<>();
    eventTagMap.put("boolean_param", false);
    eventTagMap.put("string_param", "123");
    HashMap<String, Object> attributes = new HashMap<>();
    attributes.put(validFloatAttribute.getKey(), validFloatValue);
    attributes.put(invalidFloatAttribute.getKey(), invalidFloatValue);
    attributes.put(doubleAttribute.getKey(), invalidDoubleAttribute);
    attributes.put(integerAttribute.getKey(), validLongAttribute);
    attributes.put(boolAttribute.getKey(), validBoolAttribute);
    attributes.put(emptyAttribute.getKey(), validBoolAttribute);
    LogEvent conversionEvent = createConversionEvent(validProjectConfig, userId, eventType.getId(), eventType.getKey(), attributes, eventTagMap);
    EventBatch conversion = gson.fromJson(conversionEvent.getBody(), EventBatch.class);
    // Check valid attributes are getting passed.
    assertEquals(conversion.getVisitors().get(0).getAttributes().get(0).getKey(), boolAttribute.getKey());
    assertEquals(conversion.getVisitors().get(0).getAttributes().get(0).getValue(), validBoolAttribute);
    assertEquals(conversion.getVisitors().get(0).getAttributes().get(1).getKey(), validFloatAttribute.getKey());
    // In the condition below we are checking Value of float with double value because impression gets visitors from JSON so that converts it into double
    assertEquals(conversion.getVisitors().get(0).getAttributes().get(1).getValue(), 2.1);
    assertEquals(conversion.getVisitors().get(0).getAttributes().get(2).getKey(), integerAttribute.getKey());
    assertEquals((long) ((double) conversion.getVisitors().get(0).getAttributes().get(2).getValue()), validLongAttribute);
    // verify that no Feature is created for attribute.getKey() -> invalidAttribute
    for (com.optimizely.ab.event.internal.payload.Attribute feature : conversion.getVisitors().get(0).getAttributes()) {
        assertNotSame(feature.getKey(), invalidFloatAttribute.getKey());
        assertNotSame(feature.getValue(), invalidFloatValue);
        assertNotSame(feature.getKey(), doubleAttribute.getKey());
        assertNotSame(feature.getValue(), invalidDoubleAttribute);
        assertNotSame(feature.getKey(), emptyAttribute.getKey());
        assertNotSame(feature.getValue(), doubleAttribute);
    }
}
Also used : ControlAttribute(com.optimizely.ab.internal.ControlAttribute) LogEvent(com.optimizely.ab.event.LogEvent) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) Test(org.junit.Test)

Example 55 with LogEvent

use of com.optimizely.ab.event.LogEvent in project java-sdk by optimizely.

the class EventFactoryTest method createImpressionEvent.

/**
 * Verify {@link com.optimizely.ab.event.internal.payload.EventBatch} event creation
 */
@Test
public void createImpressionEvent() throws Exception {
    // use the "valid" project config and its associated experiment, variation, and attributes
    Experiment activatedExperiment = validProjectConfig.getExperiments().get(0);
    Variation bucketedVariation = activatedExperiment.getVariations().get(0);
    Attribute attribute = validProjectConfig.getAttributes().get(0);
    String userId = "userId";
    Map<String, String> attributeMap = Collections.singletonMap(attribute.getKey(), "value");
    DecisionMetadata decisionMetadata = new DecisionMetadata.Builder().setFlagKey(activatedExperiment.getKey()).setRuleType("experiment").setVariationKey(bucketedVariation.getKey()).build();
    Decision expectedDecision = new Decision.Builder().setCampaignId(activatedExperiment.getLayerId()).setExperimentId(activatedExperiment.getId()).setVariationId(bucketedVariation.getId()).setMetadata(decisionMetadata).setIsCampaignHoldback(false).build();
    com.optimizely.ab.event.internal.payload.Attribute feature = new com.optimizely.ab.event.internal.payload.Attribute.Builder().setEntityId(attribute.getId()).setKey(attribute.getKey()).setType(com.optimizely.ab.event.internal.payload.Attribute.CUSTOM_ATTRIBUTE_TYPE).setValue("value").build();
    com.optimizely.ab.event.internal.payload.Attribute botFilteringFeature = getBotFilteringAttribute();
    List<com.optimizely.ab.event.internal.payload.Attribute> expectedUserFeatures;
    if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()))
        expectedUserFeatures = Arrays.asList(feature, botFilteringFeature);
    else
        expectedUserFeatures = Arrays.asList(feature);
    LogEvent impressionEvent = createImpressionEvent(validProjectConfig, activatedExperiment, bucketedVariation, userId, attributeMap);
    // verify that request endpoint is correct
    assertThat(impressionEvent.getEndpointUrl(), is(EventFactory.EVENT_ENDPOINT));
    EventBatch eventBatch = gson.fromJson(impressionEvent.getBody(), EventBatch.class);
    // verify payload information
    assertThat(eventBatch.getVisitors().get(0).getVisitorId(), is(userId));
    assertThat((double) eventBatch.getVisitors().get(0).getSnapshots().get(0).getEvents().get(0).getTimestamp(), closeTo((double) System.currentTimeMillis(), 1000.0));
    assertFalse(eventBatch.getVisitors().get(0).getSnapshots().get(0).getDecisions().get(0).getIsCampaignHoldback());
    assertThat(eventBatch.getAnonymizeIp(), is(validProjectConfig.getAnonymizeIP()));
    assertTrue(eventBatch.getEnrichDecisions());
    assertThat(eventBatch.getProjectId(), is(validProjectConfig.getProjectId()));
    assertThat(eventBatch.getVisitors().get(0).getSnapshots().get(0).getDecisions().get(0), is(expectedDecision));
    assertThat(eventBatch.getVisitors().get(0).getSnapshots().get(0).getDecisions().get(0).getCampaignId(), is(activatedExperiment.getLayerId()));
    assertThat(eventBatch.getAccountId(), is(validProjectConfig.getAccountId()));
    assertThat(eventBatch.getVisitors().get(0).getAttributes(), is(expectedUserFeatures));
    assertThat(eventBatch.getClientName(), is(EventBatch.ClientEngine.JAVA_SDK.getClientEngineValue()));
    assertThat(eventBatch.getClientVersion(), is(BuildVersionInfo.VERSION));
    assertNull(eventBatch.getVisitors().get(0).getSessionId());
}
Also used : ControlAttribute(com.optimizely.ab.internal.ControlAttribute) LogEvent(com.optimizely.ab.event.LogEvent) GsonBuilder(com.google.gson.GsonBuilder) Decision(com.optimizely.ab.event.internal.payload.Decision) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) DecisionMetadata(com.optimizely.ab.event.internal.payload.DecisionMetadata) Test(org.junit.Test)

Aggregations

LogEvent (com.optimizely.ab.event.LogEvent)66 Test (org.junit.Test)58 Experiment (com.optimizely.ab.config.Experiment)37 Variation (com.optimizely.ab.config.Variation)34 EventBatch (com.optimizely.ab.event.internal.payload.EventBatch)32 HashMap (java.util.HashMap)25 EventType (com.optimizely.ab.config.EventType)23 EventBuilder (com.optimizely.ab.event.internal.EventBuilder)21 Matchers.anyString (org.mockito.Matchers.anyString)21 ControlAttribute (com.optimizely.ab.internal.ControlAttribute)17 Map (java.util.Map)14 ImmutableMap (com.google.common.collect.ImmutableMap)11 Bucketer (com.optimizely.ab.bucketing.Bucketer)11 EventBuilderTest.createExperimentVariationMap (com.optimizely.ab.event.internal.EventBuilderTest.createExperimentVariationMap)11 ProjectConfig (com.optimizely.ab.config.ProjectConfig)10 Attribute (com.optimizely.ab.config.Attribute)9 Decision (com.optimizely.ab.event.internal.payload.Decision)9 DecisionService (com.optimizely.ab.bucketing.DecisionService)8 UserProfileService (com.optimizely.ab.bucketing.UserProfileService)6 NoOpErrorHandler (com.optimizely.ab.error.NoOpErrorHandler)6