คู่มือเริ่มต้นใช้งานสำหรับ Objective-C

คู่มือนักพัฒนาซอฟต์แวร์นี้อธิบายวิธีติดตั้งใช้งาน Google Tag Manager ในแอปพลิเคชันบนมือถือ

บทนำ

Google Tag Manager ช่วยให้นักพัฒนาซอฟต์แวร์เปลี่ยนค่าการกำหนดค่าในแอปพลิเคชันบนอุปกรณ์เคลื่อนที่ได้โดยใช้อินเทอร์เฟซ Google Tag Manager โดยไม่ต้องสร้างและส่งไบนารีของแอปพลิเคชันไปยัง App Marketplace อีกครั้ง

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

  • การตั้งค่า UI และสตริงการแสดงผลต่างๆ
  • ขนาด ตำแหน่ง หรือประเภทของโฆษณาที่แสดงในแอปพลิเคชัน
  • การตั้งค่าเกม

นอกจากนี้ ระบบยังอาจประเมินค่าการกำหนดค่าขณะรันไทม์โดยใช้กฎ ซึ่งจะช่วยให้กำหนดค่าแบบไดนามิกได้ เช่น

  • การใช้ขนาดหน้าจอเพื่อกำหนดขนาดแบนเนอร์โฆษณา
  • การใช้ภาษาและสถานที่ตั้งเพื่อกำหนดค่าองค์ประกอบ UI

นอกจากนี้ Google Tag Manager ยังช่วยให้ติดตั้งใช้งานแท็กติดตามและพิกเซลในแอปพลิเคชันแบบไดนามิกได้ด้วย นักพัฒนาแอปสามารถพุชเหตุการณ์สำคัญไปยังชั้นข้อมูลและตัดสินใจในภายหลังว่าจะเริ่มทำงานแท็กติดตามหรือพิกเซลใด TagManager รองรับแท็กต่อไปนี้

  • Google Mobile App Analytics
  • แท็กการเรียกใช้ฟังก์ชันที่กำหนดเอง

ก่อนที่คุณจะเริ่ม

ก่อนใช้คู่มือเริ่มต้นใช้งานนี้ คุณจะต้องมีสิ่งต่อไปนี้

หากคุณเพิ่งเริ่มใช้ Google Tag Manager เราขอแนะนำให้ดูข้อมูลเพิ่มเติมเกี่ยวกับคอนเทนเนอร์ มาโคร และกฎ (ศูนย์ช่วยเหลือ) ก่อนที่จะอ่านคู่มือนี้ต่อ

เริ่มต้นใช้งาน

ส่วนนี้จะแนะนำนักพัฒนาแอปเกี่ยวกับเวิร์กโฟลว์ Tag Manager ทั่วไป ดังนี้

  1. เพิ่ม SDK ของ Google Tag Manager ลงในโปรเจ็กต์
  2. ตั้งค่าคอนเทนเนอร์เริ่มต้น
  3. เปิดคอนเทนเนอร์
  4. รับค่าการกำหนดค่าจากคอนเทนเนอร์
  5. พุชเหตุการณ์ไปยัง DataLayer
  6. แสดงตัวอย่างและนำคอนเทนเนอร์ไปใช้จริง

1. การเพิ่ม SDK ของ Google Tag Manager ลงในโปรเจ็กต์

ก่อนใช้ SDK ของ Google Tag Manager คุณจะต้องเพิ่ม libGoogleAnalyticsServices.a และไฟล์ส่วนหัวของ Google Tag Manager (GTM) จากไดเรกทอรี Library ของแพ็กเกจ SDK ลงในโปรเจ็กต์

จากนั้นเพิ่มรายการต่อไปนี้ลงในไลบรารีที่ลิงก์ของเป้าหมายแอปพลิเคชัน หากยังไม่มี

  • CoreData.framework
  • SystemConfiguration.framework
  • libz.dylib
  • libsqlite3.dylib
  • libGoogleAnalyticsServices.a

