RewardedVideo Ad - fan-ADN/nendSDK-cocos2d-x GitHub Wiki

Preparation

If you have not yet register app/ad placements nor downloaded SDK, please refer to following link.

SDK Implementation

Add SDK/Module to a project.


The Flow to Implement Rewarded Video Ad

Implement a program that displays rewarded video Ads into the source file of Cocos2d-x.
Please follow the instruction below.

  1. Create an instance
  2. Load ad
  3. Show ad
  4. Discard ad
  5. Optional settings

Create an instance

Keep an instance controlling the Rewarded Video Ad in the class.

Header file

Declare a variable to hold the instance.

#include "NendVideoAds.h"

class YourAppScene : public cocos2d::Scene
{
  ...
private:
    // Keep the instance in the class.
    nend_module::video::VideoAds::RewardedVideoAd* _rewardedVideoAd;
  ...
}

Implementation file

Set the instance to variable that declared in the Header file.

using namespace nend_module;

bool YourAppScene::init()
{
  ...
    _rewardedVideoAd = VideoAds::createRewardedVideoAd("SpotId published on the dashboard", "ApiKey published on the dashboard");
  ...
}

Load ad

Load ads.

_rewardedVideoAd->loadAd();

Show ad

You can display ad after the loading of ad is completed. Please check the state via isReady() loading was completed or not.

if (_rewardedVideoAd->isReady()) {
    _rewardedVideoAd->showAd();
}

Note: The ad contents is not reloaded automatically when it has been closed. You must need to execute the loading process if you want to updating ad contents.

Discard ad

Discard unnecessary resources for video ad. Please call destroy() at the time when it becomes unnecessariness after use the video ad.
Also, if you want to use video ads again after calling destroy(), instantiate the RewardedVideoAd ad again.

YourAppScene::~YourAppScene()
{
    _rewardedVideoAd->destroy();
    CC_SAFE_DELETE(_rewardedVideoAd);
}

Optional settings

Set up state of using location data

Call setLocationEnabled if you change the state of using location data.
Default is true.

_rewardedVideoAd->setLocationEnabled(false);

Set user ID

You can set the user ID in the application.

_rewardedVideoAd->setUserid("user id");

Set up Features of User

In the cocos2d-x module 2.3.3 or higher, targeting-ad option of video ad is available to using Features of User. Here are available options.

  • Gender
  • Birthday
  • Age
  • Others: could be customized from own your app.
// e.g. Only single feature
NendUserFeature *rewardedUserFeature = new NendUserFeature();
rewardedUserFeature->setBirthday(2000, 1, 1); // Birthday (e.g. Jan 1, 2000)
_rewardedVideoAd->setUserFeature(rewardedUserFeature);
delete rewardedUserFeature;

...

// e.g. Multiple features
NendUserFeature *rewardedUserFeature = new NendUserFeature();
rewardedUserFeature->setGender(NendUserFeature::Gender::FEMALE); // Gender
rewardedUserFeature->setBirthday(2000, 1, 1); // Birthday (e.g. Jan 1, 2000)
rewardedUserFeature->setAge(18); // Age
rewardedUserFeature->addCustomValue(100, "intParameter"); // Others: customized parameters that formatted value-key type.
rewardedUserFeature->addCustomValue(123.45, "doubleParameter");
rewardedUserFeature->addCustomValue("test", "stringParameter");
rewardedUserFeature->addCustomValue(true, "boolParameter");
_rewardedVideoAd->setUserFeature(rewardedUserFeature);
delete rewardedUserFeature;

Set log output

You can specify whether debug logs are output to the debug console.

The setting of log output is set by the third parameter at instance creation.

_rewardedVideoAd = VideoAds::createRewardedVideoAd("SpotId published on the dashboard", "ApiKey published on the dashboard", true);
Setting value Description
true Output the debug log
false Do not output logs
Omitting setting value Output according to the setting of Logs Output

Receive notifications

You can receive notifications about each processing information of rewarded video Ad via registered callback.

Rewards given 
_rewardedVideoAd->onRewarded([](const VideoAds::RewardedVideoAd& ad, const RewardItem& item) {
    CCLOG("RewardedVideoAd - OnRewarded. %s, %d", item.getName().c_str(), item.getAmount());
});

The following information can be obtained from RewardItem set in the parameter.

getter Description
getName() Rewards currency name
getAmount() Reward amount
Load success
_rewardedVideoAd->onLoaded([](const VideoAds::RewardedVideoAd& ad) {
    CCLOG("RewardedVideoAd - OnLoaded.");
});
Load failed
_rewardedVideoAd->onFailedToLoad([](const VideoAds::RewardedVideoAd& ad, int errorCode) {
    CCLOG("RewardedVideoAd - onFailedToLoad: %d", errorCode);
});

The information of error code is as follows.

Code Description
204 No delivery ads
400 BAD request
5XX Server error
600 Error in SDK
601 Ad download failed
603 Invalid network
604 Ad acquisition network error (timeout etc.)
Display failure
_rewardedVideoAd->onFailedToPlay([](const VideoAds::RewardedVideoAd& ad) {
    CCLOG("RewardedVideoAd - onFailedToPlay.");
});
Ad displayed
_rewardedVideoAd->onShown([](const VideoAds::RewardedVideoAd& ad) {
    CCLOG("RewardedVideoAd - onShown.");
});
Video started
_rewardedVideoAd->onStarted([](const VideoAds::RewardedVideoAd& ad) {
    CCLOG("RewardedVideoAd - onStarted.");
});
Video stopped
_rewardedVideoAd->onStopped([](const VideoAds::RewardedVideoAd& ad) {
    CCLOG("RewardedVideoAd - onStopped.");
});
Video completed playing to the end
_rewardedVideoAd->onCompleted([](const VideoAds::RewardedVideoAd& ad) {
    CCLOG("RewardedVideoAd - onCompleted.");
});
Ad closed
_rewardedVideoAd->onClosed([](const VideoAds::RewardedVideoAd& ad) {
    CCLOG("RewardedVideoAd - onClosed.");
});
Ad clicked
_rewardedVideoAd->onAdClicked([](const VideoAds::RewardedVideoAd& ad) {
    CCLOG("RewardedVideoAd - onAdClicked.");
});
Information button clicked
_rewardedVideoAd->onInformationClicked([](const VideoAds::RewardedVideoAd& ad) {
    CCLOG("RewardedVideoAd - onInformationClicked.");
});