Are somewhere in database stored data about page visits? I want to create list of most popular pages (articles). If not, what is the best place to put some code, that increments database field (with number of visits).
We've moved the forum!
Please use forum.silverstripe.org for any new questions
(announcement).
The forum archive will stick around, but will be read only.
You can also use our Slack channel
or StackOverflow to ask for help.
Check out our community overview for more options to contribute.
No information in the database on page views. SS used to have it but that was a nightmare. To store the number of visits its easy to do but this means that for every page visit you're writing to the database which is slow and if you have a large site I wouldn't recommend it. You would be best to integrate with google analytics or something to get page view information.
If you really want to implement it then the easiest way is to add a field to your page type and increment the count in the init()
// Page.php
static $db = array(
'Views' => 'Int'
);
// Page_Controller
function init() {
parent::init();
$this->Views++;
$this->write();
}
// then the query for most popular would be like
function MostPopular() {
return DataObject::get('Page', '', 'Views DESC', '', '10');
}
Thx for reply. I didn't know that is possible to get data from google analytics. Google helped and I've found some usefull code. This solution will be better.
Hi Jarek,
Have you actually integrated Google analytic's most popular pages into SilverStripe. If so, are you able to share the code - it's exactly what I'm looking for!
Many thanks...
The problem with using Views page field mentioned above is that the page will be versioned every time it is accessed.
There are two ways to avoid this behavior:
- either to use SQL query directly for updating the View value
- or to use separate dataobject (e.g. PageCounter) that will keep information about page views
You can read tutorials I wrote regarding page view counter: how to implement page view counter and how to display popular articles
Google Analytics is a great web tool but I don't like to use it for my sites' logic, it's only what it's name says - an analytics tool.