Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drop ip address #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
11 changes: 4 additions & 7 deletions src/Metrics/Analyzers/UniqueVisitorAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,19 @@ class UniqueVisitorAnalyzer extends Analyzer

public function compile(Collection $visits, TimeInterval $interval)
{
$data = [];

$cookieStack = [];

foreach($visits as $visit) {
if(! in_array($visit->getCookie(), $cookieStack)) {
$cookieStack[] = $visit->getCookie();
}
}

return ['unique-visitors' => count($cookieStack)];

}

// This operation will add two array returned by the compile() method
// then return a consolidated array.
// then return a consolidated array.
public function consolidate(Collection $metrics, TimeInterval $interval)
{
$uniqueVisitors = 0;
Expand All @@ -37,10 +34,10 @@ public function consolidate(Collection $metrics, TimeInterval $interval)
$uniqueVisitors+= $statistic['unique-visitors'];
}
}

$data = ['unique-visitors' => $uniqueVisitors];
return $data;
}


}
}
95 changes: 33 additions & 62 deletions src/Metrics/Visit.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ class Visit implements Arrayable
* @var string
*/
protected $user_id;

/**
* IP of the visit
* @var string
*/
protected $ip;

/**
* Full user agent of the visit
Expand Down Expand Up @@ -57,7 +51,7 @@ class Visit implements Arrayable
protected $actions;

/**
* Custom data to be added
* Custom data to be added
* @var array
*/
protected $custom = [];
Expand Down Expand Up @@ -94,7 +88,7 @@ public function __construct()
/**
* Create a Visit instance from an array. We'll use this Essentially
* to reconstruct a Visit object from a database row.
*
*
* @param array $data
* @return Visit
*/
Expand All @@ -103,7 +97,6 @@ public static function createFromArray(array $data)
$visit = new static;

$visit->id = $data['id'] ?? null;
$visit->ip = $data['ip'];
$visit->user_agent = $data['user_agent'];
$visit->user_id = $data['user_id'];
$visit->custom = $data['custom'];
Expand All @@ -124,8 +117,8 @@ public static function createFromArray(array $data)

