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
GoogleAdUnitHandlers.cs
1using System;
2using System.Diagnostics;
3using GoogleMobileAds.Api;
4
5namespace SCG.SpaceAdMobile
6{
7 // The second partial part of the GoogleAdUnit 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 GoogleAdUnit<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
65 protected virtual void OnAdPaid(AdValue adValue)
66 {
67 Logger.Log(LogOptions.AdPaidLogText, adValue.Value, adValue.CurrencyCode);
68 base.OnAdPaid(adValue);
69 }
70
74 protected override void OnAdImpressionRecorded()
75 {
76 Logger.Log(LogOptions.AdImpressionLogText);
77 base.OnAdImpressionRecorded();
78 }
79
83 protected override void OnAdLoaded()
84 {
85 LoadTime = DateTime.Now;
86 SetLoadingTime();
88 Logger.Log(LogOptions.AdLoadedLogText);
89 base.OnAdLoaded();
90 }
91
96 protected virtual void OnAdLoadFailed(LoadAdError error)
97 {
99 Logger.LogError(LogOptions.AdLoadFailedText, error);
100 base.OnAdLoadFailed(error);
101 }
102
106 protected override void OnAdClicked()
107 {
108 Logger.Log(LogOptions.AdClickedLogText);
109 base.OnAdClicked();
110 }
111
115 protected override void OnAdShown()
116 {
117 Logger.Log(LogOptions.AdShownLogText);
118 base.OnAdShown();
119 }
120
124 protected override void OnAdClosed()
125 {
126 Logger.Log(LogOptions.AdClosedLogText);
127 base.OnAdClosed();
128 }
129
134 protected virtual void OnAdContentFailed(AdError error)
135 {
136 Logger.LogError(LogOptions.AdContentFailedLogText, error);
137 base.OnAdContentFailed(error);
138 }
139
145 protected virtual void OnUserEarnedReward(AdReward reward)
146 {
147 Logger.Log(LogOptions.AdRewardEarnedLogText, reward.Name, reward.AmountAsDouble);
148 base.OnUserEarnedReward(reward);
149 }
150 }
151}
override double AmountAsDouble
Definition AdReward.cs:30
override string Name
Definition AdReward.cs:34
Concrete adapter that wraps an SDK-specific reward object and exposes it through the SDK-agnostic AdR...
Definition AdReward.cs:19
virtual LogOptions LogOptions
Definition AdUnit.cs:59
override void OnAdImpressionRecorded()
Handles the ad impression recorded event, logging the impression.
virtual void OnAdLoadFailed(LoadAdError error)
Handles the ad load failed event, logging the error encountered during ad loading.
override void OnAdLoaded()
Handles the ad loaded event, setting the load time, preparing the ad object for display,...
void SetLoadingStartTime()
Saves the start time of an ad-loading request.
long? LoadingTimeMs
Gets the duration of the latest successful ad-loading request in milliseconds. Divide the value by 10...
virtual void OnAdContentFailed(AdError error)
Handles the ad content failed event, logging the error encountered while displaying the ad content.
override void OnAdShown()
Handles the ad shown event, logging that the ad has been displayed to the user.
override void OnAdClosed()
Handles the ad closed event, logging that the ad was closed by the user.
virtual void PrepareAdObject()
Optional overridable method for setting up the AdObject. This can be used for initializing or customi...
DateTime? LoadTime
Date and time when the ad was loaded.
void ResetLoadingStartTime()
Clears the saved start time when an ad-loading request cannot be started.
virtual void OnAdPaid(AdValue adValue)
Handles the event when an ad generates revenue, logging the ad's value and currency code.
virtual void OnUserEarnedReward(AdReward reward)
Handles the user earned reward event, logging the type and amount of the reward earned through intera...
override void OnAdClicked()
Handles the ad clicked event, logging that the ad was interacted with by the user.