售后页面上的像素代码跟踪

如果您的商店已安装可向商店结账流程中添加售后页面的应用,那么您在商店中使用的任何自定义像素代码跟踪都可能不会获取特定跟踪事件。添加到商店的自定义脚本框中的自定义跟踪像素代码仅跟踪订单状态页面上的事件,该页面在结账流程中的售后页面之后显示。如果客户从售后页面离开您的商店,则系统不会跟踪订单状态页面上的事件。

为确保正确获取转化事件,您可以添加一个脚本,用于跟踪售后页面上的事件。此脚本还可以跟踪通过售后页面进行的其他购买,例如增销。添加售后页面脚本后,您需要调整订单状态页面脚本,以忽略售后页面脚本已获取的事件。

只有使用自定义跟踪像素代码时,您才需要更改商店跟踪事件的方式。例如,如果您通过在线商店 > 偏好设置来设置 Google Analytics,那么 Google Analytics 将已经开始正确获取售后页面上的事件。

注意事项

售后页面自定义脚本与订单状态页面自定义脚本类似,但存在几项重要差异:

  • 售后页面自定义脚本将添加到售后页面,而不是订单状态页面。

  • 此字段仅允许输入 JavaScript。不接受 Liquid 代码。

  • 唯一允许使用的 HTML 标签是 <script>

  • 脚本在沙盒中运行,而未包含在主页上。

  • 只有当您的商店已安装可向结账流程中添加售后页面的应用时,您才能添加售后页面脚本。

在沙盒中运行脚本可确保脚本的安全以及可将其用于预期目的。

Shopify 支持团队无法提供售后页面脚本的相关帮助。如果您需要帮助,您可以在 Shopify 社区中发帖或聘请 Shopify 专家。

订单状态页面上脚本的兼容性

为确保像素代码正确跟踪所有转化事件,请在售后页面和订单状态页面上均设置跟踪脚本。为避免将转化事件计数两次,您可以在订单状态页面上运行的脚本中使用 post_purchase_page_accessed Liquid 变量。

如果客户到达售后页面并导航到订单状态页面,post_purchase_page_accessed 变量将返回 true。如果客户未到达售后页面,变量将返回 false

例如,您可以在订单状态页面上对脚本使用以下格式:

{% unless post_purchase_page_accessed %} <script>     //insert your tracking script </script> {% endunless %}

可用 API

您可以使用 JavaScript 全局变量为跟踪脚本提供所需的数据访问权限。可访问的数据在 window.Shopify 下提供。

跟踪售后页面上的购买

若要跟踪通过售后页面进行的其他购买,您可以订阅 Shopify.on 事件。

订阅后,只要成功应用售后更改集,就会触发以下操作:

  • 系统使用两个 Order 类型的参数 order 和 outdated order 来调用您的处理程序。

  • window.Shopify 下的全局变量将更新,以便脚本可以使用更新的数据。

订阅了此活动的处理程序可能只有 500 毫秒的执行时间。请确保提前加载您需要的任何依赖项。

添加售后页面脚本

  1. 在 Shopify 后台中,转到设置 > 结账

  2. 售后自定义脚本字段中,输入您的脚本。

  3. 点击保存

示例脚本

您可以使用以下基本脚本模板来协助您构建自己的售后页面脚本。此示例脚本使用 Google Analytics 跟踪初始转化,并说明如何跟踪其他购买。此示例非常简单,您的最终脚本可能与它不同。

示例脚本文件

订单状态页面的示例脚本:

