【问题标题】:Testing for Google Analytics JavaScript calls on a page using CasperJS?使用 CasperJS 在页面上测试 Google Analytics JavaScript 调用?
【发布时间】:2013-12-26 02:25:49
【问题描述】:

我正在使用 CasperJS 对网站进行功能测试。

我们想要测试的一件事是确保 Google Analytics 能够启动。

这篇博文 (http://viget.com/extend/testing-google-analytics-with-phantomjs) 提到在 PhantomJS 中使用 SinonJS (http://sinonjs.org) - 所以我认为它应该与 CasperJS 一起使用。

我正在使用options.clientScripts() 将脚本注入远程 DOM:

casper.options.clientScripts.push("./sinon-1.7.3.js");

然后我尝试在 CasperJS evaluate() 调用中调用它:

    casper.evaluate(function() {
        var spy = sinon.spy(_gaq, "push");
        console.log(spy.called);
        this.log(spy.called, 'debug');
    });

但是,console.log 输出似乎没有传递到 CasperJS。

this.log 是一个 CasperJS 日志调用,但我不知道它是否可以在 evaluate() 中工作 - 而且它似乎根本没有做任何事情。

更新:我也试过了:

    spy = casper.evaluate(function() {
        var spy = sinon.spy(_gaq, "push");
        return spy;
    });
    this.log(spy, 'debug');

我得到:

FAIL TypeError: No default value
#    type: uncaughtError
#    file: test_purchase.js:261
#    error: No default value
#           TypeError: No default value
#               at _replace (/usr/local/Cellar/casperjs/1.1-beta3/libexec/modules/utils.js:261)
#    stack: not provided

更新 2:我现在有了:

    spy = casper.evaluate(function() {
        var spy = sinon.spy(_gaq, "push");
        return spy;
    });
    this.echo('Has GA been called? ' + spy.called, 'debug');

总是返回 false:

Has GA been called? false

我也尝试过使用waitFor(),但似乎也没有成功:

    spy = casper.evaluate(function() {
        var spy = sinon.spy(_gaq, "push");
        return spy;
    });

    this.waitFor(function checkspy() {
        return this.evaluate(function() {
            return spy.called;
        });
    }, function then() {
        this.echo('Aha - GA has called: ' + spy.called, 'debug');
    });

这给了我:

[warning] [phantom] Casper.waitFor() timeout
FAIL "function checkspy() {
            return this.evaluate(function() {
                return spy.called;
            });
        }" did not evaluate to something truthy in 5000ms
#    type: uncaughtError
#    file: test_purchase.js
#    error: "function checkspy() {
            return this.evaluate(function() {
                return spy.called;
            });
        }" did not evaluate to something truthy in 5000ms

#    stack: not provided

我猜这可能是某种时间问题,GA 还没有被调用?在运行评估()/SinonJS 的东西之前,有没有办法让 CasperJS 等待 GA 调用?或者我可能没有足够早地注射它?

知道如何正确设置 SinonJS 和 CasperJS,以便我们检测是否调用了 GA?

或者是否有人知道在 CasperJS 中正确测试 Google Analytics 触发的任何其他方法(使用 SinonJS 或其他方法)?

另外 - 完全披露 - 我上周也在 CasperJS Google 群组 (https://groups.google.com/d/topic/casperjs/shqwRoQ-CvE/discussion) 上提出过这个问题 - 但没有得到太多回应。

干杯, 维克多

【问题讨论】:

  • 你能检测到图像请求吗? GA 每次跟踪任何内容时都会请求带有跟踪参数的 gif 文件
  • 嗯,这是 GA 设置,您还可以在本地服务器上托管跟踪 .gif 的副本吗?我可以调查一下。然而,理想情况下,我们希望这是一个独立的东西,我们可以使用 CasperJS 在本地运行,作为开发期间或部署之前的简单冒烟测试。
  • 不,GA 总是在标准设置中触发 gif。请求 utm.gif 的控制台是什么
  • 抱歉,我的意思是 - .gif 位于您自己的服务器上,而不是 Google 的服务器上?默认情况下,我认为它只针对 .gif 访问 Google 的服务器?控制台是什么意思?
  • 我明白了,该 gif 文件位于 Google 的服务器上;控制台是指位于浏览器的 Web 开发人员工具中的 Javascript 控制台。

标签: javascript google-analytics casperjs


【解决方案1】:

应该能够像这样代理对 _gaq.push 的调用:

var old_push = _gaq.push;
_gaq.push = function(options){
   console.log("Calling google analytics");
   old_push.call(this, options);
}

【讨论】:

    猜你喜欢
    • 2012-07-15
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    • 1970-01-01
    • 2016-01-28
    • 1970-01-01
    • 2014-12-08
    • 2013-01-31
    相关资源
    最近更新 更多