简体   繁体   English

未记录Google Analytics(分析)通用电子商务数据

[英]Google Analytics Universal Ecommerce data not being recorded

I have been trying to move my Google Analytics E commerce tracking over to universal but I am having a few problems. 我一直在尝试将我的Google Analytics(分析)电子商务跟踪迁移至通用,但遇到一些问题。 The tracking itself is working fine but not the e commerce. 跟踪本身运行良好,但电子商务却不能。 I have enabled ecommerce tracking in google analytics and have added the following code: 我已在Google Analytics(分析)中启用了电子商务跟踪,并添加了以下代码:

inside printerlandtest.co.uk.cara.init.js: 在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');

and inside caranew.js 在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;
}

As this is inside a clents site it is asynchronous with their own Google analytics and is set up as follows: 由于它位于clents网站内,因此与自己的Google Analytics(分析)异步,并且设置如下:

<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>

On the Add to basket button their is the following code: 在添加到购物篮按钮上,它们是以下代码:

<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>

However I am getting the following error: 但是我收到以下错误:

Uncaught ReferenceError: CaraAddToBasket is not defined 未捕获的ReferenceError:未定义CaraAddToBasket

and when I click on the Pay I get the following error: 当我单击“付款”时,出现以下错误:

Uncaught ReferenceError: CaraBeginTransaction is not defined 未捕获的ReferenceError:未定义CaraBeginTransaction

I thought I was defining them in the functions but have I got something wrong? 我以为我在函数中定义了它们,但是我出了点问题吗?

Is there anything else which stop the ecommerce tracking? 还有其他什么可以阻止电子商务跟踪吗?

Thanks 谢谢

I rewrote the code and came to this solution. 我重写了代码,并得出了这个解决方案。 Removing all the new String() functions and moving the price fix function out of the array to a var above it. 删除所有新的String()函数并将价格修复函数移出数组,移至其上方的var中。

Also there was a } missing behind a addtobasket function causing the JS to fail. 另外,在addtobasket函数后面缺少},导致JS失败。

Now the function is running fine. 现在该功能运行良好。

 (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 //ie 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 //ie 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> 

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

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