跳到主要內容
Cypher's Practical Coding
GTM 與電商追蹤

結業考試

15 題,每題 5 分,滿分 75 分。 可先執行查看結果再交卷。

已作答 0 / 15
UTM 參數

1. 請用 new URL() 解析 "https://shop.example.com?utm_source=instagram&utm_medium=social&utm_campaign=spring",用 console.log 印出 utm_campaign 的值。

UTM 參數

2. 請解析 "https://shop.example.com?utm_source=google&utm_medium=cpc",用 console.log 印出「來源:google,媒介:cpc」。

Data Layer

3. 請初始化 window.dataLayer,push 一筆 event 為 "page_view"、page_title 為 "首頁" 的資料,用 console.log 印出 dataLayer[0].page_title。

Data Layer

4. 請初始化 dataLayer,依序 push event 為 "view_item" 和 "add_to_cart" 兩筆資料,用 console.log 印出 dataLayer 的長度。

事件設計

5. 請初始化 dataLayer 並 push 一筆 view_item 事件,ecommerce 中 currency 為 "TWD"、value 為 890、items 陣列包含一個 item_name 為 "牛仔褲"。用 console.log 印出 items[0].item_name。

事件設計

6. 請初始化 dataLayer 並 push 一筆 add_to_cart 事件,ecommerce 的 items 中有 item_name 為 "外套"、price 為 1200、quantity 為 2。用 console.log 印出 price * quantity 的結果。

電商追蹤

7. 請組裝一筆 purchase 事件的 dataLayer.push,transaction_id 為 "ORD-555",value 為 3000,currency 為 "TWD",items 包含 item_name 為 "連帽外套"、price 為 1500、quantity 為 2。最後印出 transaction_id。

電商追蹤

8. 請宣告 items = [{price: 500, quantity: 3}, {price: 200, quantity: 2}],用 for 迴圈算出商品小計總和,減去 discount = 150,加上 shipping = 80,用 console.log 印出最終金額。

除錯與驗證

9. 請初始化 dataLayer 並 push 一筆 purchase 事件(含 ecommerce.currency 為 "TWD")。用 if 判斷 ecommerce.currency 是否存在,存在印出 "PASS",不存在印出 "FAIL"。

除錯與驗證

10. 請宣告 badItems = { item_name: "圍巾" }(物件,非陣列)。用 Array.isArray() 判斷,如果不是陣列印出 "items 格式錯誤",是陣列印出 "items 格式正確"。

Data Layer

11. 請初始化 dataLayer,push 三筆事件:event 分別為 "page_view"、"view_item"、"add_to_cart"。用 for 迴圈逐行印出每筆的 event 值。

電商追蹤

12. 請初始化 dataLayer,先 push { ecommerce: null } 清除資料,再 push 一筆 purchase 事件(transaction_id: "ORD-001", value: 750, currency: "TWD", items 含一個 item_name 為 "手機殼")。用 console.log 印出 dataLayer 的長度。

UTM 參數

13. 請解析 "https://shop.example.com?utm_source=email&utm_medium=newsletter&utm_campaign=weekly_deals&utm_content=hero_banner",把四個 UTM 值組成物件 utmData 後,用 console.log 印出 utmData.content。

事件設計

14. 請初始化 dataLayer 並 push 一筆自訂事件 event 為 "apply_coupon",coupon_code 為 "VIP50",discount_amount 為 500。用 console.log 印出「優惠券 VIP50 折扣 500 元」。

除錯與驗證

15. 請初始化 dataLayer 並 push 一筆 purchase 事件,ecommerce 中故意不放 currency。用 if 判斷 currency 是否為 undefined,如果是就印出 "缺少 currency,營收報表會是 0"。

確認作答完畢後,點擊交卷