ตั้งค่า IMA SDK

เลือกแพลตฟอร์ม: HTML5 Android iOS tvOS

IMA SDK ช่วยให้ผสานรวมโฆษณามัลติมีเดียลงในเว็บไซต์และแอปได้ง่ายๆ IMA SDK สามารถขอโฆษณาจากเซิร์ฟเวอร์โฆษณาที่เป็นไปตามข้อกำหนด VAST และจัดการการเล่นโฆษณาในแอปได้ SDK ฝั่งไคลเอ็นต์ IMA ช่วยให้คุณควบคุมการเล่นวิดีโอเนื้อหาได้ ขณะที่ SDK จะจัดการการเล่นโฆษณา โฆษณาจะเล่นในวิดีโอเพลเยอร์แยกต่างหากซึ่งอยู่ด้านบนของวิดีโอเพลเยอร์เนื้อหาของแอป

คู่มือนี้แสดงวิธีผสานรวม IMA SDK เข้ากับแอปวิดีโอเพลเยอร์ หากต้องการดูหรือทำตามตัวอย่างการผสานรวมที่เสร็จสมบูรณ์ ให้ดาวน์โหลด BasicExample จาก GitHub

ภาพรวมฝั่งไคลเอ็นต์ของ IMA

การติดตั้งใช้งานฝั่งไคลเอ็นต์ IMA เกี่ยวข้องกับคอมโพเนนต์ SDK หลัก 4 รายการ ซึ่งคู่มือนี้จะสาธิตให้เห็นดังนี้

  • IMAAdDisplayContainer: ออบเจ็กต์คอนเทนเนอร์ที่ระบุตำแหน่งที่ IMA แสดงผลองค์ประกอบ UI ของโฆษณาและวัดการมองเห็นโฆษณา ซึ่งรวมถึงมุมมองแอ็กทีฟและ Open Measurement
  • IMAAdsLoader: ออบเจ็กต์ที่ขอโฆษณาและจัดการเหตุการณ์จากการตอบกลับคำขอโฆษณา คุณควรสร้างอินสแตนซ์ตัวโหลดโฆษณาเพียงรายการเดียว ซึ่งสามารถนำมาใช้ซ้ำได้ตลอดอายุการใช้งานของแอปพลิเคชัน
  • IMAAdsRequest: ออบเจ็กต์ที่กำหนดคำขอโฆษณา คำขอโฆษณาจะระบุ URL สำหรับแท็กโฆษณา VAST รวมถึงพารามิเตอร์เพิ่มเติม เช่น ขนาดโฆษณา
  • IMAAdsManager: ออบเจ็กต์ที่มีการตอบกลับคำขอโฆษณา ควบคุมการเล่นโฆษณา และฟังเหตุการณ์โฆษณาที่ SDK เริ่มทำงาน

ข้อกำหนดเบื้องต้น

ก่อนเริ่มต้น คุณต้องมีสิ่งต่อไปนี้

1. สร้างโปรเจ็กต์ Xcode ใหม่

ใน Xcode ให้สร้างโปรเจ็กต์ iOS ใหม่โดยใช้ Objective-C หรือ Swift ใช้ BasicExample เป็นชื่อโปรเจ็กต์

2. เพิ่ม IMA SDK ลงในโปรเจ็กต์ Xcode

หากต้องการติดตั้ง IMA SDK ให้เลือกวิธีการที่ต้องการ

แนะนำ: ติดตั้ง SDK โดยใช้ Swift Package Manager

SDK โฆษณาสื่ออินเทอร์แอกทีฟรองรับ Swift Package Manager ตั้งแต่เวอร์ชัน 3.18.4 เป็นต้นไป หากต้องการนําเข้าแพ็กเกจ Swift ให้ทําตามขั้นตอนต่อไปนี้

  1. ใน Xcode ให้ติดตั้งแพ็กเกจ Swift ของ IMA SDK โดยไปที่ไฟล์ > เพิ่มทรัพยากร Dependency ของแพ็กเกจ...

  2. ในข้อความแจ้ง ให้ค้นหาที่เก็บ GitHub ของแพ็กเกจ Swift สำหรับ IMA iOS SDK ดังนี้ swift-package-manager-google-interactive-media-ads-ios

  3. เลือกเวอร์ชันของแพ็กเกจ IMA SDK Swift ที่ต้องการใช้ สำหรับโปรเจ็กต์ใหม่ เราขอแนะนำให้ใช้เวอร์ชันหลักถัดไป

