# Shopify 如何部署代码

> 关于Shopify应用集成配置，请参阅[Shopify集成](/cn/integrations/shopify.md)。

## Shopify电商埋码指南 <a href="#shopify-dian-shang-mai-ma-zhi-nan" id="shopify-dian-shang-mai-ma-zhi-nan"></a>

### 以下提供的脚本方便Shopify电商客户进行快速埋码设置 <a href="#yi-xia-ti-gong-de-jiao-ben-fang-bian-shopify-dian-shang-ke-hu-jin-xing-kuai-su-mai-ma-she-zhi" id="yi-xia-ti-gong-de-jiao-ben-fang-bian-shopify-dian-shang-ke-hu-jin-xing-kuai-su-mai-ma-she-zhi"></a>

#### 1. 代码中找到theme.liquid模板，设置基础埋码 <a href="#id-1-dai-ma-zhong-zhao-dao-themeliquid-mu-ban-she-zhi-ji-chu-mai-ma" id="id-1-dai-ma-zhong-zhao-dao-themeliquid-mu-ban-she-zhi-ji-chu-mai-ma"></a>

Tips: xxxxx位置[具体参考](/cn/start-guide/quick/tag-setup.md)![Alt](https://help-ptengine-com-vitepress.oss-cn-guangzhou.aliyuncs.com/Build/2-2/ji-chu-dai-ma.jpg)

```
<!-- Ptengine Tag -->
<script src="https://js.ptengine.com/xxxxx.js"></script>
<!-- End Ptengine Tag -->
```

#### 2. 事件采集部署 <a href="#id-2-shi-jian-cai-ji-bu-shu" id="id-2-shi-jian-cai-ji-bu-shu"></a>

打开设置中的“客户事件”，将下列代码中的sid和步骤一一样，替换为sid\
\
![](https://help-ptengine-com-vitepress.oss-cn-guangzhou.aliyuncs.com/shopify/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20240226132049.png)\
\
并全部粘贴进自定义像素中。\
保存并连接。 请务必将id替换为您的sid

```
function trackEvent(eventType, eventProperties, cb) {
     //请务必将id替换为您的档案id
     const sid = "请输入档案id";
     const area = "com";
     const options = eventProperties || {};
    
     loadPtengineScript(sid, area)
     .then(() => {
     console.log("ptengine脚本加载完成");
     window.ptengine && window.ptengine.track(eventType, options);
     cb && typeof cb === "function" && cb();
     })
     .catch((error) => {
     console.error("ptengine脚本加载失败:", error);
     });
    }
    
    function loadPtengineScript(sid, area) {
     return new Promise((resolve, reject) => {
     if (window.ptengine) {
     // 如果ptengine已经存在，则立即解决
     resolve();
     } else {
     // 保留原始url
     const url = window.location.href.replace(
     /\/web-pixels.*\/sandbox\/modern/,
     ""
     );
     // 使用原始url进行发包
     window._pt_sp_2 = [];
     _pt_sp_2.push(`setAccount, ${sid}`);
     _pt_sp_2.push(`setPVTag,${url},replace`);
    
     // 动态加载ptengine脚本
     const script = document.createElement("script");
     // 判断是否在checkout页面
     const isCheckoutPage = window.location.href.includes("checkouts");
     const sandboxQuery = isCheckoutPage ? "" : "?sandbox"; 
    //checkout页面不添加'?sandbox'
     script.src = `https://js.ptengine.${area}/${sid}.js${sandboxQuery}`;
     script.onload = () => resolve();
     // 加载成功
     script.onerror = () => reject(new Error("Script loading failed"));
     //加载失败
     document.head.appendChild(script);
     }
     });
    }
    
    // 使用回调函数触发identify包例子
    analytics.subscribe("checkout_completed", (event) => {
     const checkout = event?.data?.checkout;
     const checkoutTotalPrice = checkout?.totalPrice?.amount;
     const checkoutItems = checkout?.lineItems;
     const checkoutOrderId = checkout?.order?.id;
     const uid = event?.data?.clientId;
    
     const eventProperties = {
     totalPrice: checkoutTotalPrice,
     totalorderid: checkoutOrderId,
     };
    
     const identifyCallback = () => {
     checkoutItems.forEach((checkoutItem) => {
     ptengine &&
     ptengine.identify(uid, {
     totalPrice: checkoutTotalPrice,
     totalorderid: checkoutOrderId,
     totalquantity: checkoutItem?.quantity,
     totaltitle: checkoutItem?.title,
     totalid: checkoutItem?.id,
     totalsku: checkoutItem?.variant?.sku,
     totalProductType: checkoutItem?.variant?.product?.type,
     });
     });
     };
     trackEvent("checkout_completed", eventProperties, identifyCallback);
    });
    
    analytics.subscribe("search_submitted", (event) => {
     trackEvent("search_submitted");
    });
    
    analytics.subscribe("collection_viewed", (event) => {
     trackEvent("collection_viewed");
    });
    
    analytics.subscribe("product_viewed", (event) => {
     trackEvent("product_viewed");
    });
    
    analytics.subscribe("product_added_to_cart", (event) => {
     trackEvent("product_added_to_cart");
    });
    
    analytics.subscribe("product_removed_from_cart", (event) => {
     trackEvent("product_removed_from_cart");
    });
    
    analytics.subscribe("cart_viewed", (event) => {
     trackEvent("cart_viewed");
    });
    
    analytics.subscribe("checkout_started", (event) => {
     trackEvent("checkout_started");
    });
    
    analytics.subscribe("checkout_contact_info_submitted", (event) => {
     trackEvent("checkout_contact_info_submitted");
    });
    
    analytics.subscribe("checkout_address_info_submitted", (event) => {
     trackEvent("checkout_address_info_submitted");
    });
    
    analytics.subscribe("checkout_shipping_info_submitted", (event) => {
     trackEvent("checkout_shipping_info_submitted");
    });
    
    analytics.subscribe("payment_info_submitted", (event) => {
     trackEvent("payment_info_submitted");
    });
```

<br>

#### 3.事件定义： <a href="#id-3-shi-jian-ding-yi" id="id-3-shi-jian-ding-yi"></a>

search\_submitted:使用搜索功能

collection\_viewed：列表页访问

product\_viewed：商品详情页访问

product\_added\_to\_cart：添加购物车

product\_removed\_from\_cart：移除购物车

cart\_viewed：购物车页面访问

checkout\_started：进入checkout页面

checkout\_contact\_info\_submitted：输入联系方式：邮箱

checkout\_address\_info\_submitted：输入地址

checkout\_shipping\_info\_submitted：选择快递信息

payment\_info\_submitted：选择支付方式

checkout\_completed：支付成功


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://helps.ptengine.com/cn/start-guide/othertag/tag-shopify.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
