Ad Integration Guide
Introduction
Before integrating ads, the SDK initialization must be completed first.
MG Ads supports 【Splash Ad 1920x1080】【Exit Ad】【Banner 728x90】【Interstitial 1024x768】【Couplet 300x600】【Rewarded 1024x768】【Feed】【Embedded】 ad functionalities.
Splash Ad
The splash ad slot is typically implemented in the page's load method, within the SDK initialization completion event.
private async void Form1_Load(object sender, EventArgs e)
{
var result = await ApplicationManager.Initialize(YourAppId, YourSecretKey);
if (result.ReturnValue)
{
//Initialization successful...
ShowMessage("Initialization complete");
//Exit Ad Step1.
//...
//...
//Splash Ad
AdvertManager.ShowAd(this, "xxxxxxx", AdType.Splash);
}
else
{
ShowMessage("Initialization failed");
}
}
Exit Ad
The exit ad is triggered when exiting the game. To ensure the pop-up rate of the ad upon game exit, MG implements the exit screen ad in two steps:
1.After initialization is complete, load the exit screen ad information into memory.
2.When exiting the game, directly display the exit screen ad.
//Exit Ad
//Step1. After successful initialization, load exit screen ad resources
private async void Form1_Load(object sender, EventArgs e)
{
var result = await ApplicationManager.Initialize(YourAppId, YourSecretKey);
if (result.ReturnValue)
{
//Initialization successful...
ShowMessage("Initialization complete");
//Exit Ad Step1. After successful initialization, load exit screen ad resources
AdvertManager.SetupExitAd("xxxxxxx");
//...
//Splash Ad
AdvertManager.ShowAd(this, "xxxxxxx", AdType.Splash);
}
else
{
ShowMessage("Initialization failed");
}
}
// Exit Ad
// Step2. When the program closes, pop up and display the exit ad
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
_ = AdvertManager.ShowExitAdBlocking();
}
Banner Ad
private void btnAd3_Click(object sender, EventArgs e)
{
AdvertManager.ShowAd(this, "xxxxxxx", AdType.Banner);
}
Interstitial Ad
private void btnAd4_Click(object sender, EventArgs e)
{
AdvertManager.ShowAd(this, "xxxxxxx", AdType.Interstitial);
}
Couplet Ad
private void btnAd5_Click(object sender, EventArgs e)
{
AdvertManager.ShowAd(this, "xxxxxxx", AdType.Couplet);
}
Rewarded
private void btnAd6_Click(object sender, EventArgs e)
{
string comment = "id123,abc,$9.99";//Passthrough parameter, requires URL encoding
dynamic jsonObj = new
{
unitId = "xxxxxxx",
comment = Uri.EscapeDataString(comment)
};
string json = JsonConvert.SerializeObject(jsonObj);
AdvertManager.ShowAd(this, json, AdType.Rewarded);
}
Feed
Feed ads require the developer to create and maintain the control, and pass the control instance to the SDK.
private void btnAd7_Click(object sender, EventArgs e)
{
dynamic jsonObj = new
{
unitId = "xxxxxxx",//Custom size, set in the MG backend
media = "image",//Supports image, video, web
width = panelAd6.Width,
height = panelAd6.Height
};
string json = JsonConvert.SerializeObject(jsonObj);
AdvertManager.ShowAd(this.panelAd6, json, AdType.Feed);
}
Embedded
Embedded ads require the developer to create and maintain the control, and pass the control instance to the SDK.
private void btnAd8_Click(object sender, EventArgs e)
{
dynamic jsonObj = new
{
unitId = "xxxxxxx",//Custom size, set in the MG backend
media = "image",//Supports image, video, web
width = panelAd.Width,
height = panelAd.Height
};
string json = JsonConvert.SerializeObject(jsonObj);
AdvertManager.ShowAd(this.panelAd, json, AdType.Embedded);
}
Ad Close Event
Register the callback event for ad closure, typically done in the page's constructor.
Ad close event parameter description:
| Parameter Name | Description | Example |
|---|---|---|
| unitId | The ad slot ID passed by the developer | e333abaf22404c4a8d382c1e7ba42076 |
| advertStatus | Ad slot status | 1: Ad normal; 2: Ad closed by backend; 3: No ad creative |
| The following parameters are only available for rewarded video ads | ||
| completeStatus | Ad playback status | 1: Ad playback finished, reward can be issued; 0: Ad playback not finished |
| comment | Passthrough parameter passed by the developer, URL encoded | abc%2c123 |
| rewardId | MG order number for the reward, used when the game reports fulfillment to MG after issuing the reward | String |
| resourceId | Resource Id | String |
| materialId | Material Id | String |
public Form1()
{
InitializeComponent();
AdvertManager.AdClickEvent += AdvertManager_AdClickEvent;
AdvertManager.AdCloseEvent += AdvertManager_AdCloseEvent;
}
private void AdvertManager_AdCloseEvent(object sender, string e)
{
ShowMessage("Ad closed " + e);
//Regular ad {"unitId":"6bf68881673540788d096b9ea4a3cedb","advertStatus":1,"resourceId":"68d20656bd9558abfdf43465","materialId":"d235efa86ccf44acbe7053af760031b6"}
//Rewarded ad {"unitId":"0f505442fac84f098e81d6f2ca04abe1","advertStatus":1,"completeStatus":1,"resourceId":"68ecb9eb20f045c603867874","materialId":"b0817d87ee2544629bac1933a60238d2","comment":"id123%2Cabc%2C%249.99","rewardId":"D1E593C16BBD412CA880FD89F0450A14"}
JObject jsonObject = JObject.Parse(e);
string unitId = (string)jsonObject["unitId"];
if (unitId == "0f505442fac84f098e81d6f2ca04abe1")//Rewarded, issue reward items based on the return result
{
int completeStatus = (int)jsonObject["completeStatus"];
string resourceId = (string)jsonObject["resourceId"];
string materialId = (string)jsonObject["materialId"];
string rewardId = (string)jsonObject["rewardId"];
string comment = (string)jsonObject["comment"];//Passthrough parameter
if (completeStatus == 1)
{
//Video playback finished, issue reward items
//...
Task.Run(async () =>
{
_ = await AdvertManager.ReportAdRewardFulfillment(unitId, resourceId, materialId, rewardId);//Report to MG
});
}
}
}
private void AdvertManager_AdClickEvent(object sender, string e)
{
ShowMessage("Ad clicked " + e);
}