> For the complete documentation index, see [llms.txt](https://instag.gitbook.io/doc/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://instag.gitbook.io/doc/dao-ru-an-zhuang/wordpress-nei-rong-wang-zhan-she-zhi.md).

# Wordpress 內容網站設置

## 事前準備

必須先完成 [基礎追蹤 (必要)](/doc/dao-ru-an-zhuang/ji-chu-zhui-zong-bi-yao.md)

## 手動 GTM dataLayer 設置

{% hint style="info" %}
若您尚不熟悉 GTM dataLayer 可參考 GTM 官方文件：

* <https://support.google.com/tagmanager/answer/6164391>
* <https://developers.google.com/tag-platform/tag-manager/datalayer>
  {% endhint %}

在網頁中新增以下程式碼，並放入各變數對應資料

```html
<script>
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
	'content_id':'內容 ID',
	'content_category':'內容分類',
	'content_author': '內容作者（多個以逗號分隔）',
	'content_tags':'內容標籤（多個以逗號分隔）'
});
</script>
```

## Wordpress Code Snippets Plugin 設置

1. 先在外掛裡安裝好 [WPCode – Insert Headers and Footers](https://tw.wordpress.org/plugins/insert-headers-and-footers/)
2. 啟用 WPCode 外掛後，如下圖按下選單的 「 Add Snippet 」

<figure><img src="/files/Z4gwS0kZYw5UymityEAa" alt=""><figcaption></figcaption></figure>

3. 於程式區塊貼上以下程式碼，Location 選 Frontend Only，右上角選擇 Active 儲存即可

```php
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');

```

***

## 匯入 「瀏覽內容」 容器

1. 下載並選擇匯入容器檔案

{% file src="/files/IoQ4mJYj1dA3dpUfxCsY" %}

2. 選擇工作區：選擇 「現有」 並點選您正在使用的工作區，一般沒特別設定則直接選擇 "Default Workspace"。
3. 選擇匯入選項： 選擇 「合併」 > 「覆寫有衝突的代碼、觸發條件和變數。」
4. 點擊 「確認」 完成匯入。

<figure><img src="/files/qMG8an4eQUjTU8hkDJ2T" alt=""><figcaption></figcaption></figure>

{% hint style="warning" %}
請先用 「預覽」 功能測試，確認 「Tags Fired」 是否有觸發成功
{% endhint %}

5. 按下右上角 「提交」，再按下 「發布」 按鈕即可完成設置。

<figure><img src="/files/Aeejr0mfldw95u31KRO1" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/ORVW1mGPJBHP07UOOvrE" alt=""><figcaption></figcaption></figure>
