为 Google 代码设置购买事件

您可以通过在网站上设置购买事件并将其关联到 Merchant Center,来跟踪所有购买行为。本文介绍了如何在网站上设置购买事件。

准备工作

如需跟踪购买,请确保已在您的网站上添加 Google 代码。如果您还没有添加 Google 代码,可以创建新的 Google 代码并在您的网站上使用。了解如何添加 Google 代码 (gtag.js)

运作方式

如需收集购买数据,您需要跟踪网站上的购买事件。您可以在网站上定义购买事件,以便跟踪这些事件。

下面这两个示例说明了如何从网站发送购买事件。

示例 1:在页面打开时发送购买事件

您应该将购买事件放置在用户做出购买操作的网站页面上。例如,您可以在用户做出购买操作时显示的确认页面上添加事件。本教程介绍了如何将事件添加到用户点击“购买”按钮时所在的页面。

将事件放置在 <body> 标记末尾的 <script> 标记中。直接将事件放置在 <script> 标记中会在网页加载时触发该事件。

示例代码

<!--
  注意:在以下代码示例中,请务必
  将“TAG_ID”替换为您的代码 ID。
  了解详情:https://support.google.com/tagmanager/answer/12326985
-->
<!DOCTYPE html>
<html lang="en">
<head>
    <!-- Install the Google tag (gtag.js) -->
    <!-- Google tag (gtag.js) -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=TAG_ID"></script>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());


        gtag('config', 'TAG_ID');
    </script>
</head>
<body>
    <div> 这是购买确认所在的位置</div>
    <script>
    gtag("event", "purchase", {
        transaction_id: "T_12345_1",
        affiliation: "Google Merchandise Store",
        value: 25.42,
        tax: 4.90,
        shipping: 5.99,
        currency: "USD",
        coupon: "SUMMER_SALE",
        items: [
        // 如果用户购买了不止一件商品,
        // 您可以将这些商品添加到商品数组
         {
          item_id: "SKU_12345",
          item_name: "Stan and Friends Tee",
          affiliation: "Google Merchandise Store",
          coupon: "SUMMER_FUN",
          discount: 2.22,
          index: 0,
          item_brand: "Google",
          item_category: "Apparel",
          item_category2: "Adult",
          item_category3: "Shirts",
          item_category4: "Crew",
          item_category5: "Short sleeve",
          item_list_id: "related_products",
          item_list_name: "Related Products",
          item_variant: "green",
          location_id: "ChIJIQBpAG2ahYAR_6128GcTUEo",
          price: 9.99,
          quantity: 1
        }]
    });
    </script>
</body>
</html>

示例 2:在用户点击按钮时发送购买事件

您可以通过几种方式设置购买事件,使得事件在用户点击“购买”按钮时触发。一种方式是向“购买”按钮添加一个 ID,然后将事件代码放置在事件监听器中。在以下示例中,仅当用户点击包含 ID 的购买按钮时,系统才会发送该事件。

示例代码

<!--
  注意:在以下代码示例中,请务必
  将“TAG_ID”替换为您的代码 ID。
  了解详情:https://support.google.com/tagmanager/answer/12326985
-->
<!DOCTYPE html>
<html lang="en">
<head>
    <!-- Install the Google tag (gtag.js)-->
    <!-- Google tag (gtag.js) -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=TAG_ID"></script>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());


        gtag('config', 'TAG_ID');
    </script>
</head>
<body>
    <div>这是购买表单所在的位置</div>
    <button id="purchase">购买</button>
    <script>
    document.getElementById("purchase").addEventListener("click", function () {
        gtag("event", "purchase", {
                // 这个购买事件所用的交易 ID 不同于
                // 以前的购买事件,所以 Analytics
                // 不会对事件进行去重。
                // 了解详情:https://support.google.com/analytics/answer/12313109
                transaction_id: "T_12345_2",
                affiliation: "Google Merchandise Store",
                value: 25.42,
                tax: 4.90,
                shipping: 5.99,
                currency: "USD",
                coupon: "SUMMER_SALE",
                items: [
                {
                  item_id: "SKU_12345",
                  item_name: "Stan and Friends Tee",
                  affiliation: "Google Merchandise Store",
                  coupon: "SUMMER_FUN",
                  discount: 2.22,
                  index: 0,
                  item_brand: "Google",
                  item_category: "Apparel",
                  item_category2: "Adult",
                  item_category3: "Shirts",
                  item_category4: "Crew",
                  item_category5: "Short sleeve",
                  item_list_id: "related_products",
                  item_list_name: "Related Products",
                  item_variant: "green",
                  location_id: "ChIJIQBpAG2ahYAR_6128GcTUEo",
                  price: 9.99,
                  quantity: 1
                }]
          });
      });
    </script>
</body>
</html>

将购买事件关联到 Merchant Center

您可以使用关键事件来源将购买事件关联到 Merchant Center。

了解如何在 Merchant Center 中添加关键事件来源

相关链接

该内容对您有帮助吗?

您有什么改进建议?
搜索
清除搜索内容
关闭搜索框
Google 应用
主菜单
17397039213867889145
true
搜索支持中心
true
true
true
true
true
71525
false
false
false
false