Skip to content

Commit 1e4cae2

Browse files
authored
fix: set client-engine version in events (#410)
1 parent 0c126af commit 1e4cae2

File tree

4 files changed

+87
-11
lines changed

4 files changed

+87
-11
lines changed

android-sdk/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ android {
2929
versionName version_name
3030
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
3131
buildConfigField "String", "CLIENT_VERSION", "\"$version_name\""
32+
3233
// these rules will be merged to app's proguard rules
3334
consumerProguardFiles '../proguard-rules.txt'
3435
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/****************************************************************************
2+
* Copyright 2022, Optimizely, Inc. and contributors *
3+
* *
4+
* Licensed under the Apache License, Version 2.0 (the "License"); *
5+
* you may not use this file except in compliance with the License. *
6+
* You may obtain a copy of the License at *
7+
* *
8+
* http://www.apache.org/licenses/LICENSE-2.0 *
9+
* *
10+
* Unless required by applicable law or agreed to in writing, software *
11+
* distributed under the License is distributed on an "AS IS" BASIS, *
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13+
* See the License for the specific language governing permissions and *
14+
* limitations under the License. *
15+
***************************************************************************/
16+
17+
package com.optimizely.ab.android.sdk;
18+
19+
import static junit.framework.Assert.assertEquals;
20+
import static org.mockito.Mockito.mock;
21+
import static org.mockito.Mockito.timeout;
22+
import static org.mockito.Mockito.verify;
23+
import android.content.Context;
24+
import androidx.test.ext.junit.runners.AndroidJUnit4;
25+
import androidx.test.platform.app.InstrumentationRegistry;
26+
import com.optimizely.ab.event.EventHandler;
27+
import com.optimizely.ab.event.LogEvent;
28+
import org.junit.Test;
29+
import org.junit.runner.RunWith;
30+
import org.mockito.ArgumentCaptor;
31+
import java.util.concurrent.TimeUnit;
32+
33+
@RunWith(AndroidJUnit4.class)
34+
public class OptimizelyManagerEventHandlerTest {
35+
36+
private String minDatafileWithEvent = "{\n" +
37+
"experiments: [ ],\n" +
38+
"version: \"2\",\n" +
39+
"audiences: [ ],\n" +
40+
"groups: [ ],\n" +
41+
"attributes: [ ],\n" +
42+
"projectId: \"123\",\n" +
43+
"accountId: \"6365361536\",\n" +
44+
"events: [{\"experimentIds\": [\"8509139139\"], \"id\": \"8505434668\", \"key\": \"test_event\"}],\n" +
45+
"revision: \"1\"\n" +
46+
"}";
47+
48+
@Test
49+
public void eventClientNameAndVersion() throws Exception {
50+
EventHandler mockEventHandler = mock(EventHandler.class);
51+
52+
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
53+
OptimizelyManager optimizelyManager = OptimizelyManager.builder()
54+
.withSDKKey("any-sdk-key")
55+
.withEventDispatchInterval(0, TimeUnit.SECONDS)
56+
.withEventHandler(mockEventHandler)
57+
.build(context);
58+
59+
OptimizelyClient optimizelyClient = optimizelyManager.initialize(context, minDatafileWithEvent);
60+
optimizelyClient.track("test_event", "tester");
61+
62+
ArgumentCaptor<LogEvent> argument = ArgumentCaptor.forClass(LogEvent.class);
63+
verify(mockEventHandler, timeout(5000)).dispatchEvent(argument.capture());
64+
assertEquals(argument.getValue().getEventBatch().getClientName(), "android-sdk");
65+
assertEquals(argument.getValue().getEventBatch().getClientVersion(), BuildConfig.CLIENT_VERSION);
66+
}
67+
68+
}

android-sdk/src/main/java/com/optimizely/ab/android/sdk/OptimizelyManager.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2017-2021, Optimizely, Inc. and contributors *
2+
* Copyright 2017-2022, Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -86,6 +86,7 @@ public class OptimizelyManager {
8686
@Nullable private OptimizelyStartListener optimizelyStartListener;
8787

8888
@NonNull private final List<OptimizelyDecideOption> defaultDecideOptions;
89+
private String sdkVersion = null;
8990

9091
OptimizelyManager(@Nullable String projectId,
9192
@Nullable String sdkKey,
@@ -122,6 +123,13 @@ public class OptimizelyManager {
122123
this.userProfileService = userProfileService;
123124
this.notificationCenter = notificationCenter;
124125
this.defaultDecideOptions = defaultDecideOptions;
126+
127+
try {
128+
sdkVersion = BuildConfig.CLIENT_VERSION;
129+
logger.info("SDK Version: {}", sdkVersion);
130+
} catch (Exception e) {
131+
logger.warn("Error getting BuildConfig version");
132+
}
125133
}
126134

127135
@VisibleForTesting
@@ -580,8 +588,8 @@ private OptimizelyClient buildOptimizely(@NonNull Context context, @NonNull Stri
580588
builder.withDatafile(datafile);
581589
}
582590

583-
builder.withClientEngine(clientEngine)
584-
.withClientVersion(BuildConfig.CLIENT_VERSION);
591+
// override client sdk name/version to be included in events
592+
builder.withClientInfo(clientEngine, sdkVersion);
585593

586594
if (errorHandler != null) {
587595
builder.withErrorHandler(errorHandler);

build.gradle

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@
1919
buildscript {
2020
ext.kotlin_version = '1.4.0'
2121

22-
def version_name = System.getenv('TRAVIS_TAG')
23-
if (version_name != null) {
24-
rootProject.ext.version_name = version_name
25-
} else {
26-
rootProject.ext.version_name = 'debugVersion'
22+
ext.version_name = System.getenv('TRAVIS_TAG')
23+
if (version_name == null || version_name.isEmpty()) {
24+
ext.version_name = 'debugVersion'
2725
}
28-
ext.is_release_version = !rootProject.ext.version_name.endsWith("SNAPSHOT")
26+
ext.is_release_version = !version_name.endsWith("SNAPSHOT")
2927

3028
repositories {
3129
jcenter()
@@ -63,7 +61,7 @@ ext {
6361
build_tools_version = "30.0.3"
6462
min_sdk_version = 14
6563
target_sdk_version = 29
66-
java_core_ver = "3.10.1"
64+
java_core_ver = "3.10.2"
6765
android_logger_ver = "1.3.6"
6866
jacksonversion= "2.11.2"
6967
annotations_ver = "1.0.0"
@@ -99,7 +97,8 @@ task testAllModulesTravis () {
9997
dependsOn(':android-sdk:connectedAndroidTest', ':android-sdk:test',
10098
':event-handler:connectedAndroidTest', ':event-handler:test',
10199
':datafile-handler:connectedAndroidTest', ':datafile-handler:test',
102-
':user-profile:connectedAndroidTest', ':shared:connectedAndroidTest')
100+
':user-profile:connectedAndroidTest',
101+
':shared:connectedAndroidTest')
103102
}
104103

105104
// Publish to MavenCentral

0 commit comments

Comments
 (0)