หากต้องการให้แอปพลิเคชันเข้าถึงตัวระบุสำหรับผู้ลงโฆษณา (IDFA) และแฟล็กการติดตามที่เฟรมเวิร์กนั้นให้มาผ่านมาโคร SDK ของ Google Tag Manager คุณจะต้องลิงก์ไลบรารีเพิ่มเติมต่อไปนี้ด้วย

  • libAdIdAccess.a
  • AdSupport.framework

2. การเพิ่มไฟล์คอนเทนเนอร์เริ่มต้นลงในโปรเจ็กต์

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

หากต้องการดาวน์โหลดและเพิ่มไบนารีคอนเทนเนอร์เริ่มต้นลงในแอปพลิเคชัน ให้ทำตามขั้นตอนต่อไปนี้

  1. ลงชื่อเข้าใช้อินเทอร์เฟซเว็บ Google Tag Manager
  2. เลือกเวอร์ชันของคอนเทนเนอร์ที่ต้องการดาวน์โหลด
  3. คลิกปุ่มดาวน์โหลดเพื่อเรียกข้อมูลไบนารีของคอนเทนเนอร์
  4. เพิ่มไฟล์ไบนารีลงในไดเรกทอรีรากของโปรเจ็กต์และโฟลเดอร์ "Supporting Files" ในโปรเจ็กต์

ชื่อไฟล์เริ่มต้นควรเป็นรหัสคอนเทนเนอร์ (เช่น GTM-1234) เมื่อดาวน์โหลดไฟล์ไบนารีแล้ว อย่าลืมนำคำต่อท้ายเวอร์ชันออกจากชื่อไฟล์เพื่อให้เป็นไปตามรูปแบบการตั้งชื่อที่ถูกต้อง

แม้ว่าเราจะแนะนำให้ใช้ไฟล์ไบนารี แต่หากคอนเทนเนอร์ไม่มีกฎหรือแท็ก คุณอาจเลือกใช้รายการพร็อพเพอร์ตี้หรือไฟล์ JSON แทนได้ ไฟล์ควรอยู่ในแพ็กเกจหลักและควรเป็นไปตามรูปแบบการตั้งชื่อนี้: <Container_ID>.<plist|json> เช่น หากรหัสคอนเทนเนอร์คือ GTM-1234 คุณสามารถระบุค่าคอนเทนเนอร์เริ่มต้นในไฟล์รายการพร็อพเพอร์ตี้ชื่อ GTM-1234.plist

3. การเปิดคอนเทนเนอร์

ก่อนที่จะดึงค่าจากคอนเทนเนอร์ แอปพลิเคชันของคุณต้องเปิดคอนเทนเนอร์ การเปิดคอนเทนเนอร์จะโหลดจากดิสก์ (หากมี) หรือจะขอจากเครือข่าย (หากจำเป็น)

วิธีที่ง่ายที่สุดในการเปิดคอนเทนเนอร์ใน iOS คือการใช้ openContainerWithId:tagManager:openType:timeout:notifier: ดังตัวอย่างต่อไปนี้

// MyAppDelegate.h
// This example assumes this file is using ARC.
#import <UIKit/UIKit.h>

@class TAGManager;
@class TAGContainer;

@interface MyAppDelegate : UIResponder <UIApplicationDelegate>

@property (nonatomic, strong) TAGManager *tagManager;
@property (nonatomic, strong) TAGContainer *container;

@end


// MyAppDelegate.m
// This example assumes this file is using ARC.
#import "MyAppDelegate.h"
#import "TAGContainer.h"
#import "TAGContainerOpener.h"
#import "TAGManager.h"

@interface MyAppDelegate ()<TAGContainerOpenerNotifier>
@end

@implementation MyAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  self.tagManager = [TAGManager instance];

  // Optional: Change the LogLevel to Verbose to enable logging at VERBOSE and higher levels.
  [self.tagManager.logger setLogLevel:kTAGLoggerLogLevelVerbose];

  /*
   * Opens a container.
   *
   * @param containerId The ID of the container to load.
   * @param tagManager The TAGManager instance for getting the container.
   * @param openType The choice of how to open the container.
   * @param timeout The timeout period (default is 2.0 seconds).
   * @param notifier The notifier to inform on container load events.
   */
  [TAGContainerOpener openContainerWithId:@"GTM-XXXX"   // Update with your Container ID.
                               tagManager:self.tagManager
                                 openType:kTAGOpenTypePreferFresh
                                  timeout:nil
                                 notifier:self];

  // Method calls that don't need the container.

  return YES;
}

