diff --git a/core/Tracker/Visit.php b/core/Tracker/Visit.php index d0afdf0a65d2fa0aeceadc663315830014f88bbe..1dd055f56164aec6b28cc0d1346afb35d08e9a81 100644 --- a/core/Tracker/Visit.php +++ b/core/Tracker/Visit.php @@ -608,6 +608,25 @@ class Piwik_Tracker_Visit implements Piwik_Tracker_Visit_Interface $userInfo = array('lang' => $browserLang, 'ip' => Piwik_IP::N2P($this->getVisitorIp())); Piwik_PostEvent('Tracker.getVisitorLocation', $location, $userInfo); + // check for location override query parameters (ie, lat, long, country, region, city) + $locationOverrideParams = array( + 'country' => array('string', Piwik_UserCountry_LocationProvider::COUNTRY_CODE_KEY), + 'region' => array('string', Piwik_UserCountry_LocationProvider::REGION_CODE_KEY), + 'city' => array('string', Piwik_UserCountry_LocationProvider::CITY_NAME_KEY), + 'lat' => array('float', Piwik_UserCountry_LocationProvider::LATITUDE_KEY), + 'long' => array('float', Piwik_UserCountry_LocationProvider::LONGITUDE_KEY), + ); + foreach ($locationOverrideParams as $queryParamName => $info) + { + list($type, $locationResultKey) = $info; + + $value = Piwik_Common::getRequestVar($queryParamName, false, $type, $this->request); + if (!empty($value)) + { + $location[$locationResultKey] = $value; + } + } + if (empty($location['country_code'])) // sanity check { $location['country_code'] = self::UNKNOWN_CODE; diff --git a/libs/PiwikTracker/PiwikTracker.php b/libs/PiwikTracker/PiwikTracker.php index ce94f2649a82664c29560f93224f72d65465a6a8..ffb513a2066b3bbd18129f62f17b3ba768fe3dd0 100644 --- a/libs/PiwikTracker/PiwikTracker.php +++ b/libs/PiwikTracker/PiwikTracker.php @@ -251,6 +251,61 @@ class PiwikTracker $this->userAgent = $userAgent; } + /** + * Sets the country of the visitor. If not used, Piwik will try to find the country + * using either the visitor's IP address or language. + * + * @param string $country + */ + public function setCountry($country) + { + $this->country = $country; + } + + /** + * Sets the region of the visitor. If not used, Piwik may try to find the region + * using the visitor's IP address (if configured to do so). + * + * @param string $region + */ + public function setRegion($region) + { + $this->region = $region; + } + + /** + * Sets the city of the visitor. If not used, Piwik may try to find the city + * using the visitor's IP address (if configured to do so). + * + * @param string $city + */ + public function setCity($city) + { + $this->city = $city; + } + + /** + * Sets the latitude of the visitor. If not used, Piwik may try to find the visitor's + * latitude using the visitor's IP address (if configured to do so). + * + * @param float $lat + */ + public function setLatitude($lat) + { + $this->lat = $lat; + } + + /** + * Sets the longitude of the visitor. If not used, Piwik may try to find the visitor's + * longitude using the visitor's IP address (if configured to do so). + * + * @param float $long + */ + public function setLongitude($long) + { + $this->long = $long; + } + /** * Enables the bulk request feature. When used, each tracking action is stored until the * doBulkTrack method is called. This method will send all tracking data at once. @@ -993,6 +1048,13 @@ class PiwikTracker (!empty($this->attributionInfo[2]) ? '&_refts=' . $this->attributionInfo[2] : '') . // Referrer URL (!empty($this->attributionInfo[3]) ? '&_ref=' . urlencode($this->attributionInfo[3]) : '') . + + // custom location info + (!empty($this->country) ? '&country='.urlencode($this->country) : '') . + (!empty($this->region) ? '®ion='.urlencode($this->region) : '') . + (!empty($this->city) ? '&city='.urlencode($this->city) : '') . + (!empty($this->lat) ? '&lat='.urlencode($this->lat) : '') . + (!empty($this->long) ? '&long='.urlencode($this->long) : '') . // DEBUG $this->DEBUG_APPEND_URL diff --git a/tests/PHPUnit/Integration/ManyVisitorsOneWebsiteTest.php b/tests/PHPUnit/Integration/ManyVisitorsOneWebsiteTest.php index a72427b3021923b0a6d919d393362c646f307038..34c7699d8b2759d015e860a9d085a3a5d9caf909 100755 --- a/tests/PHPUnit/Integration/ManyVisitorsOneWebsiteTest.php +++ b/tests/PHPUnit/Integration/ManyVisitorsOneWebsiteTest.php @@ -53,6 +53,8 @@ class Test_Piwik_Integration_ManyVisitorsOneWebsiteTest extends IntegrationTestC self::setLocationProvider('GeoIP.dat'); self::trackVisits(2, true); + + self::trackOtherVisits(); } catch(Exception $e) { // Skip whole test suite if an error occurs while setup throw new PHPUnit_Framework_SkippedTestSuiteError($e->getMessage()); @@ -165,6 +167,23 @@ class Test_Piwik_Integration_ManyVisitorsOneWebsiteTest extends IntegrationTestC } } + protected static function trackOtherVisits() + { + $dateTime = self::$dateTime; + $idSite = self::$idSite; + + $t = self::getTracker($idSite, $dateTime, $defaultInit = true); + $t->setForceVisitDateTime(Piwik_Date::factory($dateTime)->addDay(20)->getDatetime()); + $t->setIp('194.57.91.215'); + $t->setCountry('us'); + $t->setRegion('CA'); + $t->setCity('not a city'); + $t->setLatitude(1); + $t->setLongitude(2); + $t->setUrl("http://piwik.net/grue/lair"); + self::checkResponse($t->doTrackPageView('It\'s pitch black...')); + } + public static function setLocationProvider( $file ) { Piwik_UserCountry_LocationProvider_GeoIp::$dbNames['loc'] = array($file); diff --git a/tests/PHPUnit/Integration/expected/test_ManyVisitorsOneWebsiteTest__UserCountry.getCity_month.xml b/tests/PHPUnit/Integration/expected/test_ManyVisitorsOneWebsiteTest__UserCountry.getCity_month.xml index 690e22f3f71d6c9e5a44a4aedd8784fff8929ad3..f6d9a457d07df82b44160027defea848c47bd15f 100755 --- a/tests/PHPUnit/Integration/expected/test_ManyVisitorsOneWebsiteTest__UserCountry.getCity_month.xml +++ b/tests/PHPUnit/Integration/expected/test_ManyVisitorsOneWebsiteTest__UserCountry.getCity_month.xml @@ -246,4 +246,30 @@ <region_name>Kent</region_name> <logo>plugins/UserCountry/flags/gb.png</logo> </row> + <row> + <label>not a city, California, United States</label> + <nb_visits>1</nb_visits> + <nb_actions>1</nb_actions> + <max_actions>1</max_actions> + <sum_visit_length>0</sum_visit_length> + <bounce_count>1</bounce_count> + <goals> + <row idgoal='1'> + <nb_conversions>1</nb_conversions> + <nb_visits_converted>1</nb_visits_converted> + <revenue>5</revenue> + </row> + </goals> + <nb_conversions>1</nb_conversions> + <revenue>5</revenue> + <sum_daily_nb_uniq_visitors>1</sum_daily_nb_uniq_visitors> + <lat>1</lat> + <long>2</long> + <city_name>not a city</city_name> + <region>CA</region> + <country>us</country> + <country_name>United States</country_name> + <region_name>California</region_name> + <logo>plugins/UserCountry/flags/us.png</logo> + </row> </result> \ No newline at end of file diff --git a/tests/PHPUnit/Integration/expected/test_ManyVisitorsOneWebsiteTest__UserCountry.getContinent_month.xml b/tests/PHPUnit/Integration/expected/test_ManyVisitorsOneWebsiteTest__UserCountry.getContinent_month.xml index 6a7449fde3de922f11dd26862637d35f616e670d..46931a505c6796db481f74f38bd8b3d3a52cb87c 100755 --- a/tests/PHPUnit/Integration/expected/test_ManyVisitorsOneWebsiteTest__UserCountry.getContinent_month.xml +++ b/tests/PHPUnit/Integration/expected/test_ManyVisitorsOneWebsiteTest__UserCountry.getContinent_month.xml @@ -21,21 +21,21 @@ </row> <row> <label>North America</label> - <nb_visits>6</nb_visits> - <nb_actions>6</nb_actions> + <nb_visits>7</nb_visits> + <nb_actions>7</nb_actions> <max_actions>1</max_actions> <sum_visit_length>0</sum_visit_length> - <bounce_count>6</bounce_count> + <bounce_count>7</bounce_count> <goals> <row idgoal='1'> - <nb_conversions>6</nb_conversions> - <nb_visits_converted>6</nb_visits_converted> - <revenue>30</revenue> + <nb_conversions>7</nb_conversions> + <nb_visits_converted>7</nb_visits_converted> + <revenue>35</revenue> </row> </goals> - <nb_conversions>6</nb_conversions> - <revenue>30</revenue> - <sum_daily_nb_uniq_visitors>3</sum_daily_nb_uniq_visitors> + <nb_conversions>7</nb_conversions> + <revenue>35</revenue> + <sum_daily_nb_uniq_visitors>4</sum_daily_nb_uniq_visitors> <code>North America</code> </row> <row> diff --git a/tests/PHPUnit/Integration/expected/test_ManyVisitorsOneWebsiteTest__UserCountry.getCountry_month.xml b/tests/PHPUnit/Integration/expected/test_ManyVisitorsOneWebsiteTest__UserCountry.getCountry_month.xml index 55a6de0d070db9e7f228a3982e386a3acc9c07c4..93c5f353c9bad2c6b77622bd705d613e6ef1aa32 100755 --- a/tests/PHPUnit/Integration/expected/test_ManyVisitorsOneWebsiteTest__UserCountry.getCountry_month.xml +++ b/tests/PHPUnit/Integration/expected/test_ManyVisitorsOneWebsiteTest__UserCountry.getCountry_month.xml @@ -176,4 +176,26 @@ <logoWidth>16</logoWidth> <logoHeight>11</logoHeight> </row> + <row> + <label>United States</label> + <nb_visits>1</nb_visits> + <nb_actions>1</nb_actions> + <max_actions>1</max_actions> + <sum_visit_length>0</sum_visit_length> + <bounce_count>1</bounce_count> + <goals> + <row idgoal='1'> + <nb_conversions>1</nb_conversions> + <nb_visits_converted>1</nb_visits_converted> + <revenue>5</revenue> + </row> + </goals> + <nb_conversions>1</nb_conversions> + <revenue>5</revenue> + <sum_daily_nb_uniq_visitors>1</sum_daily_nb_uniq_visitors> + <code>us</code> + <logo>plugins/UserCountry/flags/us.png</logo> + <logoWidth>16</logoWidth> + <logoHeight>11</logoHeight> + </row> </result> \ No newline at end of file diff --git a/tests/PHPUnit/Integration/expected/test_ManyVisitorsOneWebsiteTest__UserCountry.getNumberOfDistinctCountries_month.xml b/tests/PHPUnit/Integration/expected/test_ManyVisitorsOneWebsiteTest__UserCountry.getNumberOfDistinctCountries_month.xml index e52ca0ecb15aabd3c0978ada9f4886e6895a47db..39535fab28672cf21343002d60b0bcc5edfcb9d0 100755 --- a/tests/PHPUnit/Integration/expected/test_ManyVisitorsOneWebsiteTest__UserCountry.getNumberOfDistinctCountries_month.xml +++ b/tests/PHPUnit/Integration/expected/test_ManyVisitorsOneWebsiteTest__UserCountry.getNumberOfDistinctCountries_month.xml @@ -1,2 +1,2 @@ <?xml version="1.0" encoding="utf-8" ?> -<result>8</result> \ No newline at end of file +<result>9</result> \ No newline at end of file diff --git a/tests/PHPUnit/Integration/expected/test_ManyVisitorsOneWebsiteTest__UserCountry.getRegion_month.xml b/tests/PHPUnit/Integration/expected/test_ManyVisitorsOneWebsiteTest__UserCountry.getRegion_month.xml index 830b01300fa07df9633594d8998744dc18419b07..7ebddb4081406ce41ddbd54a6eca61d0bf2d3e3c 100755 --- a/tests/PHPUnit/Integration/expected/test_ManyVisitorsOneWebsiteTest__UserCountry.getRegion_month.xml +++ b/tests/PHPUnit/Integration/expected/test_ManyVisitorsOneWebsiteTest__UserCountry.getRegion_month.xml @@ -184,4 +184,27 @@ <region_name>London, City of</region_name> <logo>plugins/UserCountry/flags/gb.png</logo> </row> + <row> + <label>California, United States</label> + <nb_visits>1</nb_visits> + <nb_actions>1</nb_actions> + <max_actions>1</max_actions> + <sum_visit_length>0</sum_visit_length> + <bounce_count>1</bounce_count> + <goals> + <row idgoal='1'> + <nb_conversions>1</nb_conversions> + <nb_visits_converted>1</nb_visits_converted> + <revenue>5</revenue> + </row> + </goals> + <nb_conversions>1</nb_conversions> + <revenue>5</revenue> + <sum_daily_nb_uniq_visitors>1</sum_daily_nb_uniq_visitors> + <region>CA</region> + <country>us</country> + <country_name>United States</country_name> + <region_name>California</region_name> + <logo>plugins/UserCountry/flags/us.png</logo> + </row> </result> \ No newline at end of file