English 中文(简体)
如何建立谷歌分析 与杰克里,杰克里移动和要求JS?
原标题:How to setup Google Analytics with Jquery, Jquery Mobile and RequireJS?

这已经困扰了我好几天了,我还没有找到办法让它起作用。我的网站正在使用Jquery、Jquery-Mobile和LusingJS,我想在组合中加入谷歌分析。

我的分析器设置就像这样(也见

<script type="text/javascript">
    var _gaq = _gaq || [];
    (function() {
         ... regular stufff 
    })();
// bind to pageshow event
$(document).on( pageshow ,  div:jqmData(role="page").basePage , function (event, ui) {
try {
    _gaq.push([ _setAccount ,  ID ;
    _gaq.push([ _gat._anonymizeIp ]);
    var url = location.href;
        try  {
            if (location.hash) {
                url = location.hash;
                }
            _gaq.push([ _trackPageview , url]);
            } catch(err) {}
        }); 
    </script>

所以我在听 JQM 页面展示活动, 用于跟踪分析。 此片段被粘贴到所有页面的结尾 。

我有小分析学 要求JSS这样的:

< pronger > main.js :

require.config({ 
    baseUrl: "../js/",  
    paths: {
        "jquery":        "libs/jquery/jquery.min",
        "jqm":           "libs/jquery-mobile/jquery-mobile.min", 
        "analytics":     "https://www.google-analytics.com/ga"
        }

require([ order!jquery ,  order!jqm ,  order!analytics ,  app ],
    function(jquery, jqm, analytics, App){  
        App.start();        
        });

在“强势”App.js

var start = function() {
   require([ order!jquery ,  order!jqm ,  order!analytics ],function() {
    // do stuff
   });

问题是,分析片段在每页HTML标记的结尾处, 所以尽管我认为我已经正确地设置了它, 要求JS与Jquery和Jquery Mobile装载之前, 我仍然有一个错误:

 $ is not defined
  ....ocument).on( pageshow ,  div:jqmData(role="page").basePage , function (event, ui...

当第一页加载时。

我猜是因为 HTML 标记在需求范围之外, 并且在 JQM 装入前被解析。 尽管如此, 仍然必须有一个合适的设置方法 。

Question:
Any idea why the error pops up and what I can do about it?

谢谢你的帮助!

EDIT:
Ok. Doing it like this works:

在我页面页眉中 我宣布第一部分:

<script type="text/javascript">
var _gaq = _gaq || [];

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

然后在我的要求内开始功能 我调用第二狙击手:

    var start = function() {
        require([ order!jquery ,  order!jqm ],function() {
            var analyticsHasBeenSet; 
            ...
            var analyize = function () {

                if ( analyticsHasBeenSet != true ){
                    analyticsHasBeenSet = true;
                    require([ https://www.google-analytics.com/ga.js ], function (App) {

                        $(document).on( pageshow ,  div:jqmData(role="page"), function() {
                            _gaq.push([ _setAccount ,   ]);
                            _gaq.push([ _gat._anonymizeIp ]);
                            var url = location.href;
                            try  {
                                if (location.hash) {
                                    url = location.hash;
                                    }
                                _gaq.push([ _trackPageview , url]);
                                } catch(err) { // error catch }
                            });
                        });
                     }
                 }
            analyize();

这样一来, 第二片段只有在 Jquery 和 Jquery Mobile 被装入后才能执行。 通过设置一个全球变量( 我希望), 约束将只启动一次 。 目前看来有效 。

最佳回答

问题最可能发生在您使用订单! 插件! 插件。 这只应用于标准. js 文件, 而不是模块 。

The order plugin is best used with traditional scripts. It is not needed for scripts that use define() to define modules. It is possible to mix and match "order!" dependencies with regular dependencies, but only the "order!" ones will be evaluated in relative order to each other.

http://requirejs.org/docs/api.html#order

您的分析路径应该修改为传统的 js 文件( 因为 Ga.js 不注册为模块)

"analytics":     "https://www.google-analytics.com/ga.js"

jQuery 和 jQuery 移动确实将自己注册为模块, 所以您对这些模块的配置是正确的 。

在您需要的电话中,您需要的只是:

require([ jquery ,  jqm ,  analytics ],function($, jqm) {
    // analytics is not a module so _gaq will now exist as a global var.
   });

将 Ga.js 视为不是模块的唯一文件, 命令! 插件可能没有什么用处, 因为只有当您有多个传统脚本需要按一定顺序装入时, 插件才重要 。

The order plugin is best used with traditional scripts. It is not needed for scripts that use define() to define modules. It is possible to mix and match "order!" dependencies with regular dependencies, but only the "order!" ones will be evaluated in relative order to each other.

然而,值得注意的是,谷歌提供的用于使用谷歌分析器的代码片段已经算作非同步装载,因此可能根本不需要使用 " 要求 " 。

问题回答

暂无回答




相关问题
Mysql trigger/events vs Cronjob

I have an auction website which let my users place an unlimited amount of autobiddings. To monitor these autobiddings something has to check the database every second. My question is if it is ...

Can an event be used as an event listener?

I m trying to expose some events from a private object which is contained inside the object I am creating and it looks like the compiler is happy with this: private WindowUpdateServer ...

鸡奸

由于将javascript DOM方法放在第html页底部(在<有人>之后)远比利用“j Query”准备活动要快得多,因此我们不得不这样做:

Attaching a property to an event in Flex/AS3

I have a parameter that needs to be passed along with an event. After unsuccessful attempts to place it on the type by extending the class, I ve been advised in another SO question to write a custom ...

jQuery bind on ajax load() event

I have a page which displays multiple blocks with results details. Inside each block I have some <a> tags with thickbox jQuery plugin attached: class="thickbox" Here is an example of one kind ...

HTML text input event

I have a form, and want to disable/enable its submit button depending on whether the form s input text fields are empty or have had text entered into them. I think this means having an event handler ...

IE doesn t run Javascript when you click back button

I ve built a shop with tons of JS running. One of the pieces of the cart, is a zip code field which is required to calculate shipping cost. This works fine going through the cart. Now clicking ...

热门标签