Skip to content

Commit 7fc4aad

Browse files
Mobile Ads Developer Relationscopybara-github
authored andcommitted
No public description
PiperOrigin-RevId: 802323481
1 parent 5dd4879 commit 7fc4aad

File tree

5 files changed

+204
-0
lines changed

5 files changed

+204
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.IO;
3+
using System.Threading;
4+
using UnityEngine;
5+
6+
namespace GoogleMobileAds.Common
7+
{
8+
// Emits insights when the SDK compiles in dev build mode.
9+
// TODO: b/431227569 - We should add tracing to the Unity plugin, like for the Decagon SDK:
10+
// http://google3/java/com/google/android/libraries/ads/mobile/sdk/internal/tracing/ExportingDebugTraceMonitor.kt.
11+
public class InsightsEmitter : IInsightsEmitter
12+
{
13+
// LINT.IfChange
14+
private const string _DEFAULT_INSIGHTS_FILE_NAME = "unity_insights.jsonl";
15+
// LINT.ThenChange(//depot/google3/javatests/com/google/android/apps/internal/admobsdk/mediumtest/unityplugin/UnityTestUtils.java)
16+
17+
private readonly string _filePath;
18+
private readonly bool _canWrite = true;
19+
private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();
20+
21+
/**
22+
* Creates a new InsightsEmitter.
23+
*
24+
* @param filePath The file path to write the insights to. If null, the file will be written
25+
* to the external storage directory.
26+
*/
27+
public InsightsEmitter(string filePath = null)
28+
{
29+
_filePath = filePath ?? Path.Combine(Application.persistentDataPath,
30+
_DEFAULT_INSIGHTS_FILE_NAME);
31+
Debug.Log("Unity insights will be written to: " + _filePath);
32+
33+
try
34+
{
35+
// No-op if the directory and subdirectories already exist.
36+
Directory.CreateDirectory(Path.GetDirectoryName(_filePath));
37+
} catch (Exception e) {
38+
Debug.LogError("Failed to create directory for Unity insights (no insights will be " +
39+
"written): " + e.Message);
40+
_canWrite = false;
41+
}
42+
}
43+
44+
public void Emit(Insight insight)
45+
{
46+
if (!_canWrite)
47+
{
48+
return;
49+
}
50+
51+
_lock.EnterWriteLock();
52+
try
53+
{
54+
Debug.Log("Writing insight: " + insight.ToString());
55+
56+
// Writing needs to be synchronous for the read cursor to consume insights in order.
57+
File.AppendAllText(_filePath, insight.ToJson() + Environment.NewLine);
58+
}
59+
finally
60+
{
61+
_lock.ExitWriteLock();
62+
}
63+
}
64+
}
65+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
3+
namespace GoogleMobileAds.Common
4+
{
5+
public interface IInsightsEmitter
6+
{
7+
void Emit(Insight insight);
8+
}
9+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
// LINT.IfChange
6+
namespace GoogleMobileAds.Common
7+
{
8+
// Protos are not supported in g3 for C#: http://yaqs/6221611834537934848. Instead, we use
9+
// JSON Lines (http://jsonlines.org) to serialize the insights.
10+
[Serializable]
11+
public class Insight
12+
{
13+
public Insight()
14+
{
15+
StartTimeMillis = (long)DateTime.UtcNow
16+
.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc))
17+
.TotalMilliseconds;
18+
}
19+
20+
public enum CuiName
21+
{
22+
Unknown = 0,
23+
SdkInitialized = 1,
24+
AdRequested = 2,
25+
AdLoaded = 3,
26+
AdFailedToLoad = 4,
27+
AdShown = 5,
28+
AdClosed = 6,
29+
AdClicked = 7,
30+
}
31+
32+
public enum AdFormat
33+
{
34+
Unknown = 0,
35+
Banner = 1,
36+
Interstitial = 2,
37+
Rewarded = 3,
38+
RewardedInterstitial = 4,
39+
AppOpen = 5,
40+
Native = 6,
41+
}
42+
43+
public enum AdPlatform
44+
{
45+
Unknown = 0,
46+
Android = 1,
47+
Ios = 2,
48+
Unity = 3,
49+
}
50+
51+
// The name of the insight, commonly referred as a CUI (Critical User Interaction).
52+
public CuiName Name;
53+
54+
// If the event associated with the insight succeeded or failed.
55+
public bool Success;
56+
57+
// The Epoch time in milliseconds when the CUI started.
58+
public long StartTimeMillis;
59+
60+
// How long the operation took to complete in milliseconds.
61+
public long LatencyMillis;
62+
63+
// The GMA SDK version.
64+
public string SdkVersion;
65+
66+
// The AdMob / Google Ad Manager app id.
67+
public string AppId;
68+
69+
// The ad unit ID.
70+
public string AdUnitId;
71+
72+
// The format of the ad.
73+
public AdFormat Format;
74+
75+
// The platform on which the CUI was performed.
76+
public AdPlatform Platform;
77+
78+
// The keywords associated with the insight. They are used to group insights together for
79+
// analysis.
80+
public List<string> Tags;
81+
82+
// Any additional details about the insight.
83+
public string Details;
84+
85+
// Returns a string representation of the insight.
86+
public override string ToString()
87+
{
88+
return string.Format(
89+
"Insight[Name={0}, Success={1}, StartTimeMillis={2}, LatencyMillis={3}, " +
90+
"SdkVersion='{4}', AppId='{5}', AdUnitId='{6}', Format={7}, Platform={8}, " +
91+
"Tags='{9}', Details='{10}']",
92+
Name,
93+
Success,
94+
StartTimeMillis,
95+
LatencyMillis,
96+
SdkVersion,
97+
AppId,
98+
AdUnitId,
99+
Format,
100+
Platform,
101+
string.Join(",", Tags),
102+
Details);
103+
}
104+
105+
// Returns a JSON string representation of the insight.
106+
public string ToJson()
107+
{
108+
return JsonUtility.ToJson(this, prettyPrint: true);
109+
}
110+
}
111+
}
112+
// LINT.ThenChange(//depot/google3/javatests/com/google/android/apps/internal/admobsdk/mediumtest/unityplugin/Insight.java)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace GoogleMobileAds.Common
4+
{
5+
public class InsightsEmitter : IInsightsEmitter
6+
{
7+
public void Emit(Insight insight) {
8+
// Intentionally left blank.
9+
}
10+
}
11+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class MobileAdsClient : AndroidJavaProxy, IMobileAdsClient
2727
{
2828
private readonly static MobileAdsClient _instance = new MobileAdsClient();
2929
private readonly AndroidJavaClass _mobileAdsClass;
30+
private readonly IInsightsEmitter _insightsEmitter = new InsightsEmitter();
3031
private Action<IInitializationStatusClient> _initCompleteAction;
3132

3233
private MobileAdsClient() : base(Utils.OnInitializationCompleteListenerClassName) {
@@ -52,6 +53,12 @@ public void Initialize(Action<IInitializationStatusClient> initCompleteAction)
5253
try {
5354
_mobileAdsClass.CallStatic("initialize", Utils.GetCurrentActivityAndroidJavaObject(),
5455
this);
56+
_insightsEmitter.Emit(new Insight()
57+
{
58+
Name = Insight.CuiName.SdkInitialized,
59+
Platform = Insight.AdPlatform.Android,
60+
Success = true
61+
});
5562
} finally {
5663
AndroidJNI.DetachCurrentThread();
5764
}

0 commit comments

Comments
 (0)