// TAGContainerOpenerNotifier callback.
- (void)containerAvailable:(TAGContainer *)container {
  // Note that containerAvailable may be called on any thread, so you may need to dispatch back to
  // your main thread.
  dispatch_async(dispatch_get_main_queue(), ^{
    self.container = container;
  });
}

// The rest of your app delegate implementation.

4. การรับค่าการกำหนดค่าจากคอนเทนเนอร์

เมื่อเปิดคอนเทนเนอร์แล้ว คุณจะดึงค่าการกำหนดค่าได้โดยใช้เมธอด <type>ForKey: ดังนี้

// Retrieving a configuration value from a Tag Manager Container.

MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
TAGContainer *container = appDelegate.container;

// Get the configuration value by key.
NSString *title = [container stringForKey:@"title_string"];

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

// Empty keys will return a default value depending on the type requested.

// Key does not exist. An empty string is returned.
NSString subtitle = [container stringForKey:@"Non-existent-key"];
[subtitle isEqualToString:@""]; // Evaluates to true.

5. การพุชค่าไปยังชั้นข้อมูล

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

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

ระบบจะพุชเหตุการณ์ไปยัง DataLayer โดยใช้ push:

//
//  ViewController.m
//  Pushing an openScreen event with a screen name into the data layer.
//

#import "MyAppDelegate.h"
#import "TAGDataLayer.h"
#import "ViewController.h"

@implementation ViewController

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    // The container should have already been opened, otherwise events pushed to
    // the data layer will not fire tags in that container.
    TAGDataLayer *dataLayer = [TAGManager instance].dataLayer;

    [dataLayer push:@{@"event": @"openScreen", @"screenName": @"Home Screen"}];
}

// Rest of the ViewController implementation

@end

ในอินเทอร์เฟซเว็บ ตอนนี้คุณสามารถสร้างแท็ก (เช่น แท็ก Google Analytics) เพื่อเริ่มทำงานสำหรับการดูหน้าจอแต่ละครั้งได้โดยสร้างกฎนี้: เท่ากับ "openScreen" หากต้องการส่งชื่อหน้าจอไปยังแท็กใดแท็กหนึ่งเหล่านี้ ให้สร้างมาโครชั้นข้อมูลที่อ้างอิงคีย์ "screenName" ในชั้นข้อมูล นอกจากนี้ คุณยังสร้างแท็ก (เช่น พิกเซล Conversion ของ Google Ads) ให้เริ่มทำงานเฉพาะสำหรับการดูหน้าจอที่เจาะจงได้ด้วย โดยสร้างกฎที่ เท่ากับ "openScreen" && เท่ากับ "ConfirmationScreen"

6. การแสดงตัวอย่างและการเผยแพร่คอนเทนเนอร์

ค่ามาโครจะสอดคล้องกับเวอร์ชันที่เผยแพร่ในปัจจุบันเสมอ ก่อนเผยแพร่คอนเทนเนอร์เวอร์ชันล่าสุด คุณสามารถดูตัวอย่างคอนเทนเนอร์ฉบับร่างได้

หากต้องการดูตัวอย่างคอนเทนเนอร์ ให้สร้าง URL ตัวอย่างในอินเทอร์เฟซเว็บของ Google Tag Manager โดยเลือกเวอร์ชันของคอนเทนเนอร์ที่ต้องการดูตัวอย่าง แล้วเลือกPreview บันทึก URL ตัวอย่างนี้ไว้เนื่องจากคุณจะต้องใช้ในขั้นตอนถัดไป

URL ตัวอย่างจะอยู่ในหน้าต่างแสดงตัวอย่างของอินเทอร์เฟซเว็บ Tag Manager
รูปที่ 1: การรับ URL ตัวอย่างจากอินเทอร์เฟซเว็บของ Tag Manager

