Skip to content

Commit

Permalink
Updated data paths and fixed warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
GazRobinson committed Oct 31, 2023
1 parent cd58345 commit 5109dd0
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 45 deletions.
50 changes: 39 additions & 11 deletions Runtime/Implementations/AbertayAnalytics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
using UnityEngine;
using Newtonsoft.Json;
using System.Globalization;
using UnityEditor;
using UnityEngine.SceneManagement;

namespace Abertay.Analytics
{
public class AbertayAnalytics : IAnalytics
{
private string m_UserID = "NULL";
private string m_Environment = "";
System.Action initCallback = null;

/// <summary>
/// The environment name can be used to specify a different file name
Expand All @@ -25,10 +26,14 @@ void IAnalytics.Initialise(Action callback, string environmentName)
m_Environment = environmentName;
if (m_UserID.Length < 1)
{
m_UserID = System.Guid.NewGuid().ToString();
PlayerPrefs.SetString("UserID", m_UserID.ToString());
SetUserID(System.Guid.NewGuid().ToString());
}
CreateDirectory(Application.persistentDataPath + "/Analytics");
#if UNITY_EDITOR
string path = Application.dataPath;
#else
string path = Application.dataPath + "/..";
#endif
CreateDirectory(path + "/Analytics/Events");
callback();
}

Expand All @@ -50,17 +55,25 @@ public bool CreateDirectory(string dir)
/// <param name="environmentName"></param>
void IAnalytics.InitialiseWithCustomID(string userID, Action callback, string environmentName)
{
m_UserID = userID;
if (environmentName.Length > 0)
m_Environment = environmentName;
if (!(m_UserID.Length > 0)) {
if (!(userID.Length > 0)) {
Debug.LogError("Trying to use a custom ID of an empty string. Setting to NULL");
m_UserID = "NULL";
SetUserID("NULL");
}
else
{
SetUserID(userID);
}
CreateDirectory(Application.persistentDataPath + "/Analytics");
#if UNITY_EDITOR
string path = Application.dataPath + "/Analytics/Events";
#else
string path = Application.dataPath + "/../Analytics/Events";
#endif
CreateDirectory(path);
callback();
}

//TODO: This could be way more efficient
public void SendCustomEvent(string eventName, Dictionary<string, object> parameters, float GA_Value)
{
//Build custom event structure
Expand All @@ -75,8 +88,14 @@ public void SendCustomEvent(string eventName, Dictionary<string, object> paramet
customEvent.GA_Value = GA_Value;
customEvent.eventParams = parameters;

string fileName = "/Analytics/Event" + (m_Environment.Length > 0 ? ("_" + m_Environment):("")) + ".json";
string path = Application.persistentDataPath + fileName;
string fileName = "/Analytics/Events/Events" + (m_Environment.Length > 0 ? ("_" + m_Environment):("")) + ".json";

#if UNITY_EDITOR
string path = Application.dataPath;
#else
string path = Application.dataPath + "/..";
#endif
path += fileName;

//Load all events currently saved to disk
List<CustomEvent> events = new List<CustomEvent>();
Expand All @@ -102,6 +121,15 @@ public void SendCustomEvent(string eventName, Dictionary<string, object> paramet
Debug.LogError("Failed to save JSON data to: " + path);
Debug.LogError("Error " + e.Message);
}
#if UNITY_EDITOR
AssetDatabase.Refresh();
#endif
}

public void SetUserID(string userId)
{
m_UserID = userId;
PlayerPrefs.SetString("UserID", m_UserID);
}
#if GAMEANALYTICS
void IAnalytics.SendCustomEvent(string eventName, Dictionary<string, object> parameters, float GA_Value = 0.0f)
Expand Down
60 changes: 28 additions & 32 deletions Runtime/Implementations/UnityAnalytics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,24 @@
using Unity.Services.Core.Environments;
using Unity.Services.Analytics;
using System;
using Codice.CM.Client.Differences.Merge;

namespace Abertay.Analytics
{
public class UnityAnalytics : IAnalytics
{
async void IAnalytics.Initialise(Action callback, string environmentName)
{
try
{
Debug.Log("Initialising Unity Analytics");
InitializationOptions options = new InitializationOptions();
Debug.Log("Initialising Unity Analytics");
InitializationOptions options = new InitializationOptions();

if(environmentName.Length > 0)
options.SetEnvironmentName(environmentName);
if(environmentName.Length > 0)
options.SetEnvironmentName(environmentName);

await UnityServices.InitializeAsync(options);
List<string> consentIdentifiers = await AnalyticsService.Instance.CheckForRequiredConsents();
}
catch (ConsentCheckException e)
{
//TODO: actually deal with this...
}
await UnityServices.InitializeAsync(options);

AnalyticsService.Instance.StartDataCollection();


Debug.Log("Unity Analytics initialised");
if (callback != null)
Expand All @@ -37,30 +33,25 @@ async void IAnalytics.Initialise(Action callback, string environmentName)
//TODO: Duplication here isn't great
async void IAnalytics.InitialiseWithCustomID(string userID, Action callback, string environmentName)
{
try
{
Debug.Log("Initialising Unity Analytics w/ Custom ID");
InitializationOptions options = new InitializationOptions();

if (environmentName.Length > 0)
options.SetEnvironmentName(environmentName);
if (userID.Length > 0)
{
options.SetAnalyticsUserId(userID);
}
else
{
Debug.LogWarning("Supplied User ID was empty. Using default Unity ID for this device instead.");
}
Debug.Log("Initialising Unity Analytics w/ Custom ID");
InitializationOptions options = new InitializationOptions();

await UnityServices.InitializeAsync(options);
List<string> consentIdentifiers = await AnalyticsService.Instance.CheckForRequiredConsents();
if (environmentName.Length > 0)
options.SetEnvironmentName(environmentName);
if (userID.Length > 0)
{
SetUserID(userID);
//options.SetAnalyticsUserId(userID);
}
catch (ConsentCheckException e)
else
{
//TODO: actually deal with this...
Debug.LogWarning("Supplied User ID was empty. Using default Unity ID for this device instead.");
}

await UnityServices.InitializeAsync(options);

AnalyticsService.Instance.StartDataCollection();

Debug.Log("Unity Analytics initialised w/ Custom ID");
if (callback != null)
callback();
Expand All @@ -72,6 +63,11 @@ public void SendCustomEvent(string eventName, Dictionary<string, object> paramet
AnalyticsService.Instance.CustomData(eventName, parameters);
AnalyticsService.Instance.Flush(); //Technically don't need to do this...
}

public void SetUserID(string userID)
{
UnityServices.ExternalUserId = userID;
}
#if GAMEANALYTICS
void IAnalytics.SendCustomEvent(string eventName, Dictionary<string, object> parameters, float GA_Value = 0.0f)
{
Expand Down
2 changes: 2 additions & 0 deletions Runtime/Scripts/IAnalytics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public interface IAnalytics
public void Initialise(System.Action callback, string environmentName);
public void InitialiseWithCustomID(string userID, System.Action callback, string environmentName);
public void SendCustomEvent(string eventName, Dictionary<string, object> parameters, float GA_Value);

public void SetUserID(string userID);
#if GAMEANALYTICS
/// <summary>
/// This version of the function call is only necessary when using Game Analytics.
Expand Down
3 changes: 2 additions & 1 deletion Runtime/User/AnalyticsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,14 @@ private void Start()
{
if (!Initialised && m_InitialiseOnStart)
{
Instance.Init();
Instance.Init(m_EnvironmentName);
}
}
public static void Initialise(string environmentName = "")
{
if (!Initialised)
{
Instance.m_EnvironmentName = environmentName;
Instance.Init(environmentName);
}
else
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"unity": "2020.3",
"dependencies": {
"com.unity.nuget.newtonsoft-json": "3.0.2",
"com.unity.services.analytics": "4.2.0"
"com.unity.services.analytics": "5.0.0"
},
"author": {
"name": "Gaz Robinson",
Expand Down

0 comments on commit 5109dd0

Please sign in to comment.