From b089eac9beecc39581701cf7ed55ca119d6ce2cb Mon Sep 17 00:00:00 2001
From: mattab <matthieu.aubry@gmail.com>
Date: Fri, 8 Mar 2013 18:32:40 +1300
Subject: [PATCH] Fixes #3803 New config setting for tracker:

; The window to look back for a previous visit by this current visitor. Defaults to visit_standard_length.
; If you are looking for higher accuracy of "returning visitors" metrics, you may set this value to 86400 or more.
; This is especially useful when you use the Tracking API where tracking Returning Visitors often depends on this setting.
; The value window_look_back_for_visitor is used only if it is set to greater than visit_standard_length
window_look_back_for_visitor = 0
---
 config/global.ini.php  |  6 ++++++
 core/Tracker/Visit.php | 25 +++++++++++++++++++++++--
 2 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/config/global.ini.php b/config/global.ini.php
index a7504287df..73294cfb62 100644
--- a/config/global.ini.php
+++ b/config/global.ini.php
@@ -356,6 +356,12 @@ record_statistics			= 1
 ; after his last page view, it will be recorded as a new visit
 visit_standard_length       = 1800
 
+; The window to look back for a previous visit by this current visitor. Defaults to visit_standard_length.
+; If you are looking for higher accuracy of "returning visitors" metrics, you may set this value to 86400 or more.
+; This is especially useful when you use the Tracking API where tracking Returning Visitors often depends on this setting.
+; The value window_look_back_for_visitor is used only if it is set to greater than visit_standard_length
+window_look_back_for_visitor = 0
+
 ; visitors that stay on the website and view only one page will be considered as time on site of 0 second
 default_time_one_page_visit = 0
 
diff --git a/core/Tracker/Visit.php b/core/Tracker/Visit.php
index c5c3032bd6..0f6f2bcc60 100644
--- a/core/Tracker/Visit.php
+++ b/core/Tracker/Visit.php
@@ -1169,8 +1169,9 @@ class Piwik_Tracker_Visit implements Piwik_Tracker_Visit_Interface
 		$from = "FROM ".Piwik_Common::prefixTable('log_visit');
 
 		$bindSql = array();
-		
-		$timeLookBack = date('Y-m-d H:i:s', $this->getCurrentTimestamp() - Piwik_Config::getInstance()->Tracker['visit_standard_length']);
+
+
+        $timeLookBack = $this->getWindowLookupPreviousVisit();
 
         $shouldMatchOneFieldOnly = $this->shouldLookupOneVisitorFieldOnly($isVisitorIdToLookup);
 
@@ -1313,6 +1314,26 @@ class Piwik_Tracker_Visit implements Piwik_Tracker_Visit_Interface
 		}
 	}
 
+    /**
+     * By default, we look back 30 minutes to find a previous visitor (for performance reasons).
+     * In some cases, it is useful to look back and count unique visitors more accurately. You can set custom lookback window in
+     * [Tracker] window_look_back_for_visitor
+     *
+     * @return string
+     *
+     */
+    protected function getWindowLookupPreviousVisit()
+    {
+        $lookbackNSeconds = Piwik_Config::getInstance()->Tracker['visit_standard_length'];
+
+        $lookbackNSecondsCustom = Piwik_Config::getInstance()->Tracker['window_look_back_for_visitor'];
+        if ($lookbackNSecondsCustom > $lookbackNSeconds) {
+            $lookbackNSeconds = $lookbackNSecondsCustom;
+        }
+        $timeLookBack = date('Y-m-d H:i:s', $this->getCurrentTimestamp() - $lookbackNSeconds);
+        return $timeLookBack;
+    }
+
     protected function shouldLookupOneVisitorFieldOnly($isVisitorIdToLookup)
     {
         // This setting would be enabled for Intranet websites, to ensure that visitors using all the same computer config, same IP
-- 
GitLab