本参考文档介绍了 Tracker
对象提供的方法。
方法摘要
方法 | |
---|---|
get(fieldName) |
返回值: 获取存储在跟踪器上的某个字段值。 |
set(fieldName|fieldsObject, [fieldValue]) |
返回值: 在跟踪器上设置一个或一组字段/值对。 |
send([hitType], [...fields], [fieldsObject]) |
返回值: 向 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 |
eventCategory 、eventAction 、eventLabel 、eventValue |
social |
socialNetwork 、socialAction 、socialTarget |
timing |
timingCategory 、timingVar 、timingValue 、timingLabel |
如需查看各字段的说明,请参阅字段参考。
返回值
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 }); });