「Android Integeration with New AdMob Mediation 2.0」修訂間的差異

出自TAMedia
跳至導覽 跳至搜尋
imported>Wikiuser
imported>Wikiuser
行 1: 行 1:
*新版AdMob Mediation整合
Mopub網站: https://app.mopub.com, 登入可對後台進行設定。<br>
新版AdMob網站, https://apps.admob.com, 登入後點選 "<b>營利</b>",
: 請將擋廣告的插件關閉(如Adblock),否則後台瀏覽、設定會有問題<br><br>
並點選將要新增"<b>廣告中介服務</b>"的應用程式<br/> [[File:android_22.png]]  ,<br/>
點選"<b>編輯中介服務</b>", 出現中介服務編輯畫面,選擇"<b>新增廣告聯播網</b>"<br/> [[檔案:android_23.png]]<br/>
再點選"<b>+自訂事件</b>"<br/>[[檔案:android_24.png]],<br/>  出現自訂事件設定畫面 <br/>[[檔案:android_25.png]],<br/>
依序填入
#Class Name : 請填入實作(implements <b>com.google.ads.mediation.customevent.CustomEventBanner</b>)的類別名稱(含完整的package name, 如<b>com.taiwanmobile.pt.adp.mediation.TAMediaBanner</b>)
# Label : TAMedia (任意自定的名稱)
# Parameter : 請填入您的TAMedia Slot Id (可向您的TAMedia窗口詢問),<br/>
完成後,按下左下角的"儲存",將自訂事件的內容儲存起來, 並回到應用程式管理畫面,[[檔案:android_26.png]]<br/>再按一次"儲存"將廣告中介服務的設定進行儲存


::*整合至應用程式
== Mopub後台設定 ==
:::*Banner,依照下述方式將AdMob所提供的CustomEventBanner進行實作
<big><u>Step 1: 新增APP與版位,取得Mopub版位ID</u></big><br>
  package com.taiwanmobile.pt.adp.mediation;
進入Apps分頁後,點擊Add a New App<br>
 
[[image:Mopub_Step1-1.png | 700px]]<br><br>
  import android.app.Activity;
 
 
輸入APP資訊,如APP Name、Package Name…等<br>
  import com.google.ads.AdRequest;
[[image:Mopub_Step1-2.png | 700px]]<br><br>
  import com.google.ads.AdSize;
 
  import com.google.ads.mediation.MediationAdRequest;
新增版位並進行設定<br>
  import com.google.ads.mediation.customevent.CustomEventBanner;
:a. Format為橫幅廣告Banner (320x50):<br>
  import com.google.ads.mediation.customevent.CustomEventBannerListener;
[[image:Mopub_Step1-3.png | 700px]]<br><br>
  import com.taiwanmobile.pt.adp.view.TWMAd;
 
  import com.taiwanmobile.pt.adp.view.TWMAdRequest;
:b. Format為插頁式廣告Fullscreen (320x480):<br>
  import com.taiwanmobile.pt.adp.view.TWMAdRequest.ErrorCode;
[[image:Mopub_Step1-4.png | 700px]]<br><br>
  import com.taiwanmobile.pt.adp.view.TWMAdSize;
 
  import com.taiwanmobile.pt.adp.view.TWMAdView;
:c. Format為原生廣告Native (Custom Layout):<br>
  import com.taiwanmobile.pt.adp.view.TWMAdViewListener;
[[image:Mopub_Step1-5.png | 700px]]<br><br>
  import com.taiwanmobile.pt.util.Log;
 
  // 需將本class的完整類別名稱 com.taiwanmobile.pt.adp.mediation.TAMediaBanner註冊於mediation的設定頁面上, 如前述章節所述