<script async src="https://www.googletagmanager.com/gtag/js?id=G-FYNQ742HTX"></script> <script> (function() {   // make sure the initial conversion isn't tracked twice   {% if first_time_accessed == false or post_purchase_page_accessed == true %}     return;   {% endif %}   // set up google analytics   window.dataLayer = window.dataLayer || [];   function gtag() {     dataLayer.push(arguments);   }   gtag('js', new Date());   gtag('config', 'G-FYNQ742HTX');   // track initial conversion   var order = window.Shopify.order;   gtag('event', 'purchase', {     affiliation: 'My Shopify Store',     transaction_id: Number(order.id).toString(),     value: order.totalPrice,     currency: order.currency,     items: order.lineItems.map((item) => ({       id: Number(item.id).toString(),       name: item.title,       category: item.product.type,       price: item.price,       quantity: item.quantity,       variant: Number(item.variant.sku).toString(),     })),   }); })(); </script>

售后页面的示例脚本:

<script async src="https://www.googletagmanager.com/gtag/js?id=G-FYNQ742HTX"></script> <script> (function() {   // set up google analytics   window.dataLayer = window.dataLayer || [];   function gtag() {     dataLayer.push(arguments);   }   gtag('js', new Date());   gtag('config', 'G-FYNQ742HTX');   // make sure the initial conversion isn't tracked twice   if (!Shopify.wasPostPurchasePageSeen) {     var order = window.Shopify.order;     // track initial conversion     gtag('event', 'purchase', {       affiliation: 'My Shopify Store',       transaction_id: Number(order.id).toString(),       value: order.totalPrice,       currency: order.currency,       items: order.lineItems.map(function(item) {         return {           id: Number(item.id).toString(),           name: item.title,           category: item.product.type,           price: item.price,           quantity: item.quantity,           variant: Number(item.variant.sku).toString(),         };       }),     });   }   // set up additional conversion tracking   Shopify.on('CheckoutAmended', function(newOrder, previousOrder) {     // identify which items were recently added, if any     var oldItems = previousOrder.lineItems.map(function (line) { return line.id; });     var addedItems = newOrder.lineItems.filter(       function (line) { return oldItems.indexOf(line.id) < 0; }     );     // no new items were added, so we skip conversion tracking     if (addedItems.length === 0) {       return;     }     // track additional purchase     gtag('event', 'purchase', {       affiliation: 'My Shopify Store',       transaction_id: Number(order.id).toString(),       value: order.totalPrice,       currency: order.currency,       items: addedItems.map(function (item) {         return {           id: Number(item.id).toString(),           name: item.title,           category: item.product.type,           price: item.price,           quantity: item.quantity,           variant: Number(item.variant.sku).toString(),         };       }),     });   }); })(); </script>

参考

下表包含通过 window.Shopify 向售后自定义脚本公开的属性的类型定义:

字段定义
window.Shopify
shop下单商店的详细信息。
order订单的详细信息。
pageUrl当前页面的 URL。
wasPostPurchasePageSeen客户是否在此结账流程中查看了售后页面。如果该页面是第一次显示,则为 true,否则为 false,例如重新加载此页面。
on(event: string, handler: function): void订阅事件。目前仅支持 CheckoutAmended 事件。
off(event: string, handler: function): void处理程序取消订阅给定事件。目前仅支持 CheckoutAmended 事件。
Shop
id商店的 ID。
currency商店货币采用 ISO 4217 格式,例如 USD。有关更多详细信息,请参阅 shop.currency。
Order
id订单的内部识别码。
number订单名称的整数表示形式,例如 1025。
checkoutToken结账的内部识别码。
customer与订单关联的客户。
lineItem订单的订单项目。
subtotalPrice应用订单项目和购物车折扣后订单中所有商品的小计价格。小计不包含税费(除非税费已包含在价格中)、发货成本或小费。
totalPrice订单的总价。
currency订单所用货币的 ISO 4217 代码。
discounts应用于订单的折扣总额。
Customer
id客户的 ID。
email客户的邮箱。
acceptsMarketing客户是否已接受营销。如果客户接受营销,此值将为 true,否则为 false
hasAccount客户邮箱是否与客户账户关联。如果邮箱已在客户账户中列出,此值将为 true,否则为 false。有关详细信息,请参阅 customer.has_account。
firstName客户的名字。
lastName客户的姓氏。
ordersCount客户所下订单总数。
totalSpent客户在所有订单上花费的总金额。
LineItem
finalLinePrice订单项目中所有商品的总计价格。这等于 line_item.final_price 乘以 line_item.quantity
finalPrice订单项目的价格,包括所有订单项目级别的折扣金额。
lineLevelTotalDiscount专门应用于订单项目的所有折扣的总额。这不包括添加到购物车的折扣。
optionsWithValues商品的产品选项中所选值的数组。有关更多详细信息,请参阅 line_item.options_with_values。
originalLinePrice应用折扣之前订单中包含的所有商品的总计价格。这等于 line_item.original_price 乘以 line_item.quantity
originalPrice应用折扣前订单项目的原始价格。
price订单项目的单价。该价格反映应用于订单项目的任何折扣。仅适用于位于德国或法国的商店。
product订单项目的产品。
properties已添加到购物车的商品的一组自定义信息。有关详细信息,请参阅 line_item.properties。
quantity订单项目的数量。
title订单项目的标题。有关详细信息,请参阅 line_item.title。
variant订单项目的多属性。
Product
id产品的 ID。
type产品的类型。
ProductVariant
id多属性的 ID。
sku多属性的 SKU。
CartDiscount
id折扣应用程序的内部识别码。
code折扣码(如果有)。
type折扣的类型。可能的值为:automatic、 discount_codemanual 和 script
amount通过折扣所节省的订单价格的总金额。

Shopify商户官网原文详情:

Pixel tracking on the post-purchase page

If your store has installed an app that adds a post-purchase page to your store's checkout, then any custom pixel tracking that you use on your store might not capture certain tracking events. Custom tracking pixels that are added to your store's Additional scripts box track events on only the order status page, which comes after the post-purchase page in the checkout. If a customer leaves your store on the post-purchase page, then no events on the order status page are tracked.

To ensure that you capture conversion events properly, you can add a script that tracks events on the post-purchase page. This script can also track additional purchases that are made from the post-purchase page, such as upsells. After you add a post-purchase page script, you need to adjust your order status page script to ignore events that are already captured by the post-purchase page script.

You need to change how your store tracks events only if you use a custom tracking pixel. For example, if you set up Google Analytics through Online Store > Preferences, then it already captures events correctly on your post-purchase page.

Considerations

The post-purchase page additional script is similar to the order status page additional scripts, but with a few key differences:

  • The script is added to the post-purchase page, not the order status page.

  • The field allows only JavaScript. Liquid code isn't accepted.

  • The only HTML tag allowed is <script>.

  • The script runs within a sandbox and isn't included on the main page.

  • You can add a post-purchase page script only if your store has installed an app that adds a post-purchase page to your checkout.

Running the script within a sandbox ensures that the script is secure and is used for its intended purpose.

Shopify Support can’t help with post-purchase page scripts. If you need assistance, then you can post in the Shopify Community or hire a Shopify Expert.

Compatibility with scripts on the order status page

To ensure that your pixels correctly track all conversion events, set up tracking scripts on both the post-purchase page and the order status page. To avoid counting conversion events twice, you can use the post_purchase_page_accessed Liquid variable in scripts that run on the order status page.

If a customer lands on the post-purchase page and then navigates to the order status page, then the post_purchase_page_accessed variable returns true. If a customer doesn't arrive at the post-purchase page, then the variable returns false.

For example, you can use the following format for your scripts on the order status page:

{% unless post_purchase_page_accessed %} <script>     //insert your tracking script </script> {% endunless %}

Available APIs

You can use JavaScript globals to give tracking scripts access to the data that they need. The accessible data is available under window.Shopify.

Tracking purchases made on the post-purchase page

To track additional purchases that are made through the post-purchase page, you can subscribe to the Shopify.on event.

After you subscribe, the following actions occur whenever a post-purchase changeset is successfully applied:

  • Your handler is called with two Order-type arguments: order and outdated order.

  • The globals under window.Shopify are updated so that the scripts can use the updated data.

Handlers subscribed to this event can have as little as 500 ms to execute. Make sure to load any dependencies that you need ahead of time.

Add the post-purchase page script

  1. From your Shopify admin, go to Settings > Checkout.

  2. In the Post-purchase additional scripts field, enter your script.

  3. Click Save.

Example script

You can use the following basic script template to help you build your own post-purchase page script. This example script uses Google Analytics to track the initial conversion, and explains how to track additional purchases. This example is very simple and your final script will likely differ from it.

Example script files

Example script for the order status page:

<script async src="https://www.googletagmanager.com/gtag/js?id=G-FYNQ742HTX"></script> <script> (function() {   // make sure the initial conversion isn't tracked twice   {% if first_time_accessed == false or post_purchase_page_accessed == true %}     return;   {% endif %}   // set up google analytics   window.dataLayer = window.dataLayer || [];   function gtag() {     dataLayer.push(arguments);   }   gtag('js', new Date());   gtag('config', 'G-FYNQ742HTX');   // track initial conversion   var order = window.Shopify.order;   gtag('event', 'purchase', {     affiliation: 'My Shopify Store',     transaction_id: Number(order.id).toString(),     value: order.totalPrice,     currency: order.currency,     items: order.lineItems.map((item) => ({       id: Number(item.id).toString(),       name: item.title,       category: item.product.type,       price: item.price,       quantity: item.quantity,       variant: Number(item.variant.sku).toString(),     })),   }); })(); </script>

Example script for the post-purchase page:

<script async src="https://www.googletagmanager.com/gtag/js?id=G-FYNQ742HTX"></script> <script> (function() {   // set up google analytics   window.dataLayer = window.dataLayer || [];   function gtag() {     dataLayer.push(arguments);   }   gtag('js', new Date());   gtag('config', 'G-FYNQ742HTX');   // make sure the initial conversion isn't tracked twice   if (!Shopify.wasPostPurchasePageSeen) {     var order = window.Shopify.order;     // track initial conversion     gtag('event', 'purchase', {       affiliation: 'My Shopify Store',       transaction_id: Number(order.id).toString(),       value: order.totalPrice,       currency: order.currency,       items: order.lineItems.map(function(item) {         return {           id: Number(item.id).toString(),           name: item.title,           category: item.product.type,           price: item.price,           quantity: item.quantity,           variant: Number(item.variant.sku).toString(),         };       }),     });   }   // set up additional conversion tracking   Shopify.on('CheckoutAmended', function(newOrder, previousOrder) {     // identify which items were recently added, if any     var oldItems = previousOrder.lineItems.map(function (line) { return line.id; });     var addedItems = newOrder.lineItems.filter(       function (line) { return oldItems.indexOf(line.id) < 0; }     );     // no new items were added, so we skip conversion tracking     if (addedItems.length === 0) {       return;     }     // track additional purchase     gtag('event', 'purchase', {       affiliation: 'My Shopify Store',       transaction_id: Number(order.id).toString(),       value: order.totalPrice,       currency: order.currency,       items: addedItems.map(function (item) {         return {           id: Number(item.id).toString(),           name: item.title,           category: item.product.type,           price: item.price,           quantity: item.quantity,           variant: Number(item.variant.sku).toString(),         };       }),     });   }); })(); </script>

References

The following table contains the type definitions for the attributes that are exposed to post-purchase additional scripts through window.Shopify:

FieldDefinition
window.Shopify
shopThe details of the shop from where the order was placed.
orderThe details of the order.
pageUrlThe current page's URL.
wasPostPurchasePageSeenWhether the customer viewed the post-purchase page during this checkout. It will be true when rendering for the first time, and false otherwise, such as if the page is reloaded.
on(event: string, handler: function): voidSubscribes to an event. Currently, only the CheckoutAmended event is supported.
off(event: string, handler: function): voidUnsubscribes handler from the given event. Currently, only the CheckoutAmended event is supported.
Shop
idThe ID of the shop.
currencyThe store's currency in ISO 4217 format. For example, USD. For more details, refer to shop.currency.
Order
idThe order's internal identifier.
numberThe integer representation of the order name. For example, 1025.
checkoutTokenThe checkout's internal identifier.
customerThe customer associated with the order.
lineItemThe line items for the order.
subtotalPriceThe subtotal price of all the items in the order after both line-item and cart discounts have been applied. The subtotal doesn't include taxes (unless taxes are included in the prices), shipping costs, or tips.
totalPriceThe total price of the order.
currencyThe ISO 4217 code of the order's currency.
discountsThe sum of the amount of the discounts applied to the order.
Customer
idThe ID of the customer.
emailThe email address of the customer.
acceptsMarketingWhether the customer had accepted marketing. It will be true if the customer accepts marketing, and false if not.
hasAccountWhether the customer email is associated with a customer account. It will be true if the email is listed on a customer account, and false if not. For more details, refer to customer.has_account.
firstNameThe first name of the customer.
lastNameThe last name of the customer.
ordersCountThe total number of orders that the customer has placed.
totalSpentThe total amount that the customer has spent on all orders.
LineItem
finalLinePriceThe combined price of all the items in the line item. This is equal to line_item.final_price multiplied by line_item.quantity.
finalPriceThe price of the line item, including all line-level discount amounts.
lineLevelTotalDiscountThe total amount of all discounts applied to the line item specifically. This doesn't include discounts that are added to the cart.
optionsWithValuesAn array of selected values from the item's product options. For more details, refer to line_item.options_with_values.
originalLinePriceThe combined price of the quantity of items included in the line, before discounts were applied. This is equal to line_item.original_price multiplied by line_item.quantity.
originalPriceThe original price of the line item before discounts were applied.
priceThe unit price of the line item. The price reflects any discounts that are applied to the line item. Available only to stores located in Germany or France.
productThe product of the line item.
propertiesAn array of custom information for an item that has been added to the cart. For more information, refer to line_item.properties.
quantityThe quantity of the line item.
titleThe title of the line item. For more information, refer to line_item.title.
variantThe variant of the line item.
Product
idThe ID of the product.
typeThe type of the product.
ProductVariant
idThe ID of the variant.
skuThe SKU of the variant.
CartDiscount
idThe discount application's internal identifier.
codeThe code of the discount, if it has one.
typeThe type of the discount. Possible values are: automaticdiscount_codemanual, and script.
amountThe total amount that the price of the order is reduced by the discount.

文章内容来源:Shopify商户官方网站


(本文内容根据网络资料整理,出于传递更多信息之目的,不代表连连国际赞同其观点和立场)