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

Google anlytics - Custom dimensions not work for Android

我已将 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;
    }

然后其他 class 调用一个方法来触发 Google 分析事件:

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会return根据字符串自定义维度索引,例如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 trafficiOS traffic 的数据段对其应用自定义维度时只有 iOS 的数据,而 [=36] =]流量显示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);
}

同样适用于屏幕。