Bạn có thể theo dõi tất cả giao dịch mua bằng cách thiết lập các sự kiện mua hàng trên trang web của bạn rồi liên kết các sự kiện đó với Merchant Center. Bài viết này giải thích cách thiết lập các sự kiện mua hàng trên trang web của bạn.
Trước khi bắt đầu
Để theo dõi giao dịch mua, hãy đảm bảo rằng bạn đã thêm một thẻ Google vào trang web của mình. Nếu chưa thêm thì bạn có thể tạo một thẻ Google mới rồi dùng trên trang web của mình. Tìm hiểu cách Cài đặt thẻ Google (gtag.js).
Cách hoạt động
Để thu thập dữ liệu giao dịch mua, bạn cần theo dõi các sự kiện mua hàng trên trang web của mình. Bạn có thể theo dõi các sự kiện này bằng cách xác định trên trang web của mình.
Dưới đây là 2 ví dụ về cách hệ thống gửi sự kiện mua hàng từ trang web của bạn.
Ví dụ 1: Gửi sự kiện mua hàng khi trang mở ra
Bạn nên đặt sự kiện mua hàng trên trang mà người dùng đã mua hàng trên trang web của bạn. Ví dụ: Bạn có thể thêm sự kiện trên trang xác nhận xuất hiện khi người dùng mua hàng. Tài liệu hướng dẫn này trình bày cách thêm sự kiện vào trang mà người dùng nhấp vào nút "Mua hàng".
Đặt sự kiện vào thẻ <script> ở cuối thẻ <body>. Việc đặt sự kiện ngay trong thẻ <script> sẽ kích hoạt sự kiện đó khi tải trang.
Mã mẫu
<!--
Note: In the following code sample, make sure to
replace "TAG_ID" with your tag ID.
Learn more: 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> This is where your purchase confirmation would go </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: [
// If someone purchases more than one item,
// you can add those items to the items array
{
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>
Ví dụ 2: Gửi sự kiện mua hàng khi người dùng nhấp vào nút
Bạn có thể thiết lập sự kiện mua hàng theo một vài cách để sự kiện đó kích hoạt khi người dùng nhấp vào nút "Mua hàng". Bạn có thể thêm mã nhận dạng vào nút "Mua hàng" rồi đặt mã sự kiện vào trình nghe sự kiện. Trong ví dụ bên dưới, sự kiện chỉ được gửi khi người dùng nhấp vào nút mua hàng có mã nhận dạng.
Mã mẫu
<!--
Note: In the following code sample, make sure to
replace "TAG_ID" with your tag ID.
Learn more: 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>This is where the purchase form would go</div>
<button id="purchase">Purchase</button>
<script>
document.getElementById("purchase").addEventListener("click", function () {
gtag("event", "purchase", {
// This purchase event uses a different transaction ID
// from the previous purchase event so Analytics
// doesn't deduplicate the events.
// Learn more: 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>
Liên kết sự kiện mua hàng với Merchant Center
Bạn có thể liên kết các sự kiện mua hàng với Merchant Center bằng nguồn sự kiện quan trọng.
Tìm hiểu cách thêm nguồn sự kiện quan trọng trong Merchant Center.