Skip to content

Commit a857c0e

Browse files
Mobile Ads Developer Relationscopybara-github
authored andcommitted
Add support to batch and send CUIs.
PiperOrigin-RevId: 834467129
1 parent 5466ced commit a857c0e

File tree

3 files changed

+93
-21
lines changed

3 files changed

+93
-21
lines changed

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

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 89 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,94 @@
11
using System;
2+
using System.Collections.Generic;
3+
using UnityEngine;
24

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

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,16 @@ public class MobileAdsClient : AndroidJavaProxy, IMobileAdsClient
2828
private readonly static MobileAdsClient _instance = new MobileAdsClient();
2929

3030
private readonly AndroidJavaClass _mobileAdsClass;
31-
private readonly IInsightsEmitter _insightsEmitter = new InsightsEmitter();
31+
// Ensures InsightsEmitter is initialized from the main thread to handle CUIs.
32+
private readonly IInsightsEmitter _insightsEmitter = InsightsEmitter.Instance;
3233
private readonly ITracer _tracer;
3334
private Action<IInitializationStatusClient> _initCompleteAction;
3435

3536
private MobileAdsClient() : base(Utils.OnInitializationCompleteListenerClassName) {
3637
_mobileAdsClass = new AndroidJavaClass(Utils.UnityMobileAdsClassName);
3738
_tracer = new Tracer(_insightsEmitter);
38-
// Ensures GlobalExceptionHandler is initialized to handle Android untrapped exceptions.
39+
// Ensures GlobalExceptionHandler is initialized from the main thread to handle Android
40+
// untrapped exceptions.
3941
var _ = GlobalExceptionHandler.Instance;
4042
}
4143

0 commit comments

Comments
 (0)