【问题标题】:Google Analytics Universal Ecommerce data not being recorded未记录 Google Analytics 通用电子商务数据
【发布时间】:2015-08-01 14:35:23
【问题描述】:

我一直在尝试将我的 Google Analytics(分析)电子商务跟踪转移到通用,但我遇到了一些问题。跟踪本身运行良好,但电子商务却不行。我在谷歌分析中启用了电子商务跟踪并添加了以下代码:

printerlandtest.co.uk.cara.init.js 内部:

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-30033504-40', 'auto',{'name': 'CARATracker'});  // New tracker.
ga('CARATracker.send', 'pageview');

和 在 caranew.js 中

ga('CARATracker.require', 'ec'); 


function CaraGetWidgetDescription(manufacturerWidget) {
    CaraConfig();
     ga('CARATracker.set', 'Widget Impression ID E-tale', String(manufacturerWidget));

}



function CaraAddToBasket(productName, sku) {

    CaraConfig();
    ga('CARATracker.send', 'event', String(productName), 'AddToBasket', String(sku));
}

function CaraBeginTransaction(orderNo, totalPrice) {

CaraConfig();
   ga('CARATracker.ecommerce:addTransaction', String(orderNo), "", CaraPriceFix(String(totalPrice)), "", "0.00", "", "", "");
}

