From 1600e344012362cd19c1791fe89ecc85256319e4 Mon Sep 17 00:00:00 2001
From: mattab <matthieu.aubry@gmail.com>
Date: Fri, 9 May 2014 20:11:47 +1200
Subject: [PATCH] Fixes  #5123 PHP Tracker, added new method setForceNewVisit()
 to force creation of a new visit + with test

Close https://github.com/piwik/piwik/pull/218
---
 libs/PiwikTracker/PiwikTracker.php            | 20 +++++++++++++++++++
 .../Fixtures/SomeVisitsAllConversions.php     |  6 ++++++
 ...s_AllowMultipleConversionsPerVisitTest.php |  5 ++++-
 ...e.getVisitInformationPerServerTime_day.xml |  6 +++---
 ...ersionsPerVisit__VisitsSummary.get_day.xml |  8 ++++----
 tests/PHPUnit/UI                              |  2 +-
 6 files changed, 38 insertions(+), 9 deletions(-)

diff --git a/libs/PiwikTracker/PiwikTracker.php b/libs/PiwikTracker/PiwikTracker.php
index a62feb7a8b..95c10bf676 100644
--- a/libs/PiwikTracker/PiwikTracker.php
+++ b/libs/PiwikTracker/PiwikTracker.php
@@ -170,6 +170,7 @@ class PiwikTracker
         $this->eventCustomVar = false;
         $this->customData = false;
         $this->forcedDatetime = false;
+        $this->forcedNewVisit = false;
         $this->token_auth = false;
         $this->attributionInfo = false;
         $this->ecommerceLastOrderTimestamp = false;
@@ -902,6 +903,21 @@ class PiwikTracker
         $this->forcedDatetime = $dateTime;
     }
 
