Places UI キット コンポーネントにアナリティクスを追加する
ユーザーが [経路案内]、[通話]、[ウェブサイト] をタップした、Google マップを開いた、評価や作成者の帰属情報をタップした、写真のライトボックスを開いた、タブを切り替えた、などのアナリティクス イベントを登録できます。各イベントには、起動されたコンポーネントとコンテンツ タイプ、プレイス ID、場所が表示されます。
提供されるアナリティクス イベント
次の表に、Places UI キットで提供されるアナリティクス イベントの例を示します。
| UI の操作 | tags: Set<String> |
関連コンテンツ / コンテンツ タイプ |
|---|---|---|
| [経路案内] ボタン | {"click", "directions", "outbound"} |
"main_action_button" または "corner_action_button" |
| [マップで開く] ボタン | {"click", "maps", "outbound"} |
"main_action_button" または "corner_action_button" |
| 通話ボタン / 電話番号([概要] タブ) | {"click", "call_place", "outbound"} |
"phone_number"、"main_action_button"、"corner_action_button"のいずれか |
| ウェブサイト ボタン / ウェブサイト([概要] タブ) | {"click", "place_website", "outbound"} |
"website"、"main_action_button"、"corner_action_button"のいずれか |
| アトリビューション リンク | {"click", "attribution", "outbound"} |
"review" または "media" |
| 評価リンク | {"click", "reviews", "outbound"} |
"rating" または "review_summaries" |
法的開示リンク(ⓘ ダイアログ リンク) |
{"click", "disclosure", "outbound"} |
null(構成不可) |
| レポートのリンク | {"click", "report", "outbound"} |
"media"、"review"、"generative_summary"のいずれか |
| レビューのリンク | {"click", "review", "outbound"} |
"review" |
| メディア / 写真のリンク | {"click", "media", "outbound"} |
"media" |
| 写真のライトボックスが開いた(アプリ内) | {"click", "media"} |
"media" |
| [概要] タブが選択されている(アプリ内) | {"click", "overview_tab"} |
null |
| [レビュー] タブが選択されている(アプリ内) | {"click", "reviews_tab"} |
"review" |
| [概要] タブが選択された状態(アプリ内) | {"click", "about_tab"} |
"features" |
クチコミのオーバーフロー メニュー(⋮)を開いた(アプリ内) |
{"click", "reviews_menu"} |
"review" |
ライトボックスのオーバーフロー メニュー(⋮)が開いた(アプリ内) |
{"click", "lightbox_menu"} |
"media" |
生成 AI による要約メニュー(⋮)が開いている(アプリ内) |
{"click", "generative_summary_menu"} |
"generative_summary" |
生成 AI によるクチコミの要約メニュー(⋮)を開いた(アプリ内) |
{"click", "review_summary_menu"} |
"review" |
新しいイベントやタグが時間の経過とともに追加される可能性があるため、開発中に汎用ハンドラをアタッチして、コンポーネントを操作するときにブロードキャストされるすべてのイベントをログに記録できます。
// Wrap your Places UI Kit composable or parent layout to inspect all broadcast events. // AnalyticsEvent.toString() includes componentType, tags, place IDs, and contentType. ProvidePlacesAnalyticsHandler( handler = { event -> Log.d("PlacesAnalytics", "$event") } ) { // Places UI Kit composables... }
アナリティクス イベントを登録して処理する
親レイアウト コンテナまたはコンポーネント自体を ProvidePlacesAnalyticsHandler でラップすることで、UI Kit Compose コンポーネントからアナリティクス イベントをサブスクライブできます。
次のコードは、コンテナ階層内でアナリティクス イベントをサブスクライブして処理する方法を示しています。
import android.util.Log import androidx.compose.foundation.layout.Column import androidx.compose.runtime.Composable import com.google.android.libraries.places.compose.AnalyticsEvent import com.google.android.libraries.places.compose.AdvancedPlaceDetailsCompact import com.google.android.libraries.places.compose.PlaceReference import com.google.android.libraries.places.compose.ProvidePlacesAnalyticsHandler import com.google.firebase.* @Composable fun PlaceCardScreen() { val placeRef = PlaceReference.PlaceId("ChIJN1t_tDeuEmsRUsoyG83frY4") // Analytics callback wraps the container hierarchy ProvidePlacesAnalyticsHandler( handler = { event -> val placeId = event.places.firstOrNull()?.id ?: "none" // Option A: Check for specific interactions using built-in Tags constants if (AnalyticsEvent.Tags.DIRECTIONS in event.tags) { Log.d("PlacesAnalytics", "User requested directions for place: $placeId") } if (AnalyticsEvent.Tags.OUTBOUND in event.tags) { Log.d( "PlacesAnalytics", "User is leaving the app via: ${event.tags.sorted().joinToString(", ")}" ) } // Option B: Forward every event directly to your analytics SDK (e.g., Firebase) val firebaseAnalytics = remember { Firebase.analytics } firebaseAnalytics.logEvent("place_ui_kit_interaction") { param("component_type", event.componentType) param("content_type", event.contentType ?: "none") param("tags", event.tags.sorted().joinToString(",")) param("place_id", placeId) } } ) { Column { AdvancedPlaceDetailsCompact( reference = placeRef, // Direct card tap (for UI navigation, e.g., centering a map) onClick = { place -> Log.d("Places", "Card tapped for place ID: ${place.id ?: "unknown"}") } ) } } }
アプリに同じウィジェット タイプのカードが複数表示される場合(たとえば、2 つの AdvancedPlaceDetailsCompact コンポーザブル)、各コンポーザブルを独自の ProvidePlacesAnalyticsHandler でラップすると、どのカード インスタンスがイベントをトリガーしたかがわかります。
次のコード スニペットは、単一の AdvancedPlaceDetailsCompact コンポーザブルの分析イベントをサブスクライブして処理する方法を示しています。
import android.util.Log import androidx.compose.runtime.Composable import com.google.android.libraries.places.compose.AdvancedPlaceDetailsCompact import com.google.android.libraries.places.compose.AnalyticsEvent import com.google.android.libraries.places.compose.Orientation import com.google.android.libraries.places.compose.PlaceReference import com.google.android.libraries.places.compose.ProvidePlacesAnalyticsHandler @Composable fun SinglePlaceCardView() { val placeRef = PlaceReference.PlaceId("ChIJN1t_tDeuEmsRUsoyG83frY4") // Analytics callback scoped to this specific composable instance ProvidePlacesAnalyticsHandler( handler = { event -> val placeIds = event.places.mapNotNull { it.id }.joinToString(", ") val tags = event.tags.sorted().joinToString(", ") Log.d( "PlacesAnalytics", "[$tags] in ${event.componentType}, content: ${event.contentType ?: "null"}, places: [$placeIds]" ) if (AnalyticsEvent.Tags.DIRECTIONS in event.tags) { Log.d("PlacesAnalytics", "Directions tapped on this card") } } ) { AdvancedPlaceDetailsCompact( reference = placeRef, orientation = Orientation.HORIZONTAL, // Direct card-tap callback onClick = { place -> Log.d("Places", "Tapped card for place: ${place.id ?: "unknown"}") } ) } }
カードタップ コールバックを追加して処理する
カードタップ コールバックは、UI Kit コンポーネントごとに個別に設定して処理する必要があります。
UI キットのコンポーザブルで onClick パラメータを使用して、カード本体のタップを処理します。ユーザーがカードをタップすると、onClick は 3 つのプロパティが設定された Place オブジェクトを渡します。
place.id(String?): たとえば、完全な Place Details ビューを開いたり、マーカーを選択したりするための一意のプレイス ID。place.location(LatLng?): 地図カメラの中心に配置する場所や、マーカーを配置する場所の緯度 / 経度座標。place.viewport(LatLngBounds?): 地図上で場所をフレームに収めるための、推奨される地図ビューポートの境界(northeast座標とsouthwest座標)。
次のコードは、AdvancedPlaceDetailsCompact コンポーザブルにカードタップ コールバックを追加して処理する方法を示しています。
import android.util.Log import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import com.google.android.libraries.places.api.model.Place import com.google.android.libraries.places.compose.AdvancedPlaceDetailsCompact import com.google.android.libraries.places.compose.Orientation import com.google.android.libraries.places.compose.PlaceReference @Composable fun PlaceCardTapSampleView() { val peetsCoffeePlaceId = "ChIJT7FdmYiAhYAROFOvrIxRJDU" var selectedPlace by remember { mutableStateOf<Place?>(null) } AdvancedPlaceDetailsCompact( reference = PlaceReference.PlaceId(peetsCoffeePlaceId), orientation = Orientation.HORIZONTAL, onClick = { place -> // Handle the card tap using place.id, place.location, or place.viewport selectedPlace = place Log.d("Places", "Tapped place ID: ${place.id ?: "unknown"}") place.location?.let { location -> Log.d("Places", "Coordinates: ${location.latitude}, ${location.longitude}") } place.viewport?.let { viewport -> Log.d("Places", "Viewport NE: ${viewport.northeast}, SW: ${viewport.southwest}") } } ) }