Import the downloaded package to your project. From the Assets menu, go to Import Package > Custom Package… :
Select rudder-sdk-unity.unitypackage from 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.prefab to every scene in your Unity app. Also, make sure that RudderStack.prefab is linked to the RSMaster, RSScreenView, and RSLogger scripts.
Initializing the RudderStack client
Import the SDK:
usingRudderStack.Unity;
Initialize the SDK as shown. Replace WRITE_KEY and DATA_PLANE_URL with the actual values obtained in the SDK setup requirements section.
RSAnalytics.Initialize("WRITE_KEY",newRSConfig(dataPlaneUrl:"DATA_PLANE_URL"));// for coroutineStartCoroutine(RSAnalytics.InitializeRoutine("WRITE_KEY",newRSConfig(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 Cloud 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:
To ensure that the Application Updated lifecycle event is triggered successfully:
For iOS: Make sure the Bundle version in the Info.plist file of your application is incremented. If the Bundle version of your target points to the Bundle version of your project, then increment it.
For Android: Make sure the versionCode in the defaultConfig object nested in the android object of your app’s build.gradle is incremented.
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:
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.
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:
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 sending alias events 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:
-keepclasscom.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:
This site uses cookies to improve your experience while you navigate through the website. Out of
these
cookies, the cookies that are categorized as necessary are stored on your browser as they are as
essential
for the working of basic functionalities of the website. We also use third-party cookies that
help
us
analyze and understand how you use this website. These cookies will be stored in your browser
only
with
your
consent. You also have the option to opt-out of these cookies. But opting out of some of these
cookies
may
have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This
category only includes cookies that ensures basic functionalities and security
features of the website. These cookies do not store any personal information.
This site uses cookies to improve your experience. If you want to
learn more about cookies and why we use them, visit our cookie
policy. We'll assume you're ok with this, but you can opt-out if you wish Cookie Settings.