InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

PhantomJS tutorials

  • Install phantomjs on Mac
  • Install phantomjs on Ubuntu
  • create site snapshot thumbnail
  • find all fonts-families on a web page
  • print resources request by a page
  • skip Google analytics when fetching a web page
 
  • Home
  • > Tutorials
  • > Javascript
  • > PhantomJS

PhantomJS – how to skip Google analytics code when fetching a web page

By admin on Dec 7, 2015

PhantomJS can be used to test websites and for automating various tasks. When testing a website in production, it may not be a good idea to execute Google analytics code. Here is quick code snippet to skip fetching Google analytics code when using PhantomJS.

var url = require('system').args[1];
var page = require('webpage').create();
page.onResourceRequested = function (requestData, request) {
  console.log('url requested: ' + requestData.url);
  if (/www.google-analytics\.com\//.test(requestData.url)) {
    console.log('Aborting request to GA ' + requestData.url);
    request.abort();
  }
};
page.open('https://dev.infoheap.com/', function (status) {
  if (status !== 'success') {
    console.log('Unable to access network');
  } else {
    console.log("Done...");
    phantom.exit(0);
  }
});

Here is the output (some part of it) from above code:

...
url requested: http://www.google-analytics.com/analytics.js
Aborting request to GA http://www.google-analytics.com/analytics.js
...

Improved version of skipping Google analytics approach

This approach may have a slight problem. This will also skip Google analytics javascript to be fetched. This may cause few javascript errors on that page. Better approach is to fetch the analytics javascript and not the metric collection code as shown below:

var url = require('system').args[1];
var page = require('webpage').create();
page.onResourceRequested = function (requestData, request) {
console.log('url requested: ' + requestData.url);
if (/www.google-analytics\.com\/r\/collect/.test(requestData.url)) {
    console.log('Aborting request to GA ' + requestData.url);
    request.abort();
  }
};
page.open(url, function (status) {
  if (status !== 'success') {
    console.log('Unable to access network');
  } else {
    console.log("Done...");
    phantom.exit(0);
  }
});

Here is the output (some part of it) from above code:

...
url requested: https://www.google-analytics.com/r/collect?v=1&_v=j40&a=1975811097&t=pageview&_s=1&dl=http%3A%2F%2Fdev.infoheap.com%2F&ul=en-us&de=UTF-8&dt=dev%20InfoHeap%20-%20Tech%20tutorials%2C%20tips%2C%20tools%20and%20more&sd=32-bit&sr=1024x768&vp=400x300&je=0&_u=QEAAAUQAK~&jid=1452159943&cid=879026602.1449483079&tid=UA-38219083-1&_r=1&z=517835023
Aborting request to GA https://www.google-analytics.com/r/collect?v=1&_v=j40&a=1975811097&t=pageview&_s=1&dl=http%3A%2F%2Fdev.infoheap.com%2F&ul=en-us&de=UTF-8&dt=dev%20InfoHeap%20-%20Tech%20tutorials%2C%20tips%2C%20tools%20and%20more&sd=32-bit&sr=1024x768&vp=400x300&je=0&_u=QEAAAUQAK~&jid=1452159943&cid=879026602.1449483079&tid=UA-38219083-1&_r=1&z=517835023
...

Suggested posts:

  1. www vs non-www domain which is better for your site?
  2. Using robotexclusionrulesparser python module to parse robots.txt and crawl a site
  3. WordPress how to check if a post is being viewed by admin
  4. Verify a web site in google search console (webmaster tools)
  5. Chrome extension tutorial – hello world
  6. How to install and run Chrome PageSpeed insights for measuring site performance
  7. Traceroute outcome from Bangalore to AWS Virginia and California
  8. Ssh automation on Amazon EC2 Ubuntu Linux
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Javascript, PhantomJS, Tutorials, Web Development
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress