# Unity

## SDK Initialization

{% hint style="warning" %}
This SDK version does not support WebGL. WebGL support will be added in the next releases.
{% endhint %}

{% hint style="info" %}
If you have previously used the Unity SDK version 2+, you need to delete the following files and folders in the "Assets" folder of your project:

![](https://2105883905-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LnGcP_ZeRJ1ipj9O8dF%2Fuploads%2FVYf3yE2b7ixfbiJBFDkv%2Fimage.png?alt=media\&token=7f4dbc4d-85d0-4b0c-8f6b-289d95cbf439)
{% endhint %}

1\. [Import the **`DTDAnalytics.unitypackage`** from GitHub repository.](https://github.com/devtodev-analytics/Unity-sdk-3.0)

{% hint style="info" %}
Attention! When importing to **Unity 2020**, you may face the following error:

<mark style="color:orange;">Assembly 'Assets/Plugins/DevToDev/Analytics/Windows/AnalyticsWindowsEditor.dll' will not be loaded due to errors:</mark>\ <mark style="color:orange;">AnalyticsWindowsEditor references strong named Newtonsoft.Json Assembly references: 13.0.0.0 Found in project: 12.0.0.0.</mark> \ <mark style="color:orange;">Assembly Version Validation can be disabled in Player Settings "Assembly Version Validation"</mark>

The error might occur due to the conflict between our package’s **`NewtonsoftJson`** dependency version and the one used in the **`VersionControl`** package. There are two ways to resolve this issue:

A. If you don’t use the **`VersionControl`** package, remove it from the package list. Choose ***Window->Package Manager*** in the Unity window:

![](https://2105883905-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LnGcP_ZeRJ1ipj9O8dF%2Fuploads%2FTf7jyuaWECMOdlkNWPcT%2Fimage.png?alt=media\&token=ec87edae-325e-488d-89af-e2b59c996aa0)

In the window that appears, choose Packages: In Project from a drop-down list at the top. After that, select **Version Control** from the package list and click **Remove**.\
![](https://2105883905-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LnGcP_ZeRJ1ipj9O8dF%2Fuploads%2FxTVveXACZHC232KTpHic%2Fimage.png?alt=media\&token=3ec8b020-329a-4a46-9437-26b109ed0a64)\
B. If you use **Unity’s Version Control**, you need to replace the **`newtonsoft.Json package`**. Open the directory of your main project (the one you want to integrate analytics with) **`MainProject\Library\PackageCache\`** and move the **`com.unity.nuget.newtonsoft-json`** **folder to the `MainProject\Packages\`** folder. It should look something like this:\
![](https://2105883905-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LnGcP_ZeRJ1ipj9O8dF%2Fuploads%2FTxsEK8v3wITqECnCevZq%2Fimage.png?alt=media\&token=f6fb2dac-add0-4081-a70b-50560c3e2b51)
{% endhint %}

2\. Create a script with the following code and attach it to the **`GameObject`** that will survive the entire life cycle of the app.

```csharp
using DevToDev.Analytics;
using UnityEngine;

public class DTDObject : MonoBehaviour
{
    void Start()
    { 
#if UNITY_ANDROID
        DTDAnalytics.Initialize("androidAppID");
#elif UNITY_IOS
        DTDAnalytics.Initialize("IosAppID");
#elif UNITY_WEBGL
        DTDAnalytics.Initialize("WebAppID");
#elif UNITY_STANDALONE_WIN
        DTDAnalytics.Initialize("winAppID");
#elif UNITY_STANDALONE_OSX
        DTDAnalytics.Initialize("OsxAppID");
#elif UNITY_WSA
        DTDAnalytics.Initialize("UwpAppID");
#endif
    }
}
```

You can find the **AppID** in the settings of the respective app in devtodev  (Settings → SDK → Integration → Credentials).

**`config`** - an object instance of **`DTDAnalyticsConfiguration`**, which is used for specifying additional properties during the initialization.

**`DTDAnalyticsConfiguration`**

<table data-header-hidden><thead><tr><th width="261.3333333333333">Parameter</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><strong>Parameter</strong></td><td><strong>Type</strong></td><td><strong>Description</strong></td></tr><tr><td><strong><code>CurrentLevel</code></strong></td><td>Integer</td><td>The player level at the moment of devtodev SDK initialization. It’s optional but we recommend using it for improving data accuracy.</td></tr><tr><td><strong><code>UserId</code></strong></td><td>String</td><td>A custom user ID assigned by the developer. In the case of default calculation by device IDs, the identifier can be used for searching users in devtodev. In case the project uses calculation by user IDs, the parameter is mandatory because it becomes the principal calculation ID in devtodev.</td></tr><tr><td><strong><code>TrackingAvailability</code></strong></td><td>DTDTrackingStatus (enum)</td><td>The property allows or disallows devtodev tracking of the user. By default, it is set to <em><strong><code>DTDTrackingStatus.Enable</code></strong></em>. SDK stores the previously assigned value. Pass <em><strong><code>DTDTrackingStatus.Disable</code></strong></em> if the user opted out of tracking in line with GDPR.</td></tr><tr><td><strong><code>LogLevel</code></strong></td><td>DTDLogLevel (enum)</td><td>The level of logging the SDK activity. The <em><strong><code>DTDLogLevel.No</code></strong></em> value is used by default. For troubleshooting during integration, it is recommended to set it to <em><strong><code>DTDLogLevel.Debug</code></strong></em>, and either switch it off <em><strong><code>DTDLogLevel.No</code></strong></em> or use it only for error handling <em><strong><code>DTDLogLevel.Error</code></strong></em> in the release version.</td></tr><tr><td><strong><code>ApplicationVersion</code></strong></td><td>String</td><td>The app version during the devtodev SDK initialization. Use the property on the WinStandalone platform only. For <strong>all other platforms, data is collected automatically.</strong></td></tr></tbody></table>

Example:

```csharp
var config = new DTDAnalyticsConfiguration
{
    ApplicationVersion = "1.2.3",
    LogLevel = DTDLogLevel.Debug,
    TrackingAvailability = DTDTrackingStatus.Enable,
    CurrentLevel = 1,
    UserId = "unique_userId"
};
```

```csharp
using DevToDev.Analytics;
using UnityEngine;

public class DTDObject : MonoBehaviour
{
    void Start()
    { 
#if UNITY_ANDROID
        DTDAnalytics.Initialize("androidAppID", config);
#elif UNITY_IOS
        DTDAnalytics.Initialize("IosAppID", config);
#elif UNITY_WEBGL
        DTDAnalytics.Initialize("WebAppID", config);
#elif UNITY_STANDALONE_WIN
        DTDAnalytics.Initialize("winAppID", config);
#elif UNITY_STANDALONE_OSX
        DTDAnalytics.Initialize("OsxAppID", config);
#elif UNITY_WSA
        DTDAnalytics.Initialize("UwpAppID", config);
#endif
    }
}
```

## Specific integration features of certain platforms

### Windows Standalone

**SDK Activity**

The SDK can’t control app activity in case you use Windows Standalone therefore this responsibility is shifted to the developer. While initializing the SDK, the activity starts automatically and after that, the activity status will not auto-change. To track app activity, the developer can use the following methods: **`DTDAnalytics.StartActivity`** and **`DTDAnalytics.StopActivity`**. It is recommended to use the **`DTDAnalytics.StopActivity`** method to stop activity when the app goes into the background or gets closed. You can use the **`DTDAnalytics.StartActivity`** method to resume activity when the app gets reopened from the taskbar.

For other platforms, there is no need to manually call the **`DTDAnalytics.StartActivity`** and **`DTDAnalytics.StopActivity`** methods.

### Android

Add the following strings to **proguard.txt** ([read more about Unity proguard here](https://docs.unity3d.com/Manual/android-gradle-overview.html)):

```kotlin
-keep class com.devtodev.** { *; }
-dontwarn com.devtodev.**
```

### iOS

To integrate with Xcode, the SDK uses **`PostProcessBuild`** in the **`DTDPostProcessAnalytics`** and **`DTDPostProcessMessaging`** scripts. If you use custom **`PostProcessBuild`** scripts, add them **`callbackOrder`** of less than ***`98`*** to avoid conflicts.&#x20;

{% hint style="info" %}
Your app must **request** tracking authorization before it can get the advertising identifier. See [the detailed instruction](https://docs.unity.com/ads/ATTCompliance.html) on how to request it.
{% endhint %}

{% hint style="info" %}
If you want to disable tracking of advertising identifiers, you need to open the ***`DTDPostProcesssAnalytics.cs`*** file and comment out the line ***`39`***.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.devtodev.com/~/changes/KHSfVuDz62f75T3NzrM9/in-developing/integration-of-sdk/sdk-integration/unity.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
