內容網站設置(Wordpress)
事前準備
必須先完成 基礎追蹤 (必要)
手動 GTM dataLayer 設置
在網頁中新增以下程式碼,並放入各變數對應資料
<script>
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'content_id':'內容 ID',
'content_category':'內容分類',
'content_author': '內容作者(多個以逗號分隔)',
'content_tags':'內容標籤(多個以逗號分隔)'
});
</script>
Wordpress Code Snippets Plugin 設置
先在外掛裡安裝好 WPCode – Insert Headers and Footers
啟用 WPCode 外掛後,如下圖按下選單的 「 Add Snippet 」

於程式區塊貼上以下程式碼,Location 選 Frontend Only,右上角選擇 Active 儲存即可
function add_datalayer_for_single_posts() {
if (!is_single()) return;
$post_id = get_the_ID();
$categories = wp_get_post_categories($post_id, ['fields' => 'names']);
$categories = (count($categories) > 0)? $categories : '';
$tags = wp_get_post_tags($post_id, ['fields' => 'names']);
$tags = implode(',', $tags);
$author = get_the_author();
?>
<script>
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
content_id: <?= json_encode($post_id) ?>,
content_title: <?= json_encode(get_the_title()) ?>,
content_category: <?= json_encode($categories) ?>,
content_tags: <?= json_encode($tags) ?>,
content_author: <?= json_encode($author) ?>
});
</script>
<?php
}
add_action('wp_head', 'add_datalayer_for_single_posts');
匯入 「瀏覽內容」 容器
下載並選擇匯入容器檔案
選擇工作區:選擇 「現有」 並點選您正在使用的工作區,一般沒特別設定則直接選擇 "Default Workspace"。
選擇匯入選項: 選擇 「合併」 > 「覆寫有衝突的代碼、觸發條件和變數。」
點擊 「確認」 完成匯入。

請先用 「預覽」 功能測試,確認 「Tags Fired」 是否有觸發成功
按下右上角 「提交」,再按下 「發布」 按鈕即可完成設置。


Last updated