メインコンテンツへスキップ
見出し画像

【React×MA_15】 スクロール率のトラッキング方法

    Reactでスクロール率を正確にトラッキングする方法


    本記事のゴール

    • GA4またはGTMへスクロール率を正確に送信する

    • SPA構成でもリロード無しで計測を維持する

    • 一度送信したスクロール率を重複しないように管理

    • しきい値を多段階化(25%、50%、75%、100%)

    • Beacon APIやセッションストレージで離脱対策も対応


    そもそも「スクロール率トラッキング」とは?

    「ユーザーがどれだけページの下まで読んだか?」
    を数値化して、興味度・エンゲージメント指標として分析します。


    GA4では「scroll」は自動イベントですが...

    GA4の自動イベント scroll は「90%以上スクロールしたときだけ」発火します。
    25%・50%などの段階的な測定はされません。


    必要な実装方針

    gtag('event')
     GA4
     手動でしきい値別イベントを送信
    dataLayer.push()
     GTM
     GTM経由でイベント処理
    sendBeacon
     離脱対策
     最終到達率を安全に記録できる


    ステップ①:カスタムフック useScrollDepth.ts

    // hooks/useScrollDepth.ts
    import { useEffect, useRef } from 'react';
    
    const SCROLL_THRESHOLDS = [25, 50, 75, 90]; // 100%未満(GA4 scrollが90%)
    
    export const useScrollDepth = (pageName: string) => {
      const triggered = useRef<Set<number>>(new Set());
    
      useEffect(() => {
        const onScroll = () => {
          const scrollTop = window.scrollY;
          const winHeight = window.innerHeight;
          const docHeight = document.body.scrollHeight;
          const scrollPercent = Math.floor(((scrollTop + winHeight) / docHeight) * 100);
    
          for (const threshold of SCROLL_THRESHOLDS) {
            if (
              scrollPercent >= threshold &&
              !triggered.current.has(threshold)
            ) {
              triggered.current.add(threshold);
    
              // GA4に送信
              window.gtag?.('event', 'scroll_depth', {
                event_category: 'Scroll',
                event_label: `${threshold}%`,
                value: threshold,
                page_title: document.title,
                page_path: window.location.pathname,
                page_name: pageName,
              });
    
              // GTMに送信する場合はこちら
              // window.dataLayer?.push({
              //   event: 'scroll_depth',
              //   scroll_percent: threshold,
              //   page_name: pageName,
              // });
            }
          }
        };
    
        window.addEventListener('scroll', onScroll);
        return () => window.removeEventListener('scroll', onScroll);
      }, [pageName]);
    };

    ステップ②:App.tsxや各ページでフックを呼ぶ

    import { useScrollDepth } from './hooks/useScrollDepth';
    
    const AboutPage = () => {
      useScrollDepth('AboutPage');
    
      return (
        <div>
          <h1>About Us</h1>
          <p>...</p>
        </div>
      );
    };

    pageName をつけるとレポート集計がしやすくなります。


    ステップ③:送信されたイベントの意味

    event_category
     Scroll
    event_label
     25%、50%、75%、90%など
    value
     数値的な深さ(整数)
    page_name
     任意(探索レポート用)


    離脱時にスクロール深度を送る方法(100%到達しない場合)

    useEffect(() => {
      const onUnload = () => {
        const scrollTop = window.scrollY;
        const winHeight = window.innerHeight;
        const docHeight = document.body.scrollHeight;
        const scrollPercent = Math.floor(((scrollTop + winHeight) / docHeight) * 100);
    
        navigator.sendBeacon('/api/scroll', JSON.stringify({
          page_path: window.location.pathname,
          page_title: document.title,
          scroll_percent: scrollPercent,
        }));
      };
    
      window.addEventListener('beforeunload', onUnload);
      return () => window.removeEventListener('beforeunload', onUnload);
    }, []);

    sendBeacon() を使えば、ページ離脱の直前でも確実に送信可能。
    /api/scroll 側でBigQueryやGA連携を構築すれば最終到達率分析が可能。


    GTMで受け取る場合の設定手順

    1. カスタムイベント「scroll_depth」でトリガー作成

    2. GA4タグで「scroll_percent」などの変数を送信

    3. 探索レポートで scroll_percent をディメンションに設定


    デバッグ方法(GA4)

    • GA4リアルタイム → scroll_depth が届いているか

    • window.dataLayer を console.log で確認

    • network タブ → /collect にパラメータ付きで送信されているか


    よくある失敗とその対策

    毎スクロールで送信 → 重複計測
     Set や sessionStorage で一度だけ送信制御
    ページが短くて100%届かない
     docHeight を元に閾値設定し直す
    SPA遷移で初期化されない
     useEffect([location]) で再計測
    iOS/AndroidでscrollYが0
     モバイルブラウザはbodyではなくhtml要素見ることも検討


    応用:スクロール到達率 × ボタン押下のCV率分析

    1. scroll_percent >= 75% のセグメントをGA4で抽出

    2. CTAクリック率との相関分析

    3. スクロール率が高い=関心度が高い → A/Bテスト施策に活用


    まとめ:Reactスクロールトラッキングの要点

    しきい値の設定
     25%, 50%, 75%, 90%(+Beaconで最終)
    重複防止
     Set, sessionStorage, localStorage
    GA4送信
     gtag('event', 'scroll_depth', {...})
    GTM送信
     dataLayer.push({ event: 'scroll_depth' })
    離脱対応
     sendBeacon で最終到達率を安全に送る
    分析視点
     ページ別, スクロール率別, 滞在時間との掛け合わせ分析



     
     
    フロントエンド、バックエンドのエンジニアをやっております。 読むだけで複雑だったはずの技術が、すっと腑に落ちる。その静かな感動を届けたい。

    あなたへのおすすめ