function CaraAddTransactionItem(orderNo, sku, productName, productCategory, productPrice, quantity) {

    CaraConfig();
    ga('CARATracker.ecommerce:addItem', {
        String(orderNo),
        String(sku),
        String(productName),
        String(productCategory),
        CaraPriceFix(String(productPrice)),
        String(quantity)
    });


function CaraEndTransaction() {

    CaraConfig();
    ga('CARATracker.ecommerce:send');  
}

function CaraPriceFix(price) {

    var fixedPrice = price;

    var pLength = price.length;
    var comma = price.indexOf(",") + 1;
    var period = price.indexOf(".") + 1;

    //comma is in the price
    if (comma != 0) {
        //if the comma is not at a 2-decimal point position
        //i.e true for 1,200
        if ((pLength - comma) > 2) {
            fixedPrice = fixedPrice.replace(",", "");
        }
    }
    //period is in the price
    if (period != 0) {
        //if the period is not at a 2-decimal point position
        //i.e true for 1.200
        if ((pLength - period) > 2) {
            fixedPrice = fixedPrice.replace(".", "");
        }
    }
    return fixedPrice;
}

由于这是在一个 clents 网站内,它与他们自己的 Google 分析是异步的,并且设置如下:

<script>  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');  ga('create', 'UA-3521256-1', {'siteSpeedSampleRate': 20});  ga('send', 'pageview');</script><script src="https://where-to-buy.co/scripts/cara/printerlandtest.co.uk.cara.init.js" type="text/javascript"></script><script src="https://where-to-buy.co/scripts/cara/caranew.js" type="text/javascript"></script>

在添加到购物篮按钮上,代码如下:

<a onclick="TrackEvent('Buy Button Click','ProductList','01327701','');CaraAddToBasket('OKI C511dn','01327701');" id="ctl00_ctl00_ContentPlaceHolderMain_cntPlaceHlderMain_lstCategoryProducts_ctrl0_lnkBuy" class="button product_buy selected" href='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$ctl00$ContentPlaceHolderMain$cntPlaceHlderMain$lstCategoryProducts$ctrl0$lnkBuy", "", true, "", "", false, true))'><span>Added</span> </a>

但是我收到以下错误:

未捕获的 ReferenceError:CaraAddToBasket 未定义

当我点击支付时,我收到以下错误:

未捕获的 ReferenceError:未定义 CaraBeginTransaction

我以为我是在函数中定义它们,但我有什么问题吗?

还有什么可以阻止电子商务跟踪吗?

谢谢

【问题讨论】:

    标签: javascript google-analytics universal-analytics


    【解决方案1】:

    我重写了代码并得出了这个解决方案。删除所有新的 String() 函数并将价格固定函数从数组中移出到它上面的 var。

    addtobasket 函数后面还有一个 } 缺失,导致 JS 失败。

    现在函数运行正常。

    (function(i, s, o, g, r, a, m) {
      i['GoogleAnalyticsObject'] = r;
      i[r] = i[r] || function() {
        (i[r].q = i[r].q || []).push(arguments)
      }, i[r].l = 1 * new Date();
      a = s.createElement(o),
        m = s.getElementsByTagName(o)[0];
      a.async = 1;
      a.src = g;
      m.parentNode.insertBefore(a, m)
    })(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
    
    ga('create', 'UA-30033504-40', 'auto', {
      'name': 'CARATracker'
    }); // New tracker.
    ga('CARATracker.send', 'pageview');
    
    ga('CARATracker.require', 'ec');
    
    
    function CaraGetWidgetDescription(manufacturerWidget) {
      CaraConfig();
      ga('CARATracker.set', 'Widget Impression ID E-tale', String(manufacturerWidget));
    
    }
    
    
    
    function CaraAddToBasket(productName, sku) {
    
      CaraConfig();
      ga('CARATracker.send', 'event', String(productName), 'AddToBasket', String(sku));
      alert('Debugging: added');
    }
    
    function CaraBeginTransaction(orderNo, totalPrice) {
    
      CaraConfig();
      ga('CARATracker.ecommerce:addTransaction', String(orderNo), "", CaraPriceFix(String(totalPrice)), "", "0.00", "", "", "");
    }
    
    function CaraAddTransactionItem(orderNo, sku, productName, productCategory, productPrice, quantity) {
    
      CaraConfig();
      var price = CaraPriceFix(productPrice);
      alert(price);
      ga('CARATracker.ecommerce:addItem', {
        orderNo,
        sku,
        productName,
        productCategory,
        price,
        quantity
      });
    }
    
    
    function CaraEndTransaction() {
    
      CaraConfig();
      ga('CARATracker.ecommerce:send');
    }
    
    function CaraPriceFix(price) {
    
      var fixedPrice = price;
    
      var pLength = price.length;
      var comma = price.indexOf(",") + 1;
      var period = price.indexOf(".") + 1;
    
      //comma is in the price
      if (comma != 0) {
        //if the comma is not at a 2-decimal point position
        //i.e true for 1,200
        if ((pLength - comma) > 2) {
          fixedPrice = fixedPrice.replace(",", "");
        }
      }
      //period is in the price
      if (period != 0) {
        //if the period is not at a 2-decimal point position
        //i.e true for 1.200
        if ((pLength - period) > 2) {
          fixedPrice = fixedPrice.replace(".", "");
        }
      }
      return fixedPrice;
    }
    
    function CaraConfig() {
      //empty
    }
    <html>
    
    <body>
      <a onclick="CaraAddToBasket('OKI C511dn','01327701');" id="ctl00_ctl00_ContentPlaceHolderMain_cntPlaceHlderMain_lstCategoryProducts_ctrl0_lnkBuy" class="button product_buy selected" href='#'><span>Added</span> </a>
    
    </body>
    
    </html>

    【讨论】:

    • 谢谢你我刚刚注意到额外的 } aswel 好旧的控制台,它看起来现在应该可以工作了,我们必须等待数据通过分析(手指交叉)感谢您的时间。我实际上删除了 CaraConfig();我认为它已经过时了。
    • 嗨,Thomas 很遗憾,即使代码中设置的警报被击中,我仍然没有通过电子商务数据。你有什么其他的建议?在行为概述中,我可以看到 orderreceipt.aspx 页面被点击,但没有电子商务数据。非常感谢
    • 抱歉没有在最后的评论中标记你,请看bove 谢谢!
    • 看看是不是命名为'CARATracker'。删除它,看看它是否到达。
    • 我已经尝试删除它,但它似乎仍然没有记录它添加它的原因是因为页面上有另一个谷歌分析脚本,所以我需要区分两者 - @987654321 @查看多跟踪器支持
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    相关资源
    最近更新 更多