หลังจากดำเนินการเสร็จแล้ว Xcode จะแก้ไขการขึ้นต่อกันของแพ็กเกจและดาวน์โหลดไว้ในเบื้องหลัง ดูรายละเอียดเพิ่มเติมเกี่ยวกับวิธีเพิ่มทรัพยากร Dependency ของแพ็กเกจได้ที่บทความของ Apple

ดาวน์โหลดและติดตั้ง SDK ด้วยตนเอง

หากไม่ต้องการใช้ Swift Package Manager ให้ดาวน์โหลดและเพิ่ม IMA SDK ลงในโปรเจ็กต์ด้วยตนเอง

3. สร้างวิดีโอเพลเยอร์

ขั้นแรก ให้ติดตั้งใช้งานวิดีโอเพลเยอร์ ในตอนแรก เพลเยอร์นี้ไม่ได้ใช้ IMA SDK และไม่มีวิธีการใดๆ ในการทริกเกอร์การเล่น

Objective-C

นำเข้าทรัพยากร Dependency ของโปรแกรมเล่น

#import "ViewController.h"

@import AVFoundation;

ตั้งค่าตัวแปรเพลเยอร์ดังนี้

@interface ViewController () <IMAAdsLoaderDelegate, IMAAdsManagerDelegate>

/// Content video player.
@property(nonatomic, strong) AVPlayer *contentPlayer;

/// Play button.
@property(nonatomic, weak) IBOutlet UIButton *playButton;

/// UIView in which we will render our AVPlayer for content.
@property(nonatomic, weak) IBOutlet UIView *videoView;

เริ่มวิดีโอเพลเยอร์เมื่อโหลดมุมมอง

@implementation ViewController

// The content URL to play.
NSString *const kTestAppContentUrl_MP4 =
    @"https://storage.googleapis.com/gvabox/media/samples/stock.mp4";

// Ad tag
NSString *const kTestAppAdTagUrl = @"https://pubads.g.doubleclick.net/gampad/ads?"
  @"iu=/21775744923/external/single_ad_samples&sz=640x480&cust_params=sample_ct%3Dlinear&"
  @"ciu_szs=300x250%2C728x90&gdfp_req=1&output=vast&unviewed_position_start=1&env=vp&"
  @"correlator=";

- (void)viewDidLoad {
  [super viewDidLoad];

  self.playButton.layer.zPosition = MAXFLOAT;

  [self setupAdsLoader];
  [self setUpContentPlayer];
}

#pragma mark Content Player Setup

- (void)setUpContentPlayer {
  // Load AVPlayer with path to our content.
  NSURL *contentURL = [NSURL URLWithString:kTestAppContentUrl_MP4];
  self.contentPlayer = [AVPlayer playerWithURL:contentURL];

  // Create a player layer for the player.
  AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.contentPlayer];

  // Size, position, and display the AVPlayer.
  playerLayer.frame = self.videoView.layer.bounds;
  [self.videoView.layer addSublayer:playerLayer];

  // Set up our content playhead and contentComplete callback.
  self.contentPlayhead = [[IMAAVPlayerContentPlayhead alloc] initWithAVPlayer:self.contentPlayer];
  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(contentDidFinishPlaying:)
                                               name:AVPlayerItemDidPlayToEndTimeNotification
                                             object:self.contentPlayer.currentItem];
}

- (IBAction)onPlayButtonTouch:(id)sender {
  [self requestAds];
  self.playButton.hidden = YES;
}

Swift

นำเข้าทรัพยากร Dependency ของโปรแกรมเล่น

import AVFoundation

