diff --git a/src/GoogleAnalytics/PersistManager.php b/src/GoogleAnalytics/PersistManager.php new file mode 100644 index 0000000..65e81b7 --- /dev/null +++ b/src/GoogleAnalytics/PersistManager.php @@ -0,0 +1,98 @@ +_session = null; + if (isset($_SESSION['GA_session'])) { + $this->_session = unserialize($_SESSION['GA_session']); + } + if ($this->_session === null || get_class($this->_session) !== 'UnitedPrototype\GoogleAnalytics\Session') { + $this->_session = new Session(); + } + return $this->_session; + } + + /** + * Get singleton visitor (persist) + * + * @return mixed|Visitor + */ + public function getVisitor () + { + global $_SESSION; + $this->_visitor = null; + if (isset($_SESSION['GA_visitor'])) { + $this->_visitor = unserialize($_SESSION['GA_visitor']); + } + if ($this->_visitor === null && get_class($this->_visitor) !== 'UnitedPrototype\GoogleAnalytics\Visitor') { + $this->_visitor = new Visitor(); + $this->_visitor->fromServerVar($_SERVER); + } + $this->_visitor->setPreviousVisitTime($this->_visitor->getCurrentVisitTime()); + $this->_visitor->setCurrentVisitTime(new DateTime()); + return $this->_visitor; + } + + /** + * Store php-ga session and visitor in $_SESSION + */ + public function store () + { + global $_SESSION; + $_SESSION['GA_visitor'] = serialize($this->getVisitor()); + $_SESSION['GA_session'] = serialize($this->getSession()); + } +} \ No newline at end of file diff --git a/src/autoload.php b/src/autoload.php index 3c57790..a822eed 100644 --- a/src/autoload.php +++ b/src/autoload.php @@ -14,5 +14,3 @@ require(__DIR__ . $classPath); } }); - -?>