googleanalytics / google-analytics-plugin-for-unity

Google Analytics plugin for the Unity game creation system
Apache License 2.0
388 stars 140 forks source link

WebGL: measurement protocol not working correctly #156

Open vasyaPP opened 7 years ago

vasyaPP commented 7 years ago

I'm using GA plugin in my WebGL game and discovered that sessions and events do not count correctly. After some research I located to the problem place.

Measurement protocol required cid parameter in url. Currently in https://github.com/googleanalytics/google-analytics-plugin-for-unity/blob/master/source/Plugins/GoogleAnalyticsV4/GoogleAnalyticsMPV3.cs#L62 it's defined as clientId = SystemInfo.deviceUniqueIdentifier. But in webgl builds SystemInfo.deviceUniqueIdentifier always equals "n / a".

I fixed it by creating a random guid for clientId class member.

if UNITY_WEBGL

clientId = PlayerPrefs.GetString(WebglUidKey, string.Empty);

if (clientId == string.Empty) {

  clientId = Guid.NewGuid().ToString();

  PlayerPrefs.SetString(WebglUidKey, clientId);

}

else

clientId = SystemInfo.deviceUniqueIdentifier;

endif