googleanalytics / google-analytics-plugin-for-unity

Google Analytics plugin for the Unity game creation system

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GoogleAnalyticsV4.unitypackage does not log custom dimensions or metrics on Android

sdetwiler opened this issue · comments

The GoogleAnalyticsAndroidV4.cs file included in GoogleAnalyticsV4.unitypackage contains an implementation for LogEvent() that does not log custom dimensions or metrics.

Possible patch that works for us locally:

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);
  }

Thank you for this patch.

But I got an error with it :
AndroidJavaException: java.lang.NoSuchMethodError: no non-static method "Lcom/google/android/gms/analytics/HitBuilders$EventBuilder;.SetCustomDimension(ILjava/lang/String;)Ljava/lang/Object;"
Do you have any idea how to fix it ?

I had the same problem. By digging into the source code of Google Analytics, I found that setCustomMetric takes (int, float) as apposed to (int, string). So, you should convert the second parameter to float before calling setCustomMetric