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

时间:2016-07-12 09:57:50

标签: android google-analytics unity5

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

首先,我声明公开的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 trafficiOS traffic的数据段应用自定义维度时,只有iOS数据,而Android流量显示0记录。

Android和&amp; iOS使用相同的代码。

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

1 个答案:

答案 0 :(得分:2)

我找到了答案。似乎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);
}

同样也适用于屏幕。