เริ่มใช้งาน

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

SDK สำหรับ User Messaging Platform (UMP) ของ Google เป็นเครื่องมือด้านความเป็นส่วนตัวและการรับส่งข้อความที่จะช่วยคุณจัดการตัวเลือกความเป็นส่วนตัว ดูข้อมูลเพิ่มเติมได้ที่ หัวข้อเกี่ยวกับความเป็นส่วนตัวและการรับส่งข้อความ คุณสามารถดูการติดตั้งใช้งาน IMA ที่ใช้งานได้กับ UMP SDK ในแอปตัวอย่าง UMP ของ Objective-C หรือ Swift

สร้างประเภทข้อความ

สร้างข้อความสำหรับผู้ใช้ด้วยประเภทข้อความสำหรับผู้ใช้ประเภทใดประเภทหนึ่งที่มีให้ใช้งานในแท็บความเป็นส่วนตัวและการรับส่งข้อความ ของบัญชี Ad Manager UMP SDK จะพยายามแสดงประกาศเกี่ยวกับความเป็นส่วนตัวที่สร้างจากรหัสแอปพลิเคชัน Interactive Media Ads ที่ตั้งค่าไว้ในโปรเจ็กต์

ดูรายละเอียดเพิ่มเติมได้ที่ หัวข้อเกี่ยวกับความเป็นส่วนตัวและการรับส่งข้อความ

นำเข้า SDK

UMP SDK ไม่ได้รวมอยู่ในการขึ้นต่อกันของ IMA SDK ดังนั้นคุณต้องเพิ่ม SDK ด้วยตนเอง

CocoaPods (แนะนำ)

วิธีที่ง่ายที่สุดในการนำเข้า SDK ลงในโปรเจ็กต์ iOS คือการใช้ CocoaPods เปิด Podfile ของโปรเจ็กต์แล้วเพิ่มบรรทัดนี้ลงในเป้าหมายของแอป

pod 'GoogleUserMessagingPlatform'

จากนั้นเรียกใช้คำสั่งต่อไปนี้

pod install --repo-update

หากยังไม่คุ้นเคยกับ CocoaPods โปรดดูรายละเอียดเกี่ยวกับวิธีสร้างและใช้ Podfile ได้ที่หัวข้อการใช้ CocoaPods

Swift Package Manager

UMP SDK ยังรองรับ Swift Package Manager ด้วย ทำตามขั้นตอนต่อไปนี้เพื่อนำเข้าแพ็กเกจ Swift

  1. ใน Xcode ให้ติดตั้งแพ็กเกจ UMP SDK Swift โดยไปที่File > Add Packages...

  2. ในข้อความแจ้งที่ปรากฏขึ้น ให้ค้นหารีโปสิทอรี GitHub ของแพ็กเกจ UMP SDK Swift

    https://github.com/googleads/swift-package-manager-google-user-messaging-platform.git
    
  3. เลือกเวอร์ชันของแพ็กเกจ UMP SDK Swift ที่ต้องการใช้ สำหรับโปรเจ็กต์ใหม่ เราขอแนะนำให้ใช้Up to Next Major Version

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

เพิ่มรหัสแอปพลิเคชัน

คุณดูรหัสแอปพลิเคชันได้ใน UI ของ Ad Manager เพิ่มรหัสลงใน Info.plist ด้วยข้อมูลโค้ดต่อไปนี้

<key>UMPApplicationIdentifier</key>
<string>ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy</string>

คุณควรขอให้อัปเดตข้อมูลความยินยอมของผู้ใช้ทุกครั้งที่เปิดแอป โดยใช้ requestConsentInfoUpdateWithParameters:completionHandler: คำขอนี้จะตรวจสอบสิ่งต่อไปนี้

  • ต้องได้รับความยินยอมหรือไม่ เช่น ต้องได้รับความยินยอมเป็นครั้งแรก หรือการตัดสินใจให้ความยินยอมครั้งก่อนหมดอายุแล้ว
  • ต้องมีจุดแรกเข้าของตัวเลือกความเป็นส่วนตัวหรือไม่ ข้อความความเป็นส่วนตัวบางข้อความกำหนดให้แอปต้องอนุญาตให้ผู้ใช้แก้ไขตัวเลือกความเป็นส่วนตัวได้ทุกเมื่อ

