Analytics

ユーザーが [ルート]、[通話]、[ウェブサイト] をクリックした、Google マップを開いた、評価や作成者の帰属表示をクリックした、写真のライトボックスを開いた、タブを切り替えた、などの Places 分析イベントをサブスクライブできます。各イベントは、一連のタグと、インタラクション(または null)を含む最も近い公開 gmp-* 要素を提供します。この要素から、プレイス ID と場所を読み取ることができます。

提供されるアナリティクス イベント

次の表に、Places UI キットで提供される分析イベントの例を示します。

UI の操作 tags: Set<string>
[経路案内] ボタン {"click", "directions", "outbound"}
[マップで開く] ボタン {"click", "maps", "outbound"}
通話ボタン / 電話番号([概要] タブ) {"click", "call_place", "outbound"}
ウェブサイト ボタン / ウェブサイト([概要] タブ) {"click", "place_website", "outbound"}
アトリビューション リンク {"click", "attribution", "outbound"}
評価リンク {"click", "reviews", "outbound"}
法的開示リンク(ⓘ ダイアログ リンク) {"click", "disclosure", "outbound"}
レポートのリンク {"click", "report", "outbound"}
レビューのリンク {"click", "review", "outbound"}
メディア / 写真のリンク {"click", "media", "outbound"}
写真のライトボックスが開いた(アプリ内) {"click", "media"}
[概要] タブが選択されている(アプリ内) {"click", "overview_tab"}
[レビュー] タブが選択された状態(アプリ内) {"click", "reviews_tab"}
[概要] タブが選択された状態(アプリ内) {"click", "about_tab"}
クチコミのオーバーフロー メニュー(⋮)を開いた(アプリ内) {"click", "reviews_menu"}
ライトボックスのオーバーフロー メニュー(⋮)が開いた(アプリ内) {"click", "lightbox_menu"}
生成 AI による要約メニュー(⋮)が開いている(アプリ内) {"click", "generative_summary_menu"}
生成 AI によるクチコミの要約メニュー(⋮)を開きました {"click", "review_summary_menu"}

新しいイベントやタグが時間の経過とともに追加される可能性があるため、開発中に汎用ハンドラをアタッチして、コンポーネントを操作するときにブロードキャストされるすべてのイベントをログに記録できます。

const {Settings} = await google.maps.importLibrary('core');

// Log every gmp-analytics event broadcast on the page
Settings.getInstance().addEventListener('gmp-analytics', (event) => {
  console.log('Analytics event:', event);
});

アナリティクス イベントを登録して処理する

google.maps.Settings シングルトン インスタンスに gmp-analytics イベント リスナーを追加すると、ページ上のすべての Places UI キット 要素にわたってアナリティクス イベントをリッスンできます。

google.maps.AnalyticsEvent は、event.tags(Set<string>)と event.element(インタラクションを含む最も近い公開 gmp-* 要素(gmp-place-details-compact など)を表す Element | null。該当するものがない場合は null)を提供します。存在する場合は、event.element.place(またはリスト要素と検索要素の event.element.places)から関連する Place を読み取ることができます。

const {Settings} = await google.maps.importLibrary('core');
const {Place} = await google.maps.importLibrary('places');

const settings = Settings.getInstance();

settings.addEventListener('gmp-analytics', (event) => {
  const {tags, element} = event; // google.maps.AnalyticsEvent
  const place = element?.place;
  const placeId = place instanceof Place ? place.id : 'none';

  // Option A: Check for specific interactions using tag strings
  if (tags.has('directions')) {
    console.log(`User requested directions for place: ${placeId}`);
  }

  if (tags.has('outbound')) {
    console.log(`User is leaving the page via: ${[...tags].sort().join(', ')}`);
  }

  // Option B: Forward every event to your analytics pipeline (for instance, Google Analytics / gtag)
  gtag('event', 'maps_javascript_api_analytics', {
    element_tag: element?.localName ?? 'none',
    tags: [...tags].sort().join(','),
    place_id: placeId,
  });
});

ページに複数の Places UI キット要素が表示されている場合は、特定の DOM 要素参照に対して event.element をチェックして、どのカード インスタンスがイベントをトリガーしたかを判断できます。

const {Settings} = await google.maps.importLibrary('core');

const featuredCard = document.querySelector('#featured-place-card');

Settings.getInstance().addEventListener('gmp-analytics', (event) => {
  if (event.element?.contains(featuredCard) && event.tags.has('directions')) {
    console.log('Directions clicked on the featured place card:', featuredCard.place?.id);
  }
});