java - Google Analytics Tracker Code (GATC) [a Javascript] 需要修改以使其能够在 JSF 中运行

标签 java javascript jsf google-analytics

几天后,我将举办一个小型 Activity ,其中我必须仅在 JSF 中彻底演示 Google Analytic。我已在 Google Analytics 网站 here 上注册了自己并执行其余的必要步骤。我在该网站上获得了以下 Javascript 代码。

<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"));
</script>

<script type="text/javascript">
   try{
          var pageTracker = _gat._getTracker("UA-xxxxxx-x");
          pageTracker._trackPageview();
      } 
   catch(err) {}
</script>

它称为Google Analytics Tracker Code (GATC),当包含在网站页面上时,可以将有关该页面的相关信息作为报告查看。它可用于跟踪所有常见的网站 Activity ,例如访问量、页面浏览量、每次访问的页面数、跳出率、网站平均停留时间等。这就是 GATC 的主要内容。我必须仅在 JSF 中进行演示,因此,必须进行一些必要的更改才能使上述 Javascript 代码在 JSF 中运行,因为 JSF 使用某种不同的方式来编译 Javascript。需要对上述 Javascript 代码进行哪些必要的更改,以便它也可以在 JSF 中运行,我可以在互联网上找到使用 Google Analytic 的网站吗? (这样我就不需要上传我已经在 J​​SF 中开发的 JSF 应用程序进行演示)并且如果存在这样的网站,它可以仅用于演示吗?这段代码应该放在 Javascript 的哪个事件上?我发现这里已经发布了一些关于 Google Analytic 的相关问题,但它们与 JSF 并不是特别相关。

最佳答案

I have to demonstrate it only in JSF and therefore, some necessary changes must be done in order to make the above Javascript code run in JSF, since JSF uses some different way to compile Javascript

这没有任何意义。 JSF 不使用不同的方式来“编译”JavaScript。只需将 JavaScript 代码放在 Google Analytics told 的位置即可你把它。这是相关性的引用(强调我的):

Use the Tracking Snippet to Track Your Website

The Analytics snippet is a small piece of JavaScript code that you paste into your pages. It activates Google Analytics tracking by inserting ga.js into the page. To use this on your pages, copy the code snippet below, replacing UA-XXXXX-X with your web property ID. Paste this snippet into your website template page so that it appears before the closing </head> tag.

If you need to do more than basic page tracking, see the tracking reference for a list of methods available in the API and see the Usage Guide for details on using the asynchronous syntax. For step-by-step instructions on setting up tracking, see the Help Center article on setting up tracking.

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

The snippet above represents the minimum configuration needed to track a page asynchronously. It uses _setAccount to set the page's web property ID and then calls _trackPageview to send the tracking data back to the Google Analytics servers.

该代码不包含任何 XML 特殊字符,因此在 Facelets 上使用时不需要转义任何内容。

关于java - Google Analytics Tracker Code (GATC) [a Javascript] 需要修改以使其能够在 JSF 中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7967910/

相关文章:

Guava 不可变表的 Java 8 收集器

java - 找出目录中所有文件的创建、访问或修改日期

javascript - 禁用所有 ID 属性以

java - onRowSelect(SelectEvent event) 方法

java - 为什么 JSF 将 UI 组件的状态保存在服务器上?

java - imagebutton 设置图像大小

java - 用简单的英语解释协变、不变和逆变?

javascript - 如果提供弃用格式警告,MomentJS 使用语言环境

javascript - Facebook Javascript SDK 将邀请发送到 'Page' 而不是 'Application'

jsf - Web 过滤器中设置的 Cookie 在请求 bean 中不可用