-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathAudienceUnityHooks.cs
More file actions
90 lines (76 loc) · 4.02 KB
/
Copy pathAudienceUnityHooks.cs
File metadata and controls
90 lines (76 loc) · 4.02 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
#nullable enable
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Immutable.Audience.Unity.Mobile;
using UnityEngine;
namespace Immutable.Audience.Unity
{
internal static class AudienceUnityHooks
{
// Captured at SubsystemRegistration so the Install Referrer provider
// (called from ImmutableAudience.Init on whatever thread the user
// invokes it from) can read it without touching
// Application.persistentDataPath off the main thread.
private static string? _persistentDataPath;
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void Install()
{
ImmutableAudience.ResetState();
// Avoid stacked subscriptions on reload.
Application.quitting -= ImmutableAudience.Shutdown;
Application.quitting += ImmutableAudience.Shutdown;
_persistentDataPath = Application.persistentDataPath;
ImmutableAudience.DefaultPersistentDataPathProvider = () => Application.persistentDataPath;
// Captured once on main thread; ReadOnlyDictionary blocks downstream mutation.
IReadOnlyDictionary<string, object> launchProps =
new ReadOnlyDictionary<string, object>(DeviceCollector.CollectGameLaunchProperties());
IReadOnlyDictionary<string, object> contextProps =
new ReadOnlyDictionary<string, object>(DeviceCollector.CollectContext());
ImmutableAudience.LaunchContextProvider = () => launchProps;
ImmutableAudience.ContextProvider = () => contextProps;
#if UNITY_IOS && !UNITY_EDITOR
ImmutableAudience.MobileAttributionProvider = () => SkanRegistration.RegisterIfFirstLaunch();
ImmutableAudience.MobileAttributionContextProvider = () => AttributionContext.Capture();
ImmutableAudience.TrackingAuthorizationRequestProvider = () => ATTBridge.RequestAsync();
#endif
#if UNITY_ANDROID && !UNITY_EDITOR
ImmutableAudience.MobileInstallReferrerProvider = ProvideInstallReferrer;
#if AUDIENCE_MOBILE_ATTRIBUTION
// Gated on the define so a build that disables GAID at compile
// time can't read a stale cache file left over from a prior
// install where the define was on.
ImmutableAudience.MobileAttributionContextProvider = ProvideAndroidAttributionContext;
#endif
#endif
UnityLifecycleBridge.EnsureExists();
if (Log.Writer == null) Log.Writer = Debug.Log;
}
// Warms the install referrer cache for the next launch and returns
// the currently cached value if any. Returns null on first launch
// (cache miss while async fetch is in flight) or when the device
// reports no referrer for this install. Exceptions propagate to
// ImmutableAudience.Init's MobileInstallReferrerProviderThrew handler.
private static string? ProvideInstallReferrer()
{
var path = _persistentDataPath;
if (string.IsNullOrEmpty(path)) return null;
InstallReferrerBridge.EnsureFetchStarted(path!);
return InstallReferrerBridge.GetCachedInstallReferrer(path!);
}
#if UNITY_ANDROID && !UNITY_EDITOR && AUDIENCE_MOBILE_ATTRIBUTION
// Kicks off a background GAID fetch for the next launch (Google
// requires getAdvertisingIdInfo run off the main thread) and returns
// whatever was cached by the previous launch. First launch returns
// an empty dict; launch #2+ ships gaid + gaidLimitAdTracking.
// Exceptions propagate to ImmutableAudience.Init's
// MobileAttributionContextProviderThrew handler.
private static IReadOnlyDictionary<string, object>? ProvideAndroidAttributionContext()
{
var path = _persistentDataPath;
if (string.IsNullOrEmpty(path)) return AttributionContext.Capture();
GAIDBridge.EnsureFetchStarted(path!);
return AttributionContext.Capture(path);
}
#endif
}
}