Jump to:

23368 Posts in 18143 Topics by 2863 members

General Questions

SilverStripe Forums » General Questions » Most popular articles

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

Page: 1
Go to End
Author Topic: 1340 Views
  • Jarek
    Avatar
    Community Member
    30 Posts

    Most popular articles Link to this post

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

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: Most popular articles Link to this post

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

  • Jarek
    Avatar
    Community Member
    30 Posts

    Re: Most popular articles Link to this post

    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.

  • edski
    Avatar
    Community Member
    12 Posts

    Re: Most popular articles Link to this post

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

  • MitraX
    Avatar
    Community Member
    20 Posts

    Re: Most popular articles Link to this post

    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.

    1340 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.