ga 物件方法參考資料

本參考資料說明 ga 物件可用的方法。

方法摘要

載入 analytics.js 程式庫後,ga 物件可以提供下列方法。由於這些方法無法立即使用,因此請務必使用 ga 指令佇列的「就緒回呼」來叫用這些方法。

請勿:在 readyCallback 外使用 ga 物件方法,因為目前可能還無法使用這些方法。

var trackers = ga.getAll();

正確:請在 readyCallback 中使用 ga 物件方法,因為系統會保證提供這些方法。

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');
});