From 7156a97e60fa579da8ae2ec1e578aaf803f8f96d Mon Sep 17 00:00:00 2001 From: benakamoorthi <benaka.moorthi@gmail.com> Date: Thu, 15 Nov 2012 23:56:03 +0000 Subject: [PATCH] Fixes #3481, added extra tracking query parameters: lat, long, city, region, country. git-svn-id: http://dev.piwik.org/svn/trunk@7480 59fd770c-687e-43c8-a1e3-f5a4ff64c105 --- core/Tracker/Visit.php | 19 ++++++ libs/PiwikTracker/PiwikTracker.php | 62 +++++++++++++++++++ .../ManyVisitorsOneWebsiteTest.php | 19 ++++++ ...WebsiteTest__UserCountry.getCity_month.xml | 26 ++++++++ ...teTest__UserCountry.getContinent_month.xml | 18 +++--- ...siteTest__UserCountry.getCountry_month.xml | 22 +++++++ ...try.getNumberOfDistinctCountries_month.xml | 2 +- ...bsiteTest__UserCountry.getRegion_month.xml | 23 +++++++ 8 files changed, 181 insertions(+), 10 deletions(-) diff --git a/core/Tracker/Visit.php b/core/Tracker/Visit.php index d0afdf0a65..1dd055f561 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 ce94f2649a..ffb513a206 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 a72427b302..34c7699d8b 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 690e22f3f7..f6d9a457d0 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 6a7449fde3..46931a505c 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 55a6de0d07..93c5f353c9 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 e52ca0ecb1..39535fab28 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 830b01300f..7ebddb4081 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 -- GitLab