若您的網站使用 WordPress 架站,請先參閱此篇內容,事先準備相關設置,以利後續資料串連運用。
本文段落
- 安裝 WordPress Plugin
- 設置 Google Tag Manager
- 設置 Data Layer 資料傳送至 Google Tag Manager
- 檢查 Data Layer 資料傳送正常
▎安裝 WordPress Plugin
需要安裝下列二項插件,進行安裝相關追蹤碼,以及設置受眾資料傳送。
安裝 Insert Headers and Footers Plugin
Insert Headers and Footers by WPBeginner
安裝 Code Snippets Plugin
▎設置 Google Tag Manager
選擇「設定」點擊「Insert Headers and Footers」進入,將 Google Tag Manager 程式碼放入相對應位置,如下圖呈現。

▎設置 Data Layer 資料傳送至 Google Tag Manager
選擇「程式碼片段」點擊「新增程式碼片段」

設定 WordPress Data Layer 程式碼

- 名稱:Wordpress Datalayer (可更改)
- 程式碼:複製下面程式碼即可
add_action( 'wp_head', function () { $categories = get_the_category(); $tags = ''; $current_user = wp_get_current_user(); foreach(wp_get_post_tags(get_the_ID()) as $tag) { $tags .= (empty($tags))? $tag->name : ','. $tag->name; } ?> <script> /* 在這裡撰寫網站專屬 JavaScript 程式碼 */ window.dataLayer = window.dataLayer || []; window.dataLayer.push({ 'user_id':'<?=$current_user->ID?>', 'user_email': '<?=$current_user->user_email?>', 'category': '<?=$categories[0]->slug;?>', 'content_category': '<?=$categories[0]->cat_ID;?>', 'content_ids':'<?=get_the_ID()?>', 'content_name':'<?=get_the_title()?>', 'content_type': 'article', 'content_authors': '<?=get_the_author()?>', 'content_tags':'<?=$tags?>' }); </script> <?php } );
- 選擇「僅執行於網站前端」,按下「儲存設定」即建立完成
▎檢查 Data Layer 資料傳送正常
安裝 Tag Assistant Legacy
啟動 Tag Assistant Legacy 並重新整理頁面
打開 Tag Assistant Legacy 資訊,點擊 Google Tag Manger 查看資訊

選擇「Data Layer」頁籤

可看到 wordpress 傳送內容屬性資料,表示資料以正確傳送至 Google Tag Manager