Google Analytics - 如何使用新的异步代码跟踪页面名称?

发布于 2024-10-31 11:45:53 字数 1209 浏览 3 评论 0原文

我正在使用 Google 提供的以下代码进行 Google Analytics。

<script type="text/javascript">
            var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
            document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
         ry {
                var pageTracker = _gat._getTracker("UA-XXXXX-X");
                pageTracker._trackPageview("PAGENAME");
            } catch (err) { }
 </script>

但 Google 已经发布了异步 Javascript 代码(如下)。

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXX-X']);
  _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);
  })();

</script>

我的问题是:在旧代码中,我曾经使用 PAGENAME (pageTracker._trackPageview("PAGENAME");) 跟踪页面。如何使用新的异步代码来做到这一点?

I was using the below code provided by Google for Google Analytics.

<script type="text/javascript">
            var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
            document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
         ry {
                var pageTracker = _gat._getTracker("UA-XXXXX-X");
                pageTracker._trackPageview("PAGENAME");
            } catch (err) { }
 </script>

But Google has released Asynchronous Javascript Code (below).

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXX-X']);
  _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);
  })();

</script>

My QUESTION is: in the old code I used to track a page with PAGENAME (pageTracker._trackPageview("PAGENAME");). How can I do it using the new asynchronous code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陈独秀 2024-11-07 11:45:53

您可以重写异步跟踪代码中的第三行代码并将其更改为以下内容:

_gaq.push(['_trackPageview', 'PAGENAME']); 

这已记录在案 此处

You can rewrite that third code line in the async tracking code and change it to the following:

_gaq.push(['_trackPageview', 'PAGENAME']); 

This is documented here

不寐倦长更 2024-11-07 11:45:53

RTFM:http://code.google.com /apis/analytics/docs/gaJS/gaJSApiBasicConfiguration.html#_gat.GA_Tracker_._trackPageview

您的示例如下所示:

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXX-X']);
  _gaq.push(['_trackPageview', 'PAGENAME']);

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

</script>

这只是指定第二个参数的情况。

RTFM: http://code.google.com/apis/analytics/docs/gaJS/gaJSApiBasicConfiguration.html#_gat.GA_Tracker_._trackPageview

Your example would look like this:

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXX-X']);
  _gaq.push(['_trackPageview', 'PAGENAME']);

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

</script>

It's merely a case of specifying a second parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文