Swift


// Requesting an update to consent information should be called on every app launch.
ConsentInformation.shared.requestConsentInfoUpdate(with: parameters) {
  requestConsentError in
  // ...
}

Objective-C


// Requesting an update to consent information should be called on every app launch.
[UMPConsentInformation.sharedInstance
    requestConsentInfoUpdateWithParameters:parameters
                         completionHandler:^(NSError *_Nullable requestConsentError) {
                           // ...
                         }];

โหลดและแสดงแบบฟอร์มประกาศเกี่ยวกับความเป็นส่วนตัว

หลังจากได้รับสถานะความยินยอมที่อัปเดตล่าสุดแล้ว ให้เรียกใช้ loadAndPresentIfRequiredFromViewController:completionHandler: เพื่อโหลดแบบฟอร์มที่จำเป็นสำหรับการ รวบรวมความยินยอมของผู้ใช้ หลังจากโหลดแล้ว แบบฟอร์มจะแสดงขึ้นทันที

Swift


try await ConsentForm.loadAndPresentIfRequired(from: viewController)

Objective-C


[UMPConsentForm
    loadAndPresentIfRequiredFromViewController:viewController
                             completionHandler:^(NSError *_Nullable loadAndPresentError) {
                                 // Consent gathering process is complete.
                                }];

ตัวเลือกความเป็นส่วนตัว

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

ตรวจสอบว่าต้องมีจุดแรกเข้าของตัวเลือกความเป็นส่วนตัวหรือไม่

หลังจากเรียกใช้ requestConsentInfoUpdateWithParameters:completionHandler: แล้ว ให้ตรวจสอบ UMPConsentInformation.sharedInstance.privacyOptionsRequirementStatus เพื่อ ดูว่าแอปของคุณต้องมีจุดแรกเข้าของตัวเลือกความเป็นส่วนตัวหรือไม่ หากต้องมีจุด แรกเข้า ให้เพิ่มองค์ประกอบ UI ที่มองเห็นได้และโต้ตอบได้ลงในแอป ซึ่งจะ แสดงแบบฟอร์มตัวเลือกความเป็นส่วนตัว หากไม่จำเป็นต้องมีจุดแรกเข้าของความเป็นส่วนตัว ให้กำหนดค่าองค์ประกอบ UI ให้มองไม่เห็นและโต้ตอบไม่ได้

Swift


var isPrivacyOptionsRequired: Bool {
  return ConsentInformation.shared.privacyOptionsRequirementStatus == .required
}

Objective-C


- (BOOL)isPrivacyOptionsRequired {
  return UMPConsentInformation.sharedInstance.privacyOptionsRequirementStatus ==
         UMPPrivacyOptionsRequirementStatusRequired;
}

- (void)gatherConsentFromConsentPresentationViewController:(UIViewController *)viewController
                                  consentGatheringComplete:
                                      (void (^)(NSError *_Nullable))consentGatheringComplete {
  UMPRequestParameters *parameters = [[UMPRequestParameters alloc] init];
  // Set tag for under age of consent. Use NO constant to indicate that the user is not under age.
  parameters.tagForUnderAgeOfConsent = NO;

  UMPDebugSettings *debugSettings = [[UMPDebugSettings alloc] init];
  // Uncomment the following line to simulate a consent request from users in the
  // European Economic Area (EEA) for testing purposes.
  // debugSettings.geography = UMPDebugGeographyEEA;
  parameters.debugSettings = debugSettings;

  // Requesting an update to consent information should be called on every app launch.
  [UMPConsentInformation.sharedInstance
      requestConsentInfoUpdateWithParameters:parameters
                           completionHandler:^(NSError *_Nullable requestConsentError) {
                             // ...
                           }];
}

