如何在Google Analytics中设置AJAX通话跟踪?

时间:2021-07-08 15:19:46

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没有定义。所以我添加了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>

4 个解决方案

#1


3  

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

看起来您正在将Universal Analytics(analytics.js和ga()调用)与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);
})();

#2


13  

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托管中,您选择“通用分析”,它会使用新的代码计数器。在浏览器DOM中查找,没有对象“_gaq” - 因此是一个错误。您尝试使用空数组(_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('create','UA-XXXXXXX-1','mysite.com'); ga('发送','pageview');

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:

新的计数器谷歌有一个新的语法。有关活动使用的文档: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('发送','事件','使用计算器','计算成本');

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

类别和事件 - 是必需的! P.S.:Sorry。我的英语很差,我使用谷歌翻译:)

Upd:

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>

#3


6  

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');

#4


1  

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

是的,只需在Google Analytics脚本之后添加此内容即可定义_gaq数组:

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

#1


3  

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

看起来您正在将Universal Analytics(analytics.js和ga()调用)与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);
})();

#2


13  

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托管中,您选择“通用分析”,它会使用新的代码计数器。在浏览器DOM中查找,没有对象“_gaq” - 因此是一个错误。您尝试使用空数组(_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('create','UA-XXXXXXX-1','mysite.com'); ga('发送','pageview');

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:

新的计数器谷歌有一个新的语法。有关活动使用的文档: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('发送','事件','使用计算器','计算成本');

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

类别和事件 - 是必需的! P.S.:Sorry。我的英语很差,我使用谷歌翻译:)

Upd:

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>

#3


6  

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');

#4


1  

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

是的,只需在Google Analytics脚本之后添加此内容即可定义_gaq数组:

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