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);
}
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));
}
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"));
}
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);
}
}
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());
}
Aggregations