Multiple instances
Starting from v1.3.0, this SDK supports multiple instances. The instance that is initialized using the method described above is called the main instance, and instances created using the method described in this section are called sub-instances.
Multiple instances share device-related preset properties (including the device ID). No other properties are shared, including:
#distinct_iddistinct ID#account_idaccount ID- Super properties and dynamic super properties
- Events timed by
timeEvent
By creating a sub-instance, you can report data to another project, or report data with a different set of user IDs.
//Initialize configuration 1
var config_1 = {
appId: "app-id-1",
serverUrl: "https://youserverurl.1.com"
};
TDAnalytics.init(config_1);
//Initialize configuration 2
var config_2 = {
appId: "app-id-2",
serverUrl: "https://youserverurl.2.com"
};
TDAnalytics.init(config_2);
//Report an event to configuration 1 (if no app-id is passed, the event is reported to configuration 1 by default)
TDAnalytics.track({
eventName: 'event_from_appid_1'
});
//Report an event to configuration 1 (app-id-1 specified)
TDAnalytics.track({
eventName: 'event_from_appid_1'
}, 'app-id-1');
//Report an event to configuration 2 (app-id-2 specified)
TDAnalytics.track({
eventName: 'event_from_appid_2'
}, 'app-id-2');
If you need the persisted data of each instance to be stored separately, you can set persistenceName in config to customize the storage path
var config = {
appId: "app-id-1",
serverUrl: "https://youserverurl.1.com",
persistenceName:"persistence_name" //Customizable. Each instance must use a different value
};
TDAnalytics.init(config);
If you need to create a sub-instance for the same AppId, you can use initInstance to set an instance alias. Both instances report data to the same appId but use different initialization configurations. For example, configuration 1 reports data in normal mode, and configuration 2 reports data in Batch mode.
The sample code is as follows:
//Initialize configuration 1
TDAnalytics.init({
appId: "app-id-1",
serverUrl: "https://youserverurl.1.com"
});
//Initialize configuration 2
TDAnalytics.initInstance("td",{
appId: "app-id-1",
serverUrl: "https://youserverurl.1.com",
enableBatch: true
});

