简体   繁体   English

循环遍历表以返回Google Analytics电子商务中_addItem()的交易信息

[英]Looping through table to return transaction information for _addItem() in Google Analytics Ecommerce

I am trying to set up an ecommerce site's receipt page to push ecommerce data back to Google Analytics. 我正在尝试设置电子商务网站的收据页面,以便将电子商务数据推送回Google Analytics。 I'm stuck on populating the _addItem() method with sku, name, price, and quantity for each item purchased. 我坚持用每个购买的商品的sku,名称,价格和数量填充_addItem()方法。

I need the following for each item on the receipt: 收据上的每件商品都需要以下内容:

_addItem(transactionId, sku, name, price, quantity)

My receipt page generates the table below with the product data. 我的收据页面生成下表和产品数据。 How can I loop through this table using Javascript or jQuery to return the following for each item? 如何使用Javascript或jQuery循环遍历此表以返回每个项目的以下内容? There is no limit to the number of items that can be on a receipt. 收据上的商品数量没有限制。

<div id="receipt_detail">
   <table class="list_container" width="98%" cellspacing="0" cellpadding="4" >
     <tr class="list_heading">
        <td align="left"  nowrap >SKU</td>
        <td align="left"  nowrap >Description</td>
        <td align="left"  nowrap >Qty</td>
        <td align="right"  nowrap >Price</td>
        <td align="right"  nowrap >Extended</td>
     </tr>
     <tr class="list">
        <td align="left"  nowrap >1234</td>
        <td align="left"  nowrap >Widget 1</td>
        <td align="left"  nowrap >1</td>
        <td align="right"  nowrap > $            0.25</td>
        <td align="right"  nowrap > $            0.25</td>
     </tr>
     <tr class="listodd">
        <td align="left"  nowrap >5678</td>
        <td align="left"  nowrap >Widget 2</td>
        <td align="left"  nowrap >1</td>
        <td align="right"  nowrap > $            0.10</td>
        <td align="right"  nowrap > $            0.10</td>
     </tr>
   </table>
</div>

I've got transcationId covered (that's easy). 我有transcationId覆盖(这很容易)。 The rest has got me stumped. 其余的让我难过。 I'm new to Javascript, so any and all help is much appreciated. 我是Javascript的新手,所以非常感谢任何帮助。

Google's developer documentaion on ecommerce tracking code is here . 谷歌关于电子商务跟踪代码的开发者文档就在这里

You're gonna have to fill in some blanks, but here is some code to pull the values from your table and populate the GA code: 你将不得不填写一些空白,但这里有一些代码可以从你的表中提取值并填充GA代码:

<script language="JavaScript" type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-11111-1']); // your account # here

(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);
})();

/* you proved no details or examples of where to get any of this, so this is
   the GA manual example */
_gaq.push(['_addTrans',
  '1234',           // transaction ID - required
  'Womens Apparel', // affiliation or store name
  '28.28',          // total - required
  '1.29',           // tax
  '15.00',          // shipping
  'San Jose',       // city
  'California',     // state or province
  'USA'             // country
]);

/* scrape the html you provided and add values to _addItem */
$(document).ready(function() {
  $('div#receipt_detail tr.list,.listodd').each(function() {
    var info = [];
    $(this).find('td').each(function() {
      info.push($(this).html().replace(/^\s*\$\s*/,''));      
    });
    _gaq.push(['_addItem',
      '1234',                // transaction ID 
      info[0]||'no sku',     // SKU/code - required
      info[1]||'no product', // product name
      'category',            // category or variation
      info[3]||'0',          // unit price - required
      info[2]||'1'           // quantity - required
    ]);
  });
  _gaq.push(['_trackTrans']);
});

</script>

NOTE: This isn't really a good way to go about tracking your transactions. 注意:这不是一个跟踪交易的好方法。 Instead of trying to scrape your page for values your system is obviously already dynamically outputting, you should instead use server-side code to expose the necessary values more directly to js, preferably just dynamically populating the GA code w/ server-side code. 您应该使用服务器端代码更直接地向js公开必要的值,而不是尝试抓取页面中的值,系统显然已经动态输出,最好只是动态填充带有服务器端代码的GA代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM