本参考文档介绍了 ga
对象提供的方法。
方法摘要
在 analytics.js
库加载完成后,您可以调用 ga
对象提供的以下方法。由于这些方法并非立即可用,您应该始终使用 ga
命令队列的 ready callback 来调用它们。
不要在 readyCallback
以外的地方使用 ga
对象方法,因为可能无法保障这些方法一定可用。
var trackers = ga.getAll();
正确的做法是在 readyCallback
内使用 ga
对象方法,这样可用性才有保障。
ga(function() { var trackers = ga.getAll(); });
方法 | |
---|---|
create([trackingId], [cookieDomain], [name], [fieldsObject]); |
返回值: 使用指定字段创建一个新的跟踪器实例。 |
getByName(name) |
返回值: 获取具有指定名称的跟踪器实例。 |
getAll() |
返回值: 获取所有跟踪器实例。 |
remove(name) |
返回值: 移除具有指定名称的跟踪器实例。 |
方法详解
create
使用指定字段创建一个新的跟踪器实例。
用法
ga.create([trackingId], [cookieDomain], [name], [fieldsObject]);
参数
如需查看各字段的说明,请参阅字段参考。
返回值
示例
// 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 |
是 | 要获取的跟踪器的名称。 |
返回值
示例
// 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'); });