---
description: Google Tag Manager 安裝
---

# 範例：Easystore 加入購物車

## 新增變數

<figure><img src="https://2075385674-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA8vmX591TX7ccV7n1Qcw%2Fuploads%2F8ZpdLCrZmX9WeDFTjaSz%2Fimage.png?alt=media&#x26;token=aa68aa9d-e4d0-4c01-9a9a-4ece53fa9971" alt=""><figcaption><p>新增變數</p></figcaption></figure>

### 加入購物車 product\_items

<figure><img src="https://2075385674-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA8vmX591TX7ccV7n1Qcw%2Fuploads%2Fip6MBCH2XOOpRb44Dio4%2Fimage.png?alt=media&#x26;token=c5177f33-2420-44b7-ab75-392733b92813" alt=""><figcaption><p>加入購物變數</p></figcaption></figure>

* 變數名稱：easystore \[add\_to\_cart] prdouct\_items
* 變數類型：資料層變數
* 資料層變數名稱：eventModel.items
* 按下「儲存」

***

## 新增觸發條件

<figure><img src="https://2075385674-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA8vmX591TX7ccV7n1Qcw%2Fuploads%2FMXHIHY1kmVqRN1A9s79A%2Fimage.png?alt=media&#x26;token=1ab5a496-efaf-4fcc-a9c4-1a2bf5c69819" alt=""><figcaption><p>新增觸發事件</p></figcaption></figure>

<figure><img src="https://2075385674-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA8vmX591TX7ccV7n1Qcw%2Fuploads%2FtWGpRHQSWX4fbPVdd2l3%2Fimage.png?alt=media&#x26;token=19592ffc-aac3-4753-8076-56fedbe3cb20" alt=""><figcaption><p>加入購物車事件</p></figcaption></figure>

* 觸發名稱：easystore\[add\_to\_cart] (可更改)
* 觸發條件類型：自訂事件
* 事件名稱：add\_to\_cart
* 按下「儲存」

***

## 新增代碼

<figure><img src="https://2075385674-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA8vmX591TX7ccV7n1Qcw%2Fuploads%2FUfHMuXUYJnlpTHtIcitW%2Fimage.png?alt=media&#x26;token=07663b9e-0534-45de-8bfe-93b048cb3503" alt=""><figcaption><p>建立代碼</p></figcaption></figure>

<figure><img src="https://2075385674-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA8vmX591TX7ccV7n1Qcw%2Fuploads%2FgCuX8oVAxnDlIaaUtV3n%2Fimage.png?alt=media&#x26;token=4be467d7-cb8b-434f-959b-1f3a72983c5f" alt=""><figcaption><p>加入購物車代碼</p></figcaption></figure>

* 代碼名稱： \[easystore] add\_to\_cart
* 代碼類型：自訂 HTML
* HTML：複製下面內容程式碼貼上

```javascript
<script>
if({{easystore [add_to_cart] prdouct_items}} !== undefined && {{easystore [add_to_cart] prdouct_items}} != null){
	var items = {{easystore [add_to_cart] prdouct_items}};

	if(items.length > 0){
		for(var i in items){
			if(items[i].item_id !== undefined && items[i].item_id !== null){
				itg('event', 'product',{
				    'type':'add',
				    'id': items[i].item_id,
				    'category': (items[i].item_category !== undefined && items[i].item_category !== null)? items[i].item_category : '',
				    'brand': (items[i].item_brand !== undefined && items[i].item_brand !== null)? items[i].item_brand : '',
				    'tag': '',
				    'price': items[i].price * items[i].quantity
				});
			}
		}
	}
}
</script>  
```

* 觸發條件：easystore\[add\_to\_cart]（ [#xin-zeng-chu-fa-tiao-jian](#xin-zeng-chu-fa-tiao-jian "mention") ）

按下右上角「儲存」按鈕後，點擊右上角「提交」並且「發布」即可生效。

#### 提醒

* 請先用「預覽」功能測試，確認「Tags Fired」是否有觸發成功
