# Unity

{% hint style="success" %}
Check out [**AI-assisted integration**](https://docs.devtodev.com/integration/integration-of-sdk-v2/sdk-integration/unity/ai-assisted-integration-beta) to streamline the process.&#x20;
{% endhint %}

## SDK Integration

{% 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 %}

To integrate devtodev analytics SDK, you can use one of the two methods: by using the *Unity Package Manager* (recommended) or by manually importing the *unitypackage*.

### **Integration by using the Unity Package Manager**

{% hint style="warning" %}
If you integrated the devtodev package manually, then you need to delete the Assets/DevToDev and Plugins/DevToDev folders.
{% endhint %}

1. Open the Package Manager (Window → Package Manager), click + in the top left corner and select *Add package from git URL*.
2. Copy the repository URL <https://github.com/devtodev-analytics/package_Analytics.git> to the input box and click *Add*.

   <div align="left"><figure><img src="https://2105883905-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LnGcP_ZeRJ1ipj9O8dF%2Fuploads%2FleSrUmsLSB8tu5H0PacQ%2F2c63f606-8130-4df7-95cf-049bfde6c867.png?alt=media&#x26;token=45409d86-a8ae-4d29-a3b6-f190e162c7c8" alt=""><figcaption></figcaption></figure> <figure><img src="https://2105883905-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LnGcP_ZeRJ1ipj9O8dF%2Fuploads%2F3JTKcPN9Ob5AehvtkJwY%2F1cf693fa-0956-4a18-8c76-101603e70ae2.png?alt=media&#x26;token=4c7d3bd7-6ea8-4377-9fe3-21dcc3b002b8" alt=""><figcaption></figcaption></figure></div>
3. Wait for the Unity Package Manager to download the package.&#x20;
4. For Android projects, add an identification package:

{% tabs %}
{% tab title="Android Google" %}
{% hint style="warning" %}
If you work with SDK version 3.5.0 and above, and you want to use Google Ad ID, you need to add [devtodev-analytics/package\_Google](https://github.com/devtodev-analytics/package_Google.git). When developing and publishing apps for kids [(COPPA)](https://www.ftc.gov/tips-advice/business-center/privacy-and-security/children's-privacy), you do not need [devtodev-analytics/package\_Google](https://github.com/devtodev-analytics/package_Google.git). Read more about working with [COPPA](https://www.ftc.gov/tips-advice/business-center/privacy-and-security/children's-privacy) in the [COPPA section](#apps-targeted-at-children).
{% endhint %}
{% endtab %}

{% tab title="Android Huawei" %}
{% hint style="warning" %}
If you work with SDK version 3.5.0 and above, and you want to use Huawei Ad ID, you need to add [https://github.com/devtodev-analytics/package\_Huawei.git](https://github.com/devtodev-analytics/package_Huawei). When developing and publishing apps for kids [(COPPA)](https://www.ftc.gov/tips-advice/business-center/privacy-and-security/children's-privacy), you do not need [https://github.com/devtodev-analytics/package\_Huawei.git](https://github.com/devtodev-analytics/package_Huawei). Read more about working with [COPPA](https://www.ftc.gov/tips-advice/business-center/privacy-and-security/children's-privacy) in the [COPPA section](#apps-targeted-at-children).
{% endhint %}
{% endtab %}
{% endtabs %}

{% hint style="info" %}
You can pick a specific SDK version by adding # and a version number at the end of the URL, for example: <https://github.com/devtodev-analytics/package_Analytics.git>#v3.3.2&#x20;
{% endhint %}

### **Integration by importing&#x20;*****unitypackage***

1. Download *DTDAnalytics.unitypackage* from <http://github.com/devtodev-analytics/Unity-sdk-3.0/releases/latest>.
2. In the Unity Editor menu, open Assets → Import Package → Custom Package.
3. Select the DTDAnalytics.unitypackage that you have just downloaded.
4. Click Import.&#x20;
5. For Android projects, import an identification package:

{% tabs %}
{% tab title="Android Google" %}
{% hint style="warning" %}
If you work with SDK version 3.5.0 and above, and you want to use Google Ad ID, you need to import the DTDGoogle.*unitypackage*. When developing and publishing apps for kids [(COPPA)](https://www.ftc.gov/tips-advice/business-center/privacy-and-security/children's-privacy), you do not need the DTDGoogle.*unitypackage*. Read more about working with [COPPA](https://www.ftc.gov/tips-advice/business-center/privacy-and-security/children's-privacy) in the [COPPA section](#apps-targeted-at-children).
{% endhint %}
{% endtab %}

{% tab title="Android Huawei" %}
{% hint style="warning" %}
If you work with SDK version 3.5.0 and above, and you want to use Huawei Ad ID, you need to import the DTDHuawei.*unitypackage*. When developing and publishing apps for kids [(COPPA)](https://www.ftc.gov/tips-advice/business-center/privacy-and-security/children's-privacy), you do not need the DTDHuawei.*unitypackage*. Read more about working with [COPPA](https://www.ftc.gov/tips-advice/business-center/privacy-and-security/children's-privacy) in the [COPPA section](#apps-targeted-at-children).
{% endhint %}
{% endtab %}
{% endtabs %}

## SDK Initialization

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("Android App ID");
#elif UNITY_IOS
        DTDAnalytics.Initialize("iOS App ID");
#elif UNITY_WEBGL
        DTDAnalytics.Initialize("Web App ID");
#elif UNITY_STANDALONE_WIN
        DTDAnalytics.Initialize("Windows App ID");
#elif UNITY_STANDALONE_OSX
        DTDAnalytics.Initialize("OSX App ID");
#elif UNITY_WSA
        DTDAnalytics.Initialize("UWP App ID");
#endif
    }
}
```

You can find the `App ID` in the settings of the respective app in devtodev (Settings → SDK → Integration → [Credentials](https://docs.devtodev.com/reports-and-functionality/project-related-reports-and-fuctionality/settings#integration)).&#x20;

For [Cross-platform type projects](https://docs.devtodev.com/getting-started/cross-platform-application) use `App ID + Platform ID` .

**`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>. Use <em><strong><code>DTDLogLevel.No</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.No,
    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.

### Universal Windows Platform (WSA) <a href="#universal-windows-platform-wsa" id="universal-windows-platform-wsa"></a>

![](https://2105883905-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LnGcP_ZeRJ1ipj9O8dF%2Fuploads%2Fy3G8b25He7RXMLHEVHpg%2Fimage.png?alt=media\&token=ffd995a4-ad26-4fcb-972c-a385ed12e0f1)

{% hint style="info" %}
In the version 2019.2, the Executable Only option was added to the Build Type. This option is incompatible with the SDK because the SDK needs access to the project file in order to register the **`DevToDev.Background`** service.
{% endhint %}

### Android

{% hint style="warning" %}
To resolve external android dependencies, you need to use [External Dependency Manager for Unity](https://github.com/googlesamples/unity-jar-resolver/releases/latest).
{% endhint %}

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.**
```

### Huawei

1. Select your project in <https://developer.huawei.com/consumer/en/service/josp/agc/index.html#/myProject> \
   In the “General information” section find “App information” and download the “agconnect-services.json“ file.<br>

   <figure><img src="https://2105883905-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LnGcP_ZeRJ1ipj9O8dF%2Fuploads%2FqUqgOTfuCZwWYkW0UpWq%2Fimage.png?alt=media&#x26;token=38bc7e64-dd51-4dd2-9766-a7c9f21e8157" alt=""><figcaption></figcaption></figure>

2. If you imported the DTDGoogle package, delete the imported files Assets\Plugins\DevToDev\Android\DTDGoogleAndroid.dll and Assets\DevToDev\Analytics\Editor\GoogleDependencies.xml<br>

   <figure><img src="https://2105883905-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LnGcP_ZeRJ1ipj9O8dF%2Fuploads%2FRH7aYcucb6MmizkJCVtt%2Fimage.png?alt=media&#x26;token=c4d7c7cc-8a64-48b0-8433-61b707a36b2b" alt=""><figcaption></figcaption></figure>

3. Import the DTDHuawei.unitypackage manually from [GitHub](https://github.com/devtodev-analytics/Unity-sdk-3.0) or use Unity Package Manager with [DTDHuawei package](https://github.com/devtodev-analytics/package_Huawei). <br>

   <figure><img src="https://lh6.googleusercontent.com/fMoVg2IVKla7MwYlg96evCSHHjidhYGIqw4Y-_JQCx6FVs0tH4JTGNOeyCkDqm1LAjpqtP6w80C859f-VFlzFoi8istEzUW_mR_6mKC2DLF1_oYeO6dXYSFNqH9Ca_iNE88hpPPKVuDxQ0QxiFwMG8o" alt=""><figcaption></figcaption></figure>

4. In the assets/Plugins/Android/ folder create a settingsTemplate.gradle file with the following content:

   ```groovy
   import java.nio.file.Files

   static void enableJetifier(Project project) {
       project.ext['android.useAndroidX'] = true
       project.ext['android.enableJetifier'] = true
   }

   static void addBuildscript(Project project) {
       project.buildscript {
           repositories {
               maven { url 'https://plugins.gradle.org/m2/' } 
               maven { url 'https://developer.huawei.com/repo/' }
           }

           dependencies {
               classpath 'com.huawei.agconnect:agcp:1.6.5.300'
           }
       }
   }

   static void applyPlugins(Project project) {
       if (project.name != 'launcher') return

       project.afterEvaluate {
           it.apply plugin: 'com.huawei.agconnect'
       }
   }

   static void copyAppGalleryJson(Project project) {
       if (project.name != 'launcher') return

       def destinationFile = new File("${project.rootDir}/launcher/agconnect-services.json")
       if (destinationFile.exists()) return

       def sourceFile = new File("${project.rootDir}/unityLibrary/devtodev.plugin/agconnect-services.json")
       Files.copy(sourceFile.toPath(), destinationFile.toPath())
   }

   gradle.rootProject {
       it.afterEvaluate {
           it.allprojects {
               enableJetifier(it)
               addBuildscript(it)
               applyPlugins(it)
               copyAppGalleryJson(it)
           }
       }
   }

   include ':launcher', ':unityLibrary'
   **INCLUDES**
   ```

5. Open Window → devtodev and select Create android plugin folder

   <figure><img src="https://lh5.googleusercontent.com/R-JwAN2Ab7RoBNmuHyP2DQpvdCF3g6rT3GYO72SlkxEIU9CgZJ90PisLsnuONnUHyTU7WB5dEQp0WbPyDcOWJ66XO-vLjs9C9lp2jKUGREJFAG4-bRQFJmhNZNFj038BbIRZiJXsP3-z25eiezwLfC4" alt=""><figcaption></figcaption></figure>

6. Copy the “agconnect-services.json“ file to the Assets\Plugins\Android\devtodev.plugin folder

   <figure><img src="https://lh4.googleusercontent.com/Mge1D3wgKw59xobvI-alRU6Hq9nTqWU5LZru0F2XnJHnRMdDQu8zQCyTH9Nv-rjsg6dp_r7gPkHxuD3D2whgnySRJAqLLsN0GF6C6tvuZv9WH4SJm4XKQTqKBOXu_u1LEs2BUyPj9wYiR5wm_gDTx3w" alt=""><figcaption></figcaption></figure>

7. Open Assets → External Dependency Manager → Android ResolverAssets → External Dependency Manager → Android Resolver and click Resolve<br>

   <figure><img src="https://lh5.googleusercontent.com/e0ZAoxCh5Nq11UljtopTU6pu19Tm5ECJE0bsem-Y3rC1MQE8slj1fvh752nToW6OKy60HAs_vHuY_b1ssLWy5lNe7OQqiwkKbbFOvWFRaYkkVLOpyvPk_39gm9jsgLWRHneV5JLqTvNLgW31GOUh5EY" alt=""><figcaption></figcaption></figure>

8. &#x20;To [proguard](https://docs.unity3d.com/Manual/android-gradle-overview.html) add the following rule:

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

### 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**:*\
**project.AddFrameworkToProject(targetGuid, "AppTrackingTransparency.framework", true);**
{% endhint %}

#### iOS SDK Signature and Privacy Manifest

At [WWDC23](https://developer.apple.com/videos/play/wwdc2023/10060/) Apple introduced new privacy manifests and xcframework signature. More information about it can be found [here](https://docs.devtodev.com/integration/integration-of-sdk-v2/ios#privacy-manifest).

## **Apps targeted at children**

When developing and publishing apps targeted at children under 13 years old, you need to ensure special conditions for data processing. Any mobile app aimed at children or intended for users in a region with strict regulations on child online protection, must comply with current laws.

{% hint style="info" %}
Please study the following requirements:

* USA: [Children’s Online Privacy Protection Act (COPPA)](https://www.ftc.gov/tips-advice/business-center/privacy-and-security/children's-privacy)&#x20;
* EU: [General Data Protection Regulation (GDPR) Article 8](https://gdpr-info.eu/art-8-gdpr/)
  {% endhint %}

If your app has to comply with the legal requirements (COPPA), use the following recommendations:

{% tabs %}
{% tab title="Apple" %}

* Implement the `CoppaControlEnable` method. The method disables collection of ad IDs and vendor IDs (IDFA, IDFV).
* To comply with [Apple’s guidelines](https://developer.apple.com/news/?id=091202019a), remove from Xcode project:
  1. `AppTrackingTransparency.framework` and all the links pointing to it.
  2. `AdSupport.framework` and all the links pointing to it.
  3. Depending on the SDK version:
     1. 3.9.0 and older:\
        Set `IS_COPPA_ENABLED = true` in `DTDPostProcessAnalytics.cs` (`Assets/DevToDev/Analytics/Editor`)
     2. 3.9.1 and newer:\
        Add a new [Scripting Define Symbols](https://docs.unity3d.com/6000.0/Documentation/Manual/custom-scripting-symbols.html): DTD\_COPPA
        {% endtab %}

{% tab title="Android" %}

1. Implement the `CoppaControlEnable` method. The method disables collection of ad IDs and vendor IDs.
2. If you are using DTDGoogle or DTDHuawei from Unity Package Manager, disable it.&#x20;
3. If you are using DTDGoogle.unitypackage, remove the following files:

   ```
   Assets\DevToDev\Analytics\Editor\GoogleDependencies.xml
   Assets\Plugins\DevToDev\Android\DTDGoogleAndroid.aar
   ```
4. If you are using DTDHuawei.unitypackage, remove the following files:

   ```
   Assets\Plugins\Android\settingsTemplate.gradle
   Assets\Plugins\DevToDev\Android\DTDHuaweiAndroid.aar
   Assets\DevToDev\Analytics\Editor\HuaweiDependencies.xml
   ```

{% endtab %}
{% endtabs %}

{% hint style="warning" %}
Call the `CoppaControlEnable` method before SDK initialization. If the method was not called, the SDK will work as before.
{% endhint %}

```csharp
DTDAnalytics.CoppaControlEnable();
DTDAnalytics.Initialize("App ID", config);
```