/**
* Set anonymous flag
*
* @param boolean $anonymous
*
* @param boolean $anonymous
*/
public function setAnonymous($anonymous = true)
{
Expand All @@ -134,7 +127,7 @@ public function setAnonymous($anonymous = true)

/**
* Is Visit anonymous ?
*
*
* @return boolean
*/
public function isAnonymous()
Expand All @@ -144,7 +137,7 @@ public function isAnonymous()

/**
* Set the user id for this record
*
*
* @param int $userId
*/
public function setUserId($userId)
Expand All @@ -164,7 +157,7 @@ public function userId()

/**
* Get the visited url
*
*
* @return string
*/
public function getUrl()
Expand All @@ -174,7 +167,7 @@ public function getUrl()

/**
* Set the Url
*
*
* @param string $url
*/
public function setUrl($url)
Expand All @@ -184,38 +177,17 @@ public function setUrl($url)

/**
* Set referer
*
*
* @param string $referer
*/
public function setReferer($referer)
{
$this->referer = $referer;
}

/**
* Get request IP
*
* @return string
*/
public function getIp()
{
return $this->ip;
}

/**
* Set IP Address
*
* @param string $ip
* @return void
*/
public function setIp($ip)
{
$this->ip = $ip;
}

/**
* Return laravel cookie
*
*
* @return string
*/
public function getCookie()
Expand All @@ -225,7 +197,7 @@ public function getCookie()

/**
* Set cookie
*
*
* @param string $cookie
*/
public function setCookie($cookie = null)
Expand All @@ -245,17 +217,17 @@ public function getDate()

/**
* Set date
*
*
* @param Carbon $date
*/
public function setDate(Carbon $date)
{
{
$this->date = $date;
}

/**
* Return user agent
*
* Return user agent
*
* @return string
*/
public function getUserAgent()
Expand All @@ -275,7 +247,7 @@ public function setUserAgent($userAgent)

/**
* Set Session id
*
*
* @param string $sessionId
*/
public function setSessionId($sessionId)
Expand All @@ -285,7 +257,7 @@ public function setSessionId($sessionId)

/**
* Get session id
*
*
* @return string
*/
public function getSessionId()
Expand All @@ -295,7 +267,7 @@ public function getSessionId()

/**
* Set status code
*
*
* @param string $statusCode
*/
public function setStatusCode($statusCode)
Expand All @@ -305,7 +277,7 @@ public function setStatusCode($statusCode)

/**
* Get status code
*
*
* @return string
*/
public function getStatusCode()
Expand All @@ -315,7 +287,7 @@ public function getStatusCode()

/**
* Get actions on this visit
*
*
* @return Collection
*/
public function actions()
Expand All @@ -325,7 +297,7 @@ public function actions()

/**
* Attach an action to the visit
*
*
* @param Action $action
* @return Visit
*/
Expand All @@ -338,7 +310,7 @@ public function addAction(Action $action)

/**
* Get the action from the given class
*
*
* @param string $actionClass
* @return Action | null
*/
Expand All @@ -355,9 +327,9 @@ public function getAction($actionClass)

/**
* Return true if the Visit has an action of the given class
*
*
* @param string $actionClass
* @return boolean
* @return boolean
*/
public function hasAction($actionClass)
{
Expand All @@ -366,18 +338,18 @@ public function hasAction($actionClass)

/**
* Add a custom tracking value to the object
*
* @param string $key
*
* @param string $key
* @param mixed $value
*/
public function setCustomValue($key, $value)
{
{
$this->custom[$key] = $value;
}

/**
* Get a custom value from the object
*
*
* @param string $key
* @return mixed
*/
Expand All @@ -388,8 +360,8 @@ public function getCustomValue($key)

/**
* Check if a custom value exists
*
* @param string $key
*
* @param string $key
* @return boolean
*/
public function hasCustomValue($key)
Expand All @@ -399,14 +371,13 @@ public function hasCustomValue($key)

/**
* Convert object to array, including serialisation of actions
*
*
* @return array
*/
public function toArray()
{
return [
'id' => $this->id,
'ip' => $this->ip,
'user_id' => $this->user_id,
'user_agent' => $this->user_agent,
'actions' => $this->getSerializedActions(),
Expand All @@ -423,7 +394,7 @@ public function toArray()

/**
* Get actions as serialized objects
*
*
* @return array
*/
protected function getSerializedActions()
Expand All @@ -437,7 +408,7 @@ protected function getSerializedActions()

/**
* Magic getter for object's properties
*
*
* @param string $key
* @return mixed
*/
Expand Down
13 changes: 6 additions & 7 deletions src/Metrics/VisitCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(VisitRepository $visits, Manager $manager)

/**
* Create a Visit instance from a Request object
*
*
* @param Request $request
* @return Visit
*/
Expand All @@ -41,7 +41,6 @@ public function createFromRequest(Request $request)
$visit->setDate(Carbon::now());
$visit->setUrl($request->getUri());
$visit->setReferer($request->server('HTTP_REFERER'));
$visit->setIp($request->ip());

$cookiePresent = $request->hasCookie(config('metrics.cookie_name'));
$anonCookiePresent = $request->hasCookie(config('metrics.anonymous_cookie_name'));
Expand Down Expand Up @@ -94,9 +93,9 @@ public function createFromRequest(Request $request)
protected function getUMTFromRequest(Request $request) : array
{
$fields = config('metrics.utm_fields_mapping');

$fieldsInRequest = [];

foreach($fields as $standardUMTKey => $mappedKey) {
if($request->has($mappedKey)) {
$fieldsInRequest[$standardUMTKey] = $request->get($mappedKey);
Expand All @@ -108,9 +107,9 @@ protected function getUMTFromRequest(Request $request) : array

/**
* Check if the cookie has expired
*
*
* @param string $cookie
* @return boolean
* @return boolean
*/
protected function hasCookieExpired($cookie)
{
Expand All @@ -126,4 +125,4 @@ protected function hasCookieExpired($cookie)

return false;
}
}
}
Loading