Unity SDK Quick Start Guide

Overview

This section shows how to install the CleverTap SDK, track your first user event, and see this information within the CleverTap dashboard in less than ten minutes. CleverTap provides Unity SDK that enables app developers to track, segment, and engage their users.

Install

You can now install the CleverTap Unity SDK using the .unitypackage Unity package or as a local package through Unity Package Manager (UPM).

This installation involves the following major steps:

Import the CleverTap Unity Package

  1. Download the latest version of the CleverTap Unity package. Import the .unitypackage into your Unity Project. Go to Assets > Import Package > Custom Package.
  2. Add the PlayServiceResolver and the ExternalDependencyManager folders. These folders will install the EDM4U plugin, which automatically adds all the Android and iOS dependencies when building your project.
  3. Ensure that the AndroidPostImport script is added since it sets up clevertap-android-wrapper library for Android.

Import the CleverTap Unity Package as a Local Dependency

Clone the latest release version of CleverTap Unity SDK. The SDK can be imported as a local package through the Unity Package Manager.

Set Up the Unity SDK

CleverTap API can be accessed anywhere in your project by simply calling the static CleverTap class. No need to create GameObject with the CleverTapUnity name or attach any script. The new architecture handles the following:

  • Instantiation of platform-specific binding
  • Creation of GameObject
  • Script attachment.

You can view your CleverTap Account ID and CleverTap Account Token from the Settings page.

1449

Add CleverTap Account and Preferences

// Initialize CleverTap
CleverTap.LaunchWithCredentialsForRegion({YOUR_CLEVERTAP_ACCOUNT_ID}, {YOUR_CLEVERTAP_ACCOUNT_TOKEN}, {CLEVERTAP_ACCOUNT_REGION});

Callbacks

A new mechanism to handle callbacks is introduced. You can now add an event listener for a callback directly through the CleverTap static events. No need to set all callbacks in the CleverTapUnity.cs anymore.

CleverTap.OnCleverTapDeepLinkCallback += YOUR_CALLBACK_METHOD;  
CleverTap.OnCleverTapProfileInitializedCallback += YOUR_CALLBACK_METHOD;  
CleverTap.OnCleverTapProfileUpdatesCallback += YOUR_CALLBACK_METHOD;

This introduces breaking changes in the CleverTapUnity.cs, rendering all existing callback methods obsolete, and these methods will not be called anymore. If you still want to use the callbacks through the CleverTapUnity.cs methods, each method must subscribe to a new event listener found at CleverTap.On{CleveTapUnity_Callback_MethodName}.

The following is a sample code on how to subscribe to new event listeners with existing methods in CleverTapUnity.cs:

CleverTap.OnCleverTapDeepLinkCallback += CleverTapDeepLinkCallback;
CleverTap.OnCleverTapProfileInitializedCallback += CleverTapProfileInitializedCallback;
CleverTap.OnCleverTapProfileUpdatesCallback += CleverTapProfileUpdatesCallback;
CleverTap.OnCleverTapPushOpenedCallback += CleverTapPushOpenedCallback;
CleverTap.OnCleverTapInitCleverTapIdCallback += CleverTapInitCleverTapIdCallback;
CleverTap.OnCleverTapInAppNotificationDismissedCallback += CleverTapInAppNotificationDismissedCallback;
CleverTap.OnCleverTapInAppNotificationShowCallback += CleverTapInAppNotificationShowCallback;
CleverTap.OnCleverTapOnPushPermissionResponseCallback += CleverTapOnPushPermissionResponseCallback;
CleverTap.OnCleverTapInAppNotificationButtonTapped += CleverTapInAppNotificationButtonTapped;
CleverTap.OnCleverTapInboxDidInitializeCallback += CleverTapInboxDidInitializeCallback;
CleverTap.OnCleverTapInboxMessagesDidUpdateCallback += CleverTapInboxMessagesDidUpdateCallback;
CleverTap.OnCleverTapInboxCustomExtrasButtonSelect += CleverTapInboxCustomExtrasButtonSelect;
CleverTap.OnCleverTapInboxItemClicked += CleverTapInboxItemClicked;
CleverTap.OnCleverTapNativeDisplayUnitsUpdated += CleverTapNativeDisplayUnitsUpdated;
CleverTap.OnCleverTapProductConfigFetched += CleverTapProductConfigFetched;
CleverTap.OnCleverTapProductConfigActivated += CleverTapProductConfigActivated;
CleverTap.OnCleverTapProductConfigInitialized += CleverTapProductConfigInitialized;
CleverTap.OnCleverTapFeatureFlagsUpdated += CleverTapFeatureFlagsUpdated;

πŸ“˜

IDFV Usage for CleverTap ID

Starting from CleverTap Unity SDK 2.1.2, you can optionally disable the usage of IDFV for CleverTap ID by selecting CLEVERTAP_DISABLE_IDFV. We recommend this for apps that send data from different iOS apps to a common CleverTap account. By default, the CLEVERTAP_DISABLE_IDFV checkbox is cleared.

iOS Specific Instructions

To integrate CleverTap with your iOS project, you need to add iOS dependencies using CocoaPods. After exporting your project from Unity, these commands should be run in Xcode.

After exporting the project to Xcode, run pod install to install the required dependencies. The EDM4U plugin automates this dependency installation process during each export from Unity to Xcode. Alternatively, you can manually install the pods by navigating to the exported Xcode project folder and running pod install.

