Skip to content

Commit f37129c

Browse files
authored
useragent override and skipAdvertisingIdDetection (#17)
* Improve unit tests for eidFromURI * skipAdvertisingIdDetection and useragent override - OptableSDK constructor now accepts a useragent String argument, null by default. When not null, disables WebView based user agent detection and overrides with provided value. - OptableSDK constructor now accepts a skipAdvertisingIdDetection Boolean argument, false by default. When true, disables AdvertisingIdClient ad info detection which runs in a background coroutine. If the caller doesn't use the identify(email: String, gaid: Boolean, ppid: String) variant then ad ID detection is not needed and can be skipped. - Document in README
1 parent 67281ed commit f37129c

4 files changed

Lines changed: 42 additions & 8 deletions

File tree

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@ MainActivity.OPTABLE = OptableSDK(this, "sandbox.customer.com", "my-app", true)
108108

109109
However, since production sandboxes only listen to TLS traffic, the above is really only useful for developers of `optable-sandbox` running the sandbox locally for testing.
110110

111+
By default, the SDK detects the application user agent by sniffing `settings.userAgentString` from a `WebView`. The resulting user agent string is sent to your sandbox for analytics purposes. To disable this behavior, you can provide an optional fifth string parameter `useragent`, which allows you to set whatever user agent string you would like to send instead. For example, in Kotlin:
112+
113+
```kotlin
114+
MainActivity.OPTABLE = OptableSDK(this, "sandbox.customer.com", "my-app", false, "custom-ua")
115+
```
116+
117+
Finally, an optional sixth boolean parameter `skipAdvertisingIdDetection` can be used to skip any ID info detection from `AdvertisingIdClient` which by default runs in a background co-routine. Disabling ad ID detection means that the SDK will not be able to automatically obtain the Google Advertising ID. For example, to disable ad ID detection, in Kotlin:
118+
119+
```kotlin
120+
MainActivity.OPTABLE = OptableSDK(this, "sandbox.customer.com", "my-app", false, null, true)
121+
```
122+
111123
### Identify API
112124

113125
To associate a user device with an authenticated identifier such as an Email address, or with other known IDs such as the Google Advertising ID, or even your own vendor or app level `PPID`, you can call the `identify` API as follows:

android_sdk/src/main/java/co/optable/android_sdk/OptableSDK.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ typealias OptableTargetingResponse = HashMap<String, List<String>>
6767
* unique to the app across devices.
6868
*/
6969

70-
class OptableSDK @JvmOverloads constructor(context: Context, host: String, app: String, insecure: Boolean = false) {
70+
class OptableSDK @JvmOverloads constructor(context: Context, host: String, app: String, insecure: Boolean = false, useragent: String? = null, skipAdvertisingIdDetection: Boolean = false) {
7171
val config = Config(host, app, insecure)
72-
val client = Client(config, context)
72+
val client = Client(config, context, useragent, skipAdvertisingIdDetection)
7373

7474
/*
7575
* OptableSDK.Status lists all of the possible OptableSDK API result statuses.

android_sdk/src/main/java/co/optable/android_sdk/core/Client.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ import okhttp3.*
1818
import retrofit2.Retrofit
1919
import retrofit2.converter.gson.GsonConverterFactory
2020

21-
class Client(private val config: Config, private val context: Context) {
21+
class Client(private val config: Config, private val context: Context, private val useragent: String?, private val skipAdvertisingIdDetection: Boolean) {
2222
var gaid: String? = null
2323
var gaidLAT: Boolean? = true
2424

2525
private val edgeService: EdgeService?
26-
private val userAgent = this.userAgent()
2726
private val storage = LocalStorage(this.config, this.context)
27+
private var userAgent: String? = this.useragent
2828

2929
private class RequestInterceptor(private val userAgent: String, private val storage: LocalStorage): Interceptor {
3030
override fun intercept(chain: Interceptor.Chain): Response {
@@ -59,10 +59,16 @@ class Client(private val config: Config, private val context: Context) {
5959
}
6060

6161
init {
62-
this.determineAdvertisingInfo()
62+
if (!this.skipAdvertisingIdDetection) {
63+
this.determineAdvertisingInfo()
64+
}
65+
66+
if (this.userAgent == null) {
67+
this.userAgent = this.userAgent()
68+
}
6369

6470
val client = OkHttpClient.Builder()
65-
.addInterceptor(RequestInterceptor(userAgent, storage))
71+
.addInterceptor(RequestInterceptor(this.userAgent!!, storage))
6672
.addInterceptor(ResponseInterceptor(storage))
6773
.build()
6874

@@ -122,7 +128,7 @@ class Client(private val config: Config, private val context: Context) {
122128
}
123129

124130
fun GAID(): String? {
125-
return gaid!!
131+
return gaid
126132
}
127133

128134
private fun userAgent(): String {

android_sdk/src/test/java/co/optable/android_sdk/OptableSDKUnitTest.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,29 @@ class OptableSDKUnitTest {
7272
}
7373

7474
@Test
75-
fun eidFromURI_returnsEmptyWhenOeidAbsent() {
75+
fun eidFromURI_returnsEmptyWhenOeidAbsentFromQuerystr() {
7676
val url = "http://some.domain.com/some/path?some=query&something=else"
7777
val expected = ""
7878

7979
assertEquals(expected, OptableSDK.eidFromURI(Uri.parse(url)))
8080
}
8181

82+
@Test
83+
fun eidFromURI_returnsEmptyWhenQuerystrAbsent() {
84+
val url = "http://some.domain.com/some/path"
85+
val expected = ""
86+
87+
assertEquals(expected, OptableSDK.eidFromURI(Uri.parse(url)))
88+
}
89+
90+
@Test
91+
fun eidFromURI_returnsEmptyWhenInputEmptyString() {
92+
val url = ""
93+
val expected = ""
94+
95+
assertEquals(expected, OptableSDK.eidFromURI(Uri.parse(url)))
96+
}
97+
8298
@Test
8399
fun eidFromURI_expectsSHA256() {
84100
val url = "http://some.domain.com/some/path?some=query&something=else&oeid=AAAAAAAa665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3&foo=bar&baz"

0 commit comments

Comments
 (0)