W tym artykule opisano metody dostępne w obiekcie ga
.
Podsumowanie metody
Po wczytaniu biblioteki analytics.js
w obiekcie ga
dostępne są poniższe metody. Ponieważ te metody nie są dostępne od razu, należy je zawsze wywołać za pomocą gotowego wywołania zwrotnego kolejki poleceń ga
.
Nie – używaj metod obiektów ga
poza readyCallback
, ponieważ mogą one jeszcze nie być dostępne.
var trackers = ga.getAll();
Tak – używaj metod obiektów ga
w readyCallback
, ponieważ zapewne są one dostępne.
ga(function() { var trackers = ga.getAll(); });
Metody | |
---|---|
create([trackingId], [cookieDomain], [name], [fieldsObject]); |
zwroty: Tworzy nową instancję skryptu śledzenia z określonymi polami. |
getByName(name) |
zwroty: Pobiera instancję trackera o określonej nazwie. |
getAll() |
zwroty: Pobiera wszystkie instancje trackera. |
remove(name) |
zwroty: Usuwa instancję trackera o podanej nazwie. |
Szczegóły metody
create
Tworzy nową instancję skryptu śledzenia z określonymi polami.
Wykorzystanie
ga.create([trackingId], [cookieDomain], [name], [fieldsObject]);
Parametry
W dokumentacji pól znajdziesz dokumentację poszczególnych pól.
Zwroty
Przykłady
// 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
Pobiera instancję trackera o określonej nazwie.
Wykorzystanie
ga.getByName(name);
Parametry
Nazwa | Typ | Wymagane | Opis |
---|---|---|---|
name |
string |
tak | Nazwa lokalizatora do pobrania. |
Zwroty
Przykłady
// Gets the default tracker. ga(function() { ga.getByName('t0'); });
// Gets the tracker with the name "myTracker". ga(function() { ga.getByName('myTracker'); });
getAll
Pobiera wszystkie instancje trackera.
ga.getAll();
Zwroty
Array<Tracker>
Przykład
// 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
Usuwa instancję trackera o podanej nazwie.
Wykorzystanie
ga.remove(name);
Parametry
Nazwa | Typ | Wymagane | Opis |
---|---|---|---|
name |
string |
tak | Nazwa lokalizatora do usunięcia. |
Zwroty
undefined
Przykłady
// 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'); });