Google Analytics電子商務程式碼新增
目的:主要用於跟蹤購物車,可以在谷歌分析師中反饋訂單號和訂單金額資訊。
使用谷歌Analytics(分析)跟蹤電子商務的基本過程中總結了三個用於跟蹤您網站上的電子商務交易所需的方法。
應該在你的購物車或電子商務軟體按照順序呼叫這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. 交易裡面新增物品(產品)
Themethod tracks information about each individual item in the user's shoppingcart and associates
the item with each transaction via the orderId
_addItem()方法跟蹤購物車每一個單獨物品資訊並且通過同一個orderID欄位來關聯到同一個交易號。這個方法用來跟蹤特別的資訊,譬如sku號,價格,分類和數量。
3: Submitthe transaction to the Analytics servers. 提交交易到分析師伺服器
The 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(['', '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]);
備註:
1. $affiliation和$tax無此專案可以直接設定為空值’’。
2. 新增物品時要遍歷購物車每一個物品