diff --git a/libs/PiwikTracker/PiwikTracker.php b/libs/PiwikTracker/PiwikTracker.php index a62feb7a8b8ee3d3d9dfe2810a6361310bbe1d95..95c10bf676dd6dbaeaac6ac64d98635bb4b26120 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 6aa193568280596daf760214a8db4993bcc2503e..a18eec7b97e4b2a345154bfddffa6babe0dda24f 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 dd2ea9c17ddab14a94f5c4df856a58e28f964c97..32ee70fadbbc9bceed027ac4b605b75cc45b7925 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 69701668a6890a085915c0809ba973867c1785df..38b1c4d24766c71323011f19995b93e77db119c0 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 b0adeb0ca1101290e8617ffbebd5f227f52ecf8d..6a1390c2e5be8e2f3ec7a144922c41f97135d3f2 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 579b7c4c928800115d35e6474e0c43c29a8e1c97..8c15feb00dd40880d1872680cb84b330fd56c6ff 160000 --- a/tests/PHPUnit/UI +++ b/tests/PHPUnit/UI @@ -1 +1 @@ -Subproject commit 579b7c4c928800115d35e6474e0c43c29a8e1c97 +Subproject commit 8c15feb00dd40880d1872680cb84b330fd56c6ff