目的:主要用於跟蹤購物車,可以在Google分析師中反饋訂單號和訂單金額資訊。
頁面:http://www.yoursite.com/flow.php?step=checkout
配置方法:詳見樣本和簡化後添加代碼
電子商務代碼使用指引
參考地址:https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingEcommerce?hl=zh-CN#General
使用GoogleAnalytics(分析)跟蹤電子商務的基本過程中總結了三個用於跟蹤您網站上的電子商務證券交易所需的方法。
應該在你的購物車或電子商務軟體按照順序調用這3個方法。
1: Createa transaction object. 建立一個交易對象
Usethe
_addTrans()method to intialize a transaction object. The transaction object stores all therelated information about a single transaction, such as the order ID, shippingcharges, and billing address. The information in the transaction object isassociated
with its items by means of the order IDs for the transaction and allitems, which should be the same ID.
用_addTrans()方法來初始化一個交易對象,這個對象儲存了單一個證券交易所有相關的資訊,譬如orderID 訂單號,shipping charges運費和billingaddress帳單地址。這些交易對象資訊都是用order IDs訂單號來關聯所有的物品。必須是同一個訂單號。
2: Add items to the transaction. 交易裡面添加物品(產品)
The_addItem()
method tracks information about each individual item in the user's shoppingcart and associates
the item with each transaction via the orderId
field. This method tracks the details about a particular item, such as SKU,price, category, and quantity.
_addItem()方法跟蹤購物車每一個單獨物品資訊並且通過同一個orderID欄位來關聯到同一個交易號。這個方法用來跟蹤特別的資訊,譬如sku號,價格,分類和數量。
3: Submitthe transaction to the Analytics servers. 提交交易到分析師伺服器
The _trackTrans()
method confirms that a purchase has occurred, and all data that has been builtup
in the transaction object is finalized as a transaction.
_trackTrans()方法確認一次購買發生之後,所有資訊都已經在交易對象裡面完成就提交給伺服器。
範例程式碼
https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingEcommerce?hl=zh-CN#General
<html><head><title>Receipt for your clothing purchase from Acme Clothing</title><script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-XXXXX-X']); _gaq.push(['_trackPageview']); _gaq.push(['_addTrans', '1234', // order ID - required 'Acme Clothing', // affiliation or store name '11.99', // total - required '1.29', // tax '5', // shipping 'San Jose', // city 'California', // state or province 'USA' // country ]); // add item might be called for every item in the shopping cart // where your ecommerce engine loops through each item in the cart and // prints out _addItem for each _gaq.push(['_addItem', '1234', // order ID - required 'DD44', // SKU/code - required 'T-Shirt', // product name 'Green Medium', // category or variation '11.99', // unit price - required '1' // quantity - required ]); _gaq.push(['_trackTrans']); //submits transaction to the Analytics servers (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();</script></head><body> Thank you for your order. You will receive an email containing all your order details.</body></html>
範例程式碼中文注釋
添加一個交易對象
_gaq.push(['_addTrans',
'1234',
// order ID – required 訂單號必須
'Acme Clothing', // affiliationor store name 連署或商店名(非必須)可以為空白值’’
'11.99', // total – required
金額必須
'1.29', // tax 稅(非必須)可以為空白值’’
'5', // shipping 運費(非必須)可以為空白值’’
'San Jose', //city 城市(非必須)可以為空白值’’
'California', // stateor province 省(非必須)可以為空白值’’
'USA' //country 國家(非必須)可以為空白值’’
]);
添加每一個物品
// additem might be called for every item in the shopping cart 購物車裡面每一個物品都需要遍曆一次
// where your ecommerceengine loops through each item in the cart and
// prints out _addItem foreach
_gaq.push(['_addItem',
'1234',
// order ID – required 訂單號必須
'DD44',
// SKU/code – required sku必須
'T-Shirt', // product name 物品名稱 可以為空白值’’
'Green Medium', // category orvariation 物品類別 物品分類 可以為空白值’’
'11.99', // unit price – required
價格必須
'1'
// quantity – required 數量必須
]);
_gaq.push(['_trackTrans']); //submits transaction to the Analyticsservers
提交流水到分析師伺服器
電子商務代碼簡化之後要添加的代碼為
_gaq.push(['_addTrans',$orderId,$affiliation,$sumOfGoodsFee,$tax,$shippingFee,$city,$state,$country]);
_gaq.push(['_addItem',$orderId,$skuId,$productName,$productCategory,$unitPrice,$quanity]);
_gaq.push(['_trackTrans']);
備忘:
1. $affiliation和$tax無此項目可以直接設定為空白值’’。
2. 添加物品時要遍曆購物車每一個物品