Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace GoogleMobileAds.Samples
public class GoogleMobileAdsConsentController : MonoBehaviour
{
/// <summary>
/// Test
/// If true, it is safe to call MobileAds.Initialize() and load Ads.
/// </summary>
public bool CanRequestAds => ConsentInformation.CanRequestAds();
Expand Down
12 changes: 0 additions & 12 deletions source/plugin/Assets/GoogleMobileAds/Common/CuiHandler.cs

This file was deleted.

96 changes: 89 additions & 7 deletions source/plugin/Assets/GoogleMobileAds/Common/ProdInsightsEmitter.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,94 @@
using System;
using System.Collections.Generic;
using UnityEngine;

namespace GoogleMobileAds.Common
{
public class InsightsEmitter : IInsightsEmitter
{
public void Emit(Insight insight)
{
// Intentionally left blank.
}
}
[Serializable]
public struct CuiLoggablePayload
{
public Insight unity_gma_sdk_cui_message;
}

/// <summary>
/// A persistent singleton that captures all CUIs and sends them in batches to a backend
/// service (RCS) based on either a count or time threshold.
/// </summary>
public class InsightsEmitter : RcsClient<Insight>, IInsightsEmitter
{
private static InsightsEmitter _instance;
public static InsightsEmitter Instance
{
get
{
if (_instance == null && Application.isPlaying)
{
_instance = FindObjectOfType<InsightsEmitter>();
if (_instance == null)
{
_instance = new GameObject("InsightsEmitter")
.AddComponent<InsightsEmitter>();
}
}
return _instance;
}
private set
{
_instance = value;
}
}

#region Unity lifecycle methods
private void Awake()
{
if (_instance != null && _instance != this)
{
Destroy(gameObject);
return;
}
_instance = this;
DontDestroyOnLoad(gameObject);
}
#endregion

/// <summary>
/// Call this to report a CUI.
/// This method is thread-safe and adds the CUI to the queue.
/// </summary>
public void Emit(Insight insight)
{
if (insight == null) return;
Enqueue(insight);
}

/// <summary>
/// Builds and sends a batch of CUIs.
/// </summary>
protected override void SendBatch(List<Insight> batch)
{
var payloads = new List<CuiLoggablePayload>();
foreach (var insight in batch)
{
payloads.Add(new CuiLoggablePayload
{
unity_gma_sdk_cui_message = insight
});
}

var request = new LoggableRemoteCaptureRequest<CuiLoggablePayload>
{
payloads = payloads,
client_ping_metadata = new ClientPingMetadata
{
binary_name = 21, // UNITY_GMA_SDK
}
};

string jspbPayload = JspbConverter.ToJspb(request);
if (jspbPayload != null)
{
SendToRcs(jspbPayload);
}
}
}
}
4 changes: 2 additions & 2 deletions source/plugin/Assets/GoogleMobileAds/Common/RcsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ protected override bool ValidateCertificate(byte[] certificateData)

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

// RCS endpoint for reporting. The `e=1` URL parameter defines JSPB encoding.
private const string ProdRcsUrl = "https://pagead2.googlesyndication.com/pagead/ping?e=1";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ public class MobileAdsClient : AndroidJavaProxy, IMobileAdsClient
private readonly static MobileAdsClient _instance = new MobileAdsClient();

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

private MobileAdsClient() : base(Utils.OnInitializationCompleteListenerClassName) {
_mobileAdsClass = new AndroidJavaClass(Utils.UnityMobileAdsClassName);
_tracer = new Tracer(_insightsEmitter);
// Ensures GlobalExceptionHandler is initialized to handle Android untrapped exceptions.
// Ensures GlobalExceptionHandler is initialized from the main thread to handle Android
// untrapped exceptions.
var _ = GlobalExceptionHandler.Instance;
}

Expand Down