關於GTM的那些坑與逝去的青春
前言
原本專案一直都是以 GA 來追蹤使用者行為,但最近 GA 發出警告訊息提示追蹤數超過一個月一千萬次的額度(註1),因此決定改用 Google 力推的 Firebase Analytics 取代 GA(註2),但因為我們有額外追蹤加強型電商 (Enhanced e-commerce),這一段的報表在 Firebase console 上是沒有的,所以必須搭配 GTM(Google Tag Manager) 來做為 Firebase 與 GA 之間的橋樑,傳遞資料給 GA。
註1:追蹤額度說明文件:Google Analytics Collection Limits and Quotas
註2:Firebase Analytics 在基本追蹤上是無限制的,但是加強型電商的部分是屬於 Universal Google Analytics,就會有每個用戶一天20萬次的限制。
GTM是用來做什麼的?
首先來看官方的解釋:
Built for mobile apps.
借助Firebase,開發人員只需要安裝一次應用即可。 然後,使用易於使用的跟踪代碼管理器網頁界面,營銷人員可以修改應用內事件並將其發送到Google內部的各種測量解決方案以及Kochava,Tune,Adjust,Appsflyer和Apsalar等合作夥伴。 跟踪代碼管理器中的更新會直接發送到您安裝的應用程序,使團隊可以即時更新其測量設置。
將Google跟踪代碼管理器與Google Analytics for Firebase集成,可讓您在應用分發後,通過Web界面遠程管理您的Google Analytics實施。
再來看看網友的解釋:
- GTM acts as a bridge in between Google Analytics and Google Analytics for Firebase. If you want the Firebase data to go in same GA property then GTM is the only option.
- GTM intercepts Firebase events. This gives an advantage to the developer if he/she want to make efficient use of the 500 events restricted by Firebase framework by modifying the current events if they are to be used for other purpose going forward.
- Answer for you question about custom event come from the “Add Event” tag type. Add Event is used when you want to create a second event from the source event. For example, if you’re already logging button clicks, but want to treat a button click on a specific button as a custom event as well, you would setup a tag as an ‘Add Event’ tag, and trigger it when a button click event happened with parameters matching the specific button name.
- GTM充當Google Analytics和Google Analytics for Firebase之間的橋樑。 如果您希望Firebase數據使用相同的GA屬性,那麼GTM是唯一的選擇。
- GTM攔截Firebase事件。 如果開發人員希望通過修改當前事件來有效使用受Firebase框架限制的500個事件(如果這些事件將用於其他目的),則這對開發人員有利。
- 通過“添加事件”標籤類型為您解答有關自定義事件的問題。 當您想從源事件創建第二個事件時使用添加事件。 例如,如果您已經記錄按鈕點擊次數,但想要將一個按鈕點擊某個特定按鈕作為自定義事件,則您可以將一個標籤設置為“添加事件”標籤,並在點擊按鈕時觸發它 事件發生在與特定按鈕名稱匹配的參數上。
在專案中加入 GTM
先參照官方文件使用 Cocoapods 來安裝 GTM 與設定 GTM container,這邊列舉出幾個我遇到的坑:
- 如果原本專案裡有安裝舊的 GA,因為 GTM 裡面就包含了 GA,可能會導致衝突,這時將原 GA 移除再安裝 GTM 即可。
-
json 檔放的位置要參照文件中的方式放置在一個 container 資料夾中,然後在複製到專案中時勾選 “Create folder references”,container 在專案中資料夾的顏色會是藍色的,這樣才不會抓不到 json 檔。
-
使用模擬器才看得到 GTM 的 log 資訊,實機則不會出現。
在 GTM 上設定 Enhanced E-commerce tags
基本上 GTM 運作的模式就是: 我們先在 GTM container 網頁上設定想追蹤的 tag(標籤),如加強型電商中的 product impressions, product clicks, promotion impressions…等等,每個 tag 會包含一個 trigger(觸發條件),當 Firebase 發送事件後 GTM 會去比對這個事件是不是符合某個 tag 的觸發條件,在這次的例子中就會將數據發送到 GA 報表上。
針對加強型電商在官方文件中有提供包含 container 設定與程式碼的範例,不過不得不說這份文件的坑點真的很多,以下我整理了文件中幾個錯誤之處:
1. Product impressions
註解的 select_content 應該為 view_search_results
2. Checkout process
這裡是整份文件最雷最混亂的區塊了
Begin checkout
Begin checkout 下方的 tag configuration 不之為何不見了,他的設定應該為:
See the tag configuration for this example:
Tag Type: Universal Analytics
Track Type: Event
Event Category, e.g.: Ecommerce
Event Action, e.g. : Begin Checkout
Enable Enhanced Ecommerce Features: True
Read data from: Firebase Event
Trigger, e.g.: (Custom > Some Events) Event Name equals begin_checkout
對照Google Tag Manager for Android文件才發現,iOS 的 Checkout Process 遺漏了一個項目”Additional checkout steps”,而事實上”Checkout options”中的程式碼內容又其實是”Additional checkout steps”的內容,因為這邊錯得比較離譜,以下直接提供我修正的版本:
Additional checkout steps
Measure additional steps in a checkout process by logging a CHECKOUT_PROGRESS event with one or more items (i.e. products) defined with the relevant fields:
// Define products with relevant parameters. NSDictionary *product1 = @{ kFIRParameterItemID : @"sku1234", // ITEM_ID or ITEM_NAME is required. kFIRParameterItemName : @"Android Jogger Sweatpants", kFIRParameterItemCategory : @"Apparel/Men/Pants", kFIRParameterItemVariant : @"Blue", kFIRParameterItemBrand : @"Google", kFIRParameterPrice : @39.99, kFIRParameterCurrency : @"USD", // Item-level currency unused today. kFIRParameterQuantity : @1 }; // Prepare ecommerce dictionary. NSArray *items = @[product1]; // Set checkout step and optional checkout option. NSDictionary *ecommerce = @{ @"items" : items, kFIRParameterCheckoutStep : @2, kFIRParameterCheckoutOption : @"Visa" // Optional. }; // Log CHECKOUT_PROGRESS event with ecommerce dictionary. [FIRAnalytics logEventWithName:kFIREventCheckoutProgress parameters:ecommerce];
See the tag configuration for this example:
Tag Type: Universal Analytics
Track Type: Event
Track Type: Screen View
Fields to Set: (Field Name) screenName (value, e.g.) Checkout Step 2 Screen
Enable Enhanced Ecommerce Features: True
Read data from: Firebase Event
Trigger, e.g.: (Custom > Some Events) Event Name equals checkout_progress
Checkout options
Checkout options allow you to measure additional information about the state of the checkout process. You can either measure checkout options either as part of a checkout step event (as shown above) or upon a user selecting an option after the event for a given checkout step has already been logged.
Measure checkout options after a checkout step by logging a kFIREventCheckoutProgress event with the corresponding kFIRParameterCheckoutStep and kFIRParameterCheckoutOption parameters:
// Set checkout step and optional checkout option. NSDictionary *ecommerce = @{ kFIRParameterCheckoutStep : @2, kFIRParameterCheckoutOption : @"MasterCard" }; // Log SET_CHECKOUT_OPTION event with ecommerce dictionary. [FIRAnalytics logEventWithName:kFIREventSetCheckoutOption parameters:ecommerce];
See the tag configuration for this example:
Tag Type: Universal Analytics
Track Type: Event
Event Category, e.g.: Ecommerce
Event Action, e.g. : Set Checkout Option
Enable Enhanced Ecommerce Features: True
Read data from: Firebase Event
Trigger, e.g.: (Custom > Some Events) Event Name equals set_checkout_option
3. Purchases
這次是錯在最底下的 tag configuration for this example 的地方
3.1. Event Action 的建議應該是 “Purchase” 而非 “Set Checkout Option”
3.2. 最後一段 “set_checkout_option” 應改為 “ecommerce_purchase”
整份文件出現如此誇張多的錯誤,Google 是不是想故意讓 iOS 開發者不開心阿~(可惡
額外補充
- 在 tag 的設定中,如果有勾選 Enable overriding settings in this tag,那就不要設定 Google Analytics Settings,只要設定 Tracking ID 就好。(Tracking ID 的格式為 U-XXXXXXXXX)
- GA 報表上要接收到 GTM 傳來的資料需要一些些時間,不過通常在5分鐘以內
有其他問題可以到 Google Tag Manager 的討論串或是官方Google+專頁發問
2020/07/23 Update
最近到新公司因緣際會下又要來用 GTM 導資料,因為 Firebase 只有 E-Commerce 而缺乏 Enhanced E-Commerce 的關係,Google 官方推薦使用 Firebase Analytics + Google Tag Manager 來達到用 Firebase 送加強型電商的需求。
iOS GTM v5 SDK 的文件與兩年前比起來易讀許多!也沒有看到什麼明顯的錯誤,想當年吼~(老人講古時間
附上剛剛設定好的 GTM Container 設定截圖: