ga オブジェクト メソッド リファレンス

このリファレンスでは、ga オブジェクトで利用できるメソッドについて説明します。

メソッドの概要

以下のメソッドは、analytics.js ライブラリが読み込まれてから ga オブジェクトで利用できるようになります。これらのメソッドはすぐには利用できません。必ず、ga コマンドキューの準備完了コールバックを使用して呼び出してください。

不適切 - ga オブジェクトのメソッドは readyCallback の外側で使用しないでください。まだ準備ができていない可能性があります。

var trackers = ga.getAll();

適切 - ga オブジェクトのメソッドを readyCallback 内で使用すると、確実に準備が整った時点で呼び出されます。

ga(function() {
  var trackers = ga.getAll();
});
メソッド
create([trackingId], [cookieDomain], [name], [fieldsObject]);

戻り値: Tracker

指定されたフィールドで新しいトラッカー インスタンスを作成します。

getByName(name)

戻り値: Tracker

指定された名前のトラッカー インスタンスを取得します。

getAll()

戻り値: Array<Tracker>

すべてのトラッカー インスタンスを取得します。

remove(name)

戻り値: undefined

指定された名前のトラッカー インスタンスを削除します。

メソッドの詳細

create

指定されたフィールドで新しいトラッカー インスタンスを作成します。

使用方法

ga.create([trackingId], [cookieDomain], [name], [fieldsObject]);

パラメータ

個別のフィールドについては、フィールド リファレンスをご覧ください。

戻り値

Tracker

// Creates a default tracker for the property UA-XXXXX-Y
// and uses automatic cookie domain configuration.
ga(function() {
  var tracker = ga.create('UA-XXXXX-Y', 'auto');
})
// Creates a tracker with the name "myTracker" for the property
// UA-XXXXX-Y, sets the cookieDomain to "example.com" and specifies
// a site speed sample rate of 10%.
ga(function() {
  var myTracker = ga.create('UA-XXXXX-Y', 'example.com', 'myTracker', {
    siteSpeedSampleRate: 10
  });
});

getByName

指定された名前のトラッカー インスタンスを取得します。

使用方法

ga.getByName(name);

パラメータ

名前 タイプ 必須 / 省略可 説明
name string 必須 取得するトラッカーの名前。

戻り値

Tracker

// Gets the default tracker.
ga(function() {
  ga.getByName('t0');
});
// Gets the tracker with the name "myTracker".
ga(function() {
  ga.getByName('myTracker');
});

getAll

すべてのトラッカー インスタンスを取得します。

ga.getAll();

戻り値

Array<Tracker>

// Logs a list of all tracker names to the console.
ga(function() {
  var trackers = ga.getAll();
  trackers.forEach(function(tracker) {
    console.log(tracker.get('name'));
  });
});

remove

指定された名前のトラッカー インスタンスを削除します。

使用方法

ga.remove(name);

パラメータ

名前 タイプ 必須 / 省略可 説明
name string 必須 削除するトラッカーの名前。

戻り値

undefined

// Removes the default tracker.
ga(function() {
  // Note that, unlike the ga command queue's remove method,
  // this method requires passing a tracker name, even when
  // removing the default tracker.
  ga.remove('t0');
});
// Removes the tracker with the name "myTracker".
ga(function() {
  ga.remove('myTracker');
});