XML文件中的Google Analytics脚本

时间:2010-05-10 07:38:47

标签: xml e-commerce google-analytics

我想知道如果我可以在xml格式文件中添加我的电子商务分析代码?

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<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
    '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';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
  })();

</script>

2 个答案:

答案 0 :(得分:1)

确保此类内容不会导致冲突的最简单方法是将其包装在<![CDATA[]]>标记中。

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<script type="text/javascript">
<![CDATA[
  //javascript
]]>
</script>

答案 1 :(得分:0)

你确定你的建议确实有效吗?我尝试了类似的东西,Javascript永远不会被执行。

不确定我做错了什么,或者你的想法是否未经测试。