Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set client-engine version in events #410

Merged
merged 17 commits into from
Mar 28, 2022
1 change: 1 addition & 0 deletions android-sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ android {
versionName version_name
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
buildConfigField "String", "CLIENT_VERSION", "\"$version_name\""

// these rules will be merged to app's proguard rules
consumerProguardFiles '../proguard-rules.txt'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/****************************************************************************
* Copyright 2022, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
***************************************************************************/

package com.optimizely.ab.android.sdk;

import static junit.framework.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
import android.content.Context;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
import com.optimizely.ab.event.EventHandler;
import com.optimizely.ab.event.LogEvent;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import java.util.concurrent.TimeUnit;

@RunWith(AndroidJUnit4.class)
public class OptimizelyManagerEventHandlerTest {

private String minDatafileWithEvent = "{\n" +
"experiments: [ ],\n" +
"version: \"2\",\n" +
"audiences: [ ],\n" +
"groups: [ ],\n" +
"attributes: [ ],\n" +
"projectId: \"123\",\n" +
"accountId: \"6365361536\",\n" +
"events: [{\"experimentIds\": [\"8509139139\"], \"id\": \"8505434668\", \"key\": \"test_event\"}],\n" +
"revision: \"1\"\n" +
"}";

@Test
public void eventClientNameAndVersion() throws Exception {
EventHandler mockEventHandler = mock(EventHandler.class);

Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
OptimizelyManager optimizelyManager = OptimizelyManager.builder()
.withSDKKey("any-sdk-key")
.withEventDispatchInterval(0, TimeUnit.SECONDS)
.withEventHandler(mockEventHandler)
.build(context);

OptimizelyClient optimizelyClient = optimizelyManager.initialize(context, minDatafileWithEvent);
optimizelyClient.track("test_event", "tester");

ArgumentCaptor<LogEvent> argument = ArgumentCaptor.forClass(LogEvent.class);
verify(mockEventHandler, timeout(5000)).dispatchEvent(argument.capture());
assertEquals(argument.getValue().getEventBatch().getClientName(), "android-sdk");
assertEquals(argument.getValue().getEventBatch().getClientVersion(), BuildConfig.CLIENT_VERSION);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2017-2021, Optimizely, Inc. and contributors *
* Copyright 2017-2022, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand Down Expand Up @@ -86,6 +86,7 @@ public class OptimizelyManager {
@Nullable private OptimizelyStartListener optimizelyStartListener;

@NonNull private final List<OptimizelyDecideOption> defaultDecideOptions;
private String sdkVersion = null;

OptimizelyManager(@Nullable String projectId,
@Nullable String sdkKey,
Expand Down Expand Up @@ -122,6 +123,13 @@ public class OptimizelyManager {
this.userProfileService = userProfileService;
this.notificationCenter = notificationCenter;
this.defaultDecideOptions = defaultDecideOptions;

try {
sdkVersion = BuildConfig.CLIENT_VERSION;
logger.info("SDK Version: {}", sdkVersion);
} catch (Exception e) {
logger.warn("Error getting BuildConfig version");
}
}

@VisibleForTesting
Expand Down Expand Up @@ -580,8 +588,8 @@ private OptimizelyClient buildOptimizely(@NonNull Context context, @NonNull Stri
builder.withDatafile(datafile);
}

builder.withClientEngine(clientEngine)
.withClientVersion(BuildConfig.CLIENT_VERSION);
// override client sdk name/version to be included in events
builder.withClientInfo(clientEngine, sdkVersion);

if (errorHandler != null) {
builder.withErrorHandler(errorHandler);
Expand Down
15 changes: 7 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@
buildscript {
ext.kotlin_version = '1.4.0'

def version_name = System.getenv('TRAVIS_TAG')
if (version_name != null) {
rootProject.ext.version_name = version_name
} else {
rootProject.ext.version_name = 'debugVersion'
ext.version_name = System.getenv('TRAVIS_TAG')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is it for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@msohailhussain We get the sdk release version from a git tag forwarded by travis (TRAVIS_TAG). This change is to make it work for testing in travis as well (which fails to override with test version).

if (version_name == null || version_name.isEmpty()) {
ext.version_name = 'debugVersion'
}
ext.is_release_version = !rootProject.ext.version_name.endsWith("SNAPSHOT")
ext.is_release_version = !version_name.endsWith("SNAPSHOT")

repositories {
jcenter()
Expand Down Expand Up @@ -63,7 +61,7 @@ ext {
build_tools_version = "30.0.3"
min_sdk_version = 14
target_sdk_version = 29
java_core_ver = "3.10.1"
java_core_ver = "3.10.2"
android_logger_ver = "1.3.6"
jacksonversion= "2.11.2"
annotations_ver = "1.0.0"
Expand Down Expand Up @@ -99,7 +97,8 @@ task testAllModulesTravis () {
dependsOn(':android-sdk:connectedAndroidTest', ':android-sdk:test',
':event-handler:connectedAndroidTest', ':event-handler:test',
':datafile-handler:connectedAndroidTest', ':datafile-handler:test',
':user-profile:connectedAndroidTest', ':shared:connectedAndroidTest')
':user-profile:connectedAndroidTest',
':shared:connectedAndroidTest')
}

// Publish to MavenCentral
Expand Down