当前位置:迷你笔记 » 技术 » 如何为Shopify Thank You结账页添加Google Merchant Center Review脚本?

如何为Shopify Thank You结账页添加Google Merchant Center Review脚本?

Shopify后台找到Setting - Customer Events - Custom Pixel添加以下代码:

// 监听 checkout_completed 事件
analytics.subscribe('checkout_completed', (event) => {
  const checkout = event.data.checkout;
  // 获取订单总金额(单位为分,需要除以100转换为单位元)
  const totalPrice = checkout.totalPrice?.amount;

  // 如果订单总金额大于10元
  if (totalPrice > 10 * 100) {
    // 动态加载 Google Merchant Center 的脚本
    const script = document.createElement('script');
    script.src = 'https://apis.google.com/js/platform.js?onload=renderOptIn';
    script.async = true;
    script.defer = true;
    document.body.appendChild(script);
    window.renderOptIn = function() {
      window.gapi.load('surveyoptin', function() {
        window.gapi.surveyoptin.render({
          // 必填字段
          "merchant_id": your_merchant_id,
          "order_id": String(checkout.order_id),
          "email": String(checkout.email),

          // 获取送货国家,若无法获取则默认 'US'
          "delivery_country": checkout.shipping_address?.country_code || "US",

          // 预计交货日期,格式为 'YYYY-MM-DD',默认10天
          "estimated_delivery_date": new Date(Date.parse(checkout.created_at) + 10 * 24 * 60 * 60 * 1000).toISOString().split('T')[0]
        });
      });
    };
  }
});

参考链接:
https://support.google.com/merchants/answer/14629205
https://shopify.dev/docs/api/web-pixels-api/standard-events/checkout_completed

未经允许不得转载:迷你笔记 » 如何为Shopify Thank You结账页添加Google Merchant Center Review脚本?

相关文章

评论 (0)

6 + 2 =