Skip to content

Commit

Permalink
Add Debug tracking and Dev NPS Survey (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
zihejia authored Apr 30, 2022
1 parent 0c55ccc commit dbb71bf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 18 deletions.
44 changes: 26 additions & 18 deletions Mixpanel/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions Mixpanel/Storage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit dbb71bf

Please sign in to comment.