Skip to content

Commit b5b95fa

Browse files
Mobile Ads Developer Relationscopybara-github
authored andcommitted
do not submit
PiperOrigin-RevId: 829090479
1 parent 5466ced commit b5b95fa

File tree

5 files changed

+120
-5
lines changed

5 files changed

+120
-5
lines changed

samples/HelloWorld/Assets/Scripts/GoogleMobileAdsConsentController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace GoogleMobileAds.Samples
1212
public class GoogleMobileAdsConsentController : MonoBehaviour
1313
{
1414
/// <summary>
15+
/// Test
1516
/// If true, it is safe to call MobileAds.Initialize() and load Ads.
1617
/// </summary>
1718
public bool CanRequestAds => ConsentInformation.CanRequestAds();

source/plugin/Assets/GoogleMobileAds/Common/CuiHandler.cs

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,85 @@ public struct CuiLoggablePayload
99
{
1010
public Insight unity_gma_sdk_cui_message;
1111
}
12-
}
12+
/// <summary>
13+
/// A persistent singleton that captures all CUIs and sends them in batches to a backend
14+
/// service (RCS) based on either a count or time threshold.
15+
/// </summary>
16+
public class CuiHandler : RcsClient<Insight>
17+
{
18+
private static CuiHandler _instance;
19+
public static CuiHandler Instance
20+
{
21+
get
22+
{
23+
if (_instance == null && Application.isPlaying)
24+
{
25+
_instance = FindObjectOfType<CuiHandler>();
26+
if (_instance == null)
27+
{
28+
_instance = new GameObject("CuiHandler")
29+
.AddComponent<CuiHandler>();
30+
}
31+
}
32+
return _instance;
33+
}
34+
private set
35+
{
36+
_instance = value;
37+
}
38+
}
39+
40+
#region Unity lifecycle methods
41+
private void Awake()
42+
{
43+
if (_instance != null && _instance != this)
44+
{
45+
Destroy(gameObject);
46+
return;
47+
}
48+
_instance = this;
49+
DontDestroyOnLoad(gameObject);
50+
}
51+
#endregion
52+
53+
/// <summary>
54+
/// Call this to report a CUI.
55+
/// This method is thread-safe and adds the CUI to the queue.
56+
/// </summary>
57+
public void ReportCui(Insight insight)
58+
{
59+
if (insight == null) return;
60+
Enqueue(insight);
61+
}
62+
63+
/// <summary>
64+
/// Builds and sends a batch of CUIs.
65+
/// </summary>
66+
protected override void SendBatch(List<Insight> batch)
67+
{
68+
var payloads = new List<CuiLoggablePayload>();
69+
foreach (var insight in batch)
70+
{
71+
payloads.Add(new CuiLoggablePayload
72+
{
73+
unity_gma_sdk_cui_message = insight
74+
});
75+
}
76+
77+
var request = new LoggableRemoteCaptureRequest<CuiLoggablePayload>
78+
{
79+
payloads = payloads,
80+
client_ping_metadata = new ClientPingMetadata
81+
{
82+
binary_name = 21, // UNITY_GMA_SDK
83+
}
84+
};
85+
86+
string jspbPayload = JspbConverter.ToJspb(request);
87+
if (jspbPayload != null)
88+
{
89+
SendToRcs(jspbPayload);
90+
}
91+
}
92+
}
93+
}

source/plugin/Assets/GoogleMobileAds/Common/ProdInsightsEmitter.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,37 @@ public class InsightsEmitter : IInsightsEmitter
66
{
77
public void Emit(Insight insight)
88
{
9-
// Intentionally left blank.
9+
if (insight == null)
10+
{
11+
return;
12+
}
13+
// Currently the dynamic metadata fields are not in the Insight class.
14+
var staticMetadata = RcsPayload.GetStaticMetadata();
15+
16+
// Backfill static metadata if not set.
17+
if (string.IsNullOrEmpty(insight.AppId)) insight.AppId = staticMetadata.app_id;
18+
if (string.IsNullOrEmpty(insight.AppVersionName)) insight.AppVersionName =
19+
staticMetadata.app_version_name;
20+
if (insight.Platform == Insight.AdPlatform.Unknown)
21+
{
22+
try
23+
{
24+
insight.Platform = (Insight.AdPlatform)Enum.Parse(
25+
typeof(Insight.AdPlatform), staticMetadata.platform, true);
26+
}
27+
catch (ArgumentException)
28+
{
29+
insight.Platform = Insight.AdPlatform.Unknown;
30+
}
31+
}
32+
if (string.IsNullOrEmpty(insight.UnityVersion)) insight.UnityVersion =
33+
staticMetadata.unity_version;
34+
if (string.IsNullOrEmpty(insight.OSVersion)) insight.OSVersion =
35+
staticMetadata.os_version;
36+
if (string.IsNullOrEmpty(insight.DeviceModel)) insight.DeviceModel =
37+
staticMetadata.device_model;
38+
39+
CuiHandler.Instance.ReportCui(insight);
1040
}
1141
}
1242
}

source/plugin/Assets/GoogleMobileAds/Common/RcsClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ protected override bool ValidateCertificate(byte[] certificateData)
3636

3737
// Batching triggers can be overridden by subclasses. We don't need to expose them in Unity
3838
// Editor. If any trigger fires, a batch of items will get sent.
39-
protected virtual int CountThreshold => 20;
40-
protected virtual float TimeThresholdInSeconds => 120.0f;
39+
protected virtual int CountThreshold => 2;
40+
protected virtual float TimeThresholdInSeconds => 10.0f;
4141

4242
// RCS endpoint for reporting. The `e=1` URL parameter defines JSPB encoding.
4343
private const string ProdRcsUrl = "https://pagead2.googlesyndication.com/pagead/ping?e=1";

source/plugin/Assets/GoogleMobileAds/Platforms/Android/MobileAdsClient.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ public class MobileAdsClient : AndroidJavaProxy, IMobileAdsClient
3535
private MobileAdsClient() : base(Utils.OnInitializationCompleteListenerClassName) {
3636
_mobileAdsClass = new AndroidJavaClass(Utils.UnityMobileAdsClassName);
3737
_tracer = new Tracer(_insightsEmitter);
38-
// Ensures GlobalExceptionHandler is initialized to handle Android untrapped exceptions.
38+
// Ensures GlobalExceptionHandler is initialized from the main thread to handle Android
39+
// untrapped exceptions.
3940
var _ = GlobalExceptionHandler.Instance;
41+
// Ensures CuiHandler is initialized from the main thread to handle CUIs.
42+
var __ = CuiHandler.Instance;
4043
}
4144

4245
public static MobileAdsClient Instance

0 commit comments

Comments
 (0)