# Guru Unity Analytics

GuruAnalyticsLib 的 Unity 插件库

---

## Change Logs

### 1.8.4
- 优化Android 端 Worker 调用逻辑, 重启 Worker 有助于让打点数据更准确

### 1.8.3
- 修复 fg 打点上报时长不正确的问题

### 1.8.2
- 修复参数类型转换的BUG, param数据转换为JSON对象

### 1.8.1
- 修复自打点浮点参数精度问题
- 添加太极020数值设置接口

### 1.7.5
- 删除 `androidx.appcompat:appcompat` 库依赖


</br>

---

## Document

- 项目整合插件后, **请一定要在各插件的初始化后上报各相关ID**:

- 相关接口如下

- ### UID

  ```C#

    // ---- 需要等待中台初始化后上报: 
    // 上报中台返回的用户ID
    string uid = IPMConfig.IPM_UID 
    GuruAnalytics.SetUid(uid);

  ```

- ### DeviceID
  ```C#
    // 上报设备ID
    string deviceId = IPMConfig.IPM_DEVICE_ID
    GuruAnalytics.SetDeviceId(DeviceID);

  ```

- ### FirebaseID
  ```C#

    // ---- 需要Firebase Analytic 初始化后, 异步获取对应的ID:
    private static async void InitFirebaseAnalytics()
    {
        Debug.Log($"---[ANA] IPM UID: {IPMConfig.IPM_UID}");
        
        var task = FirebaseAnalytics.GetAnalyticsInstanceIdAsync();
        await task;
        if (task.IsCompleted)
        {
            var fid = task.Result;
            if (!string.IsNullOrEmpty(fid))
            {
                Debug.Log($"---[ANA] Firebase ID: {fid}");
                GuruAnalytics.SetFirebaseId(fid);
            }
        }
        else
        {
            Debug.LogError("---- Get Firebase Analytics Instance Fail");
        }
    }


  ```

- ### AdjustID

    ```C#

    // ---- Adjust 启动后调用: 
    string adjustID = Adjust.getAdid();
    GuruAnalytics.SetAdjustId(adjustID);    


    ```

- ### AdID

    ```C#
    string adId = "";
    Adjust.getGoogleAdId(id =>
    {
        Debug.Log($"---- [ADJ] ADId: {id}");
        adId = id;
        GuruAnalytics.SetAdId(id);
    });


    ```

- 上报用户属性:

    ```C#

        string item_category = "main";
        int level = 7;

        GuruAnalytics.SetUserProperty("item_category", item_category);
        GuruAnalytics.SetUserProperty("level", level.ToString());

    ```

- 上报视图名称

    ```C#

        string screenName = "MainView";
        GuruAnalytics.SetScreen(screenName);

    ```


- 上报自定义打点:

    ```C#

        string eventName = "user_get_coin";
        Dictionary<string, dynamic> data = new Dictionary<string, dynamic>()
        {
            { "level", 7 },
            { "user_coin", 105L },
            { "win_rate", 21.25f },
            { "b_level", 7 },
            { "result", "retry" }
        };
        GuruAnalytics.LogEvent(eventName, data);

    ```
---

</br>

## 依赖台配置说明

本项目已开始使用 `ExternalDependencyManager` 简称 `EDM` 来解决各种库的依赖问题

详细配置可见: [Dependencies.xml](Editor/Dependencies.xml)

IOS 项目注意配置如下图:

--> 取消勾选 **Link frameworks statically**

![](Editor/imgs/sc01.png)


### Android 项目配置:

于主菜单 `BuildSettings/PlayerSettings/PubishSettings:`

开启如下选项:

- [x] Custom Main Gradle Template
- [x] Custom Properties Gradle Template

之后会在项目的 `Plugins/Android`内生成对应的文件.

(A) 修改 `gradleTemplate.properties`

添加一下内容支持 `AndroidX`

```java
org.gradle.jvmargs=-Xmx**JVM_HEAP_SIZE**M
org.gradle.parallel=true
android.enableR8=false
unityStreamingAssets=.unity3d**STREAMING_ASSETS**
android.useAndroidX=true
android.enableJetifier=true
**ADDITIONAL_PROPERTIES**
```

(B) 修改 `mainTemplate.gradle`

于 `dependency` 内添加如下依赖 (目前会自动添加, 无需手动添加)

```java

dependencies {
    ...

    implementation 'androidx.core:core:1.7.0'
    compile 'com.mapzen:on-the-road:0.8.1'

    // basicDependencies
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.jakewharton.timber:timber:4.7.1'
    implementation 'com.google.code.gson:gson:2.8.5'
    // roomDependencies
    implementation 'androidx.room:room-runtime:2.4.3'
    implementation 'androidx.room:room-rxjava2:2.4.3'
    // retrofitDependencies
    implementation 'com.squareup.retrofit2:retrofit:2.7.1'
    implementation 'com.squareup.retrofit2:converter-gson:2.7.1'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.7.1'
    // okhttpDependencies
    implementation 'androidx.work:work-runtime:2.7.1'
    implementation 'androidx.work:work-runtime-ktx:2.7.1'
    implementation 'androidx.work:work-rxjava2:2.7.1'
    // process
    implementation 'androidx.lifecycle:lifecycle-process:2.4.0'
    // okhttp3
    implementation 'com.squareup.okhttp3:okhttp:4.9.3'
    
    ...
}

```

最低 `minTarget` 设置为 **21**

(D) 修改 `proguard-user.txt` 文件, 在最后追加此插件的相关代码

若项目使用了 ProGuard 压缩混淆, 需要修改此文件, 否则可能造成JAVA类无法被找到

```java

...

-keep class com.guru.** { *; }
-keep class guru.core.** { *; }

```


---

</br>


## 示例项目

- 示例项目位于 [~Sample](~Sample) 目录内. 详见 [CuruAnalyticsDemo.cs](~Sample/CuruAnalyticsDemo.cs)
- 示例借用了 BallSortPuzzle 的 `AppID` 和 `BundleID`