หากต้องการเปิดใช้ตัวอย่างคอนเทนเนอร์ คุณต้องเพิ่มโค้ดลงในไฟล์การติดตั้งใช้งาน App Delegate และกำหนดรูปแบบ URL ตัวอย่างของ Google Tag Manager ในรายการพร็อพเพอร์ตี้ของโปรเจ็กต์

ก่อนอื่น ให้เพิ่มข้อมูลโค้ดตัวหนาต่อไปนี้ลงในไฟล์ App Delegate

@implementation MyAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

  self.tagManager = [TAGManager instance];
  
  // Add the code in bold below to preview a Google Tag Manager container.
  // IMPORTANT: This code must be called before the container is opened.
  NSURL *url = [launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];
  if (url != nil) {
    [self.tagManager previewWithUrl:url];
  }
  
  id<TAGContainerFuture> future =
      [TAGContainerOpener openContainerWithId:@"GTM-XXXX"    // Placeholder Container ID.
                                   tagManager:self.tagManager
                                     openType:kTAGOpenTypePreferNonDefault
                                      timeout:nil];

  // The rest of your method implementation.

  self.container = [future get];

  return YES;
}


// Add the code in bold below preview a Google Tag Manager container.
- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {

  if ([self.tagManager previewWithUrl:url]) {
    return YES;
  }

  // Code to handle other urls.
  return NO;
}

ถัดไป ให้ลงทะเบียนตัวระบุ URL และ URL Scheme ต่อไปนี้ภายใต้คีย์ประเภท URL ของไฟล์รายการพร็อพเพอร์ตี้ของแอปพลิเคชัน

URL identifier: your.package_name
URL scheme: tagmanager.c.your.package.name
ลงทะเบียน URL Scheme แสดงตัวอย่างของ Tag Manager ในไฟล์รายการพร็อพเพอร์ตี้ของแอปพลิเคชัน
รูปที่ 3: การเพิ่ม URL Scheme สำหรับแสดงตัวอย่างของ Tag Manager ลงในไฟล์รายการพร็อพเพอร์ตี้ของแอปพลิเคชัน

เปิดลิงก์ในโปรแกรมจำลองหรืออุปกรณ์จริงเพื่อดูตัวอย่างคอนเทนเนอร์ฉบับร่างในแอป

เมื่อคุณพร้อมที่จะทำให้ค่าการกำหนดค่าฉบับร่างพร้อมใช้งานสำหรับแอปพลิเคชัน ให้เผยแพร่คอนเทนเนอร์

การกำหนดค่าขั้นสูง

Google Tag Manager สำหรับอุปกรณ์เคลื่อนที่มีตัวเลือกการกำหนดค่าขั้นสูงจำนวนหนึ่งที่ช่วยให้คุณเลือกค่าตามเงื่อนไขรันไทม์โดยใช้กฎ รีเฟรชคอนเทนเนอร์ด้วยตนเอง และรับตัวเลือกเพิ่มเติมสำหรับการเปิดคอนเทนเนอร์ ส่วนต่อไปนี้จะอธิบายการกำหนดค่าขั้นสูงที่พบบ่อยที่สุดบางส่วน

ตัวเลือกขั้นสูงสำหรับการเปิดคอนเทนเนอร์

SDK ของ Google Tag Manager มีหลายวิธีในการเปิดคอนเทนเนอร์ ซึ่งช่วยให้คุณควบคุมกระบวนการโหลดได้มากขึ้น ดังนี้

openContainerById:callback:

openContainerById:callback: เป็น API ระดับต่ำสุดและยืดหยุ่นที่สุดสำหรับการเปิดคอนเทนเนอร์ โดยจะแสดงผลทันทีด้วยคอนเทนเนอร์เริ่มต้น และยังโหลดคอนเทนเนอร์แบบอะซิงโครนัสจากดิสก์หรือเครือข่ายหากไม่มีคอนเทนเนอร์ที่บันทึกไว้ หรือหากคอนเทนเนอร์ที่บันทึกไว้ไม่ใช่คอนเทนเนอร์ใหม่ (เก่ากว่า 12 ชั่วโมง)

