Skip to main content

Multiple instances

Last updated 08/27/2026

You can call the initInstance method to create a sub-instance object. Its parameter is the sub-instance name. You can then use this name to call the sub-instance's APIs.

// Create an instance named newInstance
ta.initInstance("newInstance");
// Set distinct_id for the sub-instance and send the test_event event
ta.newInstance.setDistinctId("new_distinct_id");
ta.newInstance.track("test_event");

By default, a sub-instance uses the same configuration as the main instance (appId, serverUrl, etc.). Also by default, local caching is not enabled for sub-instances.

To configure parameters separately for a sub-instance, pass in the configuration during initialization. By passing a different appId value in the configuration, you can report data to a different project:

// Define the configuration parameters of the sub-instance
var param = {
appId: "debug-appid",
serverUrl: "ANOTHER_SERVER_URL",
persistenceEnabled: true, // Enable local caching for the sub-instance. The local cache of a sub-instance is distinguished by the sub-instance name
send_method: "image",
showLog: true
};

// Initialize the sub-instance
ta.initInstance("anotherInstance", param);

//Report data to the main instance's project
ta.track("Event");

//Report data to the sub-instance's project
ta.anotherInstance.track("Event");

The main instance and sub-instances do not share an ID system or super properties, so you can set a user ID for each instance separately. The following example uses this feature to report a successful invitation event and an invited event for the inviter and the invitee of a friend invitation, respectively

//The main instance is the invited new user, and the sub-instance is the inviter
ta.login("invitee");
ta.anotherInstance.login("inviter");
//The new user triggers the invited event
ta.track("be_invited");
//The inviter triggers the invite-new-user event
ta.anotherInstance.track("invite_new_user");
Was this page helpful?