android - Google 分析 - 自定义维度不适用于 Android

标签 android google-analytics unity-game-engine

我已将 Google Analytics Unity SDK 从第 3 版升级到第 4 版。所有跟踪事件和屏幕都会显示,但自定义维度仅适用于 iOS。虽然自定义尺寸在 SDK 版本 3 中适用于 Android 和 iOS。

首先,我声明公共(public) GoogleAnalytics 变量并分配配置:

public class GoogleAnalyticsAdaptor : MonoBehaviour
{
    public GoogleAnalyticsV4 ga;

    void Start()
    {
        ga = UnityRoot.Instance.gameObject.AddComponent<GoogleAnalyticsV4>();

        ga.IOSTrackingCode = config.IOSTrackingCode;
        ga.androidTrackingCode = config.androidTrackingCode;
        ga.otherTrackingCode = config.otherTrackingCode;
        ga.productName = config.productName;
        ga.bundleIdentifier = config.bundleIdentifier;
        ga.bundleVersion = config.bundleVersion;
        ga.sendLaunchEvent = config.sendLaunchEvent;
        ga.UncaughtExceptionReporting = config.reportUncaughtException;
        ga.dispatchPeriod = 2;
    }

然后其他类调用一个方法来触发 Google Analytics 事件:

public void RecordEvent(AnalyticsEvent e)
{
    var s = new EventHitBuilder();

    // category + action + label + value       
    s.SetEventCategory(e.Category);
    s.SetEventAction(e.Action);

    if(e.Label != null)
        s.SetEventLabel(e.Label);

    if(e.Campaign != null)
        s.SetCampaignName(e.Campaign);

    if (e.Value is int || e.Value is long)
        s.SetEventValue((long)e.Value);

    InfoDataCollection<EventHitBuilder>(s);

    int dimensionIndex;

    if (customDimensionIndexLookup.TryGetValue(dimension.Key, out dimensionIndex))
    {
        s.SetCustomDimension(dimensionIndex, dimension.Value);
    }

    ga.LogEvent(s);
}

同样适用于屏幕:

public void RecordScreen(AnalyticsScreen screen)
{        
    var appViewBuilder = new AppViewHitBuilder();

    appViewBuilder.SetScreenName(screen.Name);
    if (screen.Campaign != null)
        appViewBuilder.SetCampaignName(screen.Campaign);

    InfoDataCollection<AppViewHitBuilder>(appViewBuilder);

    int dimensionIndex;

    if (customDimensionIndexLookup.TryGetValue(dimension.Key, out dimensionIndex))
    {
        appViewBuilder.SetCustomDimension(dimensionIndex, dimension.Value);
    }         

    ga.LogScreen(appViewBuilder);
}

customDimensionIndexLookup.TryGetValue 会根据字符串返回自定义维度索引,例如UserId。 InfoDataCollection 向所有事件和屏幕添加额外的自定义维度:

private void InfoDataCollection<T>(T hitBuilder) where T : HitBuilder<T>
{
    int dimensionIndex;
    if (customDimensionIndexLookup.TryGetValue("UserId", out dimensionIndex) && !string.IsNullOrEmpty(AccountPortal.CurrentAccountID))
    {
        hitBuilder.SetCustomDimension(dimensionIndex, AccountPortal.CurrentAccountID);
    }
}

事件和屏幕数据出现在报告中,除非我使用 Android 流量iOS 流量 的数据段对其应用自定义维度时只有 iOS 的数据,而 Android流量显示0条记录。

Android 和 iOS 都使用相同的代码。

数据丢失的原因是什么?任何想法或建议表示赞赏。我想到的最后一招是回退到已经弃用的版本 3。

最佳答案

我找到了答案。 Google Analytics SDK for Unity 似乎缺少 GoogleAnalyticsAndroidV4.cs 方法中的实现。

要在事件中跟踪自定义维度,您必须将代码修改为:

internal void LogEvent(EventHitBuilder builder) 
{
    AndroidJavaObject eventBuilder = new AndroidJavaObject("com.google.android.gms.analytics.HitBuilders$EventBuilder");
    eventBuilder.Call<AndroidJavaObject>("setCategory", new object[] { builder.GetEventCategory() });
    eventBuilder.Call<AndroidJavaObject>("setAction", new object[] { builder.GetEventAction() });
    eventBuilder.Call<AndroidJavaObject>("setLabel", new object[] { builder.GetEventLabel() });
    eventBuilder.Call<AndroidJavaObject>("setValue", new object[] { builder.GetEventValue() });

    foreach(KeyValuePair<int, string> i in builder.GetCustomDimensions())
    {
        eventBuilder.Call<AndroidJavaObject>("setCustomDimension", new object[] { i.Key, i.Value });
    }

    foreach(KeyValuePair<int, string> i in builder.GetCustomMetrics())
    {
        eventBuilder.Call<AndroidJavaObject>("setCustomMetric", new object[] { i.Key, i.Value });
    }   

    object[] builtEvent = new object[] { eventBuilder.Call<AndroidJavaObject>("build") };
    tracker.Call("send", builtEvent);
}

这同样适用于屏幕。

关于android - Google 分析 - 自定义维度不适用于 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38325583/

相关文章:

android - 四舍五入 ListView 的顶角和底角

android - fragment 向下滑动动画

google-analytics - 自动检查GTM标签

android - Google Analytics for Android 不发送点击

android - Unity - Google Play 游戏服务实时多人游戏关闭 WaitingRoomUI() 方法?

android - Unity3D - 多个 dex 文件定义了为 android (Firebase) 构建时的异常

android - Android 中的断点

android - SQLiteOpenHelper "onCreate"没有被调用? (数据库不存在)

google-analytics - 如何在 Google Analytics 仪表板小部件中仅显示来自特定来源的流量

java - 无法在 API 级别 < 23 处正确检查权限