@interface ContainerCallback : NSObject<TAGContainerCallback>

@end

@implementation ContainerCallback

/**
 * Called before the refresh is about to begin.
 *
 * @param container The container being refreshed.
 * @param refreshType The type of refresh which is starting.
 */
- (void)containerRefreshBegin:(TAGContainer *)container
                  refreshType:(TAGContainerCallbackRefreshType)refreshType {
  // Notify UI that container refresh is beginning.
}

/**
 * Called when a refresh has successfully completed for the given refresh type.
 *
 * @param container The container being refreshed.
 * @param refreshType The type of refresh which completed successfully.
 */
- (void)containerRefreshSuccess:(TAGContainer *)container
                    refreshType:(TAGContainerCallbackRefreshType)refreshType {
  // Notify UI that container is available.
}

/**
 * Called when a refresh has failed to complete for the given refresh type.
 *
 * @param container The container being refreshed.
 * @param failure The reason for the refresh failure.
 * @param refreshType The type of refresh which failed.
 */
- (void)containerRefreshFailure:(TAGContainer *)container
                        failure:(TAGContainerCallbackRefreshFailure)failure
                    refreshType:(TAGContainerCallbackRefreshType)refreshType {
  // Notify UI that container request has failed.
}
@end

ตลอดกระบวนการโหลด openContainerById:callback: จะเรียกใช้ Lifecycle Callback หลายรายการเพื่อให้โค้ดของคุณทราบว่าคำขอโหลดเริ่มต้นเมื่อใด ไม่ว่าจะล้มเหลวหรือสำเร็จและสาเหตุ และคอนเทนเนอร์โหลดจากดิสก์หรือเครือข่ายในท้ายที่สุดหรือไม่

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

openContainerById:callback: จะส่งค่า enum ต่อไปนี้เป็นอาร์กิวเมนต์ไปยัง Callback เหล่านี้

RefreshType

ค่าคำอธิบาย
kTAGContainerCallbackRefreshTypeSaved คำขอรีเฟรชกำลังโหลดคอนเทนเนอร์ที่บันทึกไว้ในเครื่อง
kTAGContainerCallbackRefreshTypeNetwork คำขอรีเฟรชกำลังโหลดคอนเทนเนอร์ผ่านเครือข่าย

RefreshFailure

ค่าคำอธิบาย
kTAGContainerCallbackRefreshFailureNoSavedContainer ไม่มีคอนเทนเนอร์ที่บันทึกไว้
kTAGContainerCallbackRefreshFailureIoError ข้อผิดพลาด I/O ทำให้ไม่สามารถรีเฟรชคอนเทนเนอร์ได้
kTAGContainerCallbackRefreshFailureNoNetwork ไม่มีการเชื่อมต่อเครือข่ายที่ใช้งานได้
kTAGContainerCallbackRefreshFailureNetworkError เกิดข้อผิดพลาดเกี่ยวกับเครือข่าย
kTAGContainerCallbackRefreshFailureServerError เกิดข้อผิดพลาดในเซิร์ฟเวอร์
kTAGContainerCallbackRefreshFailureUnknownError เกิดข้อผิดพลาดที่จัดหมวดหมู่ไม่ได้

วิธีการเปิดคอนเทนเนอร์ที่ไม่ใช่ค่าเริ่มต้นและคอนเทนเนอร์ใหม่

TAGContainerOpener รวม openContainerById:callback: และมีเมธอดที่อำนวยความสะดวก 2 เมธอดสำหรับการเปิดคอนเทนเนอร์ ได้แก่ openContainerWithId:tagManager:openType:timeout:notifier: และ openContainerWithId:tagManager:openType:timeout:

แต่ละวิธีจะใช้การแจงนับที่ขอคอนเทนเนอร์ที่ไม่ใช่ค่าเริ่มต้นหรือคอนเทนเนอร์ใหม่

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

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

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

