-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalyticsServiceSource.cs
More file actions
115 lines (95 loc) · 3.91 KB
/
Copy pathAnalyticsServiceSource.cs
File metadata and controls
115 lines (95 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
namespace UniGame.Runtime.Analytics
{
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using FpsService;
using Interfaces;
using R3;
using Runtime;
using UniGame.AddressableTools.Runtime;
using UniGame.Context.Runtime;
using UniGame.Core.Runtime;
using UnityEngine;
/// <summary>
/// game analytics service
/// </summary>
[CreateAssetMenu(menuName = "Game/Services/Analytics/Analytics Service Source")]
public class AnalyticsServiceSource : DataSourceAsset
{
public AddressableValue<AnalyticsConfigurationAsset> configurationReference;
protected override async UniTask<IContext> OnRegisterAsync(IContext context)
{
var lifeTime = context.LifeTime;
var configurationAsset = await configurationReference
.reference
.LoadAssetTaskAsync(lifeTime, true);
var configuration = configurationAsset.configuration;
var channel = AnalyticsMessageChannel.DefaultChannel;
var service = CreateService(lifeTime);
var platformId = ResolvePlatformId(configuration);
channel.ToObservable()
.Subscribe(service.TrackEvent)
.AddTo(lifeTime);
var fpsService = new FpsService.FpsService();
context.Publish(fpsService);
context.Publish<IFpsService>(fpsService);
context.Publish(channel);
context.Publish(service);
if (!configuration.isEnabled || !configuration.IsPlatformAllowed(platformId))
return context;
await RegisterAdapters(service, configuration, lifeTime, platformId);
foreach (var messageHandler in configuration.messageHandlers)
service.RegisterMessageHandler(messageHandler);
return context;
}
private async UniTask RegisterAdapters(
IAnalyticsService service,
AnalyticsConfiguration configuration,
ILifeTime lifeTime,
string platformId)
{
var initializeTasks = new List<UniTask>();
foreach (var analyticsItem in configuration.analytics)
{
if (!analyticsItem.isEnabled || analyticsItem.adapter == null)
continue;
if (!analyticsItem.IsPlatformAllowed(platformId))
continue;
var adapterTask = InitializeAdapter(analyticsItem.adapter, service, lifeTime);
initializeTasks.Add(adapterTask);
}
await UniTask.WhenAll(initializeTasks);
}
private async UniTask InitializeAdapter(IAnalyticsAdapter adapter, IAnalyticsService service,ILifeTime lifeTime)
{
try
{
adapter.AddTo(lifeTime);
await adapter.InitializeAsync();
service.RegisterAdapter(adapter);
}
catch (System.Exception exception)
{
Debug.LogException(exception);
}
}
private GameAnalyticsService CreateService(
ILifeTime lifeTime)
{
var service = new GameAnalyticsService().AddTo(lifeTime);
return service;
}
private static string ResolvePlatformId(AnalyticsConfiguration configuration)
{
if (!string.IsNullOrWhiteSpace(configuration.platformIdOverride))
return configuration.platformIdOverride.ToLowerInvariant();
return Application.platform switch
{
RuntimePlatform.Android => "android",
RuntimePlatform.IPhonePlayer => "ios",
RuntimePlatform.WebGLPlayer => "webgl",
_ => Application.platform.ToString().ToLowerInvariant()
};
}
}
}