Skip to content

Commit

Permalink
Nimbus: Adding the ability to set the GDPR consent string from the Ni…
Browse files Browse the repository at this point in the history
…mbus Manager (#10)
  • Loading branch information
marcsantiago authored May 5, 2021
1 parent efb75cb commit cf72936
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 31 deletions.
18 changes: 13 additions & 5 deletions package/Runtime/Plugins/Android/UnityHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.adsbynimbus.openrtb.request.App;
import com.adsbynimbus.openrtb.request.Format;
import com.adsbynimbus.openrtb.request.Position;
import com.adsbynimbus.openrtb.request.User;
import com.adsbynimbus.render.AdController;
import com.adsbynimbus.render.AdEvent;
import com.adsbynimbus.render.BlockingAdRenderer;
Expand All @@ -27,15 +28,22 @@ public final class UnityHelper {
BlockingAdRenderer.setDismissOnComplete(true);
}

public static void setApp(Object bundleId, String appName, String domain, String storeUrl) {
public static void setApp(String bundleId, String appName, String domain, String storeUrl) {
final App app = new App();
app.bundle = bundleId.toString();
app.name = appName.toString();
app.domain = domain.toString();
app.storeurl = storeUrl.toString();
app.bundle = bundleId;
app.name = appName;
app.domain = domain;
app.storeurl = storeUrl;
RequestManager.setApp(app);
}

public static void setUser(String gdprConsent) {
final User user = new User();
user.ext = new User.Extension();
user.ext.consent = gdprConsent;
RequestManager.setUser(user);
}

public static void showInterstitialAd(Object obj, String position, float bannerFloor, float videoFloor,
int closeButtonDelay, Object listener) {
if (obj instanceof Activity) {
Expand Down
4 changes: 4 additions & 0 deletions package/Runtime/Scripts/Internal/Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,9 @@ internal override NimbusAdUnit LoadAndShowAd(ILogger logger, ref NimbusAdUnit ni
listener);
return nimbusAdUnit;
}

internal override void SetGDPRConsentString(string consent) {
_helper.CallStatic("setUser", consent);
}
}
}
4 changes: 4 additions & 0 deletions package/Runtime/Scripts/Internal/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,9 @@ internal override NimbusAdUnit LoadAndShowAd(ILogger logger, ref NimbusAdUnit ni
logger.Log($"In Editor mode, {functionCall} was called, however ads cannot be shown in the editor");
return nimbusAdUnit;
}

internal override void SetGDPRConsentString(string consent) {
Debug.unityLogger.Log("Mock SDK consent string cannot be added");
}
}
}
2 changes: 2 additions & 0 deletions package/Runtime/Scripts/Internal/NimbusAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ namespace Nimbus.Runtime.Scripts.Internal {
public abstract class NimbusAPI {
internal abstract void InitializeSDK(ILogger logger, NimbusSDKConfiguration configuration);
internal abstract NimbusAdUnit LoadAndShowAd(ILogger logger, ref NimbusAdUnit nimbusAdUnit);
// ReSharper disable once InconsistentNaming
internal abstract void SetGDPRConsentString(string consent);
}
}
64 changes: 38 additions & 26 deletions package/Runtime/Scripts/NimbusManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public class NimbusManager : MonoBehaviour {
public delegate void SetAdUnitFromCoroutine(NimbusAdUnit adUnit);

public static NimbusManager Instance;

private AdEvents _nimbusEvents;
private NimbusAPI _nimbusPlatformAPI;

public AdEvents NimbusEvents;

private void Awake() {
if (configuration == null) throw new Exception("The configuration object cannot be null");
configuration.ValidateMobileData();
Expand All @@ -30,7 +30,7 @@ private void Awake() {
Android
#endif
();
_nimbusEvents = new AdEvents();
NimbusEvents = new AdEvents();
Debug.unityLogger.logEnabled = configuration.enableUnityLogs;
_nimbusPlatformAPI.InitializeSDK(Debug.unityLogger, configuration);
Instance = this;
Expand All @@ -47,15 +47,15 @@ private IEnumerator Start() {
var eventsFound = false;
var iAdEvents = FindObjectsOfType<MonoBehaviour>().OfType<IAdEvents>();
foreach (var iAdEvent in iAdEvents) {
Instance._nimbusEvents.OnAdRendered += iAdEvent.OnAdWasRendered;
Instance._nimbusEvents.OnAdError += iAdEvent.OnAdError;
Instance._nimbusEvents.OnAdLoaded += iAdEvent.OnAdLoaded;
Instance._nimbusEvents.OnAdImpression += iAdEvent.OnAdImpression;
Instance._nimbusEvents.OnAdClicked += iAdEvent.OnAdClicked;
Instance._nimbusEvents.OnAdDestroyed += iAdEvent.OnAdDestroyed;
Instance._nimbusEvents.OnVideoAdPaused += iAdEvent.OnVideoAdPaused;
Instance._nimbusEvents.OnVideoAdResume += iAdEvent.OnVideoAdResume;
Instance._nimbusEvents.OnVideoAdCompleted += iAdEvent.OnVideoAdCompleted;
Instance.NimbusEvents.OnAdRendered += iAdEvent.OnAdWasRendered;
Instance.NimbusEvents.OnAdError += iAdEvent.OnAdError;
Instance.NimbusEvents.OnAdLoaded += iAdEvent.OnAdLoaded;
Instance.NimbusEvents.OnAdImpression += iAdEvent.OnAdImpression;
Instance.NimbusEvents.OnAdClicked += iAdEvent.OnAdClicked;
Instance.NimbusEvents.OnAdDestroyed += iAdEvent.OnAdDestroyed;
Instance.NimbusEvents.OnVideoAdPaused += iAdEvent.OnVideoAdPaused;
Instance.NimbusEvents.OnVideoAdResume += iAdEvent.OnVideoAdResume;
Instance.NimbusEvents.OnVideoAdCompleted += iAdEvent.OnVideoAdCompleted;
eventsFound = true;
}

Expand All @@ -71,15 +71,15 @@ private void OnDestroy() {
if (!shouldSubscribeToIAdEvents) return;
var iAdEvents = FindObjectsOfType<MonoBehaviour>().OfType<IAdEvents>();
foreach (var iAdEvent in iAdEvents) {
Instance._nimbusEvents.OnAdRendered -= iAdEvent.OnAdWasRendered;
Instance._nimbusEvents.OnAdError -= iAdEvent.OnAdError;
Instance._nimbusEvents.OnAdLoaded += iAdEvent.OnAdLoaded;
Instance._nimbusEvents.OnAdImpression += iAdEvent.OnAdImpression;
Instance._nimbusEvents.OnAdClicked += iAdEvent.OnAdClicked;
Instance._nimbusEvents.OnAdDestroyed += iAdEvent.OnAdDestroyed;
Instance._nimbusEvents.OnVideoAdPaused += iAdEvent.OnVideoAdPaused;
Instance._nimbusEvents.OnVideoAdResume += iAdEvent.OnVideoAdResume;
Instance._nimbusEvents.OnVideoAdCompleted += iAdEvent.OnVideoAdCompleted;
Instance.NimbusEvents.OnAdRendered -= iAdEvent.OnAdWasRendered;
Instance.NimbusEvents.OnAdError -= iAdEvent.OnAdError;
Instance.NimbusEvents.OnAdLoaded += iAdEvent.OnAdLoaded;
Instance.NimbusEvents.OnAdImpression += iAdEvent.OnAdImpression;
Instance.NimbusEvents.OnAdClicked += iAdEvent.OnAdClicked;
Instance.NimbusEvents.OnAdDestroyed += iAdEvent.OnAdDestroyed;
Instance.NimbusEvents.OnVideoAdPaused += iAdEvent.OnVideoAdPaused;
Instance.NimbusEvents.OnVideoAdResume += iAdEvent.OnVideoAdResume;
Instance.NimbusEvents.OnVideoAdCompleted += iAdEvent.OnVideoAdCompleted;
}
}

Expand All @@ -95,7 +95,7 @@ private void OnDestroy() {
/// Represents your asking price for banner ads during the auction
/// </param>
public NimbusAdUnit LoadAndShowBannerAd(string position, float bannerBidFloor) {
var adUnit = new NimbusAdUnit(AdUnityType.Banner, position, bannerBidFloor, 0f, in _nimbusEvents);
var adUnit = new NimbusAdUnit(AdUnityType.Banner, position, bannerBidFloor, 0f, in NimbusEvents);
return _nimbusPlatformAPI.LoadAndShowAd(Debug.unityLogger, ref adUnit);
}

Expand All @@ -115,7 +115,7 @@ public NimbusAdUnit LoadAndShowBannerAd(string position, float bannerBidFloor) {
/// </param>
public NimbusAdUnit LoadAndShowFullScreenAd(string position, float bannerBidFloor, float videoBidFloor) {
var adUnit = new NimbusAdUnit(AdUnityType.Interstitial, position, bannerBidFloor, videoBidFloor,
in _nimbusEvents);
in NimbusEvents);
return _nimbusPlatformAPI.LoadAndShowAd(Debug.unityLogger, ref adUnit);
}

Expand All @@ -130,7 +130,7 @@ public NimbusAdUnit LoadAndShowFullScreenAd(string position, float bannerBidFloo
/// Represents your asking price for video ads during the auction
/// </param>
public NimbusAdUnit LoadAndShowRewardedVideoAd(string position, float videoBidFloor) {
var adUnit = new NimbusAdUnit(AdUnityType.Rewarded, position, 0, videoBidFloor, in _nimbusEvents);
var adUnit = new NimbusAdUnit(AdUnityType.Rewarded, position, 0, videoBidFloor, in NimbusEvents);
return _nimbusPlatformAPI.LoadAndShowAd(Debug.unityLogger, ref adUnit);
}

Expand All @@ -152,18 +152,30 @@ public NimbusAdUnit LoadAndShowRewardedVideoAd(string position, float videoBidFl
public IEnumerator LoadAndShowBannerAdWithRefresh(string position, float bannerBidFloor,
SetAdUnitFromCoroutine currentAdUnit,
float refreshIntervalInSeconds = 30f) {
var adUnit = new NimbusAdUnit(AdUnityType.Banner, position, bannerBidFloor, 0, in _nimbusEvents);
var adUnit = new NimbusAdUnit(AdUnityType.Banner, position, bannerBidFloor, 0, in NimbusEvents);
currentAdUnit(adUnit);
_nimbusPlatformAPI.LoadAndShowAd(Debug.unityLogger, ref adUnit);
while (true) {
yield return new WaitForSeconds(refreshIntervalInSeconds);
adUnit?.Destroy();
adUnit = new NimbusAdUnit(AdUnityType.Banner, position, bannerBidFloor, 0, in _nimbusEvents);
adUnit = new NimbusAdUnit(AdUnityType.Banner, position, bannerBidFloor, 0, in NimbusEvents);
currentAdUnit(adUnit);
_nimbusPlatformAPI.LoadAndShowAd(Debug.unityLogger, ref adUnit);
}
}

// ReSharper disable once InconsistentNaming
/// <summary>
/// Allows the TCF GDPR consent string to be set globally on all request to Nimbus
/// </summary>
/// <param name="consent">
/// This is the TCF GDRP consent string
/// </param>
public void SetGDPRConsentString(string consent) {
_nimbusPlatformAPI.SetGDPRConsentString(consent);
}


#region Editor Values

public NimbusSDKConfiguration configuration;
Expand Down

0 comments on commit cf72936

Please sign in to comment.