Trying to capture Google Analytics beacon on navigation

52 views
Skip to first unread message

Teo Dragovic

unread,
Nov 21, 2016, 11:09:52 AM11/21/16
to CasperJS
Hi all,

I'm trying to write tests for Google Analytics using CasperJS. For this I want to capture GA beacons so I can parse data from query string and write assertions. 

Test is executed against staging version of website so it doesn't matter that data gets send to GA.


It's important to do this using navigation since I can't reach some pages without doing actions on previous ones (ie. have product in basket, fill in form etc.)


My problem is I can only capture GA beacon on first page and on next navigation no beacon requests get captured/requested. If I put another domain in navigation (ie. first URL is staging and next one is production) or when I navigate to URL that has redirection then I get beacon requests on both. I can't figure out is this GA or CasperJS issue. I tried to disable disk cache assuming that there is maybe issue but using --disk-cache=false doesn't seem to work for me.


Here is my code:


var startTime;
var beacons = [];

// make sure there is at least 2s since last network response
function networkIdle() {
    var time = Date.now();
    if (time - startTime > 2000) {
        return true;
    }
}

function onResourceRequested(data, req) {
    // update time for newtworkIdle check
    startTime = Date.now();
    // collect beacons data
    if (data && data.url && Utils.isBeaconUrl(data.url)) {
        beacons.push(data.url);
    }
}

casper.options.waitTimeout = 10000;

casper.test.setUp(function() {
    startTime = Date.now();
    casper.start();
    casper.on('resource.requested', onResourceRequested);
});

casper.test.begin('Google Analytics', function(test) {

    casper.open(URL1)
        .waitFor(networkIdle, function() {
            testBeacons(test);
            casper.clear();
        });

    casper.thenOpen(URL2)
        .waitFor(networkIdle, function() {
            testBeacons(test);
            casper.clear();
        });

    casper.run(function() {
        test.done();
    });

});

I also tried using click()clickLabel() and DOM click inside evaluate() with same results.

Ken

unread,
May 25, 2017, 3:52:05 PM5/25/17
to CasperJS
I have tried getting data from GA but only at a single page. Some thoughts could be is it a proactive action from GA to block automated browsers. I think some of their sites such as Google Plus may not show login button to PhantomJS user-agent. Not easy to check more without the full script. What if navigating to another dummy URL3 and back to URL1, would getting data work? Can't think of anything else at the moment, but this is interesting scenario that you gave.

Teo Dragovic

unread,
Jun 9, 2017, 10:25:02 AM6/9/17
to casp...@googlegroups.com
Hi Ken,

thanks for looking into this but it appeas this behavior was cause by weird small bug with Casper firing onpopstate event when page loads instead on exit. Since that event was tied to clearing some data from memory I could get data running tests.

Teo


--
CasperJS homepage & documentation: http://casperjs.org/
CasperJS @github: https://github.com/n1k0/casperjs
 
You received this message because you are subscribed to the Google Groups "casperjs" group.
Visit this group at http://groups.google.com/group/casperjs?hl=en.
---
You received this message because you are subscribed to a topic in the Google Groups "CasperJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/casperjs/Wg98fjeRHNk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to casperjs+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ken Soh | Tebel

unread,
Jun 11, 2017, 4:23:11 PM6/11/17
to casp...@googlegroups.com
​Welcome Teo, and thanks for sharing your findings :) I see.. Noted on that, filing this email for future reference.

For a web automation tool I'm making base on CasperJS, I do the loops by wrapping with a shell script so each run is fresh. I can avoid some of the memory related errors but run time is longer with the extra overheads.

Kind Regards,
Reply all
Reply to author
Forward
0 new messages