How to count page views and visitors

Erstellt am: Donnerstag 13 April 2017  |  Letzte Änderung am: Samstag 11 August 2018

Here's a simple Counter class based on ItemManager that you can use to count users who have visited certain pages or count the number of clicks and to create the vote plugins.

<?php
$counter = new Counter(1);
class Counter
{
    private $imanager;
    private $slug;
    private $catId;
    private $itemMapper;
    private $min = 0;

    public function __construct($catId)
    {
        $this->imanager = imanager();
        $this->itemMapper = $this->imanager->getItemMapper();
        $this->slug = $this->imanager->sanitizer->pageName(get_page_slug(false));
        $this->catId = $catId;
    }

    public function getCount($slug = null)
    {
        $slug = (is_null($slug) ? $this->slug : $this->imanager->sanitizer->pageName($slug));
        $this->itemMapper->alloc($this->catId);
        $sItems = $this->itemMapper->getSimpleItems('name='.$slug);
        if($sItems) $sItem = current($sItems);
        return (!empty($sItem->total) ? (int)$sItem->total : 0);
    }

    public function increment($slug = null)
    {
        $slug = (is_null($slug) ? $this->slug : $this->imanager->sanitizer->pageName($slug));
        $this->itemMapper->alloc($this->catId);
        $this->itemMapper->init($this->catId);
        $item = $this->itemMapper->getItem('name='.$slug);
        if(empty($item)) { $item = $this->createItem($slug); }
        $item->setFieldValue('total', (int)$item->fields->total->value + 1);
        $item->save();
        $this->itemMapper->simplify($item);
        $this->itemMapper->save();
    }

    public function decrement($slug = null)
    {
        $slug = (is_null($slug) ? $this->slug : $this->imanager->sanitizer->pageName($slug));
        $this->itemMapper->alloc($this->catId);
        $this->itemMapper->init($this->catId);
        $item = $this->itemMapper->getItem('name='.$slug);
        if(!empty($item))
        {
            if((int)$item->fields->total->value <= $this->min) return;
            $item->setFieldValue('total', (int)$item->fields->total->value - 1);
            $item->save();
            $this->itemMapper->simplify($item);
            $this->itemMapper->save();
        }
    }

    public function remove($slug = null)
    {
        $slug = (is_null($slug) ? $this->slug : $this->imanager->sanitizer->pageName($slug));
        $this->itemMapper->init($this->catId);
        $item = $this->itemMapper->getItem('name='.$slug);
        if(!empty($item)) { return $this->imanager->deleteItem($item->id, $item->categoryid); }
    }

    private function createItem($slug)
    {
        $item = new Item($this->catId);
        $item->name = $slug;
        $item->setFieldValue('total', 0);
        $item->active = 1;
        $item->save();
        return $item;
    }
}

Preparation

Create a new ItemManager category, name it vcounter for example, then create a text field for it, name it total. Copy and paste the Counter class in your functions.php file in your theme folder. Change parameter 1 in$counter = new Counter(1); with the id of your vcounter category.

Now you can use the Counter class to count the page views

To increment the counting variable for current page do this (in your template):

$counter->increment();

To increment the counting variable for specific page:

$counter->increment($slug);

To decrement the counting variable for the current page:

$counter->decrement();

To decrement the counting variable for specific page:

$counter->decrement($slug);

To output the counting variable for current page use:

echo $counter->getCount();

To output the counting variable for specific page use following:

echo $counter->getCount($slug);

To delete the counter for current page do this:

$counter->remove();

To delete the counter for specific page do this:

$counter->remove($slug);

Note, the Counter is not thread-safe.

Autor: Bigin  |  Tags:  FrameworkPHPGetSimpleDevelopmentItemManager