How to log Google Analytics calls in Testcafe?

拈花ヽ惹草 提交于 2019-12-06 14:11:33

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/51250340/how-to-log-google-analytics-calls-in-testcafe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!