【问题标题】:Google analytics event tracking failing to feedback?谷歌分析事件跟踪未能反馈?
【发布时间】:2012-10-01 10:44:54
【问题描述】:

在下面的代码中我试图:-

  • 在页面上定义谷歌分析
  • 如果页面有特定的查询字符串和域中的字符,添加jquery点击事件
  • 点击事件调用谷歌分析跟踪事件
  • 我还对域内的查询字符串和值进行代码检查

什么不工作:-

  • 第三个项目,它正在调用谷歌分析跟踪事件,我似乎正在点击它,但没有任何内容返回到我的 GA 帐户。

我尝试过的:-

  • 我已经三次检查_gaq_.push 的信息是否正确链接到正确的帐户。
  • 使用 firebug 和 IE9 开发工具跟踪 javascript 并分析发生了什么。当我的场景为真时,这就是我知道我正在点击事件跟踪的方式。
  • 在像 jsfiddle 这样的孤立环境中尝试过,不会提供指向它的链接,因为我实际上无法提供公司信息。它在 js fiddle 中不起作用。 (主要是因为jsfiddle不能用我的ga账号认证(域名不同)。
  • 我在我的相关域中尝试了一个孤立的文件,但仍然没有成功。

    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-XXXXX-X']);
    _gaq.push(['_setDomainName', 'mydomain.co.uk']);
    _gaq.push(['_setAllowLinker', true]);
    _gaq.push(['_setSiteSpeedSampleRate', 100]); // this is a new line, allowing us to see how fast all pages load
    _gaq.push(['_trackPageview']); // we’ve moved this line down, as ‘setdomain’ (etc) should appear before it
    
    (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 () {
        var querystring = (function (a) {
            if (a == "") return {};
            var b = {};
            for (var i = 0; i < a.length; ++i) {
                var p = a[i].split('=');
                if (p.length != 2) continue;
                b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
            }
            return b;
        })(window.location.search.substr(1).split('&'));
        if (querystring["utm_expid"] != null) {
            $('a').click(function () {
                if ($(this).attr("href") != 'undefined' && $(this).attr("href") != null) {
                    if ($(this).attr("href").toLowerCase().indexOf("keyword") >= 0 && $(this).attr("href").toLowerCase().indexOf("keyword2") >= 0) {
                        _gaq.push(['_trackEvent', 'eventCategories', 'eventAction', 'eventLabel']);
                    } else if (($(this).attr("href").toLowerCase().indexOf("keyword") >= 0 && $(this).attr("href").toLowerCase().indexOf("keyword2") >= 0 && $(this).attr("href").toLowerCase().indexOf("keyword3") >= 0) || ($(this).attr("href").toLowerCase().indexOf("keyword4") >= 0)) {
                        _gaq.push(['_trackEvent', 'eventCategories', 'eventAction', 'eventLabel']);
                    }
                }
            });
        }
    });
    

【问题讨论】:

    标签: javascript jquery google-analytics query-string


    【解决方案1】:

    Google Analytics _trackEvent(和_trackPageview 等)通过从分析服务器发出跟踪像素请求来工作。如果在跟踪请求完成之前点击导致将新页面加载到同一窗口,您可能会丢失数据,或者只跟踪部分数据。

    以下代码在点击链接之前添加了一点延迟:

    var delayLink = function(e, url) {
      e.preventDefault();
      setTimeout(function(){location.href = url}, 150);
    };
    
    if (querystring["utm_expid"] != null) {
        $('a').click(function (e) {
            if ($(this).attr("href") != 'undefined' && $(this).attr("href") != null) {
                if ($(this).attr("href").toLowerCase().indexOf("keyword") >= 0 && $(this).attr("href").toLowerCase().indexOf("keyword2") >= 0) {
                    _gaq.push(['_trackEvent', 'eventCategories', 'eventAction', 'eventLabel']);
                    if (this.target != '_blank') delayLink(e, this.href);
                } else if (($(this).attr("href").toLowerCase().indexOf("keyword") >= 0 && $(this).attr("href").toLowerCase().indexOf("keyword2") >= 0 && $(this).attr("href").toLowerCase().indexOf("keyword3") >= 0) || ($(this).attr("href").toLowerCase().indexOf("keyword4") >= 0)) {
                    _gaq.push(['_trackEvent', 'eventCategories', 'eventAction', 'eventLabel']);
                if (this.target != '_blank') delayLink(e, this.href);
                }
        }
        });
    }
    

    【讨论】:

    • 感谢您的回复,我们会尝试延迟并在明天回复您:D。谢谢
    猜你喜欢
    • 2013-09-18
    • 2014-02-04
    • 2016-05-21
    • 1970-01-01
    • 2014-02-15
    • 2013-08-21
    • 1970-01-01
    相关资源
    最近更新 更多