Reputation: 45
I've upgraded Google Analytics Unity SDK from version 3 to 4. All of tracking events & screens are showing up but custom dimensions only works for iOS. While custom dimensions are working just fine for both Android and iOS in SDK version 3.
First, I declare public GoogleAnalytics variable and assign configurations:
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;
}
Then a method is call by other class to fire Google Analytics event:
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);
}
The same also applied to screen:
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 will return custom dimensions index according to the string, for example UserId
. InfoDataCollection adds addition custom dimension to all event & screen:
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);
}
}
Event and screen data are appearing in report except when I applied custom dimension to it with data segment of Android traffic
and iOS traffic
there are only data for iOS while Android traffic show 0 record.
And Both Android & iOS are using the same code.
What is the cause of this missing data? Any idea or advise is appreciated. Last resort I had in mind is to fallback to version 3, which was already deprecated.
Upvotes: 1
Views: 429
Reputation: 45
I found an answer. It seems that Google Analytics SDK for Unity lack implementation in GoogleAnalyticsAndroidV4.cs methods.
To track custom dimension in event you have to modify the code to:
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);
}
The same also apply to screen as well.
Upvotes: 2