Jump to:

23001 Posts in 11702 Topics by 2827 members

General Questions

SilverStripe Forums » General Questions » Count hits on links

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: 777 Views
  • micahsheets
    Avatar
    Community Member
    164 Posts

    Count hits on links Link to this post

    I have a splash page that a client wants to count the hits on each of the three main links. All three links actually take the user to the same page but will give some analytics to the client. The usefulness of this aside I still need to figure out how to do it. Here is my SplashPage.php class:

    <?php

    class SplashPage extends Page {

       public static $db = array(
          'Contractor' => 'Int',
          'Designer' => 'Int',
          'Owner' => 'Int'
       );
       
       public static $has_one = array(
    );
       
        protected $ResetCounts = 0;
       
       function getCMSFields() {
          $fields = parent::getCMSFields();
          // If this Boolean is set to true when the page is saved in the CMS then the database is reset for this page.
          $fields->addFieldToTab("Root.Content.Main", new CheckboxField("ResetCounts", "Tick to reset click counters on save."), 'Content');
          return $fields;
       }
       
       function onBeforeWrite() {
          if ($this->ResetCounts > 0) {
             $this->Contractor = 0;
             $this->Designer = 0;
             $this->Owner = 0;
          }
          parent::onBeforeWrite();
       }
       
    }

    class SplashPage_Controller extends Page_Controller {
       
       function init() {      
          parent::init();
          
          /* Note: I put these here instead of the template files so that my page types that inherit from Page.php and use the same
                template can load thier own stylesheets and only need a Layout file. */
          Requirements::javascript("site/javascript/SplashPage.js"); // MainNav.js is used for dropdown/flyout menus in IE 6.      

          if (isset($this->URLParams['Action'])) {
             if ($this->URLParams['Action'] == "Contractor"){$this->Contractor += 1;}
             else if ($this->URLParams['Action'] == "Designer"){$this->Designer += 1;}
             else if ($this->URLParams['Action'] == "HomeOwner"){$this->HomeOwner += 1;}
    Director::redirect('/home');
          }
       }
       
    }

    ?>

    The reset checkbox in the CMS works just fine to set the values back to zero in the database however the bit of code that checks the URLParams['Action'] vales does not seem to work at least not for incrementing the database values.

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