javascript 如何在 Google Analytics 中设置 AJAX 呼叫跟踪?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16015364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:56:32  来源:igfitidea点击:

How to set up AJAX call tracking in Google Analytics?

javascriptajaxgoogle-analytics

提问by Radek

I created my google analytics account. And copied and pasted the code provided into my index.php file. It seems to me that it works as I can see calls to www.google-analytics.com from firebug.

我创建了我的谷歌分析帐户。并将提供的代码复制并粘贴到我的 index.php 文件中。在我看来,它可以工作,因为我可以看到来自 firebug 的对 www.google-analytics.com 的调用。

Now I want to track how many times the 'functions.php' is called via ajax from index file.

现在我想跟踪通过 ajax 从索引文件中调用 'functions.php' 的次数。

I tried to use _gaq.push(['_trackPageview', 'functions.php']);but it gave me Uncaught ReferenceError: _gaq is not defined. So I added var _gaq = _gaq || [];to my code. The error is gone but I cannot see any call to www.google-analytics.com after the ajax finishes.

我尝试使用,_gaq.push(['_trackPageview', 'functions.php']);但它给了我Uncaught ReferenceError: _gaq is not defined。所以我添加var _gaq = _gaq || [];到我的代码中。错误消失了,但在 ajax 完成后我看不到对 www.google-analytics.com 的任何调用。

Could someone help me to set it up so analytics would track ajax calls?

有人可以帮我设置它以便分析可以跟踪 ajax 调用吗?

My code looks like

我的代码看起来像

<script type='text/javascript'>
  (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-1234556-1', 'domain.com');
            ga('send', 'pageview');

         var _gaq = _gaq || [];

        function submit_data(){

                var text_area=$('#textarea').val();
                var url ="functions.php";
                jQuery.ajax({
                    type: "get",
                    dataType: "text",
                    url: url,
                    data: {
                        what : "generate",
                        text_area: text_area,
                        t: Math.random()
                    },
                        success: function(data, textStatus){
                        $('#textarea').val(data);
//                      _gaq.push(['_setAccount', 'UA-12345-1']);
                        _gaq.push(['_trackPageview', 'functions.php']);
                        }
                });
        }

        </script>

采纳答案by mike

Looks like you're mixing Universal Analytics (analytics.jsand ga()calls) with Async Analytics (ga.jsand _gaq.push()), but I don't see any code to initialize ga.js.

看起来您将 Universal Analytics(analytics.jsga()调用)与 Async Analytics(ga.js_gaq.push())混合使用,但我没有看到任何代码来初始化ga.js.

Try changing

尝试改变

var _gaq = _gaq || [];

to

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-12345-1']);
_gaq.push(['_trackPageview']);

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

回答by Anton Belousov

I think that at check in Google Analytics you select "Universal Analytics", and it uses a new code counter. Look in the browser DOM, there is no object "_gaq" - and is therefore an error. You tried to fix it with empty Array(_gaq).
Old code:

我认为在检查 Google Analytics 时您选择了“Universal Analytics”,它使用了一个新的代码计数器。查看浏览器 DOM,没有对象“_gaq” - 因此是一个错误。你试图用空的 Array(_gaq) 修复它。
旧代码:

var _gaq = _gaq | | [];
_gaq.push (['_setAccount', 'UA-XXXXXX-1']);

var _gaq = _gaq | | [];
_gaq.push (['_setAccount', 'UA-XXXXXX-1']);

Do not use old code! (And you can not use multiple codes counter 'UA-XXXXXX-1' - it's mistake)
New code:

不要使用旧代码!(并且您不能使用多个代码计数器 'UA-XXXXXX-1' - 这是错误的)
新代码:

ga ('create', 'UA-XXXXXXX-1', 'mysite.com');
ga ('send', 'pageview');

ga ('创建', 'UA-XXXXXXX-1', 'mysite.com');
ga ('发送', '浏览量');

The new counter Google has a new syntax.
Documentation on the use of events:https://developers.google.com/analytics/devguides/collection/analyticsjs/events
Example of use:
I have a calculator on the page and I want to keep track of events by push of a button on it.
Category- "Using the Calculator";
Event- "Calculating the cost".
Old code:

新的计数器 Google 有一个新的语法。
关于事件使用的文档:https : //developers.google.com/analytics/devguides/collection/analyticsjs/events
使用示例:
我在页面上有一个计算器,我想通过按一下按钮来跟踪事件在上面。
类别- “使用计算器”;
事件- “计算成本”。
旧代码:

_gaq.push(['_trackEvent', 'Using the Calculator', 'Calculating the cost');

_gaq.push(['_trackEvent', '使用计算器', '计算成本');

New code:

新代码:

ga('send', 'event', 'Using the Calculator', 'Calculating the cost');

ga('send', 'event', '使用计算器', '计算成本');

Category and Event - is Required!
P.S.:Sorry. I have poor English and I used Google translator :)

类别和事件 - 是必需的!
PS:对不起。我的英语很差,我使用了谷歌翻译 :)

Upd:

更新:

<script type='text/javascript'>

  (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');
//Use once per page
        ga('create', 'UA-1234556-1', 'domain.com');
        ga('send', 'pageview');
        //
        function submit_data(){

                var text_area=$('#textarea').val();
                var url ="functions.php";
                jQuery.ajax({
                    type: "get",
                    dataType: "text",
                    url: url,
                    data: {
                        what : "generate",
                        text_area: text_area,
                        t: Math.random()
                    },
                        success: function(data, textStatus){
                        $('#textarea').val(data);
                        ga('send', 'event', 'MyCategory', 'functions.php');
                        }
                });
        }

</script>

回答by Mike

If you're using Universal Analytics (analytics.js) then switch this:

如果您使用的是 Universal Analytics ( analytics.js),请切换:

_gaq.push(['_trackPageview', 'functions.php']);

to this:

对此:

ga('send', 'pageview', 'functions.php');

回答by kaleazy

Yes, just add this after your Google Analytics script to define the _gaqarray:

是的,只需在您的 Google Analytics 脚本之后添加它来定义_gaq数组:

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-65432-1']);
_gaq.push(['_trackPageview']);