如何用 Google Tag Manager(GTM) 在 Google Analytics (GA) 中記錄有 URL Fragment (#) 的網址?
如果想要看看有 URL Fragment(#)的網頁流量該怎麼看呢?
一如往常的我們進入到 Google Analytics (GA)查看網頁流量 ( Behavior → Site Content → All Pages),在 Filter 中輸入網址「/productA/homeseries#aaa123」後卻發現完全沒資料?咦?怎麼會這樣子呢?是網頁沒有流量嗎?
不不不!如果你輸入了含有 URL Fragment 的網址,但報表卻沒有跑出任何數字,先別懷疑自己的眼睛!
因為在 Google Analytics (GA) 所記錄的網頁(pagePath)中,預設是會自動忽略 URL Fragment(#))的!
什麼是 URL Fragment?
Fragment 是提供給瀏覽器使用的錨點(anchor),並不會被傳送到伺服器。通常出現在單頁應用程式(SPA) 或著是重度使用 AJAX 的應用程式,利用 Fragment 來控制一些效果,或是顯示網頁中的特定部份。
如何用 Google Tag Manager(GTM) 在 Google Analytics (GA) 中記錄有 URL Fragment (#) 的網址?
Step1. 開啟 GTM 預設變數(Variable) — New History Fragment、History Source
變數(Variables) → 內建變數(Build-In Variables) → 設定(Configure) → 勾選:記錄(History)中的「New History Fragment」與「History Source」
- New History Fragment:用來紀錄現在的網址片段(URL Fragment)
- History Source:用來作為網址片段(URL Fragment)改變時的觸發條件(Trigger)
Step2. 建立自訂 JavaScript 變數
用以紀錄包含網址片段(URL Fragment)的網址(pagepath)。
變數(Variables) → 使用者定義的變數(User-Defined Variables) → 新增(New)
function() {
return document.location.pathname + document.location.search + document.location.hash;
}
Step3. 建立觸發條件(Trigger)
觸發條件(Triggers) → 新增(New)
Step 4. 建立代碼(Tag):Page with URL Fragment
除了 GA 裡用來記錄 All pageviews 的代碼(Tags)之外,需要另建立一個代碼(Tags) ,來記錄有網址片段(URL Fragment)的網址,並寫入 GA 的網頁(page)。
代碼(Tags) → 新增(New)
(4.1) 追蹤類型(Track Type):網頁瀏覽(Page View)
(4.2) 需勾「在這個代碼中啟用覆寫設定(Enable overriding settings in this tag)」
(4.3) 更多設定(More Settings) → 設定的欄位(Fields to Set) →
欄位名稱(Field Name):page
值(Value):{{Page with URL Fragment}} ps. 值(Value) 選 Step2 建立的變數(Variables)
(4.4) 觸發條件設定:選擇 Step3.設定的觸發條件:History Fragment Change
Reference
- #GTMtips: Track URL Fragments As Pageviews
https://www.simoahava.com/gtm-tips/track-url-fragments-as-pageviews/