ตั้งค่าตัวแปรเพลเยอร์ดังนี้

class PlayerContainerViewController: UIViewController, IMAAdsLoaderDelegate, IMAAdsManagerDelegate {
  static let contentURL = URL(
    string: "https://storage.googleapis.com/gvabox/media/samples/stock.mp4")!

  private var contentPlayer = AVPlayer(url: PlayerContainerViewController.contentURL)

  private lazy var playerLayer: AVPlayerLayer = {
    AVPlayerLayer(player: contentPlayer)
  }()

เริ่มวิดีโอเพลเยอร์เมื่อโหลดมุมมอง

private lazy var videoView: UIView = {
  let videoView = UIView()
  videoView.translatesAutoresizingMaskIntoConstraints = false
  view.addSubview(videoView)

  NSLayoutConstraint.activate([
    videoView.bottomAnchor.constraint(
      equalTo: view.safeAreaLayoutGuide.bottomAnchor),
    videoView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
    videoView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
    videoView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
  ])
  return videoView
}()

// MARK: - View controller lifecycle methods

override func viewDidLoad() {
  super.viewDidLoad()

  videoView.layer.addSublayer(playerLayer)
  adsLoader.delegate = self

  NotificationCenter.default.addObserver(
    self,
    selector: #selector(contentDidFinishPlaying(_:)),
    name: .AVPlayerItemDidPlayToEndTime,
    object: contentPlayer.currentItem)
}

override func viewDidAppear(_ animated: Bool) {
  super.viewDidAppear(animated)
  playerLayer.frame = videoView.layer.bounds
}

override func viewWillTransition(
  to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator
) {
  coordinator.animate { _ in
    // do nothing
  } completion: { _ in
    self.playerLayer.frame = self.videoView.layer.bounds
  }
}

// MARK: - Public methods

func playButtonPressed() {
  requestAds()
}

4. นำเข้า IMA SDK

หากต้องการนําเข้า IMA SDK ให้ทําดังนี้

Objective-C

  1. นำเข้า IMA SDK โดยทำดังนี้

    @import GoogleInteractiveMediaAds;
    
  2. สร้างตัวแปรสำหรับคลาส IMAAdsLoader, IMAAVPlayerContentPlayhead และ IMAAdsManager ที่ใช้ในแอป ดังนี้

    // SDK
    /// Entry point for the SDK. Used to make ad requests.
    @property(nonatomic, strong) IMAAdsLoader *adsLoader;
    
    /// Playhead used by the SDK to track content video progress and insert mid-rolls.
    @property(nonatomic, strong) IMAAVPlayerContentPlayhead *contentPlayhead;
    
    /// Main point of interaction with the SDK. Created by the SDK as the result of an ad request.
    @property(nonatomic, strong) IMAAdsManager *adsManager;
    

Swift

  1. นำเข้า IMA SDK โดยทำดังนี้

    import GoogleInteractiveMediaAds
    
    
  2. สร้างตัวแปรสำหรับคลาส IMAAdsLoader, IMAAVPlayerContentPlayhead และ IMAAdsManager ที่ใช้ในแอป ดังนี้

    static let adTagURLString =
      "https://pubads.g.doubleclick.net/gampad/ads?iu=/21775744923/external/"
      + "single_ad_samples&sz=640x480&cust_params=sample_ct%3Dlinear&ciu_szs=300x250%2C728x90&"
      + "gdfp_req=1&output=vast&unviewed_position_start=1&env=vp&correlator="
    
    private let adsLoader = IMAAdsLoader()
    private var adsManager: IMAAdsManager?
    
