Skip to content

Instantly share code, notes, and snippets.

@htuscher
Created April 26, 2015 12:52
Show Gist options
  • Save htuscher/cdf59ac9aa5d7bcacdc8 to your computer and use it in GitHub Desktop.
Save htuscher/cdf59ac9aa5d7bcacdc8 to your computer and use it in GitHub Desktop.
TYPO3 Neos track PageViews
<?php
namespace Jhoechtl\MySite\Domain\Model;
/* *
* This script belongs to the TYPO3 Flow package "Jhoechtl.MySite". *
* *
* */
use TYPO3\Flow\Annotations as Flow;
use Doctrine\ORM\Mapping as ORM;
/**
* @Flow\Entity
*/
class PageView {
/**
* @ORM\ManyToOne
* @ORM\JoinColumn(onDelete="CASCADE")
* @var \TYPO3\TYPO3CR\Domain\Model\NodeData
*/
protected $nodeData;
/**
* @var \DateTime
*/
protected $date;
/**
* @return \TYPO3\TYPO3CR\Domain\Model\NodeData
*/
public function getNodeData() {
return $this->nodeData;
}
/**
* @param \TYPO3\TYPO3CR\Domain\Model\NodeData $nodeData
* @return void
*/
public function setNodeData($nodeData) {
$this->nodeData = $nodeData;
}
/**
* @return \DateTime
*/
public function getDate() {
return $this->date;
}
/**
* @param \DateTime $date
* @return void
*/
public function setDate($date) {
$this->date = $date;
}
}
<?php
/***************************************************************
* Copyright notice
*
* (c) 2015 Hans Höchtl <jhoechtl@gmail.com>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
* A copy is found in the textfile GPL.txt and important notices to the license
* from the author is found in LICENSE.txt distributed with these scripts.
*
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
namespace Jhoechtl\MySite\Aspect;
use Jhoechtl\MySite\Domain\Model\PageView;
use TYPO3\Flow\Annotations as Flow;
use TYPO3\Flow\AOP\JoinPointInterface;
use TYPO3\TYPO3CR\Domain\Model\NodeInterface;
/**
* @Flow\Scope("singleton")
* @Flow\Aspect
*/
class PageViewAspect {
/**
* @Flow\Inject
* @var \Jhoechtl\MySite\Domain\Repository\PageViewRepository
*/
protected $pageViewRepository;
/**
* @Flow\Inject
* @var \TYPO3\Flow\Persistence\PersistenceManagerInterface
*/
protected $persistenceManager;
/**
* @Flow\Before("method(TYPO3\Neos\Controller\Frontend\NodeController->showAction())")
* @param JoinPointInterface $joinPoint
* @return void
*/
public function trackPageViewAspect(JoinPointInterface $joinPoint) {
/** @var NodeInterface $node */
$node = $joinPoint->getMethodArgument('node');
$pageView = new PageView();
$pageView->setDate(new \DateTime());
$pageView->setNodeData($node->getNodeData());
$this->persistenceManager->whitelistObject($pageView);
$this->pageViewRepository->add($pageView);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment