diff --git a/Mixpanel/Controller.cs b/Mixpanel/Controller.cs index bd0232a..e38ae04 100644 --- a/Mixpanel/Controller.cs +++ b/Mixpanel/Controller.cs @@ -88,11 +88,33 @@ void OnApplicationPause(bool pauseStatus) private void Start() { MigrateFrom1To2(); - StartCoroutine(TrackIntegrationEvent()); + MixpanelTracking(); + CheckForSurvey(); Mixpanel.Log($"Mixpanel Component Started"); StartCoroutine(WaitAndFlush()); } + private void MixpanelTracking() + { + if (!MixpanelStorage.HasIntegratedLibrary) { + StartCoroutine(SendHttpEvent("Integration", "85053bf24bba75239b16a601d9387e17", MixpanelSettings.Instance.Token, "")); + MixpanelStorage.HasIntegratedLibrary = true; + } + if (Debug.isDebugBuild) { + StartCoroutine(SendHttpEvent("SDK Debug Launch", "metrics-1", MixpanelSettings.Instance.Token, $",\"Debug Launch Count\":{MixpanelStorage.MPDebugInitCount}")); + } + } + private void CheckForSurvey() + { + if (Debug.isDebugBuild) { + MixpanelStorage.MPDebugInitCount += 1; + } + if (MixpanelStorage.MPDebugInitCount == 10 || MixpanelStorage.MPDebugInitCount == 20 || MixpanelStorage.MPDebugInitCount == 30) { + Mixpanel.Log("*** Hi, Zihe & Jared here, please give feedback or tell us about the Mixpanel developer experience! Open -> https://www.mixpanel.com/devnps ***"); + StartCoroutine(SendHttpEvent("Dev NPS Survey Logged", "metrics-1", MixpanelSettings.Instance.Token, $",\"Survey Shown Count\":{MixpanelStorage.MPDebugInitCount / 10}")); + } + } + private IEnumerator WaitAndFlush() { while (true) @@ -150,30 +172,16 @@ private IEnumerator SendData(MixpanelStorage.FlushType flushType) } } - private IEnumerator TrackIntegrationEvent() + private IEnumerator SendHttpEvent(string eventName, string apiToken, string distinctId, string properties) { - if (MixpanelStorage.HasIntegratedLibrary) { - yield break; - } - string body = "{\"event\":\"Integration\",\"properties\":{\"token\":\"85053bf24bba75239b16a601d9387e17\",\"mp_lib\":\"unity\",\"distinct_id\":\"" + MixpanelSettings.Instance.Token +"\"}}"; + string body = "{\"event\":\"" + eventName + "\",\"properties\":{\"token\":\"" + + apiToken + "\",\"mp_lib\":\"unity\",\"$lib_version\":\""+ Mixpanel.MixpanelUnityVersion + "\",\"distinct_id\":\"" + distinctId + "\"" + properties + "}}"; string payload = Convert.ToBase64String(Encoding.UTF8.GetBytes(body)); WWWForm form = new WWWForm(); form.AddField("data", payload); using (UnityWebRequest request = UnityWebRequest.Post(Config.TrackUrl, form)) { yield return request.SendWebRequest(); - #if UNITY_2020_1_OR_NEWER - if (request.result != UnityWebRequest.Result.Success) - #else - if (request.isHttpError || request.isNetworkError) - #endif - { - Mixpanel.Log(request.error); - } - else - { - MixpanelStorage.HasIntegratedLibrary = true; - } } } diff --git a/Mixpanel/Storage.cs b/Mixpanel/Storage.cs index 58b5f47..568b4a9 100644 --- a/Mixpanel/Storage.cs +++ b/Mixpanel/Storage.cs @@ -50,6 +50,30 @@ internal static bool HasIntegratedLibrary #endregion + #region MPDebugInitCount + + private const string MPDebugInitCountName = "Mixpanel.MPDebugInitCount"; + + internal static int MPDebugInitCount + { + get => PreferencesSource.GetInt(MPDebugInitCountName, 0); + set => PreferencesSource.SetInt(MPDebugInitCountName, value); + } + + #endregion + + #region HasTrackedFirstSDKDebugLaunch + + private const string HasTrackedFirstSDKDebugLaunchName = "Mixpanel.HasTrackedFirstSDKDebugLaunch"; + + internal static bool HasTrackedFirstSDKDebugLaunch + { + get => Convert.ToBoolean(PreferencesSource.GetInt(HasTrackedFirstSDKDebugLaunchName, 0)); + set => PreferencesSource.SetInt(HasTrackedFirstSDKDebugLaunchName, Convert.ToInt32(value)); + } + + #endregion + #region DistinctId private const string DistinctIdName = "Mixpanel.DistinctId";