如何在 Testcafe 中记录 Google Analytics 调用? [英] How to log Google Analytics calls in Testcafe?

查看:20
本文介绍了如何在 Testcafe 中记录 Google Analytics 调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自动测试跟踪代码,我正在使用

但是,这些测试是绿色的

import { RequestLogger } from 'testcafe';const logger = RequestLogger('http://example.com');夹具`示例`.page('http://example.com');测试.requestHooks(记录器)('记录对 example.com 的调用', async t => {等待 t.expect(logger.contains(record =>record.response.statusCode === 200)).ok();//绿色});const logger_localhost = RequestLogger('http://localhost:8000');夹具`localhost`.page('http://localhost:8000');测试.requestHooks(logger_localhost)('记录对本地主机的调用', async t => {等待 t.expect(logger_localhost.contains(record =>record.response.statusCode === 200)).ok();//绿色});

如何成功拦截对 Google Analytics 的调用?

解决方案

正如 Marion 所说,这可能是由于时间问题.以下代码有效:

import { Selector, RequestLogger } from 'testcafe';常量 gaCollect = 'https://www.google-analytics.com/collect';常量 gaLogger = RequestLogger({gaCollect}, {logRequestHeaders:真,日志请求体:真,});夹具`夹具`.page('http://localhost:8000').requestHooks(gaLogger);test('记录谷歌分析调用', async t => {等待 t.click('#ga-button')等待 t.expect(gaLogger.contains(record =>record.request.url.match(/ec=my_event_category&ea=my_event_action&el=my_event_label/))).ok();for(让 gaLogger.requests 的 r) {console.log("*** 记录器 url:", r.request.url);}});

@Marion 提到的时间因素似乎起了作用.将前一个与以下代码段及其输出进行比较.在这里,我们没有看到记录到

I am trying to automatically test tracking code and I am using the RequestLogger from Testcafé. I succeeded to intercept calls to example.com and localhost but not to https://www.google-analytics.com/. What could be the reason?

Expected

This test should be green

Test code

import { RequestLogger } from 'testcafe';
const logger_ga = RequestLogger('https://www.google-analytics.com/');

fixture `localhost`
 .page('http://localhost:8000')

test  
   .requestHooks(logger_ga)
     ('logs calls to Google Analytics', async t => {
       await t.click("#ga-button");
       console.log(logger_ga.requests); // is empty due to timing
       await t.expect(logger_ga.contains(record => record.response.statusCode === 200)).ok();
});

Fixture for this test

I am serving the following index.html page via python -m SimpleHTTPServer 8000

<!doctype html>
<html>

<head>
  <meta charset="utf-8">
  <title>Test page</title>
</head>

<body>
  <p>Hello world!</p>

  <!-- Google Analytics: change UA-XXXXX-Y to be your site's ID. -->
  <script>
    window.ga = function () { ga.q.push(arguments) }; ga.q = []; ga.l = +new Date;
    ga('create', 'UA-XXXXX-Y', 'auto'); ga('send', 'pageview')
  </script>
  <script src="https://www.google-analytics.com/analytics.js" async defer></script>

  <a onclick="ga('send', 'event', 'my_event_category', 'my_event_action', 'my_event_label');" href="#" id="ga-button">Google Analytics</a>
</body>

</html>

Observed

The above test is red

However, these tests are green

import { RequestLogger } from 'testcafe';

const logger = RequestLogger('http://example.com');

fixture `example`
    .page('http://example.com');

test
    .requestHooks(logger)
    ('logs calls to example.com', async t => {
      await t.expect(logger.contains(record => record.response.statusCode === 200)).ok(); // green
});


const logger_localhost = RequestLogger('http://localhost:8000');

fixture `localhost`
    .page('http://localhost:8000');

test
    .requestHooks(logger_localhost)
    ('logs calls to localhost', async t => {
      await t.expect(logger_localhost.contains(record => record.response.statusCode === 200)).ok(); // green
});

How can I intercept calls to Google Analytics successfully?

解决方案

As Marion suggested it is probably due to timing. The following code works:

import { Selector, RequestLogger } from 'testcafe';

const gaCollect = 'https://www.google-analytics.com/collect';

const gaLogger = RequestLogger({gaCollect}, {
  logRequestHeaders:     true,
  logRequestBody:        true,
});


fixture `Fixture`
    .page('http://localhost:8000')
    .requestHooks(gaLogger);

test('Log Google Analytics call', async t => {
    await t.click('#ga-button')

    await t.expect(gaLogger.contains(record =>
      record.request.url.match(/ec=my_event_category&ea=my_event_action&el=my_event_label/))).ok();

    for(let r of gaLogger.requests) {
      console.log("*** logger url: ", r.request.url);
    }

});

The timing factor @Marion mentioned seems to play a role. Compare the previous with the following snippet and its output. Here, we do not see the calls logged to https://google-analytics.com/collect.

fixture `Fixture`
    .page('http://localhost:8000')
    .requestHooks(gaLogger);

test('Log Google Analytics call', async t => {
    await t.click('#ga-button')

    for(let r of gaLogger.requests) {
      console.log("*** logger url: ", r.request.url);
    }

    await t.expect(gaLogger.contains(record =>
      record.request.url.match(/ec=my_event_category&ea=my_event_action&el=my_event_label/))).ok();
});

这篇关于如何在 Testcafe 中记录 Google Analytics 调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