คู่มือนักพัฒนาซอฟต์แวร์นี้จะอธิบายวิธีติดตั้ง Google Tag Manager ในแอปพลิเคชันบนอุปกรณ์เคลื่อนที่
เกริ่นนำ
Google Tag Manager ช่วยให้นักพัฒนาซอฟต์แวร์เปลี่ยนการกำหนดค่าในแอปพลิเคชันบนอุปกรณ์เคลื่อนที่ของตนโดยใช้อินเทอร์เฟซ Google Tag Manager โดยไม่ต้องสร้างและส่งคำขอไบนารีแอปพลิเคชันไปยังตลาดกลางแอปอีกครั้ง
ซึ่งจะมีประโยชน์ในการจัดการค่าของการกำหนดค่าหรือแฟล็กในแอปพลิเคชันที่คุณอาจต้องเปลี่ยนแปลงในอนาคต เช่น
- การตั้งค่า UI และสตริงการแสดงผลแบบต่างๆ
- ขนาด สถานที่ตั้ง หรือประเภทของโฆษณาที่แสดงในแอปพลิเคชัน
- การตั้งค่าเกม
ระบบอาจประเมินค่าของการกำหนดค่าขณะรันไทม์โดยใช้กฎต่างๆ ซึ่งจะเป็นการเปิดใช้การกำหนดค่าแบบไดนามิก เช่น
- การใช้ขนาดหน้าจอเพื่อกำหนดขนาดโฆษณาแบนเนอร์
- การใช้ภาษาและตำแหน่งเพื่อกำหนดค่าองค์ประกอบ UI
Google Tag Manager ยังเปิดใช้การติดตั้งใช้งานแท็กและพิกเซลติดตามในแอปพลิเคชันแบบไดนามิกได้ด้วย นักพัฒนาซอฟต์แวร์จะพุชเหตุการณ์สำคัญเข้าไปในชั้นข้อมูลและตัดสินใจในภายหลังได้ว่าควรให้แท็กหรือพิกเซลติดตามใดเริ่มทำงาน ปัจจุบัน Tag Manager รองรับแท็กต่อไปนี้
- Analytics สำหรับ Google Mobile App
- แท็กการเรียกใช้ฟังก์ชันที่กำหนดเอง
ก่อนที่คุณจะเริ่ม
ก่อนใช้คู่มือเริ่มต้นใช้งานนี้ คุณจะต้องมีสิ่งต่อไปนี้
- บัญชี Google Tag Manager
- มาโครคอลเล็กชันคอนเทนเนอร์และค่าใหม่ของ Tag Manager
- แอปพลิเคชันบนอุปกรณ์เคลื่อนที่สำหรับ iOS ที่จะใช้ Google Tag Manager
- SDK บริการ Google Analytics ซึ่งมีไลบรารี Tag Manager
หากคุณเพิ่งเริ่มใช้ Google Tag Manager เราขอแนะนําให้ ดูข้อมูลเพิ่มเติมเกี่ยวกับคอนเทนเนอร์ มาโคร และกฎ (ศูนย์ช่วยเหลือ) ก่อนดําเนินการต่อตามคู่มือนี้
เริ่มต้นใช้งาน
ส่วนนี้จะแนะนำนักพัฒนาซอฟต์แวร์เกี่ยวกับเวิร์กโฟลว์ทั่วไปของ Tag Manager ดังนี้
- เพิ่ม Google Tag Manager SDK ลงในโปรเจ็กต์
- กำหนดค่าคอนเทนเนอร์เริ่มต้น
- เปิดคอนเทนเนอร์
- รับค่าการกำหนดค่าจากคอนเทนเนอร์
- พุชเหตุการณ์ไปยัง DataLayer
- ดูตัวอย่างและนำคอนเทนเนอร์ไปใช้จริง
1. การเพิ่ม Google Tag Manager SDK ลงในโครงการของคุณ
ก่อนใช้ Google Tag Manager SDK คุณจะต้องเพิ่ม 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 เครื่องจัดการแท็กใช้คอนเทนเนอร์เริ่มต้นเมื่อเรียกใช้แอปพลิเคชันครั้งแรก ระบบจะใช้คอนเทนเนอร์เริ่มต้นจนกว่าแอปจะสามารถดึงข้อมูลคอนเทนเนอร์ใหม่ผ่านเครือข่ายได้
หากต้องการดาวน์โหลดและเพิ่มไบนารีของคอนเทนเนอร์เริ่มต้นลงในแอปพลิเคชัน ให้ทำตามขั้นตอนต่อไปนี้
- ลงชื่อเข้าใช้เว็บอินเทอร์เฟซของ 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. การพุชค่าไปยัง DataLayer
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 ตัวอย่างของ 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 ต่อไปนี้ภายใต้คีย์ประเภท URL ของไฟล์รายการพร็อพเพอร์ตี้ของแอปพลิเคชัน
URL identifier: your.package_name URL scheme: tagmanager.c.your.package.name
เปิดลิงก์บนโปรแกรมจำลองหรืออุปกรณ์จริงเพื่อ แสดงตัวอย่างคอนเทนเนอร์แบบร่างในแอป
เมื่อพร้อมทำให้ค่าของการกำหนดค่าฉบับร่างใช้งานได้กับแอปพลิเคชันแล้ว ให้ นำคอนเทนเนอร์ไปใช้จริง
การกำหนดค่าขั้นสูง
Google Tag Manager สำหรับอุปกรณ์เคลื่อนที่มีตัวเลือกการกำหนดค่าขั้นสูงจำนวนหนึ่งที่ให้คุณเลือกค่าตามเงื่อนไขรันไทม์ โดยใช้กฎ รีเฟรชคอนเทนเนอร์ด้วยตนเอง และรับตัวเลือกเพิ่มเติมสำหรับการเปิดคอนเทนเนอร์ ส่วนต่อไปนี้จะอธิบายถึงการกำหนดค่าขั้นสูงที่พบบ่อยหลายรายการ
ตัวเลือกขั้นสูงสำหรับการเปิดคอนเทนเนอร์
Google Tag Manager SDK มีวิธีเปิดคอนเทนเนอร์หลายวิธีที่ช่วยให้คุณควบคุมกระบวนการโหลดได้มากขึ้น ดังนี้
openContainerById:โค้ดเรียกกลับ:
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
เพื่อบังคับคำขอคอนเทนเนอร์เครือข่ายได้ทุกเมื่อ
ความสะดวกทั้งสองนี้แบบไม่บล็อกการทำงาน
openContainerWithId:tagManager:openType:timeout:
แสดงผลออบเจ็กต์ TAGContainerFuture
ซึ่งเมธอด get
แสดงผล TAGContainer
ทันทีที่โหลด (แต่จะบล็อกจนกว่าจะถึงเวลาดังกล่าว)
เมธอด openContainerWithId:tagManager:openType:timeout:notifier:
จะใช้โค้ดเรียกกลับทางเดียว โดยเรียกใช้เมื่อคอนเทนเนอร์พร้อมใช้งาน
ทั้ง 2 วิธีมีระยะหมดเวลาเริ่มต้นอยู่ที่ 2.0
วินาที
การประเมินมาโครขณะรันไทม์โดยใช้กฎ
คอนเทนเนอร์จะประเมินค่าขณะรันไทม์โดยใช้กฎได้ กฎอาจอิงตามเกณฑ์ต่างๆ เช่น ภาษาของอุปกรณ์ แพลตฟอร์ม หรือค่ามาโครอื่นๆ ตัวอย่างเช่น กฎอาจใช้เพื่อเลือกสตริงที่แสดงที่แปลแล้วโดยอิงตามภาษาของอุปกรณ์ขณะรันไทม์ได้ ซึ่งคุณจะกำหนดค่าได้โดยใช้กฎต่อไปนี้
จากนั้นคุณสามารถสร้างมาโครคอลเล็กชันค่าสำหรับแต่ละภาษา และเพิ่มกฎนี้ลงในมาโครแต่ละรายการโดยใส่รหัสภาษาที่เหมาะสม เมื่อนำคอนเทนเนอร์นี้ไปใช้จริง แอปพลิเคชันจะแสดงสตริงการแสดงผลที่แปลแล้ว โดยขึ้นอยู่กับภาษาของอุปกรณ์ของผู้ใช้ในขณะรันไทม์
โปรดทราบว่าหากคอนเทนเนอร์เริ่มต้นจำเป็นต้องใช้กฎ คุณต้องใช้ไฟล์คอนเทนเนอร์ไบนารีเป็นคอนเทนเนอร์เริ่มต้น
ดูข้อมูลเพิ่มเติมเกี่ยวกับการกำหนดค่ากฎ (ศูนย์ช่วยเหลือ)
ไฟล์คอนเทนเนอร์เริ่มต้นแบบไบนารี
คอนเทนเนอร์เริ่มต้นที่ต้องใช้กฎควรใช้ไฟล์คอนเทนเนอร์แบบไบนารีแทนที่จะใช้ไฟล์รายการพร็อพเพอร์ตี้หรือไฟล์ JSON เป็นคอนเทนเนอร์เริ่มต้น คอนเทนเนอร์ไบนารีรองรับการกำหนดค่ามาโครขณะรันไทม์ด้วยกฎ Google Tag Manager ในขณะที่รายการพร็อพเพอร์ตี้หรือไฟล์ JSON ไม่รองรับ
คุณสามารถดาวน์โหลดไฟล์คอนเทนเนอร์ไบนารีจากอินเทอร์เฟซเว็บ Google Tag Manager และควรเพิ่ม
ลงใน Bundle แอปพลิเคชันหลักตามแบบแผนการตั้งชื่อนี้
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];
การแก้ไขข้อบกพร่องด้วยตัวบันทึก
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 ของตัวบันทึกที่มีอยู่ได้โดยใช้
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];