Skip to content

Commit

Permalink
Fixes #5123 PHP Tracker, added new method setForceNewVisit() to force…
Browse files Browse the repository at this point in the history
… creation of a new visit + with test

Close #218
  • Loading branch information
mattab committed May 9, 2014
1 parent 7e617f1 commit 1600e34
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 9 deletions.
20 changes: 20 additions & 0 deletions libs/PiwikTracker/PiwikTracker.php
Expand Up @@ -170,6 +170,7 @@ function __construct($idSite, $apiUrl = '')
$this->eventCustomVar = false;
$this->customData = false;
$this->forcedDatetime = false;
$this->forcedNewVisit = false;
$this->token_auth = false;
$this->attributionInfo = false;
$this->ecommerceLastOrderTimestamp = false;
Expand Down Expand Up @@ -902,6 +903,21 @@ public function setForceVisitDateTime($dateTime)
$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
*
Expand Down Expand Up @@ -1274,6 +1290,7 @@ protected function getRequest($idSite)
(!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
Expand Down Expand Up @@ -1324,6 +1341,9 @@ protected function getRequest($idSite)
$this->pageCustomVar = false;
$this->eventCustomVar = false;

// force new visit only once, user must call again setForceNewVisit()
$this->forcedNewVisit = false;

return $url;
}

Expand Down
6 changes: 6 additions & 0 deletions tests/PHPUnit/Fixtures/SomeVisitsAllConversions.php
Expand Up @@ -84,5 +84,11 @@ private function trackVisits()
// 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.');
}
}
Expand Up @@ -43,7 +43,10 @@ public function testCheck()

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))
Expand Down
Expand Up @@ -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>
Expand Down
@@ -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>
2 changes: 1 addition & 1 deletion tests/PHPUnit/UI
Submodule UI updated from 579b7c to 8c15fe

0 comments on commit 1600e34

Please sign in to comment.