    private lazy var contentPlayhead: IMAAVPlayerContentPlayhead = {
      IMAAVPlayerContentPlayhead(avPlayer: contentPlayer)
    }()
    

5. ใช้เครื่องมือติดตามส่วนหัวของตัวควบคุมการเล่นเนื้อหาและตัวสังเกตการณ์การสิ้นสุดสตรีม

IMA SDK ต้องติดตามตำแหน่งปัจจุบันของเนื้อหาวิดีโอเพื่อเล่นโฆษณาตอนกลาง ซึ่งทำได้โดยสร้างคลาสที่ใช้ IMAContentPlayhead หากคุณใช้ AVPlayer ดังที่แสดงในตัวอย่างนี้ SDK จะมีคลาส IMAAVPlayerContentPlayhead ซึ่งจะดำเนินการนี้ให้คุณ หากไม่ได้ใช้ AVPlayer คุณต้องใช้ IMAContentPlayhead ในคลาสของคุณเอง

นอกจากนี้ คุณยังต้องแจ้งให้ SDK ทราบเมื่อเนื้อหาเล่นจบแล้ว เพื่อให้ SDK แสดงโฆษณาตอนท้ายได้ โดยเรียกใช้เมธอด contentComplete ใน IMAAdsLoader โดยใช้ AVPlayerItemDidPlayToEndTimeNotification

Objective-C

สร้างอินสแตนซ์ IMAAVPlayerContentPlayhead ในการตั้งค่าเพลเยอร์ โดยทำดังนี้

// Set up our content playhead and contentComplete callback.
self.contentPlayhead = [[IMAAVPlayerContentPlayhead alloc] initWithAVPlayer:self.contentPlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(contentDidFinishPlaying:)
                                             name:AVPlayerItemDidPlayToEndTimeNotification
                                           object:self.contentPlayer.currentItem];

สร้างเมธอด contentDidFinishPlaying() เพื่อเรียกใช้ IMAAdsLoader.contentComplete() เมื่อเนื้อหาเล่นจบ

- (void)contentDidFinishPlaying:(NSNotification *)notification {
  // Make sure we don't call contentComplete as a result of an ad completing.
  if (notification.object == self.contentPlayer.currentItem) {
    [self.adsLoader contentComplete];
  }
}

Swift

สร้างออบเซอร์เวอร์ที่ตรวจจับการสิ้นสุดของเนื้อหาในการตั้งค่าโปรแกรมเล่น ดังนี้

NotificationCenter.default.addObserver(
  self,
  selector: #selector(contentDidFinishPlaying(_:)),
  name: .AVPlayerItemDidPlayToEndTime,
  object: contentPlayer.currentItem)

สร้างเมธอด contentDidFinishPlaying() เพื่อเรียกใช้ IMAAdsLoader.contentComplete() เมื่อเนื้อหาเล่นจบ

@objc func contentDidFinishPlaying(_ notification: Notification) {
  // Make sure we don't call contentComplete as a result of an ad completing.
  if notification.object as? AVPlayerItem == contentPlayer.currentItem {
    adsLoader.contentComplete()
  }
}

6. เริ่มต้นตัวโหลดโฆษณาและส่งคำขอโฆษณา

หากต้องการขอชุดโฆษณา คุณต้องสร้างอินสแตนซ์ IMAAdsLoader ตัวโหลดนี้จะประมวลผลออบเจ็กต์ IMAAdsRequest ที่เชื่อมโยงกับ URL แท็กโฆษณาที่ระบุ

แนวทางปฏิบัติแนะนำคือให้คง IMAAdsLoader ไว้เพียงอินสแตนซ์เดียวตลอดวงจรของแอป หากต้องการส่งคำขอโฆษณาเพิ่มเติม ให้สร้างออบเจ็กต์ IMAAdsRequest ใหม่ แต่ใช้ IMAAdsLoader เดิมซ้ำ ดูข้อมูลเพิ่มเติมได้ที่คำถามที่พบบ่อยเกี่ยวกับ IMA SDK

Objective-C

- (void)setupAdsLoader {
  self.adsLoader = [[IMAAdsLoader alloc] initWithSettings:nil];
  self.adsLoader.delegate = self;
}

- (void)requestAds {
  // Create an ad display container for ad rendering.
  IMAAdDisplayContainer *adDisplayContainer =
      [[IMAAdDisplayContainer alloc] initWithAdContainer:self.videoView
                                          viewController:self
                                          companionSlots:nil];
  // Create an ad request with our ad tag, display container, and optional user context.
  IMAAdsRequest *request = [[IMAAdsRequest alloc] initWithAdTagUrl:kTestAppAdTagUrl
                                                adDisplayContainer:adDisplayContainer
                                                   contentPlayhead:self.contentPlayhead
                                                       userContext:nil];
  [self.adsLoader requestAdsWithRequest:request];
}

Swift

private func requestAds() {
  // Create ad display container for ad rendering.
  let adDisplayContainer = IMAAdDisplayContainer(
    adContainer: videoView, viewController: self, companionSlots: nil)
  // Create an ad request with our ad tag, display container, and optional user context.
  let request = IMAAdsRequest(
    adTagUrl: PlayerContainerViewController.adTagURLString,
    adDisplayContainer: adDisplayContainer,
    contentPlayhead: contentPlayhead,
    userContext: nil)

  adsLoader.requestAds(with: request)
}

7. ตั้งค่าผู้รับมอบสิทธิ์ตัวโหลดโฆษณา

เมื่อเหตุการณ์การโหลดสำเร็จ IMAAdsLoader จะเรียกใช้เมธอด adsLoadedWithData ของผู้รับมอบสิทธิ์ที่ได้รับมอบหมาย โดยส่งอินสแตนซ์ของ IMAAdsManager ไปให้ จากนั้นคุณสามารถเริ่มต้นใช้งาน Ad Manager ซึ่งจะโหลดโฆษณาเดี่ยวตามที่กำหนดโดยการตอบกลับ URL ของแท็กโฆษณา

นอกจากนี้ โปรดตรวจสอบว่าได้จัดการข้อผิดพลาดที่อาจเกิดขึ้นระหว่างกระบวนการโหลดแล้ว หากโฆษณาไม่โหลด ให้ตรวจสอบว่าการเล่นสื่อยังคงดำเนินต่อไปโดยไม่มีโฆษณา เพื่อไม่ให้รบกวนประสบการณ์ของผู้ใช้

Objective-C

- (void)adsLoader:(IMAAdsLoader *)loader adsLoadedWithData:(IMAAdsLoadedData *)adsLoadedData {
  // Grab the instance of the IMAAdsManager and set ourselves as the delegate.
  self.adsManager = adsLoadedData.adsManager;
  self.adsManager.delegate = self;
  // Create ads rendering settings to tell the SDK to use the in-app browser.
  IMAAdsRenderingSettings *adsRenderingSettings = [[IMAAdsRenderingSettings alloc] init];
  adsRenderingSettings.linkOpenerPresentingController = self;
  // Initialize the ads manager.
  [self.adsManager initializeWithAdsRenderingSettings:adsRenderingSettings];
}

- (void)adsLoader:(IMAAdsLoader *)loader failedWithErrorData:(IMAAdLoadingErrorData *)adErrorData {
  // Something went wrong loading ads. Log the error and play the content.
  NSLog(@"Error loading ads: %@", adErrorData.adError.message);
  [self.contentPlayer play];
}

Swift

func adsLoader(_ loader: IMAAdsLoader, adsLoadedWith adsLoadedData: IMAAdsLoadedData) {
  // Grab the instance of the IMAAdsManager and set ourselves as the delegate.
  adsManager = adsLoadedData.adsManager
  adsManager?.delegate = self

  // Create ads rendering settings and tell the SDK to use the in-app browser.
  let adsRenderingSettings = IMAAdsRenderingSettings()
  adsRenderingSettings.linkOpenerPresentingController = self

  // Initialize the ads manager.
  adsManager?.initialize(with: adsRenderingSettings)
}

func adsLoader(_ loader: IMAAdsLoader, failedWith adErrorData: IMAAdLoadingErrorData) {
  if let message = adErrorData.adError.message {
    print("Error loading ads: \(message)")
  }
  contentPlayer.play()
}

8. ตั้งค่าผู้รับมอบสิทธิ์ Ad Manager

สุดท้าย ในการจัดการเหตุการณ์และการเปลี่ยนแปลงสถานะ ตัวจัดการโฆษณาต้องมีผู้รับมอบสิทธิ์ของตนเอง IMAAdManagerDelegate มีเมธอดในการจัดการเหตุการณ์และข้อผิดพลาดของโฆษณา รวมถึงเมธอดในการทริกเกอร์การเล่นและการหยุดชั่วคราวในเนื้อหาวิดีโอ

เริ่มเล่น

รอเหตุการณ์ LOADED เพื่อเริ่มเล่นเนื้อหาและโฆษณา ดูรายละเอียดเพิ่มเติมได้ที่ didReceiveAdEvent

Objective-C

- (void)adsManager:(IMAAdsManager *)adsManager didReceiveAdEvent:(IMAAdEvent *)event {
  // When the SDK notified us that ads have been loaded, play them.
  if (event.type == kIMAAdEvent_LOADED) {
    [adsManager start];
  }
}

Swift

func adsManager(_ adsManager: IMAAdsManager, didReceive event: IMAAdEvent) {
  // When the SDK notifies us the ads have been loaded, play them.
  if event.type == IMAAdEventType.LOADED {
    adsManager.start()
  }
}

จัดการข้อผิดพลาด

เพิ่มเครื่องมือจัดการข้อผิดพลาดของโฆษณาด้วย หากเกิดข้อผิดพลาดเหมือนในขั้นตอนก่อนหน้า ให้เล่นเนื้อหาต่อ

Objective-C

- (void)adsManager:(IMAAdsManager *)adsManager didReceiveAdError:(IMAAdError *)error {
  // Something went wrong with the ads manager after ads were loaded. Log the error and play the
  // content.
  NSLog(@"AdsManager error: %@", error.message);
  [self.contentPlayer play];
}

Swift

func adsManager(_ adsManager: IMAAdsManager, didReceive error: IMAAdError) {
  // Something went wrong with the ads manager after ads were loaded.
  // Log the error and play the content.
  if let message = error.message {
    print("AdsManager error: \(message)")
  }
  contentPlayer.play()
}

ฟังเหตุการณ์การเล่นและการหยุดชั่วคราว

เมธอด Delegate 2 รายการสุดท้ายที่คุณต้องใช้จะทริกเกอร์เหตุการณ์การเล่นและหยุดชั่วคราวในเนื้อหาวิดีโอที่เกี่ยวข้องเมื่อ IMA SDK ส่งคำขอ การทริกเกอร์การหยุดชั่วคราวและการเล่นเมื่อมีการขอจะช่วยป้องกันไม่ให้ผู้ใช้พลาดเนื้อหาวิดีโอบางส่วนเมื่อโฆษณาแสดง

Objective-C

- (void)adsManagerDidRequestContentPause:(IMAAdsManager *)adsManager {
  // The SDK is going to play ads, so pause the content.
  [self.contentPlayer pause];
}

- (void)adsManagerDidRequestContentResume:(IMAAdsManager *)adsManager {
  // The SDK is done playing ads (at least for now), so resume the content.
  [self.contentPlayer play];
}

Swift

func adsManagerDidRequestContentPause(_ adsManager: IMAAdsManager) {
  // The SDK is going to play ads, so pause the content.
  contentPlayer.pause()
}

func adsManagerDidRequestContentResume(_ adsManager: IMAAdsManager) {
  // The SDK is done playing ads (at least for now), so resume the content.
  contentPlayer.play()
}

เท่านี้ก็เรียบร้อย ตอนนี้คุณกำลังขอและแสดงโฆษณาด้วย IMA SDK หากต้องการดูข้อมูลเกี่ยวกับฟีเจอร์เพิ่มเติมของ SDK โปรดดูคู่มืออื่นๆ หรือตัวอย่างใน GitHub

ขั้นตอนถัดไป

หากต้องการเพิ่มรายได้จากโฆษณาในแพลตฟอร์ม iOS ให้สูงสุด ให้ขอสิทธิ์ App Transparency and Tracking เพื่อใช้ IDFA