Space Ad Mobile 9.5.1
Space Ad Mobile makes ads easy to add into your mobile game: one unified workflow for LevelPlay, AdMob, Yandex.Ads across banners, interstitials and rewarded.
Loading...
Searching...
No Matches
YandexAdUnitHandlers.cs
1using System;
2using System.Diagnostics;
3using YandexMobileAds.Base;
4
5namespace SCG.SpaceAdMobile
6{
7 // The second partial part of the YandexAdUnit class focuses on handling various advertisement events,
8 // particularly logging and responding to key lifecycle events such as ad load failures and ad clicks.
9 // This part of the class includes methods that are invoked in response to specific ad events,
10 // allowing for comprehensive logging and additional processing through base class methods.
11 // Each handler method is designed to capture detailed information about the event,
12 // such as failure reasons or simple acknowledgment of user interaction, and log this information
13 // for debugging, analytics, and operational insights. These methods ensure that any significant
14 // event in the ad lifecycle is not only noted but also acted upon, following the principle of
15 // observability and responsiveness in ad management.
16 public abstract partial class YandexAdUnit<TAd>
17 {
21 public DateTime? LoadTime { get; protected set; }
22
23 private long? loadingStartTimestamp;
24
38 public long? LoadingTimeMs { get; protected set; }
39
43 protected void SetLoadingStartTime() => loadingStartTimestamp = Stopwatch.GetTimestamp();
44
48 protected void ResetLoadingStartTime() => loadingStartTimestamp = null;
49
53 private void SetLoadingTime()
54 {
55 LoadingTimeMs = loadingStartTimestamp is { } startTimestamp
56 ? (long)((Stopwatch.GetTimestamp() - startTimestamp) * 1000d / Stopwatch.Frequency)
57 : null;
58 loadingStartTimestamp = null;
59 }
60
62 protected override void OnAdPaid(object adValue) => throw new NotSupportedException();
63
69 protected virtual void OnAdImpression(object sender, ImpressionData e)
70 {
71 Logger.Log(LogOptions.AdImpressionLogText, e?.rawData ?? string.Empty);
72 base.OnAdImpressionRecorded();
73 }
74
78 protected override void OnAdLoaded()
79 {
80 LoadTime = DateTime.Now;
81 SetLoadingTime();
83 Logger.Log(LogOptions.AdLoadedLogText);
84 base.OnAdLoaded();
85 }
86
92 protected virtual void OnAdLoadFailed(object sender, AdFailedToLoadEventArgs args)
93 {
95 Logger.LogError(LogOptions.AdLoadFailedText, args.Message, args.AdUnitId);
96 base.OnAdLoadFailed(args.Message);
97 }
98
99#if !LEGACY_YANDEX_MOBILE_ADS
101 protected void OnAdLoadFailed(object sender, AdFailureEventArgs e)
102 => OnAdLoadFailed(sender, new AdLoadingException(e.Message, GetId()));
103
109 protected virtual void OnAdLoadFailed(object sender, AdLoadingException args)
110 {
112 Logger.LogError(LogOptions.AdLoadFailedText, args.Message, args.AdUnitId);
113 base.OnAdLoadFailed(args.Message);
114 }
115#endif
116
122 protected virtual void OnAdClicked(object sender, EventArgs e)
123 {
124 Logger.Log(LogOptions.AdClickedLogText);
125 base.OnAdClicked();
126 }
127
133 protected virtual void OnAdShown(object sender, EventArgs e)
134 {
135 Logger.Log(LogOptions.AdShownLogText);
136 base.OnAdShown();
137 }
138
144 protected virtual void OnAdClosed(object sender, EventArgs e)
145 {
146 Logger.Log(LogOptions.AdClosedLogText);
147 base.OnAdClosed();
148 }
149
155 protected virtual void OnAdContentFailed(object sender, AdFailureEventArgs e)
156 {
157 Logger.LogError(LogOptions.AdContentFailedLogText, e.Message);
158 base.OnAdContentFailed(e.Message);
159 }
160
166 protected virtual void OnUserEarnedReward(object sender, AdRewardBase reward)
167 {
168 Logger.Log(LogOptions.AdRewardEarnedLogText, reward.Name, reward.Amount);
169 base.OnUserEarnedReward(reward);
170 }
171 }
172}
int Amount
The numeric amount of the reward.
string Name
The semantic type or name of the reward.
Represents a backend-agnostic description of an ad reward.
string GetId()
Determines and returns the appropriate ad ID based on the current platform and ad mode....
Definition AdUnit.cs:332
virtual LogOptions LogOptions
Definition AdUnit.cs:59
virtual void PrepareAdObject()
Optional overridable method for setting up the AdObject. This can be used for initializing or customi...
virtual void OnAdContentFailed(object sender, AdFailureEventArgs e)
Handles the ad content failed event, logging the error encountered while displaying the ad content.
long? LoadingTimeMs
Gets the duration of the latest successful ad-loading request in milliseconds. Divide the value by 10...
override void OnAdPaid(object adValue)
Raised when the AdObject is estimated to have earned money.
virtual void OnAdLoadFailed(object sender, AdLoadingException args)
Handles the ad load failure event, logging the failure reason and the ad unit ID, and reporting the f...
virtual void OnAdClosed(object sender, EventArgs e)
Handles the ad closed event, logging that the ad was closed by the user.
virtual void OnAdClicked(object sender, EventArgs e)
Handles the ad click event, logging that the ad was clicked by the user.
virtual void OnAdLoadFailed(object sender, AdFailedToLoadEventArgs args)
Handles the ad load failure event, logging the failure reason and the ad unit ID, and reporting the f...
void ResetLoadingStartTime()
Clears the saved start time when an ad-loading request cannot be started.
void OnAdLoadFailed(object sender, AdFailureEventArgs e)
DateTime? LoadTime
Date and time when the ad was loaded.
void SetLoadingStartTime()
Saves the start time of an ad-loading request.
virtual void OnAdShown(object sender, EventArgs e)
Handles the event when an ad is shown to the user, logging the occurrence.
virtual void OnUserEarnedReward(object sender, AdRewardBase reward)
Handles the event where a user earns a reward from interacting with an ad, logging the reward type an...
override void OnAdLoaded()
Handles the ad loaded event, setting the load time, preparing the ad object for display,...
virtual void OnAdImpression(object sender, ImpressionData e)
Handles the impression event for an ad, logging the impression data and marking the impression as rec...