- (void)loadAndPresentIfRequiredFromViewController:(UIViewController *)viewController
                           completionHandler:(void (^)(NSError *_Nullable))completionHandler {
  [UMPConsentForm
      loadAndPresentIfRequiredFromViewController:viewController
                               completionHandler:^(NSError *_Nullable loadAndPresentError) {
                                   // Consent gathering process is complete.
                                  }];
}

- (void)presentPrivacyOptionsFormFromViewController:(UIViewController *)viewController
                                  completionHandler:
                                      (void (^)(NSError *_Nullable))completionHandler {
  [UMPConsentForm presentPrivacyOptionsFormFromViewController:viewController
                                            completionHandler:completionHandler];
}

@end

ดูรายการสถานะข้อกำหนดตัวเลือกความเป็นส่วนตัวทั้งหมดได้ที่ UMPPrivacyOptionsRequirementStatus

แสดงแบบฟอร์มตัวเลือกความเป็นส่วนตัว

เมื่อผู้ใช้โต้ตอบกับองค์ประกอบของคุณ ให้แสดงแบบฟอร์มตัวเลือกความเป็นส่วนตัว

Swift


ConsentForm.presentPrivacyOptionsForm(
  from: viewController, completionHandler: completionHandler)

Objective-C


[UMPConsentForm presentPrivacyOptionsFormFromViewController:viewController
                                          completionHandler:completionHandler];

ส่งคำขอโฆษณาโดยได้รับความยินยอมของผู้ใช้

ก่อนส่งคำขอโฆษณา ให้ใช้ UMPConsentInformation.sharedInstance.canRequestAds เพื่อตรวจสอบว่าคุณได้รับความยินยอมจากผู้ใช้แล้วหรือไม่

Swift

ConsentInformation.shared.canRequestAds

Objective-C

UMPConsentInformation.sharedInstance.canRequestAds;

ต่อไปนี้คือตำแหน่งที่คุณสามารถตรวจสอบว่าส่งคำขอโฆษณาได้หรือไม่ขณะรวบรวมความยินยอม

  • หลังจาก UMP SDK รวบรวมความยินยอมในเซสชันปัจจุบัน
  • ทันทีหลังจากเรียกใช้ requestConsentInfoUpdateWithParameters:completionHandler: UMP SDK อาจได้รับความยินยอมในเซสชันแอปก่อนหน้า

หากเกิดข้อผิดพลาดระหว่างกระบวนการรวบรวมความยินยอม ให้ตรวจสอบว่าคุณส่งคำขอโฆษณาได้หรือไม่ UMP SDK จะใช้สถานะความยินยอมจากเซสชันแอปก่อนหน้า

ป้องกันไม่ให้เกิดงานส่งคำขอโฆษณาซ้ำซ้อน

เมื่อตรวจสอบ UMPConsentInformation.sharedInstance.canRequestAds หลังจากรวบรวมความยินยอมและหลังจากเรียกใช้ requestConsentInfoUpdateWithParameters:completionHandler: แล้ว ให้ตรวจสอบว่าตรรกะของคุณป้องกันไม่ให้เกิดคำขอโฆษณาซ้ำซ้อนซึ่ง อาจทำให้การตรวจสอบทั้ง 2 รายการแสดงผล true เช่น ใช้ตัวแปรบูลีน

การทดสอบ

