通过引用将 Google Analytics 对象传递给函数时出现问题

发布于 2025-01-04 12:53:21 字数 1429 浏览 1 评论 0原文

我为我的 javascript 做了一个帮助器,以便跟踪一些 ajax 事件,这是它的设置的简短版本

analytics:{
        active: false,
        gaq:  null,
        init: function(gaq){
            this.active = true;
            this.gaq = gaq;
            $('a[href^=\"http://\"]').live('click', function() {
                helper.analytics.trackPageview('/outgoing/' + $(this).attr('href'));
                return true;
            });
        },
        trackPageview: function(page){
            if(this.active === false){
                return;
            }
            this.gaq.push(['_trackPageview',page]);
        }
    },

我有通用的谷歌分析设置

<script type="text/javascript">
  var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-xxxxxxxx-1']); 
   _gaq.push(['_setDomainName', '.example.com']);
   _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);
  })();
  $(document).ready( function() {
    helper.analytics.init(_gaq);
  });
</script>

但是在控制台中记录 _gaq 会导致目的。记录 helper.analytics.gaq 会生成一个数组,其中附加了新的综合浏览量,但 Google Analytics 中并未跟踪该综合浏览量。
为什么 _gaq 不通过引用传递给助手?

I made a helper for my javascript in order to track some ajax events, here's a short version of what it's set up to be

analytics:{
        active: false,
        gaq:  null,
        init: function(gaq){
            this.active = true;
            this.gaq = gaq;
            $('a[href^=\"http://\"]').live('click', function() {
                helper.analytics.trackPageview('/outgoing/' + $(this).attr('href'));
                return true;
            });
        },
        trackPageview: function(page){
            if(this.active === false){
                return;
            }
            this.gaq.push(['_trackPageview',page]);
        }
    },

And I have the common google analytics setup

<script type="text/javascript">
  var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-xxxxxxxx-1']); 
   _gaq.push(['_setDomainName', '.example.com']);
   _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);
  })();
  $(document).ready( function() {
    helper.analytics.init(_gaq);
  });
</script>

However in the console logging _gaq results in an object. logging helper.analytics.gaq results in an array, whith new pageviews appended, but the pageview is not being tracked in google analytics.
Why isn't _gaq being passed to the helper by reference?

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

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

发布评论

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

评论(2

就此别过 2025-01-11 12:53:21

创建脚本标记时,ga 代码段将 async 属性设置为 true。因此,它将独立于身体加载。您需要将事件处理程序绑定到 ga 脚本标记的 onload 事件。像这样:

(function() {
 var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;

 ga.onload = function(){
  herlper.analytics.init(_gaq);
 };

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

我还没有测试过这个,但我认为它可能有用。

When creating the script tag, the ga snippet sets the async attribute to true. Therefore, it'll load independently from the body. You'll need to bind an event handler to the ga script tag's onload event. Something like so:

(function() {
 var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;

 ga.onload = function(){
  herlper.analytics.init(_gaq);
 };

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

I've not tested this, but I think it might work.

相守太难 2025-01-11 12:53:21

您在 Crome 开发工具控制台或 Firefox & 中是否看到任何语法错误?萤火虫?

初始脚本标记后有一个 '."

Do you see any syntax errors either in Crome dev tools console or Firefox & firebug?

You've got a '." after the initial script tag.

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