Class: Analytics

Analytics

With analytics, you can track various activity patterns of the user, and modify your application features accordingly.

new Analytics(trackAnonymously, batchSize, superProperties){Analytics}

analytics.js, line 10
To see how your application is faring in the wild use Analytics.
Name Type Description
trackAnonymously object Enables annonymous tracking. (Logged-in users details is not traced) (Optional)
batchSize object Batch size specifies the number of triggers to be buffered before making a network call. (Optional)
superProperties object Set of custom properties that are to be sent along with each event. To send static super properties using the static key which should be a JavaScript object, for dynamic properties attach a function which should be executed before trigger. This function should return a JavaScript Object. Refer Example for better understanding (Optional)
Example
//'blt5d4sample2633b' is a dummy Application API key  
var Analytics   = Built.App('blt5d4sample2633b').Analytics; //Returns Analytics constructor
var analytics   = app.Analytics();
var analytics1  = app.Analytics(true); 
var analytics2  = app.Analytics(false, 10); 
// Static properties to be sent along with each event
var staticProps = {
   name:'John',
   gender: 'Male'  
}
var dynamicProps = function(){
   return {
      time: new Date(),
      random: Math.Random()
   }
}
var analytics3 = app.Analytics(true, 20, {
   static: staticProps,
   dynamic: dynamicProps
});

Members

staticAnalytics.Event

Use Event to trigger an event in Built.io Backend.
Example
//'blt5d4sample2633b' is a dummy Application API key 
var event = Built.App('blt5d4sample2633b').Analytics().Event('login').

Methods

flush()

analytics.js, line 366
Flushes all the events that are buffered
Example
var analytics = Built.App('blt5d4sample2633b').Analytics(true, 10);
var eventObj = analytics.Event('test');
var eventObj1 = analytics.Event('test1');

//This event trigger will be buffered
eventObj.trigger();

//Even this event trigger will be buffered 
eventObj1.trigger(); 

console.log(analytics.getQueue().length)

// Triggers the two events and empties the queue
analytics.flush(); 

getBatchSize(){Number}

analytics.js, line 138
Returns the batchSize.
Example
var analytics = Built.App('blt5d4sample2633b').Analytics(true, 10);
//Will return 10
console.log(analytics.getBatchSize()); 

getHeaders(){object}

analytics.js, line 306
Returns headers set on analytics level
Example
var analytics = Built.App('blt5d4sample2633b').Analytics();
analytics = analytics.setHeader('abc','pqr')
console.log(analytics.getHeaders());

getQueue(){array}

analytics.js, line 393
Returns the queue
Example
var analytics = Built.App('blt5d4sample2633b').Analytics(true, 10);
var eventObj = analytics.Event('test');
var eventObj1 = analytics.Event('test1');

//This event trigger will be buffered
eventObj.trigger();

//Even this event trigger will be buffered
eventObj1.trigger(); //buffered

console.log(analytics.getQueue())

getSuperProperties(){object}

analytics.js, line 269
Returns the super properties set on this instance
Example
var staticProps = {
   name:'John',
   gender: 'Male'  
}
var analytics = Built.App('blt5d4sample2633b').Analytics(true, 10, {
   static: staticProps
});
console.log(analytics.getSuperProperties());

getTrackAnonymously(){Boolean}

analytics.js, line 168
Determines whether users are tracked anonymously or not
Example
var analytics = Built.App('blt5d4sample2633b').Analytics(true, 10);
console.log(analytics.getTrackAnonymously());

setBatchSize(size){Analytics}

analytics.js, line 115
Batch size specifies the number of triggers to be buffered before making a network call.The idea is to reduce the number of api calls. The default batch size is set to 1.
Name Type Description
size Number The batch size.
Example
var analytics = Built.App('blt5d4sample2633b').Analytics();
analytics = analytics.setBatchSize(3);
//This event trigger will be buffered
analytics.Event('test').trigger();
 
//Event this event trigger will be buffered 
analytics.Event('test1').trigger();
 
// All three events are triggered in a single network call.
analytics.Event('test3').trigger(); 

setDynamicSuperProperties(propertiesGenerator){Analytics}

analytics.js, line 239
Super properties are a set of properties that need to be sent along with each trigger.
Name Type Description
propertiesGenerator function A function which will generate event properties dynamically
Example
var analytics = Built.App('blt5d4sample2633b').Analytics();

var fn = function(){
   return {
      time: new Date(),
      randome: Math.random() 
   }	
	}

analytics = analytics.setDynamicSuperProperties(fn);

analytics.Event('test',{
   abc: "pqr"
}).trigger();

setHeader(){Analytics}

analytics.js, line 288
Sets a header on analytics level
Example
var analytics = Built.App('blt5d4sample2633b').Analytics();
analytics = analytics.setHeader('abc','pqr')
console.log(analytics.getHeaders());

setStaticSuperProperties(properties){Analytics}

analytics.js, line 215
Static properties that should be sent along with each event
Name Type Description
properties Object A set of static properties to be sent with each trigger
Example
var analytics = Built.App('blt5d4sample2633b').Analytics();
analytics  = analytics.setStaticSuperProperties({
   name:'John',
   gender: 'Male'  
});
analytics.Event('test',{
   abc: "pqr"
}).trigger();

setSuperProperties(properties){Analytics}

analytics.js, line 181
Super properties are a set of properties that need to be sent along with each trigger.
Name Type Description
properties Object Set of static and dynamic properties to be sent along with each trigger. For static properties use 'static' key with a JavaScript object as its value. For dyanmic properties use 'dynamic' key with a function to be executed before each trigger. (Function should return a JavaScript Object)
Example
var analytics = Built.App('blt5d4sample2633b').Analytics();

var staticProps = {
   name:'John',
   gender: 'Male'  
}

var dynamicProps: function(){
   return {
      time: new Date(),
      random: Math.Random()
   }
}

analytics = analytics.setSuperProperties({
   static: staticProps,
   dynamic: dynamicProps
});

analytics.Event('test',{
   abc: "pqr"
}).trigger();

setTrackAnonymously(trackAnonymously){Analytics}

analytics.js, line 152
By default, the logged-in user's uid is sent to the server for analysis. To track anaymously, set it to false.
Name Type Description
trackAnonymously Boolean Whether to track the logged-in user's uid
Example
// By default will track user's uid
var analytics = Built.App('blt5d4sample2633b').Analytics(); 
// Would track anonymously
analytics = analytics.setTrackAnonymously(true); 

trigger(event(s), forceTrigger)

analytics.js, line 320
Triggers or enqueues a set of events based on the batchSize
Name Type Description
event(s) Array | Object Accepts an array of 'Event' objects or a single 'Event' object
forceTrigger Boolean Triggers the events regardless of the batchSize (Optional)
Example
var analytics = Built.App('blt5d4sample2633b').Analytics(true, 10);
var eventObj = analytics.Event('test');
var eventObj1 = analytics.Event('test1');

//Case 1:
//Enqueues the two events in buffer
analytics.trigger([eventObj, eventObj1]); 
//Enqueues the eventObj in buffer
analytics.trigger(eventObj); 

//Case 2:
// Triggers immediately
analytics.trigger([eventObj, eventObj1], true) 
//Triggers eventObj immediately
analytics.trigger(eventObj, true);