Skip to content

Commit 1327178

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

File tree

5 files changed

+97
-25
lines changed

5 files changed

+97
-25
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: 0 additions & 12 deletions
This file was deleted.
Lines changed: 88 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,93 @@
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+
/// <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 InsightsEmitter : RcsClient<Insight>, IInsightsEmitter
17+
{
18+
private static InsightsEmitter _instance;
19+
public static InsightsEmitter Instance
20+
{
21+
get
22+
{
23+
if (_instance == null && Application.isPlaying)
24+
{
25+
_instance = FindObjectOfType<InsightsEmitter>();
26+
if (_instance == null)
27+
{
28+
_instance = new GameObject("InsightsEmitter")
29+
.AddComponent<InsightsEmitter>();
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 Emit(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+
}
1293
}

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: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,17 @@ 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();
3231
private readonly ITracer _tracer;
3332
private Action<IInitializationStatusClient> _initCompleteAction;
3433

3534
private MobileAdsClient() : base(Utils.OnInitializationCompleteListenerClassName) {
3635
_mobileAdsClass = new AndroidJavaClass(Utils.UnityMobileAdsClassName);
37-
_tracer = new Tracer(_insightsEmitter);
38-
// Ensures GlobalExceptionHandler is initialized to handle Android untrapped exceptions.
36+
_tracer = new Tracer(InsightsEmitter.Instance);
37+
// Ensures GlobalExceptionHandler is initialized from the main thread to handle Android
38+
// untrapped exceptions.
3939
var _ = GlobalExceptionHandler.Instance;
40+
// Ensures InsightsEmitter is initialized from the main thread to handle CUIs.
41+
var __ = InsightsEmitter.Instance;
4042
}
4143

4244
public static MobileAdsClient Instance
@@ -69,7 +71,7 @@ public void Initialize(Action<IInitializationStatusClient> initCompleteAction)
6971
}
7072
});
7173
}
72-
_insightsEmitter.Emit(new Insight()
74+
InsightsEmitter.Instance.Emit(new Insight()
7375
{
7476
Name = Insight.CuiName.SdkInitialized
7577
});

0 commit comments

Comments
 (0)