Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Most popular articles


Reply


5 Posts   1405 Views

Avatar
Jarek

Community Member, 30 Posts

31 July 2010 at 11:48pm

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).

Avatar
Willr

Forum Moderator, 5513 Posts

1 August 2010 at 11:08am

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');
}

Avatar
Jarek

Community Member, 30 Posts

2 August 2010 at 7:13pm

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.

Avatar
edski

Community Member, 12 Posts

12 April 2011 at 1:58am

Edited: 12/04/2011 1:58am

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...

Avatar
MitraX

Community Member, 20 Posts

10 May 2012 at 9:13pm

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: [url=http://www.inforbiro.com/blog-eng/silverstripe-cms-page-hit-view-counter/]how to implement page view counter[/url] and [url=http://www.inforbiro.com/blog-eng/silverstripe-most-popular-articles/]how to display popular articles[/url]

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.