เมธอดอำนวยความสะดวกทั้ง 2 รายการนี้เป็นแบบไม่บล็อก openContainerWithId:tagManager:openType:timeout: แสดงผลออบเจ็กต์ TAGContainerFuture ซึ่งเมธอด get จะแสดงผล TAGContainer ทันทีที่โหลดเสร็จ (แต่จะบล็อกจนกว่าจะถึงตอนนั้น) เมธอด openContainerWithId:tagManager:openType:timeout:notifier: จะใช้ Callback เดียว ซึ่งจะเรียกใช้เมื่อคอนเทนเนอร์พร้อมใช้งาน ทั้ง 2 วิธีมีระยะหมดเวลาเริ่มต้น 2.0 วินาที

การประเมินมาโครที่รันไทม์โดยใช้กฎ

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

ระบบจะใช้กฎเพื่อเลือกสตริงที่แสดงตามภาษาของอุปกรณ์ที่รันไทม์: ภาษาเท่ากับ es กฎนี้ใช้มาโครภาษาที่กำหนดไว้ล่วงหน้าและรหัสภาษา ISO 639-1 แบบ 2 อักขระ
รูปที่ 1: การเพิ่มกฎเพื่อเปิดใช้มาโครการรวบรวมค่าสำหรับอุปกรณ์ที่กำหนดค่าให้ใช้ภาษาสเปนเท่านั้น

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

โปรดทราบว่าหากคอนเทนเนอร์เริ่มต้นต้องมีกฎ คุณต้องใช้ไฟล์คอนเทนเนอร์แบบไบนารีเป็นคอนเทนเนอร์เริ่มต้น

ดูข้อมูลเพิ่มเติมเกี่ยวกับการกำหนดค่ากฎ (ศูนย์ช่วยเหลือ)

ไฟล์คอนเทนเนอร์เริ่มต้นแบบไบนารี

คอนเทนเนอร์เริ่มต้นที่ต้องใช้กฎควรใช้ไฟล์คอนเทนเนอร์แบบไบนารีแทนไฟล์รายการพร็อพเพอร์ตี้หรือไฟล์ JSON เป็นคอนเทนเนอร์เริ่มต้น คอนเทนเนอร์แบบไบนารีรองรับการกำหนดค่ามาโครที่รันไทม์ด้วยกฎของ Google Tag Manager ส่วนไฟล์รายการพร็อพเพอร์ตี้หรือ JSON ไม่รองรับ

คุณสามารถดาวน์โหลดไฟล์คอนเทนเนอร์ไบนารีได้จากอินเทอร์เฟซเว็บของ Google Tag Manager และควรเพิ่มลงในแพ็กเกจแอปพลิเคชันหลักตามรูปแบบการตั้งชื่อต่อไปนี้ GTM-XXXX โดยที่ชื่อไฟล์แสดงถึงรหัสคอนเทนเนอร์ของคุณ

ในกรณีที่มีไฟล์รายการพร็อพเพอร์ตี้และ/หรือไฟล์ JSON รวมถึงไฟล์คอนเทนเนอร์ไบนารี SDK จะใช้ไฟล์คอนเทนเนอร์ไบนารีเป็นคอนเทนเนอร์เริ่มต้น

การใช้มาโครการเรียกใช้ฟังก์ชัน

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

