1

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

  • 在页面上定义谷歌分析
  • 如果页面在域中有特定的查询字符串和字符,则添加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']);
                    }
                }
            });
        }
    });
    
4

1 回答 1

3

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);
            }
    }
    });
}
于 2012-10-01T15:01:52.943 回答