101 lines
3.2 KiB
C#
101 lines
3.2 KiB
C#
using System;
|
|
using Cysharp.Threading.Tasks;
|
|
using Unity.Services.Analytics;
|
|
using Unity.Services.Core;
|
|
using UnityEngine;
|
|
|
|
namespace DanieleMarotta.RiversongCodeShowcase
|
|
{
|
|
public class UnityAnalyticsService : IAnalyticsService
|
|
{
|
|
private bool _canRecord;
|
|
|
|
public async UniTask InitializeAsync()
|
|
{
|
|
if (_canRecord) return;
|
|
|
|
try
|
|
{
|
|
if (UnityServices.State == ServicesInitializationState.Uninitialized) await UnityServices.InitializeAsync();
|
|
|
|
AnalyticsService.Instance.StartDataCollection();
|
|
|
|
_canRecord = true;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Debug.LogError($"Failed to initialize analytics. Analytics will be disabled.\n{exception}");
|
|
}
|
|
}
|
|
|
|
public void RecordSessionStarted(string sessionId)
|
|
{
|
|
if (!_canRecord) return;
|
|
|
|
var analyticsEvent = new SessionStartedAnalyticsEvent { SessionId = sessionId };
|
|
|
|
AnalyticsService.Instance.RecordEvent(analyticsEvent);
|
|
}
|
|
|
|
public void RecordBuildingConstructionCompleted(string sessionId, string buildingName)
|
|
{
|
|
if (!_canRecord) return;
|
|
|
|
var analyticsEvent = new BuildingConstructionCompletedAnalyticsEvent
|
|
{
|
|
SessionId = sessionId,
|
|
BuildingName = buildingName
|
|
};
|
|
|
|
AnalyticsService.Instance.RecordEvent(analyticsEvent);
|
|
}
|
|
|
|
public void RecordHouseUpgraded(string sessionId, string buildingName, int fromTier, int toTier)
|
|
{
|
|
if (!_canRecord) return;
|
|
|
|
var analyticsEvent = new HouseUpgradedAnalyticsEvent
|
|
{
|
|
SessionId = sessionId,
|
|
BuildingName = buildingName,
|
|
FromTier = fromTier,
|
|
ToTier = toTier
|
|
};
|
|
|
|
AnalyticsService.Instance.RecordEvent(analyticsEvent);
|
|
}
|
|
|
|
public void RecordHeartbeat(string sessionId, int heartbeatIndex, float playtimeSeconds, int totalBuildingsPlaced, int population)
|
|
{
|
|
if (!_canRecord) return;
|
|
|
|
var analyticsEvent = new SessionHeartbeatAnalyticsEvent
|
|
{
|
|
SessionId = sessionId,
|
|
HeartbeatIndex = heartbeatIndex,
|
|
PlaytimeSeconds = playtimeSeconds,
|
|
TotalBuildingsPlaced = totalBuildingsPlaced,
|
|
Population = population
|
|
};
|
|
|
|
AnalyticsService.Instance.RecordEvent(analyticsEvent);
|
|
}
|
|
|
|
public void RecordDemoCompleted(string sessionId, float playtimeSeconds, int totalWeeks, int population, float happiness)
|
|
{
|
|
if (!_canRecord) return;
|
|
|
|
var analyticsEvent = new DemoCompletedAnalyticsEvent
|
|
{
|
|
SessionId = sessionId,
|
|
PlaytimeSeconds = playtimeSeconds,
|
|
TotalWeeks = totalWeeks,
|
|
Population = population,
|
|
Happiness = Mathf.Clamp01(happiness)
|
|
};
|
|
|
|
AnalyticsService.Instance.RecordEvent(analyticsEvent);
|
|
AnalyticsService.Instance.Flush();
|
|
}
|
|
}
|
|
} |