วิธีกำหนดค่ามาโครการเรียกใช้ฟังก์ชัน

  1. กำหนดมาโครการเรียกใช้ฟังก์ชันในอินเทอร์เฟซเว็บของ Google Tag Manager คุณจะกำหนดค่าอาร์กิวเมนต์เป็นคู่คีย์-ค่าหรือไม่ก็ได้
  2. กำหนดตัวแฮนเดิลที่ใช้โปรโตคอล TAGFunctionCallMacroHandler ดังนี้
    // MyFunctionCallMacroHandler.h
    #import "TAGContainer.h"
    
    // The function name field of the macro, as defined in the Google Tag Manager
    // web interface.
    extern NSString *const kMyMacroFunctionName;
    
    @interface MyFunctionCallMacroHandler : NSObject<TAGFunctionCallMacroHandler>
    
    @end
    
    
    // MyFunctionCallMacroHandler.m
    #import "MyFunctionCallMacroHandler.h"
    
    // Corresponds to the function name field in the Google Tag Manager interface.
    NSString *const kMyMacroFunctionName = @"myConfiguredFunctionName";
    
    @implementation MacroHandler
    
    - (id)valueForMacro:(NSString *)functionName parameters:(NSDictionary *)parameters {
    
      if ([functionName isEqualToString:kMyMacroFunctionName]) {
        // Process and return the calculated value of this macro accordingly.
        return macro_value;
      }
      return nil;
    }
    
    @end
  3. ลงทะเบียนเครื่องจัดการโดยใช้ TAGContainer::registerFunctionCallMacroHandler:forMacro: และชื่อฟังก์ชันที่ระบุในอินเทอร์เฟซ Google Tag Manager
    //
    // MyAppDelegate.h
    //
    #import <UIKit/UIKit.h>
    
    @interface MyAppDelegate : UIResponder <UIApplicationDelegate>
    
    @end
    
    
    //
    // MyAppDelegate.m
    //
    #import "MyAppDelegate.h"
    #import "MyFunctionCallMacroHandler.h"
    #import "TAGContainer.h"
    #import "TAGContainerOpener.h"
    #import "TAGManager.h"
    
    @implementation MyAppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
      // Open the container.
      id<TAGContainerFuture> future =
          [TAGContainerOpener openContainerWithId:@"GTM-XXXX"    // Placeholder Container ID.
                                       tagManager:[TAGManager instance]
                                         openType:kTAGOpenTypePreferNonDefault
                                          timeout:nil];
    
      // Method calls that don't need the container.
    
      self.container = [future get];
    
      // Register a function call macro handler using the macro name defined
      // in the Google Tag Manager web interface.
      [self.container registerFunctionCallMacroHandler:[[MyFunctionCallMacroHandler alloc] init]
                                              forMacro:kMyMacroFunctionName];
    }
    
    @end

การใช้แท็กการเรียกใช้ฟังก์ชัน

แท็กการเรียกใช้ฟังก์ชันช่วยให้ฟังก์ชันที่ลงทะเบียนไว้ล่วงหน้าทำงานได้ทุกเมื่อที่มีการพุชเหตุการณ์ไปยังชั้นข้อมูลและกฎแท็กประเมินเป็น true

วิธีกำหนดค่าแท็กการเรียกใช้ฟังก์ชัน

  1. กำหนดแท็กการเรียกใช้ฟังก์ชันในอินเทอร์เฟซเว็บของ Google Tag Manager คุณจะกำหนดค่าอาร์กิวเมนต์เป็นคู่คีย์-ค่าหรือไม่ก็ได้
  2. ใช้โปรโตคอล TAGFunctionCallTagHandler:
    //
    // MyFunctionCallTagHandler.h
    //
    
    #import "TAGContainer.h"
    
    extern NSString *const kMyTagFunctionName;
    
    @interface MyFunctionCallTagHandler : NSObject<TAGFunctionCallTagHandler>
    
    @end
    
    
    //
    // MyFunctionCallTagHandler.m
    //
    
    // Corresponds to the function name field in the Google Tag Manager interface.
    NSString *const kMyTagFunctionName = @"myConfiguredFunctionName";
    
    @implementation MyFunctionCallTagHandler
    
    /**
     * This method will be called when any custom tag's rule(s) evaluate to true and
     * should check the functionName and process accordingly.
     *
     * @param functionName corresponds to the function name field, not tag
     *     name field, defined in the Google Tag Manager web interface.
     * @param parameters An optional map of parameters as defined in the Google
     *     Tag Manager web interface.
     */
    - (void)execute:(NSString *)functionName parameters:(NSDictionary *)parameters {
    
      if ([functionName isEqualToString:kMyTagFunctionName]) {
        // Process accordingly.
      }
    }
    @end
  3. ลงทะเบียนเครื่องจัดการแท็กการเรียกใช้ฟังก์ชันโดยใช้ชื่อแท็กที่กำหนดค่าไว้ในอินเทอร์เฟซเว็บของ Google Tag Manager ดังนี้
    //
    // MyAppDelegate.h
    //
    #import <UIKit/UIKit.h>
    
    @interface MyAppDelegate : UIResponder <UIApplicationDelegate>
    
    @end
    
    
    //
    // MyAppDelegate.m
    //
    #import "MyAppDelegate.h"
    #import "MyFunctionCallTagHandler.h"
    #import "TAGContainer.h"
    #import "TAGContainerOpener.h"
    #import "TAGManager.h"
    
    @implementation MyAppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
      // Open the container.
      id<TAGContainerFuture> future =
          [TAGContainerOpener openContainerWithId:@"GTM-XXXX"    // Placeholder Container ID.
                                       tagManager:[TAGManager instance]
                                         openType:kTAGOpenTypePreferNonDefault
                                          timeout:nil];
    
      // Method calls that don't need the container.
    
      self.container = [future get];
    
      // Register a function call tag handler using the function name of the tag as
      // defined in the Google Tag Manager web interface.
      [self.container registerFunctionCallTagHandler:[[MyFunctionCallTagHandler alloc] init]
                                              forTag:kMyTagFunctionName];
    }
    @end

