Skip to content

Commit b510575

Browse files
Mobile Ads Developer Relationscopybara-github
authored andcommitted
do not submit
PiperOrigin-RevId: 829090479
1 parent 16f7b3f commit b510575

File tree

7 files changed

+178
-9
lines changed

7 files changed

+178
-9
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();
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace GoogleMobileAds.Common
6+
{
7+
[Serializable]
8+
public struct CuiLoggablePayload
9+
{
10+
public Insight unity_gma_sdk_cui_message;
11+
}
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+
if (Debug.isDebugBuild)
62+
{
63+
Debug.Log("CUI queued for batch.");
64+
}
65+
}
66+
67+
/// <summary>
68+
/// Builds and sends a batch of CUI reports.
69+
/// </summary>
70+
protected override void SendBatch(List<Insight> batch)
71+
{
72+
if (Debug.isDebugBuild)
73+
{
74+
Debug.Log(string.Format("Processing and sending a batch of {0} CUIs...",
75+
batch.Count));
76+
}
77+
78+
var payloads = new List<CuiLoggablePayload>();
79+
foreach (var report in batch)
80+
{
81+
payloads.Add(new CuiLoggablePayload
82+
{
83+
unity_gma_sdk_cui_message = report
84+
});
85+
}
86+
87+
var request = new LoggableRemoteCaptureRequest<CuiLoggablePayload>
88+
{
89+
payloads = payloads,
90+
client_ping_metadata = new ClientPingMetadata
91+
{
92+
binary_name = 21, // UNITY_GMA_SDK
93+
}
94+
};
95+
96+
string jspbPayload = JspbConverter.ToJspb(request);
97+
if (jspbPayload != null)
98+
{
99+
Debug.Log("rcs jspb payload is not null: " + jspbPayload);
100+
SendToRcs(jspbPayload);
101+
}
102+
}
103+
}
104+
}

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ internal static string ToJspb<TPayload>(LoggableRemoteCaptureRequest<TPayload> r
2020
{
2121
payloads.Add(ToJspb((ExceptionLoggablePayload)(object)payload));
2222
}
23+
else if (payload is CuiLoggablePayload)
24+
{
25+
payloads.Add(ToJspb((CuiLoggablePayload)(object)payload));
26+
}
2327
else
2428
{
2529
Debug.LogError("JspbConverter encountered an unknown payload type: " +
@@ -43,6 +47,18 @@ internal static string ToJspb(ClientPingMetadata metadata)
4347
return string.Format("[{0}]", metadata.binary_name);
4448
}
4549

50+
// VisibleForTesting
51+
internal static string ToListJspb(List<string> list)
52+
{
53+
if (list == null) return "null";
54+
var quotedElements = new List<string>();
55+
foreach(string item in list)
56+
{
57+
quotedElements.Add(QuoteString(item));
58+
}
59+
return string.Format("[{0}]", string.Join(",", quotedElements.ToArray()));
60+
}
61+
4662
// VisibleForTesting
4763
internal static string QuoteString(string s)
4864
{
@@ -140,5 +156,50 @@ internal static string ToJspb(ExceptionReport report)
140156
return string.Format("[{0}]", string.Join(",", fields.ToArray()));
141157
}
142158
#endregion
159+
160+
#region CUI handling
161+
// VisibleForTesting
162+
internal static string ToJspb(CuiLoggablePayload payload)
163+
{
164+
if (payload.unity_gma_sdk_cui_message == null) return "[]";
165+
166+
// unity_gma_sdk_cui_message has field index 36.
167+
return string.Format("[{{\"36\":{0}}}]",
168+
ToJspb(payload.unity_gma_sdk_cui_message));
169+
}
170+
171+
// VisibleForTesting
172+
internal static string ToJspb(Insight insight)
173+
{
174+
return string.Format(
175+
"[{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14}]",
176+
(int)insight.Name,
177+
insight.Success.ToString().ToLower(),
178+
insight.StartTimeEpochMillis.ToString(),
179+
QuoteString(insight.SdkVersion),
180+
QuoteString(insight.AppId),
181+
QuoteString(insight.AdUnitId),
182+
(int)insight.Format,
183+
(int)insight.Platform,
184+
QuoteString(insight.AppVersionName),
185+
QuoteString(insight.UnityVersion),
186+
QuoteString(insight.OSVersion),
187+
QuoteString(insight.DeviceModel),
188+
ToListJspb(insight.Tags),
189+
ToJspb(insight.Tracing),
190+
QuoteString(insight.Details));
191+
}
192+
// VisibleForTesting
193+
internal static string ToJspb(Insight.TracingActivity tracing)
194+
{
195+
if (tracing == null) return "null";
196+
return string.Format("[{0},{1},{2},{3},{4}]",
197+
QuoteString(tracing.OperationName),
198+
QuoteString(tracing.Id),
199+
QuoteString(tracing.ParentId),
200+
tracing.DurationMillis,
201+
tracing.HasEnded.ToString().ToLower());
202+
}
203+
#endregion
143204
}
144205
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ protected override void SendBatch(List<ExceptionReport> batch)
230230
string jspbPayload = JspbConverter.ToJspb(request);
231231
if (jspbPayload != null)
232232
{
233-
SendToRcs(jspbPayload);
233+
// SendToRcs(jspbPayload);
234234
}
235235
}
236236
#endregion

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ public class InsightsEmitter : IInsightsEmitter
66
{
77
public void Emit(Insight insight)
88
{
9-
// Intentionally left blank.
9+
CuiHandler.Instance.ReportCui(insight);
10+
1011
}
1112
}
1213
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public abstract class RcsClient<TReport> : MonoBehaviour where TReport : class
2828
{
2929
// Batching triggers can be overridden by subclasses. We don't need to expose them in Unity
3030
// Editor. If any trigger fires, a batch of items will get sent.
31-
protected virtual int CountThreshold => 20;
32-
protected virtual float TimeThresholdInSeconds => 120.0f;
31+
protected virtual int CountThreshold => 2;
32+
protected virtual float TimeThresholdInSeconds => 10.0f;
3333

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

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ private MobileAdsClient() : base(Utils.OnInitializationCompleteListenerClassName
3636
_mobileAdsClass = new AndroidJavaClass(Utils.UnityMobileAdsClassName);
3737
_tracer = new Tracer(_insightsEmitter);
3838
// Ensures GlobalExceptionHandler is initialized to handle Android untrapped exceptions.
39-
var _ = GlobalExceptionHandler.Instance;
39+
// var _ = GlobalExceptionHandler.Instance;
40+
// Ensures CuiHandler is initialized to handle CUIs.
41+
var __ = CuiHandler.Instance;
4042
}
4143

4244
public static MobileAdsClient Instance
@@ -184,10 +186,10 @@ public void onInitializationComplete(AndroidJavaObject initStatus)
184186
nativePluginVersion = string.Format("{0}.{1}.{2}", assemblyVersion.Major,
185187
assemblyVersion.Minor, assemblyVersion.Revision);
186188
} catch (Exception e) {
187-
if (GlobalExceptionHandler.Instance != null)
188-
{
189-
GlobalExceptionHandler.Instance.ReportTrappedException(e);
190-
}
189+
// if (GlobalExceptionHandler.Instance != null)
190+
// {
191+
// GlobalExceptionHandler.Instance.ReportTrappedException(e);
192+
// }
191193
}
192194
string versionString = AdRequest.BuildVersionString(nativePluginVersion);
193195
_mobileAdsClass.CallStatic("setPlugin", versionString);

0 commit comments

Comments
 (0)