追蹤程式物件參考資料

這個參考資料說明 Tracker 物件上可用的方法。

方法摘要

方法
get(fieldName)

傳回:*

取得追蹤器上儲存欄位的值。

set(fieldName|fieldsObject, [fieldValue])

傳回:undefined

在追蹤器中設定欄位/值組合或一組欄位/值組合。

send([hitType], [...fields], [fieldsObject])

傳回:undefined

傳送命中至 Google Analytics (分析)。

方法說明

get

取得追蹤器上儲存欄位的值。

使用方式

tracker.get(fieldName);

參數

名稱 類型 必填 說明
fieldName string 取得值的欄位值。

傳回

*

範例

// Creates a default tracker.
ga('create', 'UA-XXXXX-Y', auto);

// Gets the client ID of the default tracker and logs it.
ga(function(tracker) {
  var clientId = tracker.get('clientId');
  console.log(clientId);
});

set

在追蹤器中設定欄位/值組合或一組欄位/值組合。

使用方式

// Sets a single field/value pair.
tracker.set(fieldName, fieldValue);
// Sets a group of field/value pairs.
tracker.set(fieldsObject);

參數

如需個別欄位說明文件,請參閱欄位參考資料

傳回

undefined

範例

// Creates a default tracker.
ga('create', 'UA-XXXXX-Y', auto);

ga(function(tracker) {
  // Sets the page field to "/about.html".
  tracker.set('page', '/about.html');
});
// Creates a default tracker.
ga('create', 'UA-XXXXX-Y', auto);

ga(function(tracker) {
  // Sets both the page and title fields.
  tracker.set({
    page: '/about.html',
    title: 'About'
  });
});

send

傳送命中至 Google Analytics (分析)。

使用方式

tracker.send([hitType], [...fields], [fieldsObject]);

傳送的欄位是 ...fields 參數和 fieldsObject 中指定的值,並與目前儲存在追蹤器中的欄位合併。

參數

...fields 參數可指定的欄位會因命中類型而異。下表列出與各種命中類型對應的欄位。未列出的命中類型不接受 ...fields 參數,但僅限 fieldsObject

命中類型 ...fields
pageview page
event eventCategoryeventActioneventLabeleventValue
social socialNetworksocialActionsocialTarget
timing timingCategorytimingVartimingValuetimingLabel

如需個別欄位說明文件,請參閱欄位參考資料

傳回

undefined

範例

// Creates a default tracker.
ga('create', 'UA-XXXXX-Y', auto);

ga(function(tracker) {
  // Sends a pageview hit.
  tracker.send('pageview');
});
// Creates a default tracker.
ga('create', 'UA-XXXXX-Y', auto);

ga(function(tracker) {
  // Sends an event hit for the tracker named "myTracker" with the
  // following category, action, and label, and sets the nonInteraction
  // field value to true.
  tracker.send('event', 'link', 'click', 'http://example.com', {
    nonInteraction: true
  });
});