Multiple client ids
Stay organized with collections
Save and categorize content based on your preferences.
Right now one client ID can only support one search feature. If you want to use
multiple search features in your app, you should use multiple client IDs. In
addition, if you want to use the same search feature in different entry points
in your app, you should also use different client IDs for each of them to
distinguish the traffic.
For your each request, you can proactively set a client ID in
GetSearchSuggestionsViewOptions
, GetTrendingSearchesViewOptions
or
GetSearchContentViewOptions
, and this ID will override the default client ID
in the AndroidManifest.xml
file (only for this request).
Sample code
Java
package ...;
...
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewGeneratorCallback;
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewOptions;
import com.google.android.libraries.searchinapps.SearchInAppsService;
import com.google.android.libraries.searchinapps.SearchSuggestionsViewGenerator;
import java.util.Arrays;
import java.util.List;
...
public class MainActivity extends AppCompatActivity implements GetSearchSuggestionsViewGeneratorCallback {
private SearchInAppsService service;
@Override
public void onSuccess(SearchSuggestionsViewGenerator generator) {
ViewGroup container = findViewById(R.id.[container_id]);
container.removeAllViews();
container.addView(generator.populateView(this));
}
@Override
public void onError(String errorMessage) {
...
}
@Override
protected void onCreate(Bundle savedInstanceState) {
...
service = SearchInAppsService.create(this);
List<String> searchContext = Arrays.asList(new String[]{"Query"});
service.getSearchSuggestionsView(
new GetSearchSuggestionsViewOptions().setTextContext(searchContext)
.setClientId("client id"),this);
...
}
@Override
public void onDestroy() {
service.shutDown();
super.onDestroy();
}
}
Jetpack Compose
package ...
...
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.viewinterop.AndroidView
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewGeneratorCallback
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewOptions
import com.google.android.libraries.searchinapps.SearchInAppsService
import com.google.android.libraries.searchinapps.SearchSuggestionsViewGenerator
...
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent { SearchSuggestionsUI() }
}
@Composable
fun SearchSuggestionsUI() {
...
var service by remember {
mutableStateOf<SearchInAppsService?>(
SearchInAppsService.create(LocalContext.current))
}
var viewGenerator = mutableStateOf<SearchSuggestionsViewGenerator?>(null)
DisposableEffect(Unit) { onDispose { service?.shutDown() } }
val callback =
object : GetSearchSuggestionsViewGeneratorCallback {
override fun onSuccess(generator: SearchSuggestionsViewGenerator) {
viewGenerator.value = generator
}
override fun onError(errorMessage: String) {}
}
var searchContexts: List<String> = listOf<String>("Query")
var options: GetSearchSuggestionsViewOptions =
GetSearchSuggestionsViewOptions().setTextContext(searchContexts)
.setClientId("client id")
service?.getSearchSuggestionsView(options, callback)
ChipGroupUI(viewGenerator)
...
}
@Composable
fun ChipGroupUI(viewGenerator: MutableState<SearchSuggestionsViewGenerator?>) {
viewGenerator.value?.let { viewGenerator ->
var context = LocalContext.current
AndroidView(
factory = { context -> viewGenerator.populateView(context) },
update = { view -> viewGenerator.updateView(view, context) },
)
}
}
}
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-11-21 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-11-21 UTC."],[[["\u003cp\u003eEach search feature and app entry point using it requires a unique client ID for proper functionality and traffic differentiation.\u003c/p\u003e\n"],["\u003cp\u003eMultiple client IDs are necessary if your app utilizes more than one search feature.\u003c/p\u003e\n"],["\u003cp\u003eYou can override the default client ID set in the \u003ccode\u003eAndroidManifest.xml\u003c/code\u003e for individual requests by specifying a client ID within the request options (\u003ccode\u003eGetSearchSuggestionsViewOptions\u003c/code\u003e, \u003ccode\u003eGetTrendingSearchesViewOptions\u003c/code\u003e, or \u003ccode\u003eGetSearchContentViewOptions\u003c/code\u003e).\u003c/p\u003e\n"],["\u003cp\u003eThe provided code samples (in Java and Jetpack Compose) demonstrate how to integrate SearchInApps and set client IDs for search requests.\u003c/p\u003e\n"]]],["Each search feature or unique entry point in an app requires a distinct client ID. To use multiple search features, use multiple client IDs. For the same search feature in various entry points, use different IDs to track traffic. Client IDs can be set per request in `GetSearchSuggestionsViewOptions`, `GetTrendingSearchesViewOptions`, or `GetSearchContentViewOptions`, overriding the default ID from `AndroidManifest.xml`. Examples of implementation in Java and Jetpack Compose to set a specific client id are provided.\n"],null,["# Multiple client ids\n\nRight now one client ID can only support one search feature. If you want to use\nmultiple search features in your app, you should use multiple client IDs. In\naddition, if you want to use the same search feature in different entry points\nin your app, you should also use different client IDs for each of them to\ndistinguish the traffic.\n\nFor your each request, you can proactively set a client ID in\n`GetSearchSuggestionsViewOptions`, `GetTrendingSearchesViewOptions` or\n`GetSearchContentViewOptions`, and this ID will override the default client ID\nin the `AndroidManifest.xml` file (only for this request).\n\nSample code \n\n### Java\n\n package ...;\n\n ...\n import androidx.appcompat.app.AppCompatActivity;\n import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewGeneratorCallback;\n import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewOptions;\n import com.google.android.libraries.searchinapps.SearchInAppsService;\n import com.google.android.libraries.searchinapps.SearchSuggestionsViewGenerator;\n import java.util.Arrays;\n import java.util.List;\n ...\n\n public class MainActivity extends AppCompatActivity implements GetSearchSuggestionsViewGeneratorCallback {\n private SearchInAppsService service;\n\n @Override\n public void onSuccess(SearchSuggestionsViewGenerator generator) {\n ViewGroup container = findViewById(R.id.[container_id]);\n container.removeAllViews();\n container.addView(generator.populateView(this));\n }\n\n @Override\n public void onError(String errorMessage) {\n ...\n }\n\n @Override\n protected void onCreate(Bundle savedInstanceState) {\n ...\n service = SearchInAppsService.create(this);\n List\u003cString\u003e searchContext = Arrays.asList(new String[]{\"Query\"});\n service.getSearchSuggestionsView(\n new GetSearchSuggestionsViewOptions().setTextContext(searchContext)\n .setClientId(\"client id\"),this);\n ...\n }\n\n @Override\n public void onDestroy() {\n service.shutDown();\n super.onDestroy();\n }\n }\n\n### Jetpack Compose\n\n package ...\n\n ...\n import android.os.Bundle\n import androidx.activity.compose.setContent\n import androidx.appcompat.app.AppCompatActivity\n import androidx.compose.runtime.Composable\n import androidx.compose.runtime.DisposableEffect\n import androidx.compose.runtime.MutableState\n import androidx.compose.runtime.mutableStateOf\n import androidx.compose.ui.platform.LocalContext\n import androidx.compose.ui.viewinterop.AndroidView\n import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewGeneratorCallback\n import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewOptions\n import com.google.android.libraries.searchinapps.SearchInAppsService\n import com.google.android.libraries.searchinapps.SearchSuggestionsViewGenerator\n ...\n\n class MainActivity : AppCompatActivity() {\n override fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n setContent { SearchSuggestionsUI() }\n }\n\n @Composable\n fun SearchSuggestionsUI() {\n ...\n var service by remember {\n mutableStateOf\u003cSearchInAppsService?\u003e(\n SearchInAppsService.create(LocalContext.current))\n }\n var viewGenerator = mutableStateOf\u003cSearchSuggestionsViewGenerator?\u003e(null)\n DisposableEffect(Unit) { onDispose { service?.shutDown() } }\n val callback =\n object : GetSearchSuggestionsViewGeneratorCallback {\n override fun onSuccess(generator: SearchSuggestionsViewGenerator) {\n viewGenerator.value = generator\n }\n\n override fun onError(errorMessage: String) {}\n }\n var searchContexts: List\u003cString\u003e = listOf\u003cString\u003e(\"Query\")\n var options: GetSearchSuggestionsViewOptions =\n GetSearchSuggestionsViewOptions().setTextContext(searchContexts)\n .setClientId(\"client id\")\n service?.getSearchSuggestionsView(options, callback)\n ChipGroupUI(viewGenerator)\n ...\n }\n\n @Composable\n fun ChipGroupUI(viewGenerator: MutableState\u003cSearchSuggestionsViewGenerator?\u003e) {\n viewGenerator.value?.let { viewGenerator -\u003e\n var context = LocalContext.current\n AndroidView(\n factory = { context -\u003e viewGenerator.populateView(context) },\n update = { view -\u003e viewGenerator.updateView(view, context) },\n )\n }\n }\n }"]]