Im thinking in a cron which executes daily one php script.
This script will make a file_get_contents() to one url I assign.
Can i do this for simulate a user’s visit?
Does it work like a visit?
$page = file_get_contents('http://www.example.com/');
echo $page;
2
Answers
You can “simulate” this kind of action, but it’s better to be done with curl. Also to do this I would recommend going through this stackoverflow post, it explains all the variables which are needed to be supplied by doing a server side request, rather than opening the page through a browser and loading the analytics js.
If your visit is counted by Google Analytics implemented through javascript, then no, it won’t work –
file_get_contents()
doesn’t run any javascript, just downloads the file. However, you could do it by sending page view through PHP: https://developers.google.com/analytics/devguides/collection/protocol/v1/devguideNote that javascript gathers more information about the user than PHP can, so use with caution.
If your intention is to check whether the script was run, there are better and easier ways, such as logging any opening, or from specific IP, to your database or a text file.
Everything I suggested here presumes you can edit the opened file – your
http://www.example.com/index.php
. If you need to trigger Google Analytics on a site you can’t edit, you need something way more robust, like a web crawler or scraper, to execute the javascript. For inspiration: http://www.jacobward.co.uk/using-php-to-scrape-javascript-jquery-json-websites/