Once the installation is complete, open the .xcworkspace file in Xcode to build the game with the installed dependencies.

  1. Set your Account ID, Account token, and Region, as well as personalization and IDFV settings, in the CleverTap Settings Assets > CleverTap Settings. You can find the CleverTap Account ID, CleverTap Account token, and CleverTap Region on the Settings page of the CleverTap dashboard.
Set your Account

Set your Account

This ensures that the CleverTap Post Processor script automatically adds these settings to the Info.plist file.

  1. Set up EDM4U plugin to install CocoaPods automatically. Go to Assets > External Dependency Manager > iOS Resolver > Settings. You can also install CocoaPods manually usingpod install.
Set up EDM4U plugin

Set up EDM4U plugin

  1. Open the .xcworkspace project.
  2. To enable Push Notifications, ensure to add the Push Notifications capability to your Xcode project.
  3. Build and run your iOS project.

Add Region Code

To know how to add region code for iOS in Unity SDK, refer to Region Codes.

Android Specific Instructions

To integrate the required dependencies and configurations for your Android project, follow these steps in Unity Editor:

  1. Go to File > Build Settings > Android > Player Settings > Publishing Settings > Build.
  2. Enable .gradle templates and custom AndroidManifest.
  3. EDM4U populates the Custom Main Gradle Template and Gradle Properties Template with the required Android dependencies.
Build Settings

Build Settings

  1. Once you enable the custom Android Manifest, add additional configurations to enable Push Notifications and override the default UnityActivity.
  2. Edit the AndroidManifest.xml file in Assets/Plugins/Android to add your Bundle Identifier, FCM Sender ID, CleverTap Account ID, CleverTap Account Token, and Deep Link URL scheme (if applicable):
<?xml version="1.0" encoding="utf-8"?>
<manifest package="YOUR_BUNDLE_IDENTIFIER"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:installLocation="preferExternal"
    android:versionCode="1"
    android:versionName="1.0">

    <!-- Screen Support -->
    <supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="true"
        android:xlargeScreens="true" />

    <!-- Permissions -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <!-- Features -->
    <uses-feature android:glEsVersion="0x00020000" />

    <uses-feature
        android:name="android.hardware.touchscreen"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.touchscreen.multitouch"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.touchscreen.multitouch.distinct"
        android:required="false" />

    <application
        android:debuggable="true"
        android:icon="@drawable/app_icon"
        android:isGame="true"
        android:label="@string/app_name"
        android:theme="@style/UnityThemeSelector">

        <!-- Main Activity -->
        <activity
            android:name="com.clevertap.unity.CleverTapOverrideActivity"
            android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:screenOrientation="fullSensor">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
            </intent-filter>

            <!-- Deep Links -->
            <!-- Uncomment and replace YOUR_URL_SCHEME if applicable, or remove if not supporting deep links -->
            <!--
            <intent-filter android:label="@string/app_name">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="YOUR_URL_SCHEME" />
            </intent-filter>
            -->

            <meta-data
                android:name="unityplayer.UnityActivity"
                android:value="true" />
        </activity>

        <!-- Services -->
        <service
            android:name="com.clevertap.android.sdk.pushnotification.fcm.FcmMessageListenerService"
            android:exported="true">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

        <service
            android:name="com.clevertap.android.sdk.pushnotification.CTNotificationIntentService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.clevertap.PUSH_EVENT" />
            </intent-filter>
        </service>

        <!-- CleverTap Configuration -->
        <meta-data
            android:name="FCM_SENDER_ID"
            android:value="id:YOUR_FCM_SENDER_ID" />

        <meta-data
            android:name="CLEVERTAP_ACCOUNT_ID"
            android:value="Your CleverTap Account ID" />

        <meta-data
            android:name="CLEVERTAP_TOKEN"
            android:value="Your CleverTap Account Token" />

    </application>

</manifest>
  1. Add your google-services.json file to the project's Assets folder.
  2. Build your app or Android project as usual.

Add Region Code

To know how to add region code for Android in Unity SDK, refer to Region Codes.

Initialize CleverTap SDK

#if (UNITY_IPHONE)
    CleverTap.LaunchWithCredentials(CLEVERTAP_ACCOUNT_ID, CLEVERTAP_ACCOUNT_TOKEN);
#endif

#if (UNITY_ANDROID)
    CleverTap.Initialize(CLEVERTAP_ACCOUNT_ID, CLEVERTAP_ACCOUNT_TOKEN);
#endif

Track User Profiles

Create a User profile when the user logs in (On User Login):

Dictionary<string, string> newProps = new Dictionary<string, string>();
newProps.Add("Identity", "123456");
newProps.put("Name", "Jack Montana");    // String
newProps.put("Identity", "61026032");      // String
newProps.put("Email", "jack@gmail.com"); // Email address of the user
newProps.put("Phone", "+14155551234");   // Phone (with the country code, starting with +)
newProps.put("Gender", "M");             // Can be either M or F
CleverTap.OnUserLogin(newProps);

Track User Events

Record an event without event data:

// record basic event with no properties
CleverTap.RecordEvent("testEvent");

Record an event with event data:

// record event with properties
Dictionary<string, object> Props = new Dictionary<string, object>();
Props.Add("testKey", "testValue");
CleverTap.RecordEvent("testEventWithProps", Props);