diff --git a/src/Metrics/Analyzers/UniqueVisitorAnalyzer.php b/src/Metrics/Analyzers/UniqueVisitorAnalyzer.php index c8e0467..f7eb67d 100755 --- a/src/Metrics/Analyzers/UniqueVisitorAnalyzer.php +++ b/src/Metrics/Analyzers/UniqueVisitorAnalyzer.php @@ -11,10 +11,8 @@ 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(); @@ -22,11 +20,10 @@ public function compile(Collection $visits, TimeInterval $interval) } 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; @@ -37,10 +34,10 @@ public function consolidate(Collection $metrics, TimeInterval $interval) $uniqueVisitors+= $statistic['unique-visitors']; } } - + $data = ['unique-visitors' => $uniqueVisitors]; return $data; } -} \ No newline at end of file +} diff --git a/src/Metrics/Visit.php b/src/Metrics/Visit.php index 1327928..adc6f73 100755 --- a/src/Metrics/Visit.php +++ b/src/Metrics/Visit.php @@ -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 @@ -57,7 +51,7 @@ class Visit implements Arrayable protected $actions; /** - * Custom data to be added + * Custom data to be added * @var array */ protected $custom = []; @@ -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 */ @@ -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']; @@ -124,8 +117,8 @@ public static function createFromArray(array $data) /** * Set anonymous flag - * - * @param boolean $anonymous + * + * @param boolean $anonymous */ public function setAnonymous($anonymous = true) { @@ -134,7 +127,7 @@ public function setAnonymous($anonymous = true) /** * Is Visit anonymous ? - * + * * @return boolean */ public function isAnonymous() @@ -144,7 +137,7 @@ public function isAnonymous() /** * Set the user id for this record - * + * * @param int $userId */ public function setUserId($userId) @@ -164,7 +157,7 @@ public function userId() /** * Get the visited url - * + * * @return string */ public function getUrl() @@ -174,7 +167,7 @@ public function getUrl() /** * Set the Url - * + * * @param string $url */ public function setUrl($url) @@ -184,7 +177,7 @@ public function setUrl($url) /** * Set referer - * + * * @param string $referer */ public function setReferer($referer) @@ -192,30 +185,9 @@ 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() @@ -225,7 +197,7 @@ public function getCookie() /** * Set cookie - * + * * @param string $cookie */ public function setCookie($cookie = null) @@ -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() @@ -275,7 +247,7 @@ public function setUserAgent($userAgent) /** * Set Session id - * + * * @param string $sessionId */ public function setSessionId($sessionId) @@ -285,7 +257,7 @@ public function setSessionId($sessionId) /** * Get session id - * + * * @return string */ public function getSessionId() @@ -295,7 +267,7 @@ public function getSessionId() /** * Set status code - * + * * @param string $statusCode */ public function setStatusCode($statusCode) @@ -305,7 +277,7 @@ public function setStatusCode($statusCode) /** * Get status code - * + * * @return string */ public function getStatusCode() @@ -315,7 +287,7 @@ public function getStatusCode() /** * Get actions on this visit - * + * * @return Collection */ public function actions() @@ -325,7 +297,7 @@ public function actions() /** * Attach an action to the visit - * + * * @param Action $action * @return Visit */ @@ -338,7 +310,7 @@ public function addAction(Action $action) /** * Get the action from the given class - * + * * @param string $actionClass * @return Action | null */ @@ -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) { @@ -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 */ @@ -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) @@ -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(), @@ -423,7 +394,7 @@ public function toArray() /** * Get actions as serialized objects - * + * * @return array */ protected function getSerializedActions() @@ -437,7 +408,7 @@ protected function getSerializedActions() /** * Magic getter for object's properties - * + * * @param string $key * @return mixed */ diff --git a/src/Metrics/VisitCreator.php b/src/Metrics/VisitCreator.php index 8177a9d..46f81be 100755 --- a/src/Metrics/VisitCreator.php +++ b/src/Metrics/VisitCreator.php @@ -31,7 +31,7 @@ public function __construct(VisitRepository $visits, Manager $manager) /** * Create a Visit instance from a Request object - * + * * @param Request $request * @return Visit */ @@ -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')); @@ -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); @@ -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) { @@ -126,4 +125,4 @@ protected function hasCookieExpired($cookie) return false; } -} \ No newline at end of file +} diff --git a/src/database/migrations/2022_09_05_111333_drop_ip_address.php b/src/database/migrations/2022_09_05_111333_drop_ip_address.php new file mode 100644 index 0000000..b61d2c4 --- /dev/null +++ b/src/database/migrations/2022_09_05_111333_drop_ip_address.php @@ -0,0 +1,28 @@ +getConnection())->table('metric_visits', function(Blueprint $table) { + $table->dropColumn('ip'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + } +} diff --git a/tests/MetricTestCase.php b/tests/MetricTestCase.php index 85bcb93..84bc516 100755 --- a/tests/MetricTestCase.php +++ b/tests/MetricTestCase.php @@ -24,7 +24,7 @@ abstract class MetricTestCase extends \Orchestra\Testbench\TestCase public function setUp(): void { $this->faker = Factory::create(); - + parent::setUp(); $this->loadLaravelMigrations(['--database' => 'sqlite']); @@ -46,8 +46,8 @@ public function setUp(): void protected function getEnvironmentSetUp($app) { $app['config']->set('app.debug', true); - $app['config']->set('mail.driver', 'log'); - $app['config']->set('session.driver', 'array'); + $app['config']->set('mail.driver', 'log'); + $app['config']->set('session.driver', 'array'); $app['config']->set('database.default', 'sqlite'); $app['config']->set('database.connections.sqlite.database', ':memory:'); $app['config']->set('metrics.logging', true); @@ -94,8 +94,8 @@ protected function generateVisits($number, $timeInterval = '-1 week', $attribute $visits = []; for($x=0;$x<$number;$x++) { $visits[] = $this->makeVisit($attributes, $timeInterval); - } - return new Collection($visits); + } + return new Collection($visits); } // Generate fake visits we can parse for metrics @@ -107,8 +107,8 @@ protected function generateVisitsByDates($number, Carbon $start, Carbon $end , $ for($x=0;$x<$number;$x++) { $attributes['date'] = $faker->dateTimeBetween($start,$end); $visits[] = $this->makeVisit($attributes); - } - return new Collection($visits); + } + return new Collection($visits); } // Generate fake visits and save them in database @@ -137,11 +137,11 @@ protected function createVisitsInTimeInterval(TimeInterval $interval, $number, $ /** * Create Random visit in every subdivision (hour) of the given time interval - * - * @param TimeInterval $interval - * @param integer $number number of visits per hour - * @param array $attributes - * @return void + * + * @param TimeInterval $interval + * @param integer $number number of visits per hour + * @param array $attributes + * @return void */ protected function createVisitsInEveryTimeInterval(TimeInterval $interval, $number, $attributes = []) { @@ -152,7 +152,7 @@ protected function createVisitsInEveryTimeInterval(TimeInterval $interval, $numb /** * Generate some realistic sitemap from a website - * + * * @return array */ protected function getUrlStack() @@ -175,13 +175,13 @@ protected function getUrlStack() '/movies/transpotting', ]; } - + /** * Build a visit record. Attributes overrides generated data - * + * * @param array $attributes manually set some attribute * @param string $startDate the time interval in the past to create the visits - * @return Visit + * @return Visit */ protected function makeVisit(array $attributes = [], $startDate = '-1 year') { @@ -191,7 +191,6 @@ protected function makeVisit(array $attributes = [], $startDate = '-1 year') 'user_id' => null, 'url' => $faker->randomElement($this->getUrlStack()), 'user_agent' => $faker->userAgent, - 'ip' => $faker->randomElement([$faker->ipv4, $faker->ipv6]), 'date' => $faker->dateTimeBetween($startDate), 'cookie' => $faker->sha256, 'referer' => '', @@ -221,7 +220,7 @@ protected function generateRandomUTMData() : array /** * Create a User - * + * * @return App\User */ protected function createTestUser() @@ -237,7 +236,7 @@ protected function createTestUser() /** * Assert all visit are unique to Cookie, useful to validate * some analyzers, consoliders - * + * * @return void */ protected function assertVisitsAreUnique() @@ -269,33 +268,33 @@ protected function dontSeeCookie($cookieName, $response) /** * Get last day as time interval - * + * * @return TimeInterval */ protected function getLastDay() { $start = Carbon::now()->subDay(1)->startOfDay(); $end = Carbon::now()->subDay(1)->endOfDay(); - + return new TimeInterval($start, $end, Metric::DAILY); } /** * Get last month as time interval - * + * * @return TimeInterval */ protected function getLastMonth() { $start = Carbon::now()->subMonth(1)->startOfMonth(); $end = Carbon::now()->subMonth(1)->endOfMonth(); - + return new TimeInterval($start, $end, Metric::MONTHLY); } /** * Get last year as time interval - * + * * @return TimeInterval */ protected function getLastYear() @@ -331,10 +330,10 @@ protected function dontSeeInDatabase($table, $constraints) /** * Create a collection of metric objects, by analyzing every hour * in the time interval - * - * @param AnalyzerInterface $analyzer - * @param TimeInterval $interval - * @param integer $visitCount + * + * @param AnalyzerInterface $analyzer + * @param TimeInterval $interval + * @param integer $visitCount * @return Collection */ protected function createMetrics(AnalyzerInterface $analyzer, TimeInterval $interval, $visitCount) @@ -342,7 +341,7 @@ protected function createMetrics(AnalyzerInterface $analyzer, TimeInterval $inte $metrics = new Collection; $this->createVisitsInTimeInterval($interval, $visitCount); $visits = $this->visits->getByTimeInterval($interval); - + $compiler = new Compiler([$analyzer]); foreach($interval->toHours() as $hour) { diff --git a/tests/VisitRepositoryTest.php b/tests/VisitRepositoryTest.php index 8ff3763..fb61465 100755 --- a/tests/VisitRepositoryTest.php +++ b/tests/VisitRepositoryTest.php @@ -21,7 +21,7 @@ public function we_can_store_a_single_visit() { $visit = $this->makeVisit(); $this->repository->store($visit); - $this->seeInDatabase('metric_visits', ['ip' => $visit->getIp() ]); + $this->assertDatabaseCount('metric_visits', 1); } /** @test */