Unity SDK v2 Beta
6 minute read
See the GitHub codebase to get a more hands-on understanding of the SDK.
SDK setup requirements
- Download and install the Unity development kit.
- Sign up for RudderStack.
- Set up a Unity source in your RudderStack dashboard. Note the write keyThe write key (or source write key) is a unique identifier for your source. RudderStack uses this key to send events from a source to the specified destination. for this source.
- You will also need the data plane URL associated with your RudderStack workspace.
Installing the Unity SDK
- Download
rudder-sdk-unity.unitypackage. - Import the downloaded package to your project. From the Assets menu, go to Import Package > Custom Package… :

- Select
rudder-sdk-unity.unitypackagefrom the downloaded location and click Open:

- Click Import in the import popup:

- Add the RudderStack.prefab file from the path
Assets/RudderStack/Unity/Prefabs/RudderStack.prefabto every scene in your Unity app. Also, make sure thatRudderStack.prefabis linked to theRSMaster,RSScreenView, andRSLoggerscripts.

Initializing the RudderStack client
- Import the SDK:
using RudderStack.Unity;- Initialize the SDK as shown. Replace
WRITE_KEYandDATA_PLANE_URLwith the actual values obtained in the SDK setup requirements section.
RSAnalytics.Initialize("WRITE_KEY",
new RSConfig(dataPlaneUrl: "DATA_PLANE_URL"));
// for coroutine
StartCoroutine(RSAnalytics.InitializeRoutine("WRITE_KEY",
new RSConfig(dataPlaneUrl: "DATA_PLANE_URL")));SDK initialization options
You can configure your client based on the following parameters using RudderConfigBuilder:
| Parameter | Type | Description |
|---|---|---|
logLevel | Enum | Controls how much of the log you want to see from the SDK. Default value: Logger.Level.INFO |
dataPlaneUrl | String | Your data plane URL. |
dbThresholdCount | Integer | Number of events to be saved in the SQLite database. Once the limit is reached, older events are deleted from the database.Default value: 10000 |
sleepcount | Integer | Minimum waiting time to flush the events to the RudderStack server. The minimum value can be set to 1 second.Default value: 10 seconds |
trackLifecycleEvents | Boolean | Determines if the SDK will automatically capture the application lifecycle events. Default value: true |
recordScreenViews | Boolean | Determines if the SDK will automatically capture the screen view events. Default value: false |
controlPlaneUrl | String | Change this parameter only if you are self-hosting the control plane. Check the Self-hosted control plane section below for more information. The SDK will add /sourceConfig along with this URL to fetch the source configuration.Default value: https://api.rudderlabs.com |
gzip | Boolean | Gzips the event requests. Default value: true |
Self-hosted control plane
Do not pass the controlPlaneUrl parameter during the SDK initialization if you are using the RudderStack dashboard to set up your connections. The SDK supports this parameter only if you are using the open source Control Plane Lite utility to self-host your control plane.
Identify
The identify call lets you identify a visiting user and associate them to their actions. It also lets you record the traits about them like their name, email address, etc.
The Unity SDK captures deviceId and uses that as anonymousId for identifying the user. This helps in tracking the users across the application installation. To attach more information to the user, use the identify method.
Once the SDK identifies the user, the SDK persists and passes the user information to the subsequent calls. To reset the user identification, use the reset method.
The following snippet highlights a sample identify call:
RSAnalytics.Client.Identify("unity_user_id",
new Dictionary<string, object> { { "email", "abc@example.com" }, { "age", 24 }, { "name", "First Last" }, { "gender", "Male" } });Override anonymousId using setAnonymousId
You can explicitly set the anonymousId for all future events using the PutAnonymousId() method.
RSClient.PutAnonymousId("anonymous_id");Track
The track call lets you record the users’ in-game activity. Each user action is called an event.
The following snippet highlights a sample track call:
RSAnalytics.Client.Track("sample_track",
new Dictionary<string, object> { { "key_1", "value_1" }, { "key_2", 4 }, { "key_3", 4.2 }, { "key_4", true } });Track application lifecycle events
The Unity SDK lets you track the Application Lifecycle Events to get insights into the app metrics like installs, opens, updates, etc.
To enable automatic tracking, set trackLifeCycleEvents method to true while initializing the SDK:
RSAnalytics.Initialize("WRITE_KEY",
new RSConfig(dataPlaneUrl: "DATA_PLANE_URL")
.SetTrackLifeCycleEvents(true));Trigger Application Updated lifecycle event
To ensure that the Application Updated lifecycle event is triggered successfully:
- For iOS: Make sure the
Bundle versionin theInfo.plistfile of your application is incremented. If theBundle versionof yourtargetpoints to theBundle versionof yourproject, then increment it. - For Android: Make sure the
versionCodein thedefaultConfigobject nested in theandroidobject of your app’sbuild.gradleis incremented.
Refer to the Application Lifecycle Events Specification guide for more information.
Screen
The screen call is the mobile equivalent of the page call. It lets you record the screen views on your mobile app along with other relevant information about the screen.
The following snippet highlights a sample screen call:
RSAnalytics.Client.Screen("sample_screen",
new Dictionary<string, object> { { "key_1", "value_1" }, { "key_2", 4 }, { "key_3", 4.2 }, { "key_4", true } });Automatic screen recording
You can enable the automatic recording of screen views by setting recordScreenViews to true while initializing the SDK. This automatically sends a screen call for every screen that a user views. By default, recordScreenViews is set to false.
RSAnalytics.Initialize("WRITE_KEY",
new RSConfig(dataPlaneUrl: "DATA_PLANE_URL")
.SetRecordScreenViews(true));TherecordScreenViewsparameter records the screen views of the native Android Activities or the iOS Views only and not by the React Native Views.
Page
The page call lets you record the page views on your application along with the other relevant information about the page.
A sample page call is shown below:
RSAnalytics.Client.Page("sample_page",
new Dictionary<string, object> { { "key_1", "value_1" }, { "key_2", 4 }, { "key_3", 4.2 }, { "key_4", true } });Group
The group call lets you link an identified user with a group, such as a company, organization, or an account. It also lets you record any custom traits or properties associated with that group.
The following snippet highlights a sample group call made using the Unity SDK:
RSAnalytics.Client.Group("group_id",
new Dictionary<string, object> { { "key_1", "value_1" }, { "key_2", 4 }, { "key_3", 4.2 }, { "key_4", true } });Alias
The alias call lets you merge different identities of a known user. It is an advanced method that lets you change the tracked user’s ID explicitly. You can use alias for managing the user’s identity in some of the downstream destinations.
RudderStack supports sendingaliasevents only to select downstream destinations. Refer to the destination-specific documentation for more details.
A sample alias call is shown below:
RSAnalytics.Client.Alias("new_unity_user_id");Reset
The reset method clears all persisted traits of the previously identified user.
RSAnalytics.Client.Reset();Set the advertisement ID
Use the PutAdvertisingId() method to explicitly set an advertising ID.
RSClient.PutAdvertisingId("advertisement_id");Set the device token
You can pass your device token for push notifications for the destinations which support the feature using the PutDeviceToken() method. RudderStack sets the token under context.device.token.
RSClient.PutDeviceToken("device_token");FAQ
Which platforms are does Unity SDK support?
The Unity SDK v2 supports the following platforms:
- Android
- iOS
- Windows
- macOS
- Xbox
- PlayStation
Do I need to add anything to my Android ProGuard rules?
If you are facing any event delivery issues in your production environment, verify if you have added the following line in your ProGuard rules:
-keep class com.rudderstack.android.** { *; }How do I gzip event requests in Unity SDK?
The Unity SDK automatically gzips requests. To disable this feature, set the Gzip parameter to false while initializing the SDK:
RSAnalytics.Initialize("WRITE_KEY",
new RSConfig(dataPlaneUrl: "DATA_PLANE_URL")
.SetGzip(false));Gzip requires rudder-server v1.4 or higher. Otherwise, your events might fail.