หากต้องการทดสอบการผสานรวมในแอปขณะพัฒนา ให้ทำตามขั้นตอนต่อไปนี้เพื่อลงทะเบียนอุปกรณ์ทดสอบแบบเป็นโปรแกรม อย่าลืมนำโค้ดที่ตั้งค่ารหัสอุปกรณ์ทดสอบเหล่านี้ออกก่อนเผยแพร่แอป

  1. เรียกใช้ requestConsentInfoUpdateWithParameters:completionHandler:
  2. ตรวจสอบเอาต์พุตบันทึกเพื่อหาข้อความที่คล้ายกับตัวอย่างต่อไปนี้ ซึ่งแสดงรหัสอุปกรณ์และวิธีเพิ่มรหัสอุปกรณ์เป็นอุปกรณ์ทดสอบ

    <UMP SDK>To enable debug mode for this device, set: UMPDebugSettings.testDeviceIdentifiers = @[2077ef9a63d2b398840261c8221a0c9b]
    
  3. คัดลอกรหัสอุปกรณ์ทดสอบไปยังคลิปบอร์ด

  4. แก้ไขโค้ดเพื่อเรียกใช้ UMPDebugSettings().testDeviceIdentifiers และส่งรายการรหัสอุปกรณ์ทดสอบ

    Swift

    let parameters = RequestParameters()
    let debugSettings = DebugSettings()
    
    debugSettings.testDeviceIdentifiers = ["TEST-DEVICE-HASHED-ID"]
    parameters.debugSettings = debugSettings
    
    // Include the UMPRequestParameters in your consent request.
    ConsentInformation.shared.requestConsentInfoUpdate(
        with: parameters,
        completionHandler: { error in
          // ...
        })
    

    Objective-C

    UMPRequestParameters *parameters = [[UMPRequestParameters alloc] init];
    UMPDebugSettings *debugSettings = [[UMPDebugSettings alloc] init];
    
    debugSettings.testDeviceIdentifiers = @[ @"TEST-DEVICE-HASHED-ID" ];
    parameters.debugSettings = debugSettings;
    
    // Include the UMPRequestParameters in your consent request.
    [UMPConsentInformation.sharedInstance
        requestConsentInfoUpdateWithParameters:parameters
                            completionHandler:^(NSError *_Nullable error){
                              // ...
    }];
    

บังคับใช้ภูมิศาสตร์

UMP SDK มีวิธีทดสอบลักษณะการทำงานของแอปเสมือนว่าอุปกรณ์ อยู่ในภูมิภาคต่างๆ เช่น เขตเศรษฐกิจยุโรป (EEA) สหราชอาณาจักร (UK) และสวิตเซอร์แลนด์ โดยใช้ UMPDebugGeography โปรดทราบว่าการตั้งค่าการแก้ไขข้อบกพร่องจะทำงานในอุปกรณ์ทดสอบเท่านั้น

Swift

let parameters = RequestParameters()
let debugSettings = DebugSettings()

debugSettings.testDeviceIdentifiers = ["TEST-DEVICE-HASHED-ID"]
debugSettings.geography = .EEA
parameters.debugSettings = debugSettings

// Include the UMPRequestParameters in your consent request.
ConsentInformation.shared.requestConsentInfoUpdate(
    with: parameters,
    completionHandler: { error in
      // ...
    })

Objective-C

UMPRequestParameters *parameters = [[UMPRequestParameters alloc] init];
UMPDebugSettings *debugSettings = [[UMPDebugSettings alloc] init];

debugSettings.testDeviceIdentifiers = @[ @"TEST-DEVICE-HASHED-ID" ];
debugSettings.geography = UMPDebugGeographyEEA;
parameters.debugSettings = debugSettings;

// Include the UMPRequestParameters in your consent request.
[UMPConsentInformation.sharedInstance
    requestConsentInfoUpdateWithParameters:parameters
                         completionHandler:^(NSError *_Nullable error){
                           // ...
}];

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

Swift

ConsentInformation.shared.reset()

Objective-C

[UMPConsentInformation.sharedInstance reset];

ตัวอย่างใน GitHub

ดูตัวอย่างการผสานรวม UMP SDK แบบเต็มที่กล่าวถึงในหน้านี้ได้ใน Swift UmpExample และ Objective-C UmpExample