Jump to content

Questions regarding the making of a simple a viewtracker module


hellomoto
 Share

Recommended Posts

I want to make a little module that tracks pageviews. This is my first module. I'm starting with a copy of helloworld.module.

I already created the template pageviews and pageview manually, but I'll add their creation into the module later.

Here's what I have:

	public function viewTracker($event) {

		$page = $event->object; 

		// don't add this to the admin pages
		if($page->template == 'admin') return;
		if($page->template == 'vessel') {
			$r = new Page();
			$r->template = 'pageview';
			$r->parent = wire('pages')->get('/viewtracker/');
			$r->user = $user->name;
			$r->vessel_id = $page->id;
			$r->name = $user->id . $page->id . date('YmdHisu');
			//$r->status = $r->status | Page::statusLocked;
			$r->save();
		}

		// add a "Hello World" paragraph right before the closing body tag
		$event->return = str_replace("</body>", "<p>Hello $user World!</p></body>", $event->return); 
	}

but the $user variable isn't coming through. How can I access the logged in $user?

Edit: Got it. Replaced the $user with $this->user.....


	public function viewTracker($event) {

		$page = $event->object; 

		// don't add this to the admin pages
		if($page->template == 'admin') return;
		if($page->template == 'vessel') {
			$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();
		}
	}
  • Like 1
Link to comment
Share on other sites

How can I access the input variable from within this function? I want to add URL segments to the records.

I tried adding: 


			// record if segment
			$event->return = str_replace("</body>", "<p>Hello {$this->input->urlSegment1} World!</p></body>", $event->return);
		}

also tried $page->input->urlSegment1 and $input->urlSegment1. 

Update: never mind, just had to make it $this->input->urlSegment(1) (in parentheses). 

  • Like 1
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...