我现在正在使用谷歌分析增强型电子商务我遇到问题是当用户将结帐表单提交到付款页面(不是我自己的)时,最后一步会触发,当用户完成付款时,付款页面将返回信息到我的服务器。我的问题是当我收到返回信息并使用C#WebClient发送ga值时 GA销售业绩页面显示付款订单列表,但订单详细信息中没有产品信息。我尝试通过javascript发送所有结帐步骤数据。 那么如果GA不允许混合javascript和服务器端?感谢
代码如下:
//Function to get ga event value;
var getGaValue = function (eventAction, eventLabel) {
var obj = {
"eventCategory": "GA Payment Event Test",
"eventAction": eventAction,
};
if (eventLabel)
obj["eventLabel"] = eventLabel;
return obj;
};
//PageView.
var pageViewFunc = function(items) {
items.forEach(function (item, index) {
ga(tracker + ".ec:addImpression", {
"id": item.Id,
"name": item.ItemName,
"brand": item.Brand,
"price": item.Price,
"list": "Items List",
"position": index
});
});
ga(tracker + ".send", "pageview");
};
//Checkout step 1.
var checkoutFunc = function(items) {
items.forEach(function (item, index) {
ga(tracker + ".ec:addProduct", {
"id": item.Id,
"name": item.ItemName,
"brand": item.Brand,
"price": item.Price,
"quantity": item.Qty
});
});
ga(tracker + ".ec:setAction", "checkout", {
"step": 1,
"option": "Checkout"
});
ga(tracker + ".send", "event", getGaValue("Checkout"));
};
//Checkout step 2
var payFunc = function (items) {
items.forEach(function (item, index) {
ga(tracker + ".ec:addProduct", {
"id": item.Id,
"name": item.ItemName,
"brand": item.Brand,
"price": item.Price,
"quantity": item.Qty
});
});
ga(tracker + ".ec:setAction", "checkout", {
"step": 2,
"option": "Pay"
});
ga(tracker + ".send", "event", getGaValue("Pay"));
//Submit form to payment page.
form.submit();
};
当付款页面返回信息时:
using (WebClient wc = new WebClient())
{
string url = "https://www.google-analytics.com/collect";
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
Order order=OrderService.GetById("Test12345");
IEnumerable<OrderItem> items = OrderItemService.GetByOrderId(order.Id);
IDictionary<string, string> purchaseParams = new Dictionary<string, string>();
foreach (var item in items.Select((it, i) => new { it, or }))
{
purchaseParams.Add("pr" + item.i + "id", HttpUtility.UrlEncode(item.it.Id));
purchaseParams.Add("pr" + item.i + "nm", HttpUtility.UrlEncode(item.it.ItemName));
purchaseParams.Add("pr" + item.i + "br", HttpUtility.UrlEncode(item.it.Brand));
purchaseParams.Add("pr" + item.i + "pr", item.it.Price);
purchaseParams.Add("pr" + item.i + "qt", item.it.Qty);
}
purchaseParams.Add("pa", "purchase");
purchaseParams.Add("ti", order.Id);
purchaseParams.Add("tr", order.Amount);
purchaseParams.Add("tt", "0");
purchaseParams.Add("ts", order.ShippingFee);
purchaseParams.Add("v", "1");
purchaseParams.Add("tid","UA-XXXXXX");
purchaseParams.Add("cid",order.GaClientid );
purchaseParams.Add("t", "event");
purchaseParams.Add("ec", HttpUtility.UrlEncode("GA Payment Event Test"));
purchaseParams.Add("ea", "Purchase");
purchaseParams.Add("dp", HttpUtility.UrlEncode("/Pay"));
wc.UploadString(url, purchaseParams.ToQueryString());
}