คู่มือนักพัฒนาแอปนี้อธิบายวิธีติดตั้งใช้งาน Google Tag Manager ในแอปพลิเคชันบนอุปกรณ์เคลื่อนที่
บทนำ
Google Tag Manager ช่วยให้นักพัฒนาแอปเปลี่ยนค่าการกำหนดค่าในแอปพลิเคชันบนอุปกรณ์เคลื่อนที่ได้โดยใช้อินเทอร์เฟซของ Google Tag Manager โดยไม่ต้องสร้างใหม่และส่งไบนารีของแอปพลิเคชันไปยังตลาดกลางของแอปอีกครั้ง
ซึ่งมีประโยชน์สำหรับการจัดการค่าการกำหนดค่าหรือแฟล็กใดๆ ในแอปพลิเคชันที่คุณอาจต้องเปลี่ยนในอนาคต รวมถึงสิ่งต่อไปนี้
- การตั้งค่า UI และสตริงการแสดงผลต่างๆ
- ขนาด สถานที่ หรือประเภทโฆษณาที่แสดงในแอปพลิเคชัน
- การตั้งค่าเกม
นอกจากนี้ ระบบยังประเมินค่าการกำหนดค่าได้ในรันไทม์โดยใช้กฎ ซึ่งช่วยให้กำหนดค่าแบบไดนามิกได้ เช่น
- ใช้ขนาดหน้าจอเพื่อกำหนดขนาดแบนเนอร์โฆษณา
- ใช้ภาษาและสถานที่เพื่อกำหนดค่าองค์ประกอบ UI
นอกจากนี้ Google Tag Manager ยังช่วยให้ติดตั้งใช้งานแท็กและพิกเซลการติดตามในแอปพลิเคชันแบบไดนามิกได้ด้วย นักพัฒนาแอปสามารถพุชเหตุการณ์สำคัญลงในชั้นข้อมูล และตัดสินใจในภายหลังว่าจะให้แท็กหรือพิกเซลการติดตามใดเริ่มทำงาน Tag Manager รองรับแท็กต่อไปนี้
- Google Mobile App Analytics
- แท็กการเรียกใช้ฟังก์ชันที่กำหนดเอง
ก่อนที่คุณจะเริ่ม
ก่อนที่จะใช้คู่มือการเริ่มต้นใช้งานนี้ คุณจะต้องมีสิ่งต่อไปนี้
- บัญชี Google Tag Manager
- คอนเทนเนอร์ Tag Manager ใหม่ และการเก็บรวบรวมค่า
- แอปพลิเคชันบนอุปกรณ์เคลื่อนที่สำหรับ iOS ที่จะติดตั้งใช้งาน Google Tag Manager
- SDK บริการ Google Analytics ซึ่งมีไลบรารี Tag Manager
หากคุณเพิ่งเริ่มใช้ Google Tag Manager เราขอแนะนำให้คุณ ดูข้อมูลเพิ่มเติมเกี่ยวกับคอนเทนเนอร์ มาโคร และกฎ (ศูนย์ช่วยเหลือ) ก่อนที่จะอ่านคู่มือนี้ต่อ
เริ่มต้นใช้งาน
ส่วนนี้จะแนะนำนักพัฒนาแอปเกี่ยวกับเวิร์กโฟลว์ทั่วไปของ Tag Manager
- เพิ่ม Google Tag Manager SDK ลงในโปรเจ็กต์
- ตั้งค่าเริ่มต้นของคอนเทนเนอร์
- เปิดคอนเทนเนอร์
- รับค่าการกำหนดค่าจากคอนเทนเนอร์
- พุชเหตุการณ์ไปยังชั้นข้อมูล
- แสดงตัวอย่างและเผยแพร่คอนเทนเนอร์
1. การเพิ่ม Google Tag Manager SDK ลงในโปรเจ็กต์
ก่อนที่จะใช้ Google Tag Manager SDK คุณจะต้องเพิ่ม libGoogleAnalyticsServices.a และไฟล์ส่วนหัวของ Google Tag Manager (GTM) จากไดเรกทอรี Library ของแพ็กเกจ SDK ลงในโปรเจ็กต์
จากนั้นเพิ่มรายการต่อไปนี้ลงในไลบรารีที่ลิงก์ของเป้าหมายแอปพลิเคชันหากยังไม่มี
CoreData.frameworkSystemConfiguration.frameworklibz.dyliblibsqlite3.dyliblibGoogleAnalyticsServices.a
หากต้องการให้แอปพลิเคชันเข้าถึงตัวระบุผู้ลงโฆษณา (IDFA) และแฟล็กการติดตามที่เฟรมเวิร์กนั้นให้ไว้ผ่านมาโคร Google Tag Manager SDK คุณจะต้องลิงก์ไลบรารีเพิ่มเติมต่อไปนี้ด้วย
libAdIdAccess.aAdSupport.framework
2. การเพิ่มไฟล์คอนเทนเนอร์เริ่มต้นลงในโปรเจ็กต์
Google Tag Manager ใช้คอนเทนเนอร์เริ่มต้นในการเรียกใช้แอปพลิเคชันครั้งแรก ระบบจะใช้คอนเทนเนอร์เริ่มต้น จนกว่าแอปจะดึงข้อมูลคอนเทนเนอร์ใหม่ผ่านเครือข่าย ได้
หากต้องการดาวน์โหลดและเพิ่มไบนารีของคอนเทนเนอร์เริ่มต้นลงในแอปพลิเคชัน ให้ทำตามขั้นตอนต่อไปนี้
- ลงชื่อเข้าใช้อินเทอร์เฟซเว็บของ Google Tag Manager
- เลือกเวอร์ชัน ของคอนเทนเนอร์ที่ต้องการดาวน์โหลด
- คลิกปุ่มดาวน์โหลด เพื่อดึงข้อมูลไบนารีของคอนเทนเนอร์
- เพิ่มไฟล์ไบนารีลงใน ไดเรกทอรีรากของโปรเจ็กต์และโฟลเดอร์ "ไฟล์สนับสนุน" ในโปรเจ็กต์
ชื่อไฟล์เริ่มต้นควรเป็นรหัสคอนเทนเนอร์ (เช่น 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. การพุชค่าไปยังชั้นข้อมูล
ชั้นข้อมูลคือแผนที่ที่ช่วยให้ข้อมูลรันไทม์เกี่ยวกับแอป เช่น เหตุการณ์การสัมผัส หรือการดูหน้าจอ พร้อมใช้งานสำหรับมาโครและแท็ก Tag Manager ในคอนเทนเนอร์
ตัวอย่างเช่น การพุชข้อมูลเกี่ยวกับการดูหน้าจอลงในแผนที่ชั้นข้อมูล จะช่วยให้คุณตั้งค่าแท็กในอินเทอร์เฟซเว็บของ Tag Manager ให้เริ่มทำงานพิกเซล Conversion และการเรียกการติดตามเพื่อตอบสนองต่อการดูหน้าจอเหล่านั้นได้โดยไม่ต้องฮาร์ด โค้ดลงในแอป
ระบบจะพุชเหตุการณ์ไปยังชั้นข้อมูลโดยใช้ 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 ตัวอย่างของ Google Tag Manager ในไฟล์พร็อพเพอร์ตี้ลิสต์ของโปรเจ็กต์
ขั้นแรก ให้เพิ่มข้อมูลโค้ดที่แสดงเป็นตัวหนาต่อไปนี้ลงในไฟล์ผู้รับมอบสิทธิ์ของแอป
@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
เปิดลิงก์ในโปรแกรมจำลองหรืออุปกรณ์จริงเพื่อแสดงตัวอย่างคอนเทนเนอร์ฉบับร่างในแอป
เมื่อพร้อมที่จะทำให้ค่าการกำหนดค่าฉบับร่างพร้อมใช้งานสำหรับ แอปพลิเคชันแล้ว ให้เผยแพร่คอนเทนเนอร์
การกำหนดค่าขั้นสูง
Google Tag Manager สำหรับมือถือมีตัวเลือกการกำหนดค่าขั้นสูงมากมายที่ช่วยให้คุณเลือกค่าตามเงื่อนไขรันไทม์โดยใช้กฎ รีเฟรชคอนเทนเนอร์ด้วยตนเอง และรับตัวเลือกเพิ่มเติมสำหรับการเปิดคอนเทนเนอร์ ส่วนต่อไปนี้จะสรุปการกำหนดค่าขั้นสูงที่ใช้กันโดยทั่วไป
ตัวเลือกขั้นสูงสำหรับการเปิดคอนเทนเนอร์
Google Tag Manager SDK มีหลายวิธีในการเปิดคอนเทนเนอร์ ซึ่งช่วยให้คุณควบคุมกระบวนการโหลดได้มากขึ้น
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: จะออกการเรียกกลับวงจรชีวิตหลายรายการตลอดกระบวนการโหลด เพื่อให้โค้ดของคุณทราบว่าคำขอโหลดเริ่มต้นเมื่อใด คำขอโหลดล้มเหลวหรือสำเร็จหรือไม่และเพราะเหตุใด รวมถึงคอนเทนเนอร์โหลดจากดิสก์หรือเครือข่ายในท้ายที่สุดหรือไม่
คุณจะต้องใช้การเรียกกลับเหล่านี้เพื่อทราบว่าคอนเทนเนอร์ที่บันทึกไว้หรือคอนเทนเนอร์เครือข่ายโหลดเสร็จเมื่อใด เว้นแต่ว่าแอปพลิเคชันของคุณจะใช้ค่าเริ่มต้นได้ โปรดทราบว่าคุณจะไม่สามารถโหลดคอนเทนเนอร์ที่บันทึกไว้หรือคอนเทนเนอร์เครือข่ายได้หากนี่เป็นการเรียกใช้แอปเป็นครั้งแรกและไม่มีการเชื่อมต่อเครือข่าย
openContainerById:callback: จะส่งค่า enum ต่อไปนี้เป็นอาร์กิวเมนต์ไปยังการเรียกกลับเหล่านี้
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: ใช้การเรียกกลับรายการเดียว ซึ่งจะเรียกใช้เมื่อคอนเทนเนอร์พร้อมใช้งาน
ทั้ง 2 วิธีมีระยะหมดเวลาเริ่มต้นที่
2.0 วินาที
การประเมินมาโครในรันไทม์โดยใช้กฎ
คอนเทนเนอร์สามารถประเมินค่าในรันไทม์โดยใช้กฎ กฎอาจอิงตามเกณฑ์ต่างๆ เช่น ภาษาของอุปกรณ์ แพลตฟอร์ม หรือค่ามาโครอื่นๆ ตัวอย่างเช่น คุณสามารถใช้กฎเพื่อเลือกสตริงการแสดงผลที่แปลเป็นภาษาท้องถิ่นตามภาษาของอุปกรณ์ในรันไทม์ได้ โดยกำหนดค่าได้โดยใช้กฎต่อไปนี้
จากนั้นสร้างมาโครการเก็บรวบรวมค่าสำหรับแต่ละภาษา และเพิ่มกฎนี้ลงในมาโครแต่ละรายการโดยแทรกรหัสภาษาที่เหมาะสม เมื่อเผยแพร่คอนเทนเนอร์นี้แล้ว แอปพลิเคชันจะแสดงสตริงการแสดงผลที่แปลเป็นภาษาท้องถิ่นได้ ทั้งนี้ขึ้นอยู่กับภาษาของอุปกรณ์ของผู้ใช้ในรันไทม์
โปรดทราบว่าหากคอนเทนเนอร์เริ่มต้นต้องมีกฎ คุณต้องใช้ไฟล์คอนเทนเนอร์ไบนารีเป็นคอนเทนเนอร์เริ่มต้น
ดูข้อมูลเพิ่มเติมเกี่ยวกับการกำหนดค่ากฎ (ศูนย์ช่วยเหลือ)
ไฟล์คอนเทนเนอร์เริ่มต้นแบบไบนารี
คอนเทนเนอร์เริ่มต้นที่ต้องมีกฎควรใช้ไฟล์คอนเทนเนอร์ไบนารี แทนไฟล์พร็อพเพอร์ตี้ลิสต์หรือ JSON ไฟล์เป็นคอนเทนเนอร์เริ่มต้น คอนเทนเนอร์ไบนารีรองรับการกำหนดค่ามาโครในรันไทม์ด้วยกฎ Google Tag Manager ในขณะที่ไฟล์พร็อพเพอร์ตี้ลิสต์หรือไฟล์ JSON ไม่รองรับ
คุณสามารถดาวน์โหลดไฟล์คอนเทนเนอร์ไบนารีจากอินเทอร์เฟซเว็บของ Google Tag Manager และควรเพิ่มลงในแพ็กเกจหลักของแอปพลิเคชันโดยใช้แบบแผนการตั้งชื่อ GTM-XXXX ซึ่งชื่อไฟล์แสดงถึงรหัสคอนเทนเนอร์
ในกรณีที่มีทั้งไฟล์พร็อพเพอร์ตี้ลิสต์และ/หรือไฟล์ JSON รวมถึงไฟล์คอนเทนเนอร์ไบนารี SDK จะใช้ไฟล์คอนเทนเนอร์ไบนารีเป็นคอนเทนเนอร์เริ่มต้น
การใช้มาโครการเรียกใช้ฟังก์ชัน
มาโครการเรียกใช้ฟังก์ชันคือมาโครที่ตั้งค่าเป็นค่าส่งคืนของฟังก์ชันที่ระบุในแอปพลิเคชัน คุณสามารถใช้มาโครการเรียกใช้ฟังก์ชันเพื่อรวมค่ารันไทม์เข้ากับกฎ Google Tag Manager เช่น กำหนดในรันไทม์ว่าจะแสดงราคาใดให้ผู้ใช้โดยอิงตามภาษาและสกุลเงินที่กำหนดค่าของอุปกรณ์
วิธีกำหนดค่ามาโครการเรียกใช้ฟังก์ชัน
- กำหนดมาโครการเรียกใช้ฟังก์ชันในอินเทอร์เฟซเว็บของ Google Tag Manager คุณจะกำหนดค่าอาร์กิวเมนต์เป็นคู่คีย์-ค่าก็ได้
- กำหนดตัวแฮนเดิลที่ใช้โปรโตคอล
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
- ลงทะเบียนตัวแฮนเดิลโดยใช้ 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
วิธีกำหนดค่าแท็กการเรียกใช้ฟังก์ชัน
- กำหนดแท็กการเรียกใช้ฟังก์ชันในอินเทอร์เฟซเว็บของ Google Tag Manager คุณจะกำหนดค่าอาร์กิวเมนต์เป็นคู่คีย์-ค่าก็ได้
- ใช้โปรโตคอล
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
- ลงทะเบียนตัวแฮนเดิลแท็กการเรียกใช้ฟังก์ชันโดยใช้ชื่อแท็กที่กำหนดค่าใน
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
การตั้งค่าระยะเวลารีเฟรชที่กำหนดเอง
Google Tag Manager SDK จะพยายามดึงข้อมูลคอนเทนเนอร์ใหม่หากคอนเทนเนอร์ปัจจุบันมีอายุเกิน 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
Google Tag Manager SDK จะพิมพ์ข้อผิดพลาดและคำเตือนลงในบันทึกโดยค่าเริ่มต้น
การเปิดใช้การบันทึกแบบละเอียดมากขึ้นอาจเป็นประโยชน์สำหรับการแก้ไขข้อบกพร่อง และทำได้โดยการใช้ 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];