Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions src/GoogleAnalytics/PersistManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* User: sj
* Date: 2014/7/5
* Time: 00:25
*/
namespace UnitedPrototype\GoogleAnalytics;

use DateTime;

/**
* Class PersistManager
* Use PHP $_SESSION to store php-ga session and visitor object
*
* @package UnitedPrototype\GoogleAnalytics
*/
class PersistManager
{
/**
* @var null|Visitor
*/
private $_visitor = null;
/**
* @var null|Session
*/
private $_session = null;

/**
* Constructor
*/
public function __construct ()
{
ini_set('session.cookie_lifetime', 0);
ini_set('session.gc_maxlifetime', 0);
@session_start();
}

/**
* Make a tracker instance
*
* @param $trackID
* @param $domain
* @return Tracker
*/
public function makeTracker ($trackID, $domain)
{
return new Tracker($trackID, $domain);
}

/**
* Get singleton session (persist)
*
* @return mixed|Session
*/
public function getSession ()
{
global $_SESSION;
$this->_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());
}
}
2 changes: 0 additions & 2 deletions src/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,3 @@
require(__DIR__ . $classPath);
}
});

?>