+    /**
+     * Forces Piwik to create a new visit for the tracking request.
+     *
+     * By default, Piwik will create a new visit if the last request by this user was more than 30 minutes ago.
+     * If you call setForceNewVisit() before calling doTrack*, then a new visit will be created for this request.
+     *
+     * Allowed only for Super User, must be used along with setTokenAuth()
+     *
+     * @see setTokenAuth()
+     */
+    public function setForceNewVisit()
+    {
+        $this->forcedNewVisit = true;
+    }
+
     /**
      * Overrides IP address
      *
@@ -1274,6 +1290,7 @@ class PiwikTracker
             (!empty($this->ip) ? '&cip=' . $this->ip : '') .
             (!empty($this->forcedVisitorId) ? '&cid=' . $this->forcedVisitorId : '&_id=' . $this->getVisitorId()) .
             (!empty($this->forcedDatetime) ? '&cdt=' . urlencode($this->forcedDatetime) : '') .
+            (!empty($this->forcedNewVisit) ? '&new_visit=1' : '') .
             ((!empty($this->token_auth) && !$this->doBulkRequests) ? '&token_auth=' . urlencode($this->token_auth) : '') .
 
             // Values collected from cookie
@@ -1324,6 +1341,9 @@ class PiwikTracker
         $this->pageCustomVar = false;
         $this->eventCustomVar = false;
 
+        // force new visit only once, user must call again setForceNewVisit()
+        $this->forcedNewVisit = false;
+
         return $url;
     }
 
diff --git a/tests/PHPUnit/Fixtures/SomeVisitsAllConversions.php b/tests/PHPUnit/Fixtures/SomeVisitsAllConversions.php
index 6aa1935682..a18eec7b97 100644
--- a/tests/PHPUnit/Fixtures/SomeVisitsAllConversions.php
+++ b/tests/PHPUnit/Fixtures/SomeVisitsAllConversions.php
@@ -84,5 +84,11 @@ class Piwik_Test_Fixture_SomeVisitsAllConversions extends Fixture
         // 1st goal should Now be tracked
         $t->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.61)->getDatetime());
         self::checkResponse($t->doTrackGoal($idGoal_OneConversionPerVisit, $revenue = 656));
+
+        // few minutes later, create a new_visit
+        $t->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.7)->getDatetime());
+        $t->setTokenAuth($this->getTokenAuth());
+        $t->setForceNewVisit();
+        $t->doTrackPageView('This is tracked in a new visit.');
     }
 }
diff --git a/tests/PHPUnit/Integration/TrackGoals_AllowMultipleConversionsPerVisitTest.php b/tests/PHPUnit/Integration/TrackGoals_AllowMultipleConversionsPerVisitTest.php
index dd2ea9c17d..32ee70fadb 100755
--- a/tests/PHPUnit/Integration/TrackGoals_AllowMultipleConversionsPerVisitTest.php
+++ b/tests/PHPUnit/Integration/TrackGoals_AllowMultipleConversionsPerVisitTest.php
@@ -43,7 +43,10 @@ class Test_Piwik_Integration_TrackGoals_AllowMultipleConversionsPerVisit extends
 
     public function getApiForTesting()
     {
-        $apiToCall = array('VisitTime.getVisitInformationPerServerTime', 'VisitsSummary.get');
+        $apiToCall = array(
+            'VisitTime.getVisitInformationPerServerTime',
+            'VisitsSummary.get'
+        );
 
         return array(
             array($apiToCall, array('idSite' => self::$fixture->idSite, 'date' => self::$fixture->dateTime))
diff --git a/tests/PHPUnit/Integration/expected/test_trackGoals_allowMultipleConversionsPerVisit__VisitTime.getVisitInformationPerServerTime_day.xml b/tests/PHPUnit/Integration/expected/test_trackGoals_allowMultipleConversionsPerVisit__VisitTime.getVisitInformationPerServerTime_day.xml
index 69701668a6..38b1c4d247 100644
--- a/tests/PHPUnit/Integration/expected/test_trackGoals_allowMultipleConversionsPerVisit__VisitTime.getVisitInformationPerServerTime_day.xml
+++ b/tests/PHPUnit/Integration/expected/test_trackGoals_allowMultipleConversionsPerVisit__VisitTime.getVisitInformationPerServerTime_day.xml
@@ -3,11 +3,11 @@
 	<row>
 		<label>0h</label>
 		<nb_uniq_visitors>1</nb_uniq_visitors>
-		<nb_visits>1</nb_visits>
-		<nb_actions>1</nb_actions>
+		<nb_visits>2</nb_visits>
+		<nb_actions>2</nb_actions>
 		<max_actions>1</max_actions>
 		<sum_visit_length>1120</sum_visit_length>
-		<bounce_count>1</bounce_count>
+		<bounce_count>2</bounce_count>
 		<goals>
 			<row idgoal='1'>
 				<nb_conversions>2</nb_conversions>
diff --git a/tests/PHPUnit/Integration/expected/test_trackGoals_allowMultipleConversionsPerVisit__VisitsSummary.get_day.xml b/tests/PHPUnit/Integration/expected/test_trackGoals_allowMultipleConversionsPerVisit__VisitsSummary.get_day.xml
index b0adeb0ca1..6a1390c2e5 100644
--- a/tests/PHPUnit/Integration/expected/test_trackGoals_allowMultipleConversionsPerVisit__VisitsSummary.get_day.xml
+++ b/tests/PHPUnit/Integration/expected/test_trackGoals_allowMultipleConversionsPerVisit__VisitsSummary.get_day.xml
@@ -1,13 +1,13 @@
 <?xml version="1.0" encoding="utf-8" ?>
 <result>
 	<nb_uniq_visitors>1</nb_uniq_visitors>
-	<nb_visits>1</nb_visits>
-	<nb_actions>1</nb_actions>
+	<nb_visits>2</nb_visits>
+	<nb_actions>2</nb_actions>
 	<nb_visits_converted>1</nb_visits_converted>
-	<bounce_count>1</bounce_count>
+	<bounce_count>2</bounce_count>
 	<sum_visit_length>1120</sum_visit_length>
 	<max_actions>1</max_actions>
 	<bounce_rate>100%</bounce_rate>
 	<nb_actions_per_visit>1</nb_actions_per_visit>
-	<avg_time_on_site>1120</avg_time_on_site>
+	<avg_time_on_site>560</avg_time_on_site>
 </result>
\ No newline at end of file
diff --git a/tests/PHPUnit/UI b/tests/PHPUnit/UI
index 579b7c4c92..8c15feb00d 160000
--- a/tests/PHPUnit/UI
+++ b/tests/PHPUnit/UI
@@ -1 +1 @@
-Subproject commit 579b7c4c928800115d35e6474e0c43c29a8e1c97
+Subproject commit 8c15feb00dd40880d1872680cb84b330fd56c6ff
-- 
GitLab