![Page 1: Using PhantomJS to QA your analytics implementation](https://reader036.vdocuments.us/reader036/viewer/2022070603/587018d91a28ab7f428b603d/html5/thumbnails/1.jpg)
PhantomJS and Analytics
MeasureCamp London
19 Sep 2015
![Page 2: Using PhantomJS to QA your analytics implementation](https://reader036.vdocuments.us/reader036/viewer/2022070603/587018d91a28ab7f428b603d/html5/thumbnails/2.jpg)
Spiders and robots
Until a few years ago spiders and bots could only read the HTML. They could not execute Javascript.
![Page 3: Using PhantomJS to QA your analytics implementation](https://reader036.vdocuments.us/reader036/viewer/2022070603/587018d91a28ab7f428b603d/html5/thumbnails/3.jpg)
Spiders and robots
Until a few years ago spiders and bots could only read the HTML. They could not execute Javascript.
Now they can:
![Page 4: Using PhantomJS to QA your analytics implementation](https://reader036.vdocuments.us/reader036/viewer/2022070603/587018d91a28ab7f428b603d/html5/thumbnails/4.jpg)
Spiders and robots
Until a few years ago spiders and bots could only read the HTML. They could not execute Javascript.
Now they can:• Access the DOM
![Page 5: Using PhantomJS to QA your analytics implementation](https://reader036.vdocuments.us/reader036/viewer/2022070603/587018d91a28ab7f428b603d/html5/thumbnails/5.jpg)
Spiders and robots
Until a few years ago spiders and bots could only read the HTML. They could not execute Javascript.
Now they can:• Access the DOM• Interpret Javascript
![Page 6: Using PhantomJS to QA your analytics implementation](https://reader036.vdocuments.us/reader036/viewer/2022070603/587018d91a28ab7f428b603d/html5/thumbnails/6.jpg)
Spiders and robots
Until a few years ago spiders and bots could only read the HTML. They could not execute Javascript.
Now they can:• Access the DOM• Interpret Javascript• Fill login forms
![Page 7: Using PhantomJS to QA your analytics implementation](https://reader036.vdocuments.us/reader036/viewer/2022070603/587018d91a28ab7f428b603d/html5/thumbnails/7.jpg)
Just like you and me
![Page 8: Using PhantomJS to QA your analytics implementation](https://reader036.vdocuments.us/reader036/viewer/2022070603/587018d91a28ab7f428b603d/html5/thumbnails/8.jpg)
Headless testing
Increasingly testing requires writing code
• Test Driven Development• Extreme Programming
An automated test goes through the pages, executes the relevant tests and produces a bug report
![Page 9: Using PhantomJS to QA your analytics implementation](https://reader036.vdocuments.us/reader036/viewer/2022070603/587018d91a28ab7f428b603d/html5/thumbnails/9.jpg)
Introducing
![Page 10: Using PhantomJS to QA your analytics implementation](https://reader036.vdocuments.us/reader036/viewer/2022070603/587018d91a28ab7f428b603d/html5/thumbnails/10.jpg)
Introducing
A scriptable WebKit chrome-less browser
![Page 11: Using PhantomJS to QA your analytics implementation](https://reader036.vdocuments.us/reader036/viewer/2022070603/587018d91a28ab7f428b603d/html5/thumbnails/11.jpg)
Simulate user behaviour and more
PhantomJS can respond to different stages of the page load
• Page initialised• Page load start• Page load finished• Page closed• Page not found
![Page 12: Using PhantomJS to QA your analytics implementation](https://reader036.vdocuments.us/reader036/viewer/2022070603/587018d91a28ab7f428b603d/html5/thumbnails/12.jpg)
Simulate user behaviour and more
PhantomJS can respond to HTTP requests
• Resource requested• Resource received• Resource error• HTTP status text and codes
![Page 13: Using PhantomJS to QA your analytics implementation](https://reader036.vdocuments.us/reader036/viewer/2022070603/587018d91a28ab7f428b603d/html5/thumbnails/13.jpg)
Simulate user behaviour and more
PhantomJS can respond to alerts and messages
• Javascript alert sent• Javascript console sent• Javascript prompt
![Page 14: Using PhantomJS to QA your analytics implementation](https://reader036.vdocuments.us/reader036/viewer/2022070603/587018d91a28ab7f428b603d/html5/thumbnails/14.jpg)
Simulate user behaviour and more
PhantomJS can also
• Scan and manipulate the DOM• Catch Javascript errors in the page analysed
![Page 15: Using PhantomJS to QA your analytics implementation](https://reader036.vdocuments.us/reader036/viewer/2022070603/587018d91a28ab7f428b603d/html5/thumbnails/15.jpg)
Simulate user behaviour and more
PhantomJS can also
• Scan and manipulate the DOM• Catch Javascript errors in the page analysed
• It can do screenshots
![Page 16: Using PhantomJS to QA your analytics implementation](https://reader036.vdocuments.us/reader036/viewer/2022070603/587018d91a28ab7f428b603d/html5/thumbnails/16.jpg)
With PhantomJS you can write a spider to audit your analytics
implementation
![Page 17: Using PhantomJS to QA your analytics implementation](https://reader036.vdocuments.us/reader036/viewer/2022070603/587018d91a28ab7f428b603d/html5/thumbnails/17.jpg)
Continuously
![Page 19: Using PhantomJS to QA your analytics implementation](https://reader036.vdocuments.us/reader036/viewer/2022070603/587018d91a28ab7f428b603d/html5/thumbnails/19.jpg)
// Set the start URLvar page = require("webpage").create();var url = "http://www.adobe.com";//var url = "http://www.webtrends.com";var links;
page.viewPortSize={ width: 1280, height: 800};page.clipRect={ top:0, left:0, width:1280, height:800}phantom.onError=function(){};page.onError=function(){};page.onLoadStarted=function(){};page.onResourceReceived=function(responseData){};
Source code
![Page 20: Using PhantomJS to QA your analytics implementation](https://reader036.vdocuments.us/reader036/viewer/2022070603/587018d91a28ab7f428b603d/html5/thumbnails/20.jpg)
page.onResourceRequested=function(requestData,networkRequest){ if(requestData.url.match(/\/b\/ss\//)) console.log(requestData.url);//Adobe Analytics if(requestData.url.match(/dcs\.gif/)) console.log(requestData.url);//Webtrends};page.onLoadFinished=function(status){ if(status=="success"){ var links = page.evaluate(function(){ var a=document.createElement("a"); return [].map.call(document.querySelectorAll("a[href]"), function(link){ a.href=link.getAttribute("href"); return a.href; }); }); console.log(links.join("\n")); }else{ //log error }; //check next page};
Source code
![Page 21: Using PhantomJS to QA your analytics implementation](https://reader036.vdocuments.us/reader036/viewer/2022070603/587018d91a28ab7f428b603d/html5/thumbnails/21.jpg)
page.open(url,function (status){ if(status=="success"){ }else{ //log error }; phantom.exit();});
Source code
![Page 22: Using PhantomJS to QA your analytics implementation](https://reader036.vdocuments.us/reader036/viewer/2022070603/587018d91a28ab7f428b603d/html5/thumbnails/22.jpg)
Alban Gérômeemail: [email protected]: @albangerome
All pictures from this deck taken by myself in Barcelona