analytics.js の使用経験がある場合は、analytics.js と gtag.js の違いを理解すると参考になります。 このページでは、両者の主な違いと、analytics.js から gtag.js へ移行する際に役立つヒントをご紹介します。
概要
analytics.js では、トラッカーを使用してデータを Google アナリティクスへ送信し、ヒットタイプを使用してデータの種類を指定します。
analytics.js とは異なり、gtag.js では、Google アナリティクスへのデータの送信にトラッカーを使用しません。代わりに、config
コマンドで設定した ID で識別される Google アナリティクス プロパティにデータを送信します。gtag.js に提供されるイベント名で、Google アナリティクスに送信されるデータの種類を指定します。
analytics.js から gtag.js に移行するには、サイトのすべてのページに対して次の操作を行います。
次の処理について、analytics.js コードを同等の gtag.js コードに置き換えます。
analytics.js のスニペットを gtag.js スニペットに置き換える
ウェブページの analytics.js スニペットを見つけます。
<!-- Google Analytics --> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); ga('create', 'GA_MEASUREMENT_ID', 'auto'); ga('send', 'pageview'); </script> <!-- End Google Analytics -->
これを次の gtag.js スニペットに置き換えます。
<!-- Global Site Tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'GA_MEASUREMENT_ID'); </script>
GA_MEASUREMENT_ID
は、ご自分の Google アナリティクス ID に置き換えてください。
ページビューの測定
analytics.js では、トラッカーを使用してページビューを Google アナリティクスに送信します。トラッカーには、Google アナリティクス プロパティの測定 ID が設定されています。gtag.js では、config
コマンドで指定された GA_MEASUREMENT_ID
で識別される Google アナリティクス プロパティにページビューを送信します。
デフォルトのトラッカーでページビューを測定する
次のように、デフォルトのトラッカーを使用して Google アナリティクスにページビューを送信(send
)している analytics.js コードを見つけて削除します。
// Creates the default tracker.
ga('create', 'GA_MEASUREMENT_ID', 'auto');
// Uses the default tracker to send a pageview to the
// Google Analytics property with tracking ID GA_MEASUREMENT_ID.
ga('send', 'pageview');
gtag.js グローバル トラッキング スニペットに含まれる次のコードは、トラッキング ID が GA_MEASUREMENT_ID
の Google アナリティクス プロパティにページビューを自動的に送信します。
gtag('config', 'GA_MEASUREMENT_ID');
指定したトラッカーでページビューを測定する
次のコードは、指定したトラッカーを使用して Google アナリティクスにページビューを送信する analytics.js のコードです。
ga('create', 'GA_MEASUREMENT_ID', 'auto', 'trackerName');
ga('trackerName.send', 'pageview');
これを次の gtag.js event
コマンドに置き換えます。
gtag('event', 'page_view', { 'send_to': 'GA_MEASUREMENT_ID' });
イベントの測定
前述のとおり、analytics.js はトラッカーを使用して Google アナリティクスにイベントを送信します。トラッカーには、Google アナリティクス プロパティのトラッキング ID が設定されています。一方、gtag.js は config
コマンドで指定された GA_MEASUREMENT_ID
で識別される Google アナリティクス プロパティにイベントを送信します。
デフォルトのトラッカーでイベントを測定する
次のように、デフォルトのトラッカーを使用して Google アナリティクスにイベントを送信(send
)している analytics.js コードを見つけます。
ga('create', 'GA_MEASUREMENT_ID', 'auto');
ga('send', 'event', [eventCategory], [eventAction], [eventLabel], [eventValue], [fieldsObject]);
それらすべてを、次の gtag.js event
コマンドに置き換えます。
gtag('event', eventName, eventParameters);
eventName
は、ログに記録するイベントの名前です。
例:
analytics.js:
// Creates the default tracker.
ga('create', 'GA_MEASUREMENT_ID', 'auto');
// Uses the default tracker to send the event to the
// Google Analytics property with tracking ID GA_MEASUREMENT_ID.
ga('send', 'event', 'Videos', 'play', 'Fall Campaign');
gtag.js:
// Sends the event to the Google Analytics property with
// tracking ID GA_MEASUREMENT_ID set by the config command in
// the global tracking snippet.
gtag('event', 'play', {
'event_category': 'Videos',
'event_label': 'Fall Campaign'
});
指定したトラッカーでイベントを測定する
次のコードは、指定したトラッカーを使用して Google アナリティクスにイベントを送信する analytics.js のコードです。
ga('create', 'GA_MEASUREMENT_ID', 'auto', 'trackerName');
ga('trackerName.send', 'event', [eventCategory], [eventAction], [eventLabel], [eventValue], [fieldsObject]);
これを次の gtag.js event
コマンドに置き換えます。
gtag('event', eventName, {
'send_to': 'GA_MEASUREMENT_ID',
'parameter1': 'value1',
'parameter2': 'value2',
// ...
});
例:
analytics.js:
// Creates a tracker named clientTracker. ga('create', 'GA_MEASUREMENT_ID', 'auto', 'clientTracker'); // Uses tracker clientTracker to send the event to the // Google Analytics property with tracking ID GA_MEASUREMENT_ID. ga('clientTracker.send', 'event', 'Videos', 'play', 'Fall Campaign');
gtag.js:
// Send the event to the Google Analytics property
// with tracking ID GA_MEASUREMENT_ID.
gtag('event', 'play', {
'send_to': 'GA_MEASUREMENT_ID',
'event_category': 'Videos',
'event_label': 'Fall Campaign'
});
カスタム ディメンションとカスタム指標の送信
ウェブページで、Google アナリティクスにカスタム ディメンションを送信する analytics.js send
コマンドを見つけます。
ga('send', 'hitType', { 'dimension<Index>': 'dimension_value'});
それらすべてを、次の gtag.js コードに置き換えます。
gtag('config', 'GA_MEASUREMENT_ID', { 'custom_map': {'dimension<Index>': 'dimension_name'} }); gtag('event', 'any_event_name', {'dimension_name': 'dimension_value'});
GA_MEASUREMENT_ID
は、ご自分の Google アナリティクス ID に置き換えてください。
ウェブページで、Google アナリティクスにカスタム指標を送信する analytics.js send
コマンドを見つけます。
ga('send', 'hitType', { 'metric<Index>': 'metric_value'});
それらすべてを、次の gtag.js コードに置き換えます。
gtag('config', 'GA_MEASUREMENT_ID', { 'custom_map': {'metric<Index>': 'metric_name'} }); gtag('event', 'any_event_name', {'metric_name': 'metric_value'});
GA_MEASUREMENT_ID
は、ご自分の Google アナリティクス ID に置き換えてください。
カスタム速度の測定
ウェブページで、カスタム指標を測定する analytics.js send
コマンドを見つけます。
ga('send', 'timing', 'timingCategory', 'timingVar', timingValue, 'timingLabel');
それらすべてを、次の gtag.js event
コマンドに置き換えます。
gtag('event', 'timing_complete', {
'name': 'timingVar',
'value': timingValue,
'event_category': 'timingCategory',
'event_label': 'timingLabel'
});
例外の測定
ウェブページで、Google アナリティクスに例外を測定する analytics.js send
コマンドを見つけます。
ga('send', 'exception', {
'exDescription': 'error_message',
'exFatal': false // set to true if the exception is fatal
});
それらすべてを、次の gtag.js event
コマンドに置き換えます。
gtag('event', 'exception', {
'description': 'error_message',
'fatal': false // set to true if the exception is fatal
});
analytics.js のフィールドを gtag.js のパラメータにマッピングする
次の表は、analytics.js のフィールドを対応する gtag.js のパラメータにマッピングしています。
イベント
analytics.js のフィールド | gtag.js のパラメータ |
---|---|
eventAction |
event_action |
eventCategory |
event_category |
eventLabel |
event_label |
eventValue |
value |
カスタム ディメンションとカスタム指標
analytics.js のフィールド | gtag.js のパラメータ |
---|---|
dimension<Index> |
dimension<Index> |
metric<Index> |
metric<Index> |
<Index>
は、カスタム ディメンションまたはカスタム指標のインデックスを表す負ではない整数です。
カスタム速度
analytics.js のフィールド | gtag.js のパラメータ |
---|---|
timingCategory |
event_category |
timingLabel |
event_label |
timingValue |
value |
timingVar |
name |
例外測定
analytics.js のフィールド | gtag.js のパラメータ |
---|---|
exDescription |
description |
exFatal |
fatal |
拡張 e コマースの行動データ
analytics.js のフィールド | gtag.js のパラメータ |
---|---|
id |
transaction_id |
affiliation |
affiliation |
revenue |
value |
tax |
tax |
shipping |
shipping |
coupon |
coupon |
list |
list_name |
step |
checkout_step |
option |
checkout_option |
プロモーション データ
analytics.js のフィールド | gtag.js のパラメータ |
---|---|
creative |
creative_name |
position (インプレッション、商品) |
list_position |
position (プロモーション) |
creative_slot |
商品アクションとプロモーション アクション
analytics.js のフィールド | gtag.js のイベント |
---|---|
add |
add_to_cart |
checkout (最初の手順) |
begin_checkout |
checkout (後続のステップ) |
checkout_progress |
checkout_option |
set_checkout_option |
click |
select_content (プロモーションなし) |
detail |
view_item |
promo_click |
select_content (プロモーションあり) |
purchase |
purchase |
refund |
refund |
remove |
remove_from_cart |
クライアント ID とユーザー ID
analytics.js のフィールド | gtag.js のパラメータ |
---|---|
clientId |
client_id |
userId |
user_id |