1

I want to make my code fake the refferer header in analytic systems(such as google analytics) , but It doesn't work. I have add 'var settings ={...//...}' and add 'page.onLoadStarted = function() {page.customHeaders = {};' and add - 'page.open(...,settings, ...' , but it still recognised like direct traffic in the analytics. Here is the code:

var page = require('webpage').create();
var settings = {
  headers: {
   "Referer": "http://google.com"
  }
};
var urls = ['http://china.com/','http://usa.com/','http://emirates.com/'];
var i = 0;

function OpenPage(){
    setTimeout(function(){
        page.onLoadStarted = function() {
    page.customHeaders = {};
};
        page.open(urls[i],settings, function(status) {
            if (status == 'success') {
                    page.render('example' + i + '.png');
            }
            i++;
            if(i <= urls.length - 1){ 
                OpenPage();
            }else{
               phantom.exit();
            }
        });
    },5000);
}    
OpenPage();

I get this code from this question https://stackoverflow.com/a/42468998/4999509 and it worked like a charm, respect for coder - Flash Thunder !

Community
  • 1
  • 1
Great Serg
  • 71
  • 1
  • 2
  • 10
  • You can also add the following to output the request headers, actually sent: page.onResourceRequested = function(requestData) { console.log('requestData: ' + JSON.stringify(requestData)); }; – a-bobkov Feb 26 '17 at 19:36
  • too bad for me - I'm new to JS) – Great Serg Feb 26 '17 at 20:43

2 Answers2

1

Try this:

var page = require('webpage').create();

page.onLoadStarted = function() {
    page.customHeaders = {};
};

var urls = ['http://china.com/','http://usa.com/','http://emirates.com/'];
var i = 0;

function OpenPage(){
    setTimeout(function(){
        page.customHeaders = {
            "Referer": "https://google.com"
        };
        page.open(urls[i], function(status) {
            if (status == 'success') {
                    page.render('example' + i + '.png');
            }
            i++;
            if(i <= urls.length - 1){ 
                OpenPage();
            }else{
               phantom.exit();
            }
        });
    },5000);
}    
OpenPage();
Flash Thunder
  • 11,672
  • 8
  • 47
  • 91
  • thank you! your code is working!. But if I try to run the phantomjs myscript,js with a proxy - I am not get any of referrers in analytics. Do you now something about that? – Great Serg Mar 12 '17 at 13:03
  • 1
    I think that Google treats Phantom as a bot, that's why there are no results in Analytics. Try setting up User-Agent. – Flash Thunder Mar 13 '17 at 09:01
1

This works for Google Analytics,

var settings = {
    headers: {
        "Referer": "http://www.google.com/"
    }
};

page.open(urlToVisit, settings, function (status) {
    ...
}

Basically, you need to make your custom referer available using a second argument on page.open() function in the page context for the Google Analytics code to be able to read it

Krishna Modi
  • 377
  • 2
  • 12