Jump to content

My starter ViewTracker module: scalable as pages..?


hellomoto
 Share

Recommended Posts

Hi. I made a little module a while ago to track pageviews. It stores each pageview record as a PW page (hidden, under a hidden parent page "ViewTracker"), with the user & page IDs and timestamp. 

<?php

/**
 * Processwire ProcessViewtracker Module
 * 
 * ProcessWire 2.5.3
 * Copyright (C) 2014 by Ryan Cramer 
 * Licensed under GNU/GPL v2, see LICENSE.TXT
 * http://processwire.com
 *
 */

/* TO-DO:
- add settings: templates select, clear/archive option..?
- add auto-creation of templates/fields on install
*/

class ProcessViewtracker extends WireData implements Module {

	/**
	 * getModuleInfo is a module required by all modules to tell ProcessWire about them
	 *
	 * @return array
	 *
	 */
	public static function getModuleInfo() {

		return array(
			'title' => 'ViewTracker', 
			'version' => 1, 
			'summary' => 'Records pageviews.',
			'href' => '',
			'singular' => true, 
			'autoload' => true, 
			'icon' => 'eye', 
			);
	}

	public function init() {

		// add a hook after each page is rendered
		$this->addHookAfter('Page::render', $this, 'viewTracker'); 

	}

	/**
	 * ViewTracker hooks into every page after it's rendered and records the views for specified template(s)
	 * (and adds "Hello World" text at the bottom)
	 *
	 */
	public function viewTracker($event) {

		$page = $event->object; 

		// don't add this to the admin pages
		if($page->template == 'admin') return;

		// add to selected templates
		if($page->template == 'vessel') {
			// create record
			$r = new Page();
			$r->template = 'pageview';
			$r->parent = wire('pages')->get('/viewtracker/');
			$r->user = $this->user;
			$r->vessel_id = $page;
			$r->name = $this->user . $page . date('YmdHisu');
			$r->status = $r->status | Page::statusHidden;
			$r->save();

			// record if segment
			if($this->input->urlSegment(1) !== '') {
				//$segment = "$this->input->urlSegment(1)";
				$this->sanitizer->text($this->input->urlSegment(1));
				$r->page_segment = "{$this->input->urlSegment(1)}";
				$r->save();
			}

			$event->return = str_replace("</body>", "<p>Hello {$this->input->urlSegment(1)} World!</p></body>", $event->return);
		}

	}
	
}

I want to add a configuration option for choosing the template(s) this module should activate for, maybe an admin view, but for right now this is all I got, so that the view data can be pulled easily. I know PW can handle a great many pages, but like I've kept this module uninstalled when not developing it... though maybe if I'd just left it enabled I'd have an answer to my own question here. Because tracking page views compiles much more quickly than any other sort of page, really... Should I just be storing this data in its own table?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...