完成設定後,點選Save and View Code Integration,並記下Mopub的版位ID<br>
  public class TAMediaBanner implements CustomEventBanner {
[[image:Mopub_Step1-6.png | 700px]]<br><br>
 
[[image:Mopub_Step1-7.png | 700px]]<br><br>
  private static final String TAG = "TAMediaBanner";
 
 
若欲新增其他版位,請點選剛剛新增的APP並點選Add an Ad Unit<br>
  @Override
[[image:Mopub_Step1-8.png | 700px]]<br><br>
  public void destroy() {
[[image:Mopub_Step1-9.png | 700px]]<br><br>
 
<br>
  if(adView != null){
<br>
  adView.destroy();
 
  }
<big><u>Step 2: 新增廣告聯播網</u></big><br>
 
進入Network分頁,並點選Add a Network<br>
  }
[[image:Mopub_Step2-1.png | 700px]]<br><br>
 
 
  //將AdMob所提供的MediationAdRequest轉換為TWMAdRequest
點選Custom Native Network<br>
  private TWMAdRequest convertAdRequest(MediationAdRequest request) {
[[image:Mopub_Step2-2.png | 700px]]<br><br>
  TWMAdRequest adRequest = new TWMAdRequest();
 
  if (request.getBirthday() != null) {
設定Network Title(名字可任意取),並設定實作的類別名稱與參數,以下以Banner作為範例:<br>
  adRequest.setBirthday(request.getBirthday());
[[image:Mopub_Step2-3.png | 700px]]<br><br>
  }
 
  if (request.getAgeInYears() != null) {
依欄位填入<br>
  adRequest.setAge(request.getAgeInYears());
<ol>
  }
<li>Custom Event Class: 用於實作com.mopub.mobileads.CustomEventBanner的類別名稱,必須輸入完整的package name,如com.taiwanmobile.pt.adp.mediation.TAMediaMopubBanner</li>
 
<li>Custom Event Class Data: 請依指定格式輸入您的版位ID (可向您的TAMedia窗口詢問),格式為 {"adUnitId":"<版位ID>"}</li>
  if (request.getGender() != null) {
</ol>
  if (request.getGender().equals(AdRequest.Gender.FEMALE)) {
輸入完畢後,後台會自動儲存<br>
  adRequest.setGender(TWMAdRequest.Gender.FEMALE);
[[image:Mopub_Step2-4.png | 700px]]<br><br>
  } else if (request.getGender().equals(AdRequest.Gender.MALE)) {
 
  adRequest.setGender(TWMAdRequest.Gender.MALE);
最後進入Segment分頁,啟用方才設定的聯播網<br>
  } else {
[[image:Mopub_Step2-5.png | 700px]]<br><br>
  adRequest.setGender(TWMAdRequest.Gender.UNKNOWN);
 
  }
若有多個聯播網,可透過eCPM調整不同聯播網的優先權<br>
  }
[[image:Mopub_Step2-6.png | 700px]]<br><br>
 
<br>
  return adRequest;
<br>
  }
 
 
== Mopub程式整合 ==
  //將AdMob的AdSize轉換為TWMAdSize
<big><u>Step 1: Download TAMedia & Mopub SDK</u></big><br>
  private TWMAdSize convertAdSize(AdSize adSize) {
:a. 請參照 [https://github.com/mopub/mopub-android-sdk/wiki/Getting-Started#requirements-and-dependencies Mopub Github] 所提供的方式,將Mopub SDK加到您的專案中。<br>
  if(adSize.equals(AdSize.BANNER)){
:b. [[Download Android SDK 2 | Download TAMedia SDK]]<br><br>
  return TWMAdSize.BANNER;
 
  }else if(adSize.equals(AdSize.IAB_BANNER)){
<big><u>Step 2: 在AndroidManifest.xml加入所需的權限與Activity</u></big><br>
  return TWMAdSize.IAB_BANNER;
   &lt;!-- Mopub & TAMedia Permission --&gt;
  }else if(adSize.equals(AdSize.IAB_MRECT)){
   <uses-permission android:name="android.permission.INTERNET" />
  return TWMAdSize.IAB_MRECT;
   <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  }else if(adSize.equals(AdSize.IAB_LEADERBOARD)){
   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> &lt;!-- optional --&gt;
  return TWMAdSize.IAB_LEADERBOARD;
   <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> &lt;!-- optional --&gt;<br>
  }else if(adSize.equals(AdSize.SMART_BANNER)){
   &lt;!-- TAMedia Permission --&gt;
          return TWMAdSize.SMART_BANNER;
   <uses-permission android:name="android.permission.READ_PHONE_STATE" /> &lt;!-- SDK4.0以上版本請移除 --&gt;
  }else{
   <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
                        return TWMAdSize.BANNER;
   <uses-permission android:name="android.permission.VIBRATE" /> &lt;!-- optional --&gt;
  }
  <uses-permission android:name="android.permission.CAMERA"/>  &lt;!-- optional, SDK4.0上請加入, 以獲得更豐富的廣告內容 --&gt;
  }
   <uses-permission android:name="android.permission.RECORD_AUDIO"/>  &lt;!-- optional, SDK4.0上請加入, 以獲得更豐富的廣告內容 --&gt;<br>
 
   &lt;!-- Mopub Permission --&gt;
 
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  private TWMAdView adView = null;
 
 
   &lt;!-- TAMedia Activities --&gt;
  @Override
   <activity
  public void requestBannerAd(final CustomEventBannerListener customListener, Activity activity,
  android:name="com.taiwanmobile.pt.adp.view.TWMAdActivity"
  String label, String serverParameter, AdSize adSize, MediationAdRequest request,
   android:configChanges="orientation|keyboardHidden|navigation|keyboard|screenLayout|uiMode|screenSize|smallestScreenSize"
  Object extras) {
   android:hardwareAccelerated="true"
  Log.d(TAG, "requestBannerAd("+label+"/"+serverParameter+") invoked!!");
   android:theme="@android:style/Theme.Translucent" >
 
   </activity>
  adView = new TWMAdView(activity,convertAdSize(adSize),label);
 
  adView.setAdListener(new TWMAdViewListener(){
 
  @Override
  public void onReceiveAd(TWMAd ad) {
  customListener.onReceivedAd(adView);
 
  }
 
  @Override
  public void onFailedToReceiveAd(TWMAd ad, ErrorCode errorCode) {
  customListener.onFailedToReceiveAd();
 
  }
 
  @Override
  public void onPresentScreen(TWMAd ad) {
  customListener.onPresentScreen();
                                //若您需要在AdMob報表上觀察點擊率, 請加入CustomEventBannerListener.onClick()
                                customListener.onClick();
 
  }
 
  @Override
  public void onDismissScreen(TWMAd ad) {
  customListener.onDismissScreen();
 
  }
    
  @Override
  public void onLeaveApplication(TWMAd ad) {
  customListener.onLeaveApplication();
 
  }
 
  });
 
  adView.loadAd(convertAdRequest(request));
 
 
  }
    
  }
:::*Interstitial,依照下述方式將AdMob所提供的CustomEventInterstitial進行實作
  package com.taiwanmobile.pt.adp.mediation;
    
  import android.app.Activity;
    
  import com.google.ads.AdRequest;
  import com.google.ads.mediation.MediationAdRequest;
   import com.google.ads.mediation.customevent.CustomEventInterstitial;
  import com.google.ads.mediation.customevent.CustomEventInterstitialListener;
   import com.taiwanmobile.pt.adp.view.TWMAd;
  import com.taiwanmobile.pt.adp.view.TWMAdRequest;
   import com.taiwanmobile.pt.adp.view.TWMAdRequest.ErrorCode;
  import com.taiwanmobile.pt.adp.view.TWMAdViewListener;
   import com.taiwanmobile.pt.adp.view.TWMInterstitialAd;
   // 需將本class的完整類別名稱 com.taiwanmobile.pt.adp.mediation.TAMediaBanner註冊於mediation的設定頁面上, 如前述章節所述
  public class TAMediaInterstitial implements CustomEventInterstitial {
 
  // 將AdMob所提供的MediationAdRequest轉換為TWMAdRequest
  private TWMAdRequest convertAdRequest(MediationAdRequest request) {
  TWMAdRequest adRequest = new TWMAdRequest();
  if (request.getBirthday() != null) {
  adRequest.setBirthday(request.getBirthday());
  }
  if (request.getAgeInYears() != null) {
  adRequest.setAge(request.getAgeInYears());
  }
    
  if (request.getGender() != null) {
  if (request.getGender().equals(AdRequest.Gender.FEMALE)) {
  adRequest.setGender(TWMAdRequest.Gender.FEMALE);
  } else if (request.getGender().equals(AdRequest.Gender.MALE)) {
  adRequest.setGender(TWMAdRequest.Gender.MALE);
  } else {
  adRequest.setGender(TWMAdRequest.Gender.UNKNOWN);
  }
  }
    
  return adRequest;
  }
 
  @Override
  public void destroy() {
  // TODO Auto-generated method stub
 
  }
 
  private TWMInterstitialAd interstitialAd = null;
    
  @Override
  public void requestInterstitialAd(
  final CustomEventInterstitialListener customListener,
  Activity activity, String label, String serverParameter,
  MediationAdRequest request, Object extra) {
  interstitialAd = new TWMInterstitialAd(activity, serverParameter);
 
  interstitialAd.setAdListener(new TWMAdViewListener() {
 
  @Override
  public void onReceiveAd(TWMAd ad) {
  customListener.onReceivedAd();
 
  }
    
  @Override
  public void onFailedToReceiveAd(TWMAd ad, ErrorCode errorCode) {
  customListener.onFailedToReceiveAd();
    
  }
 
  @Override
  public void onPresentScreen(TWMAd ad) {
  customListener.onPresentScreen();
 
  }
 
  @Override
  public void onDismissScreen(TWMAd ad) {
  customListener.onDismissScreen();
 
  }
 
  @Override
  public void onLeaveApplication(TWMAd ad) {
  customListener.onLeaveApplication();
 
  }
 
  });
 
  interstitialAd.loadAd(convertAdRequest(request));
 
   }
 
   @Override
   public void showInterstitial() {
  if (interstitialAd != null) {
  interstitialAd.show();
  }
    
  }
    
    
   }
   &lt;!-- Mopub activity --&gt;
  <activity android:name="com.mopub.mobileads.MoPubActivity" android:configChanges="keyboardHidden|orientation|screenSize"/>
  <activity android:name="com.mopub.mobileads.MraidActivity" android:configChanges="keyboardHidden|orientation|screenSize"/>
  <activity android:name="com.mopub.common.MoPubBrowser" android:configChanges="keyboardHidden|orientation|screenSize"/>
  <activity android:name="com.mopub.mobileads.RewardedMraidActivity" android:configChanges="keyboardHidden|orientation|screenSize" />
  <activity android:name="com.mopub.mobileads.MraidVideoPlayerActivity" android:configChanges="keyboardHidden|orientation|screenSize"/>
  <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />


::[[Download_Android_SDK_2|Download Android SDK and Sample]]
  &lt;!-- Mopub meta-data --&gt;
::加入必要的權限<br/>TWMAd服務需使用”存取網路狀態”、”讀取約略位置”、”網路”等權限,請在AndroidManifest.xml檔案當中進行下列權限的宣告。
   <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> &lt;!-- optional --&gt;
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> &lt;!-- optional --&gt;
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.VIRBATE" /> &lt;!-- optional --&gt;
::加入必要Activity
  &lt;!-- TAMedia Activities --&gt;
<activity
  android:name="com.taiwanmobile.pt.adp.view.TWMAdActivity"         
   android:configChanges="orientation|keyboardHidden|navigation|keyboard|screenLayout|uiMode|screenSize|smallestScreenSize"
  android:hardwareAccelerated="true"
  android:theme="@android:style/Theme.Translucent" >
</activity>
</activity>
&lt;!-- Google Play Service Activities --&gt;
<meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
<activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
::在layout檔案中加入可放置廣告的LinearLayout(※僅供參考,開發者可依照自訂的layout進行配置使用)
<LinearLayout
android:id="@+id/Layout_AdView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
::在程式加入Ad的宣告
  public class MainActivity extends Activity {
  LinearLayout layout = null;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    layout = (LinearLayout) this.findViewById(R.id.Layout_AdView);
    adView = new AdView(this);
    adView.setAdUnitId("<Mediation ID>");
    adView.setAdSize(AdSize.BANNER);   
    layout.addView(adView);
    AdRequest request = new AdRequest.Builder().build();
    adView.loadAd(request);
  }
::*聯播網投放與維護
::當APP設定好Mediation並且發佈出去後,可直接透過Mediation網頁進行版位管理與聯播網比率調整,此設定會在1小時後生效。

於 2017年5月17日 (三) 09:11 的修訂

Mopub網站: https://app.mopub.com, 登入可對後台進行設定。
註: 請將擋廣告的插件關閉(如Adblock),否則後台瀏覽、設定會有問題

Mopub後台設定

Step 1: 新增APP與版位,取得Mopub版位ID
進入Apps分頁後,點擊Add a New App
Mopub Step1-1.png

輸入APP資訊,如APP Name、Package Name…等
Mopub Step1-2.png

新增版位並進行設定

a. Format為橫幅廣告Banner (320x50):

Mopub Step1-3.png

b. Format為插頁式廣告Fullscreen (320x480):

Mopub Step1-4.png

c. Format為原生廣告Native (Custom Layout):

Mopub Step1-5.png

完成設定後,點選Save and View Code Integration,並記下Mopub的版位ID
Mopub Step1-6.png

Mopub Step1-7.png

若欲新增其他版位,請點選剛剛新增的APP並點選Add an Ad Unit
Mopub Step1-8.png

Mopub Step1-9.png



Step 2: 新增廣告聯播網
進入Network分頁,並點選Add a Network
Mopub Step2-1.png

點選Custom Native Network
Mopub Step2-2.png

設定Network Title(名字可任意取),並設定實作的類別名稱與參數,以下以Banner作為範例:
Mopub Step2-3.png

依欄位填入

  1. Custom Event Class: 用於實作com.mopub.mobileads.CustomEventBanner的類別名稱,必須輸入完整的package name,如com.taiwanmobile.pt.adp.mediation.TAMediaMopubBanner
  2. Custom Event Class Data: 請依指定格式輸入您的版位ID (可向您的TAMedia窗口詢問),格式為 {"adUnitId":"<版位ID>"}

輸入完畢後,後台會自動儲存
Mopub Step2-4.png

最後進入Segment分頁,啟用方才設定的聯播網
Mopub Step2-5.png

若有多個聯播網,可透過eCPM調整不同聯播網的優先權
Mopub Step2-6.png



Mopub程式整合

Step 1: Download TAMedia & Mopub SDK

a. 請參照 Mopub Github 所提供的方式,將Mopub SDK加到您的專案中。
b. Download TAMedia SDK

Step 2: 在AndroidManifest.xml加入所需的權限與Activity

 <!-- Mopub & TAMedia Permission -->
 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- optional -->
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- optional -->
<!-- TAMedia Permission --> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <!-- SDK4.0以上版本請移除 --> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.VIBRATE" /> <!-- optional --> <uses-permission android:name="android.permission.CAMERA"/> <!-- optional, SDK4.0上請加入, 以獲得更豐富的廣告內容 --> <uses-permission android:name="android.permission.RECORD_AUDIO"/> <!-- optional, SDK4.0上請加入, 以獲得更豐富的廣告內容 -->
<!-- Mopub Permission --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <!-- TAMedia Activities -->
 <activity
 	android:name="com.taiwanmobile.pt.adp.view.TWMAdActivity"
 	android:configChanges="orientation|keyboardHidden|navigation|keyboard|screenLayout|uiMode|screenSize|smallestScreenSize"
 	android:hardwareAccelerated="true"
 	android:theme="@android:style/Theme.Translucent" >
 </activity>
 
 <!-- Mopub activity -->
 <activity android:name="com.mopub.mobileads.MoPubActivity" android:configChanges="keyboardHidden|orientation|screenSize"/>
 <activity android:name="com.mopub.mobileads.MraidActivity" android:configChanges="keyboardHidden|orientation|screenSize"/>
 <activity android:name="com.mopub.common.MoPubBrowser" android:configChanges="keyboardHidden|orientation|screenSize"/>
 <activity android:name="com.mopub.mobileads.RewardedMraidActivity" android:configChanges="keyboardHidden|orientation|screenSize" />
 <activity android:name="com.mopub.mobileads.MraidVideoPlayerActivity" android:configChanges="keyboardHidden|orientation|screenSize"/>
 <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
 <!-- Mopub meta-data  -->
 <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>