{"id":7795,"date":"2015-10-27T08:48:37","date_gmt":"2015-10-27T08:48:37","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/10\/27\/hide-the-footprint-of-casperjs-with-google-analytics-open-source-projects-n1k0-casperjs\/"},"modified":"2015-10-27T08:48:37","modified_gmt":"2015-10-27T08:48:37","slug":"hide-the-footprint-of-casperjs-with-google-analytics-open-source-projects-n1k0-casperjs","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/10\/27\/hide-the-footprint-of-casperjs-with-google-analytics-open-source-projects-n1k0-casperjs\/","title":{"rendered":"Hide the footprint of CasperJS with Google Analytics-open source projects n1k0\/casperjs"},"content":{"rendered":"<p>I&#8217;m trying to hide the utilisation of CasperJS with one my script. Currently I&#8217;m trying to change the resolution, the user agent and the language with that :<\/p>\n<pre><code>casper.userAgent(\"My UA\");\ncasper.viewport(1600, 900);\ncasper.page.customHeaders = {'Accept-Language': 'fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3'};\n<\/code><\/pre>\n<p>The casper.viewport() and casper.page.customHeaders doesn&#8217;t seem to work with Google Analytics&#8230; When trying on some websites, it seems to be okay, but Google Analytics gets to see I&#8217;m a web scraper :<\/p>\n<pre><code>My lang is \"c\"\nCompatibility with JAVA : no\nScreen resolution : 1024x768\nFlash version : not set\n<\/code><\/pre>\n<p>Is there anything I can do to fake this ?<\/p>\n<p><strong>(Piece of) Solution<\/strong><\/p>\n<p>Thanks to kasper pedersen, here is a part of the solution :<\/p>\n<p>We can override some variable during the initialize part with :<\/p>\n<pre><code>casper.on('page.initialized', function (page) {\n    page.evaluate(function () { \n        (function() {\n            window.screen = {\n                width: 1600,\n                height: 900\n            };\n            window.navigator.__defineGetter__('javaEnabled', function () {\n                return function() { return true; };\n            });\n        })\n    });\n});\n<\/code><\/pre>\n<p>This fakes the windows resolution and the plugin for Java. To fake the flash plugins, we could do something like that :<\/p>\n<pre><code>casper.on('page.initialized', function (page) {\npage.evaluate(function () { \n    (function() {\n        window.screen = {\n            width: 1600,\n            height: 900\n        };\n        var fake_navigator = {};\n        for (var i in navigator) {\n          fake_navigator[i] =  navigator[i];\n        }\n        fake_navigator.javaEnabled = function() { return true; };\n        fake_navigator.language = 'en-US';\n        fake_navigator.plugins = {\n            length: 1,\n            'Shockwave Flash': {\n                description: 'Shockwave Flash 11.9 r900',\n                name: 'Shockwave Flash',\n                version: '11.9.900.117'\n            }\n        };\n        window.navigator = fake_navigator;\n    })();\n});\n});\n<\/code><\/pre>\n<p>When doing this and verifying the info in the window.navigator of PhantomJS, everything seems okay, but Google Analytics doesn&#8217;t track me as a visitor any more (I don&#8217;t appear in the real time window of Google Analytics&#8230;).<\/p>\n<p>So I just fake the first two info, and for the language, I fake it in changing the language of my server (<code>export LC_ALL=en_US.utf8<\/code>).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m trying to hide the utilisation of CasperJS with one my script. Currently I&#8217;m trying to change the resolution, the user agent and the language with that : casper.userAgent(&#8220;My UA&#8221;); casper.viewport(1600, 900); casper.page.customHeaders = {&#8216;Accept-Language&#8217;: &#8216;fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3&#8242;}; The casper.viewport() and casper.page.customHeaders doesn&#8217;t seem to work with Google Analytics&#8230; When trying on some websites, it seems to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7795","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7795","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7795"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7795\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7795"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}