Getting and Setting tracker data


Getting and Setting tracker data

AFS's analytics.js library allows you to interact with the trackers, which is to say to get or set the data stored by them. To do this, there are two commands: the command "GET" is used to obtain the data, the command "SET" to define or modify data.

The Get command

Since the calls to the aa() function are saved in a queue and the download of analytics.js is done asynchronously, it is necessary to get confirmation of the creation of the tracker before being able to access its data with the command get.

There are two ways to do this:
  • a: The first is to define a callback function when creating the tracker with the create command. (See: the "Creating tracker" guide for details.)
  • b: The second is to call the function ready callback after the creation of the tracker.

Ready Callback function

Ready Callback is a function that you define by calling the aa() function. As all waitlist commands are executed in order, this function will be executed after the tracker is created. Ready Callback returns the tracker object.

Note: The Ready Callback function can also be called after sending the send command, you will then have access to the data sent by the AFS servers, such as the visitor ID.

The following code shows access to the default tracker object using Ready Callback function


 aa('create', 'XXXXXXXX’, 'auto');	
//adding the ready callback function
aa(function(tracker) {
  // Display the default tracker data on the console.
  console.log(tracker);
});

Access a tracker by name

If you use multiple trackers, you can access a specific tracker by using the getByName() function.


getByName('name');

The GetByName() function returns the tracker object with the specified name. If the name field is empty, the default tracker will be returned.


aa('create', 'XXXXXXXX', 'auto', 'moncapteur');
aa(function() {
  // displays data from the tracker called 'moncapteur'.
  console.log(aa.getByName('moncapteur'));
});

Get a list of all trackers

The function getAll() returns an array containing the list of all the trackers (objects) created.


aa('create', 'XXXXXXXX', 'auto');
aa('create', 'YYYYYYYY', 'auto', 'moncapteur');
aa(function() {
  // displays a table containing all the trackers
    console.log(aa.getAll());
});

Get tracker data using the GET command

Tracker data can be obtained using the get command.


aa('create', 'XXXXXXXX', 'auto');
aa(function(tracker) {
  // display the name and reference of the tracker.
    console.log(tracker.get('name'));
    console.log(tracker.get('referrer'));   
});
Using getByName():

aa('create', 'XXXXXXXX', 'auto','moncapteur');
aa(function() {
  // displays data from the “mon capteur “ tracker.
    var tracker=aa.getByName("moncapteur");
    console.log(tracker.get('name'));
    console.log(tracker.get('referrer'));   
});

Updating tracker data with SET command.

With the set command, the tracker data can be changed or updated. The command can be linked to a tracker name or sent alone.

The set command is sent via the function aa(). Arguments can be defined in two distinct ways:
  • a. Two parameters: the first specifies the field to be modified and the second, the replacement value.
  • b. By an object.

The following example changes the default tracker title:


aa('set','title','Home page') ;

The following example changes the tracker's URL and title through an object.


aa('set ',{
  title :'Home Page',
  location:'https://www.afsanalytics.com'
});

Updating a tracker using its name

You can set a tracker variable by using its name.


aa('moncapteur.set','title','About Us');

Use the get command directly on the tracker object


aa(function() {
  // displays data from the tracker named “moncapteur”.
    var tracker=aa.getByName("moncapteur")
    tracker.set("title","About");
    console.log(tracker.get('name'));
    console.log(tracker.get('title'));  
})

The next step:

Now that creating and accessing trackers has no secrets for you, you can continue your learning process by studying the following guide: Sending data to AFS Analytics



 
By AFS Website Analytics

Go Top
AFS Analytics uses LiveDisplay Tachnology