追蹤器物件參考

本參考資料說明 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
  });
});