การตั้งค่าระยะเวลารีเฟรชที่กำหนดเอง

SDK ของ Google Tag Manager จะพยายามดึงข้อมูลคอนเทนเนอร์ใหม่หากคอนเทนเนอร์ปัจจุบันมีอายุเกิน 12 ชั่วโมง หากต้องการตั้งค่าระยะเวลารีเฟรชคอนเทนเนอร์ที่กำหนดเอง ให้ใช้ NSTimer ดังตัวอย่างต่อไปนี้

- (void)refreshContainer:(NSTimer *)timer {
  [self.container refresh];
}

self.refreshTimer = [NSTimer scheduledTimerWithTimeInterval:<refresh_interval>
                                                     target:self
                                                   selector:@selector(refreshContainer:)
                                                   userInfo:nil
                                                    repeats:YES];

การแก้ไขข้อบกพร่องด้วย Logger

SDK ของ Google Tag Manager จะพิมพ์ข้อผิดพลาดและคำเตือนลงในบันทึกโดยค่าเริ่มต้น การเปิดใช้การบันทึกแบบละเอียดมากขึ้นอาจเป็นประโยชน์สำหรับการแก้ไขข้อบกพร่อง และทำได้โดยการใช้ Logger ของคุณเอง ดังตัวอย่างต่อไปนี้

// MyAppDelegate.h
// This example assumes this file is using ARC.
// This Logger class will print out not just errors and warnings (as the default
// logger does), but also info, debug, and verbose messages.
@interface MyLogger: NSObject<TAGLogger>
@end

@implementation MyLogger
- (void)error:(NSString *)message {
  NSLog(@"Error: %@", message);
}

- (void)warning:(NSString *)message {
  NSLog(@"Warning: %@", message);
}

- (void)info:(NSString *)message {
  NSLog(@"Info: %@", message);
}

- (void)debug:(NSString *)message {
  NSLog(@"Debug: %@", message);
}

- (void)verbose:(NSString *)message {
  NSLog(@"Verbose: %@", message);
}
@end

// MyAppDelegate.m
// This example assumes this file is using ARC.
@implementation MyAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  self.tagManager = [TAGManager instance];
  
  self.tagManager.logger = [[MyLogger alloc] init];
  
  // Rest of Tag Manager and method implementation.
  return YES;
}
// Rest of app delegate implementation.
@end

หรือจะตั้งค่า LogLevel ของ Logger ที่มีอยู่โดยใช้ TagManager::logger::setLogLevel ดังตัวอย่างต่อไปนี้ก็ได้

// Change the LogLevel to INFO to enable logging at INFO and higher levels.
self.tagManager = [TAGManager instance];
[self.tagManager.logger setLogLevel:kTAGLoggerLogLevelInfo];