การอ้างอิงเมธอดออบเจ็กต์ ga

การอ้างอิงนี้อธิบายถึงเมธอดที่มีในออบเจ็กต์ ga

ข้อมูลสรุปของเมธอด

เมธอดจะมีอยู่ในออบเจ็กต์ ga หลังจากโหลดไลบรารี analytics.js แล้ว เนื่องจากเมธอดเหล่านี้ไม่พร้อมใช้งานทันที คุณจึงควรเรียกใช้เมธอดเหล่านี้เสมอโดยใช้โค้ดเรียกกลับที่พร้อมของคิวคำสั่ง ga

ไม่ควร — ใช้วิธี ga ออบเจ็กต์นอก readyCallback เนื่องจากเมธอดอาจยังไม่พร้อมใช้งาน

var trackers = ga.getAll();

ควร — ใช้เมธอดออบเจ็กต์ ga ภายใน readyCallback เนื่องจากรับประกันว่าพร้อมใช้งาน

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