use of com.optimizely.ab.event.EventHandler in project android-sdk by optimizely.
the class APISamplesInJava method samplesForDoc_EventBatchingAdvanced.
public static void samplesForDoc_EventBatchingAdvanced(Context context) {
// -- sample starts here
EventHandler eventHandler = DefaultEventHandler.getInstance(context);
// Here we are using the builder options to set batch size
// to 5 events and flush interval to a minute.
BatchEventProcessor batchProcessor = BatchEventProcessor.builder().withBatchSize(5).withEventHandler(eventHandler).withFlushInterval(TimeUnit.MINUTES.toMillis(1L)).build();
OptimizelyManager optimizelyManager = OptimizelyManager.builder().withSDKKey("<Your_SDK_Key>").withEventHandler(eventHandler).withDatafileDownloadInterval(15, TimeUnit.MINUTES).withEventProcessor(batchProcessor).build(context);
OptimizelyClient optimizely = optimizelyManager.initialize(context, R.raw.datafile);
// log event
// -- sample starts here
optimizely.addLogEventNotificationHandler(logEvent -> {
Log.d("Optimizely", "event dispatched: " + logEvent);
});
}
use of com.optimizely.ab.event.EventHandler in project android-sdk by optimizely.
the class OptimizelyManagerTest method setup.
@Before
public void setup() throws Exception {
logger = mock(Logger.class);
executor = Executors.newSingleThreadExecutor();
defaultDatafileHandler = mock(DefaultDatafileHandler.class);
EventHandler eventHandler = mock(DefaultEventHandler.class);
EventProcessor eventProcessor = mock(EventProcessor.class);
optimizelyManager = OptimizelyManager.builder(testProjectId).withLogger(logger).withDatafileDownloadInterval(3600L).withDatafileHandler(defaultDatafileHandler).withEventDispatchInterval(3600L).withEventHandler(eventHandler).withEventProcessor(eventProcessor).build(InstrumentationRegistry.getInstrumentation().getTargetContext());
defaultDatafile = optimizelyManager.getDatafile(InstrumentationRegistry.getInstrumentation().getTargetContext(), R.raw.datafile);
ProjectConfig config = new DatafileProjectConfig.Builder().withDatafile(defaultDatafile).build();
when(defaultDatafileHandler.getConfig()).thenReturn(config);
}
use of com.optimizely.ab.event.EventHandler in project android-sdk by optimizely.
the class OptimizelyManagerTest method initializeAsyncWithEnvironment.
@Test
public void initializeAsyncWithEnvironment() {
Logger logger = mock(Logger.class);
DatafileHandler datafileHandler = mock(DefaultDatafileHandler.class);
EventHandler eventHandler = mock(DefaultEventHandler.class);
EventProcessor eventProcessor = mock(EventProcessor.class);
final OptimizelyManager optimizelyManager = new OptimizelyManager(testProjectId, testSdkKey, null, logger, 3600L, datafileHandler, null, 3600L, eventHandler, eventProcessor, null, null, null);
/*
* Scenario#1: when datafile is not Empty
* Scenario#2: when datafile is Empty
*/
doAnswer(new Answer<Object>() {
public Object answer(InvocationOnMock invocation) {
((DatafileLoadedListener) invocation.getArguments()[2]).onDatafileLoaded(null);
return null;
}
}).when(optimizelyManager.getDatafileHandler()).downloadDatafile(any(Context.class), any(DatafileConfig.class), any(DatafileLoadedListener.class));
OptimizelyStartListener listener = new OptimizelyStartListener() {
@Override
public void onStart(OptimizelyClient optimizely) {
assertNotNull(optimizelyManager.getOptimizely());
assertNotNull(optimizelyManager.getDatafileHandler());
assertNull(optimizelyManager.getOptimizelyStartListener());
}
};
optimizelyManager.initialize(InstrumentationRegistry.getInstrumentation().getContext(), R.raw.datafile, listener);
verify(optimizelyManager.getDatafileHandler()).startBackgroundUpdates(any(Context.class), eq(new DatafileConfig(testProjectId, testSdkKey)), eq(3600L), any(DatafileLoadedListener.class));
assertEquals(optimizelyManager.isDatafileCached(InstrumentationRegistry.getInstrumentation().getTargetContext()), false);
assertEquals(optimizelyManager.getDatafileUrl(), String.format((DatafileConfig.defaultHost + DatafileConfig.environmentUrlSuffix), testSdkKey));
}
use of com.optimizely.ab.event.EventHandler in project android-sdk by optimizely.
the class OptimizelyManager method buildOptimizely.
private OptimizelyClient buildOptimizely(@NonNull Context context, @NonNull String datafile) throws ConfigParseException {
EventHandler eventHandler = getEventHandler(context);
EventBatch.ClientEngine clientEngine = OptimizelyClientEngine.getClientEngineFromContext(context);
Optimizely.Builder builder = Optimizely.builder();
builder.withEventHandler(eventHandler);
builder.withEventProcessor(eventProcessor);
if (datafileHandler instanceof DefaultDatafileHandler) {
DefaultDatafileHandler handler = (DefaultDatafileHandler) datafileHandler;
handler.setDatafile(datafile);
builder.withConfigManager(handler);
} else {
builder.withDatafile(datafile);
}
// override client sdk name/version to be included in events
builder.withClientInfo(clientEngine, sdkVersion);
if (errorHandler != null) {
builder.withErrorHandler(errorHandler);
}
builder.withUserProfileService(userProfileService);
builder.withNotificationCenter(notificationCenter);
builder.withDefaultDecideOptions(defaultDecideOptions);
Optimizely optimizely = builder.build();
return new OptimizelyClient(optimizely, LoggerFactory.getLogger(OptimizelyClient.class));
}
use of com.optimizely.ab.event.EventHandler in project android-sdk by optimizely.
the class OptimizelyManagerBuilderTest method testBuildWithEventHandler.
@Test
public void testBuildWithEventHandler() {
EventHandler eventHandler = mock(EventHandler.class);
OptimizelyManager manager = OptimizelyManager.builder().withSDKKey(testSdkKey).withDatafileDownloadInterval(901L, TimeUnit.SECONDS).withEventHandler(eventHandler).build(mockContext);
assertEquals(901L, manager.getDatafileDownloadInterval().longValue());
assertEquals(manager.getEventHandler(mockContext), eventHandler);
}
Aggregations