Search Unity

OnUnityAdsReady and OnUnityAdsDidFinish get triggered twice on Android

Discussion in 'Unity Ads & User Acquisition' started by TheLastVertex, Sep 3, 2020.

  1. TheLastVertex

    TheLastVertex

    Joined:
    Sep 30, 2015
    Posts:
    126
    OnUnityAdsReady() and OnUnityAdsDidFinish() get triggered twice. (Unity Version 2019.3.11f1).

    For some reason these two are getting triggered twice on Android and I can't figure out why. To make this more complicated this does not occur on PC when running in editor (iOS untested).

    I originally assumed there was multiple listeners as I had to fix an issue due to scene reloading but that does not appear to be the case (This also appears to be the most common problem returned by google). I only have 1 listener added on Start. The scene is never reloaded during the testing and attempting to find all references on Advertisement.AddListener only shows the one.

    I tested removing the listener and it prevents any calls to OnUnityAdsReady from being triggered on PC or Android. If there was a hidden listener being added somewhere I would assume this would still be called but that's not the case.

    There is so little code I'm not sure what could be the cause. I double checked what I could for unwanted calls to Advertiements and to make sure there was only 1 instance in the scene. I also made sure the button to watch is only getting triggered once as well. The code path where the initialization and listener happen is also only called once.

    Logs from android nothing exciting:
    Ad Ready: freePlayVideo
    09-03 13:15:47.976 17473 17529 I Unity : UnityEngine.Logger:Log(LogType, Object)
    09-03 13:15:47.976 17473 17529 I Unity : UnityEngine.Advertisements.Platform.<UnityAdsReady>c__AnonStorey2:<>m__0()
    09-03 13:15:47.976 17473 17529 I Unity : System.Action:Invoke()
    09-03 13:15:47.976 17473 17529 I Unity : UnityEngine.Advertisements.Utilities.CoroutineExecutor:Update()
    09-03 13:15:47.976 17473 17529 I Unity :
    09-03 13:15:47.976 17473 17529 I Unity : (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)


    Ad Ready: freePlayVideo
    09-03 13:15:48.010 17473 17529 I Unity : UnityEngine.Logger:Log(LogType, Object)
    09-03 13:15:48.010 17473 17529 I Unity : UnityEngine.Advertisements.Platform.<UnityAdsReady>c__AnonStorey2:<>m__0()
    09-03 13:15:48.010 17473 17529 I Unity : System.Action:Invoke()
    09-03 13:15:48.010 17473 17529 I Unity : UnityEngine.Advertisements.Utilities.CoroutineExecutor:Update()
    09-03 13:15:48.010 17473 17529 I Unity :
    09-03 13:15:48.010 17473 17529 I Unity : (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)

    Code (CSharp):
    1. public void InitializeAds()
    2. {
    3.     //Initialize the Ads service if not already initialized
    4.     if (Advertisement.isInitialized == false)
    5.     {
    6.         Debug.LogError("Ads adding listener! " + this.gameObject.name);
    7.  
    8.         Advertisement.AddListener(this);
    9.  
    10.         //initialize
    11.         #if UNITY_IOS
    12.             Debug.Log("Unity IOS");
    13.             Advertisement.Initialize(iOSGameId, testMode);
    14.         #elif UNITY_ANDROID
    15.             Debug.Log("Unity Android");
    16.             Advertisement.Initialize(androidGameId, testMode);
    17.         #elif UNITY_EDITOR
    18.             Debug.Log("Unity Editor");
    19.             Advertisement.Initialize(androidGameId, testMode);
    20.         #endif
    21.     }
    22. }
    23.  
    24. public void OnUnityAdsReady(string placementId)
    25. {
    26.     Debug.LogError("UnityAdsReady: " + this.gameObject.name + "   " + this.name);
    27.     // If the placement is ready
    28.     if (placementId == doubleRewardsPlacementId)
    29.     {
    30.         Debug.Log("Ad Ready: " + placementId);
    31.     }
    32.     else if(placementId == freePlayPlacementId)
    33.     {
    34.         Debug.Log("Ad Ready: " + placementId);
    35.     }
    36.     else
    37.     {
    38.         Debug.Log("Unknown Ad Ready: " + placementId);
    39.     }
    40.  
    41. }
     
    Last edited: Sep 3, 2020
  2. TheLastVertex

    TheLastVertex

    Joined:
    Sep 30, 2015
    Posts:
    126
    Still at a loss on this.

    I've had a prompt to update the monetization package for awhile now and reading through the sticky I was worried perhaps when I originally set this up I messed something up. Maybe there was duplicate plugins hanging around resulting in issues... idk?

    So..I deleted everything in the assets related to ads, or advertising. Compiler then complained there was no reference for IUnityAdsListener, as anticipated. Updated the monetization packaged and re-imported. Unfortunately OnUnityAdsReady, OnUnityAdsDidStart, and OnUnityAdsDidFinish still get triggered twice. Bummer.

    There a way to get more details on what or who or how these are getting triggered? Object and component names just return exactly what It should.
     
  3. TheLastVertex

    TheLastVertex

    Joined:
    Sep 30, 2015
    Posts:
    126
    Fiddled some more with enabling and disabling ad based plugins that were enabled for android but no luck. Decided to just copy paste the example from the documentation located here, minus the button logic and still no luck. Everything still getting called twice on android.

    Searched entire solution for anything regarding ads or advertisement calls, nothing else found. Double checked disabling the script prevents ads from initializing and playing, which is does.

    Pretty much out of ideas at this point besides putting in some manual checks to prevent the OnUnityAdsDidFinish from triggering the reward function twice in a row.
    FreePlayRequest()
    09-04 14:02:37.815 23448 23491 I Unity : UnityEngine.Logger:Log(LogType, Object)
    09-04 14:02:37.815 23448 23491 I Unity : AdManager:FreePlayRequest(ProcessingDialog)
    09-04 14:02:37.815 23448 23491 I Unity : UnityEngine.Events.UnityAction`1:Invoke(T0)
    09-04 14:02:37.815 23448 23491 I Unity : UnityEngine.Events.UnityEvent:Invoke()
    09-04 14:02:37.815 23448 23491 I Unity : UnityEngine.EventSystems.EventFunction`1:Invoke(T1, BaseEventData)
    09-04 14:02:37.815 23448 23491 I Unity : UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1)
    09-04 14:02:37.815 23448 23491 I Unity : UnityEngine.EventSystems.StandaloneInputModule:processTouchPress(PointerEventData, Boolean, Boolean)
    09-04 14:02:37.815 23448 23491 I Unity : UnityEngine.EventSystems.StandaloneInputModule:processTouchEvents()
    09-04 14:02:37.815 23448 23491 I Unity : UnityEngine.EventSystems.StandaloneInputModule:process()
    09-04 14:02:37.815 23448 23491 I Unity :
    09-04 14:02:37.815 23448 23491 I Unity : (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)
    09-04 14:02:37.815 23448 23491 I Unity :
    09-04 14:02:37.816 23448 23491 I Unity : FreePlayRequest() IS READY
    09-04 14:02:37.816 23448 23491 I Unity : UnityEngine.Logger:Log(LogType, Object)
    09-04 14:02:37.816 23448 23491 I Unity : AdManager:FreePlayRequest(ProcessingDialog)
    09-04 14:02:37.816 23448 23491 I Unity : UnityEngine.Events.UnityAction`1:Invoke(T0)
    09-04 14:02:37.816 23448 23491 I Unity : UnityEngine.Events.UnityEvent:Invoke()
    09-04 14:02:37.816 23448 23491 I Unity : UnityEngine.EventSystems.EventFunction`1:Invoke(T1, BaseEventData)
    09-04 14:02:37.816 23448 23491 I Unity : UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1)
    09-04 14:02:37.816 23448 23491 I Unity : UnityEngine.EventSystems.StandaloneInputModule:processTouchPress(PointerEventData, Boolean, Boolean)
    09-04 14:02:37.816 23448 23491 I Unity : UnityEngine.EventSystems.StandaloneInputModule:processTouchEvents()
    09-04 14:02:37.816 23448 23491 I Unity : UnityEngine.EventSystems.StandaloneInputModule:process()
    09-04 14:02:37.816 23448 23491 I Unity :
    09-04 14:02:37.816 23448 23491 I Unity : (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)
    09-04 14:02:37.816 23448 23491 I Unity :
    09-04 14:02:37.817 23448 23491 I UnityAds: com.unity3d.services.ads.UnityAdsImplementation.show() (line:215) :: Unity Ads opening new ad unit for placement freePlayVideo
    09-04 14:02:37.935 23448 23699 I UnityAds: com.unity3d.services.core.api.Sdk.logInfo() (line:82) :: Opening performance ad unit with orientation PORTRAIT, hardware acceleration enabled
    09-04 14:02:38.297 772 1531 I ActivityManager: START u0 {flg=0x10010000 cmp=unity.TheLastVertex.QwertyKingdom/com.unity3d.services.ads.adunit.AdUnitActivity (has extras)} from uid 10161 on display 0
    09-04 14:02:38.518 772 793 I ActivityManager: Displayed unity.TheLastVertex.QwertyKingdom/com.unity3d.services.ads.adunit.AdUnitActivity: +165ms
    09-04 14:02:39.273 23448 23699 I UnityAds: com.unity3d.services.core.api.Sdk.logInfo() (line:82) :: Unity Ads event: sending start event to https://impact.applifier.com/events/v2/video/video_start//000000000000000000000000
    09-04 14:02:41.451 23448 23699 I UnityAds: com.unity3d.services.core.api.Sdk.logInfo() (line:82) :: Unity Ads event: sending first_quartile event to https://impact.applifier.com/events/v2/video/first_quartile//000000000000000000000000
    09-04 14:02:43.458 23448 23699 I UnityAds: com.unity3d.services.core.api.Sdk.logInfo() (line:82) :: Unity Ads event: sending midpoint event to https://impact.applifier.com/events/v2/video/midpoint//000000000000000000000000
    09-04 14:02:44.541 23448 23699 I UnityAds: com.unity3d.services.core.api.Sdk.logInfo() (line:82) :: Requesting ad plan from https://auction.unityads.unity3d.com/v4/test/games//requests?idfi=5656d3e6-0aaf-4016-b009-cc0e22e58ca1&advertisingTrackingId=55b04ab5-268b-4052-9501-577846540d7a&limitAdTracking=false&deviceModel=Nexus 5&platform=android&sdkVersion=3480&stores=google&deviceMake=LGE&screenSize=268435794&screenDensity=480&apiLevel=23&test=true&screenWidth=1080&screenHeight=1776&connectionType=wifi&networkType=13
    09-04 14:02:44.675 23448 23699 I UnityAds: com.unity3d.services.core.api.Sdk.logInfo() (line:82) :: AdPlan received with 1 campaigns and refreshDelay 0
    09-04 14:02:45.434 23448 23699 I UnityAds: com.unity3d.services.core.api.Sdk.logInfo() (line:82) :: Unity Ads event: sending third_quartile event to https://impact.applifier.com/events/v2/video/third_quartile//000000000000000000000000
    09-04 14:02:47.462 23448 23699 I UnityAds: com.unity3d.services.core.api.Sdk.logInfo() (line:82) :: Unity Ads event: sending view event to https://impact.applifier.com/events/v2/video/video_end//000000000000000000000000
    09-04 14:02:49.657 23448 23699 I UnityAds: com.unity3d.services.core.api.Sdk.logInfo() (line:82) :: Closing Unity Ads ad unit
    09-04 14:02:49.842 23448 23491 E Unity : Ad finished! freePlayVideo
    09-04 14:02:49.842 23448 23491 E Unity : UnityEngine.Logger:Log(LogType, Object)
    09-04 14:02:49.842 23448 23491 E Unity : UnityEngine.Advertisements.Platform.<UnityAdsDidFinish>c__AnonStorey5:<>m__0()
    09-04 14:02:49.842 23448 23491 E Unity : System.Action:Invoke()
    09-04 14:02:49.842 23448 23491 E Unity : UnityEngine.Advertisements.Utilities.CoroutineExecutor:Update()
    09-04 14:02:49.842 23448 23491 E Unity :
    09-04 14:02:49.842 23448 23491 E Unity : (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)
    09-04 14:02:49.842 23448 23491 E Unity :
    09-04 14:02:49.843 23448 23491 E Unity : Ad finished! freePlayVideo
    09-04 14:02:49.843 23448 23491 E Unity : UnityEngine.Logger:Log(LogType, Object)
    09-04 14:02:49.843 23448 23491 E Unity : UnityEngine.Advertisements.Platform.<UnityAdsDidFinish>c__AnonStorey5:<>m__0()
    09-04 14:02:49.843 23448 23491 E Unity : System.Action:Invoke()
    09-04 14:02:49.843 23448 23491 E Unity : UnityEngine.Advertisements.Utilities.CoroutineExecutor:Update()
    09-04 14:02:49.843 23448 23491 E Unity :
    09-04 14:02:49.843 23448 23491 E Unity : (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)
    09-04 14:02:49.843 23448 23491 E Unity :
    09-04 14:02:50.439 23448 23491 E Unity : Ad ready! freePlayVideo
    09-04 14:02:50.439 23448 23491 E Unity : UnityEngine.Logger:Log(LogType, Object)
    09-04 14:02:50.439 23448 23491 E Unity : UnityEngine.Advertisements.Platform.<UnityAdsReady>c__AnonStorey2:<>m__0()
    09-04 14:02:50.439 23448 23491 E Unity : System.Action:Invoke()
    09-04 14:02:50.439 23448 23491 E Unity : UnityEngine.Advertisements.Utilities.CoroutineExecutor:Update()
    09-04 14:02:50.439 23448 23491 E Unity :
    09-04 14:02:50.439 23448 23491 E Unity : (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)
    09-04 14:02:50.439 23448 23491 E Unity :
    09-04 14:02:50.471 23448 23491 E Unity : Ad ready! freePlayVideo
    09-04 14:02:50.471 23448 23491 E Unity : UnityEngine.Logger:Log(LogType, Object)
    09-04 14:02:50.471 23448 23491 E Unity : UnityEngine.Advertisements.Platform.<UnityAdsReady>c__AnonStorey2:<>m__0()
    09-04 14:02:50.471 23448 23491 E Unity : System.Action:Invoke()
    09-04 14:02:50.471 23448 23491 E Unity : UnityEngine.Advertisements.Utilities.CoroutineExecutor:Update()
    09-04 14:02:50.471 23448 23491 E Unity :
    09-04 14:02:50.471 23448 23491 E Unity : (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)

    Code below
    Code (CSharp):
    1. #if UNITY_IOS
    2. private string gameId = "1486551";
    3. #elif UNITY_ANDROID
    4. private string gameId = "1486550";
    5. #endif
    6.  
    7.  
    8. public string myPlacementId = "rewardedVideo";
    9.  
    10. void Start()
    11. {
    12.  
    13.  
    14.     // Initialize the Ads listener and service:
    15.     Advertisement.AddListener(this);
    16.     Advertisement.Initialize(gameId, true);
    17. }
    18.  
    19. // Implement a function for showing a rewarded video ad:
    20. void ShowRewardedVideo()
    21. {
    22.     Advertisement.Show(myPlacementId);
    23. }
    24.  
    25. // Implement IUnityAdsListener interface methods:
    26. public void OnUnityAdsReady(string placementId)
    27. {
    28.     // If the ready Placement is rewarded, activate the button:
    29.     if (placementId == myPlacementId)
    30.     {
    31.         Debug.LogError("Ad ready! " + myPlacementId);
    32.     }
    33. }
    34.  
    35. public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    36. {
    37.     // Define conditional logic for each ad completion status:
    38.     if (showResult == ShowResult.Finished)
    39.     {
    40.         // Reward the user for watching the ad to completion.
    41.         Debug.LogError("Ad finished! " + placementId);
    42.     }
    43.     else if (showResult == ShowResult.Skipped)
    44.     {
    45.         // Do not reward the user for skipping the ad.
    46.     }
    47.     else if (showResult == ShowResult.Failed)
    48.     {
    49.         Debug.LogError("The ad did not finish due to an error.");
    50.     }
    51. }
    52.  
    53. public void OnUnityAdsDidError(string message)
    54. {
    55.     // Log the error.
    56. }
    57.  
    58. public void OnUnityAdsDidStart(string placementId)
    59. {
    60.     // Optional actions to take when the end-users triggers an ad.
    61. }
     
    Last edited: Sep 5, 2020
  4. TheLastVertex

    TheLastVertex

    Joined:
    Sep 30, 2015
    Posts:
    126
    Finally solved it!

    It didn't' feel right trying to figure out a work around and re-reading the sticky yet again convinced me there had to be duplicates. Something, somewhere was resulting in double listeners and double triggers.

    Decided to try using the Ads service via the Services Window instead of the Unity Monetization package from the asset store. I deleted all Unity Monetization package assets from the project first, then enabled the Ads service which still resulted in compile errors stating there was conflicts in the project (50+ errors).

    I then went through and just deleted every single file from the project that was listed. Enabled the Ad service again and tada! OnUnityAdsReady, OnUnityAdsDidStart, and OnUnityAdsDidFinish are now working correctly and only getting triggered once on Android, with the exact same code as before.
     
    YukiKomine and egeatici like this.
  5. YukiKomine

    YukiKomine

    Joined:
    Feb 10, 2019
    Posts:
    1
    In my case this was a duplicate Listener when moving the scene.
    The Advertisement class would remain even if I moved the scene, so a listener was added every time the scene was loaded.
    So I made a static bool detect the first boot.

    Code (CSharp):
    1. public class A : MonoBehaviour{
    2.     public static bool first = true;
    3.  
    4.     void Start(){
    5.         if(first){
    6.             Advertisement.AddListener(this);
    7.             Advertisement.Initialize("*******");
    8.         }
    9.      
    10.          first = false;
    11.     }
    12. }