Opened 5 years ago

Closed 4 years ago

#908 closed New feature (fixed)

Add a hook to allow plugins to define the website ID to load by default when going to stats

Reported by: aaabbb2 Owned by:
Priority: low Milestone: Piwik 0.5.5
Component: Core Keywords: idSite, login, domain, subdomain
Cc: Sensitive: no


I have 1 piwik installation running 3 sites. I have mapped the server alias of stats.domainname to /usr/share/piwik. But, no matter which site I login on, I'm always given the first site I made as my starting idSite.

It would be a really nice enhancement to look at the domainname for the URL and try to find a match in the list of registered sites and select that idSite by default. This would only be done on login. Having this small feature would make setups like mine seem to function just a tiny bit slicker.

Suggested algorithm:

try to find the part before .com or but after www. or stats.
find idSite by soundex() match to list of sites
if not found, choose idSite normally

Attachments (1)

sort_idsites_by_http_host_relevance.diff (2.3 KB) - added by aaabbb2 5 years ago.
Patch to sort the default list of idSites based on the closest match to the current HTTP_HOST env variable.

Download all attachments as: .zip

Change History (11)

comment:1 Changed 5 years ago by vipsoft (robocoder)

  • Milestone set to 2- DigitalVibes

This overlaps #401.

Changed 5 years ago by aaabbb2

Patch to sort the default list of idSites based on the closest match to the current HTTP_HOST env variable.

comment:2 Changed 5 years ago by aaabbb2

I have uploaded a simple brute-force method of identifying the closest match.

It takes the current HTTP_HOST (if any) and iterates over the db results of viewable site ids. It splits on dots, and trims the two comparable hosts to the same level, so that would also match If the matches are more than 1 (so, ignore any matches of just .net or .uk) but also equal to the number of parts less one (so give fudge factor to and then the match is found.

If a match is found, array_unshift it to a new array (so it's at the first position) then push all the other ids back onto this new array.

I've tested this with my two sites, one of which has a subdomain.

so, and are my two sites. But I access both with "" or "". Logging in to either of the "stats" sites will give me my correct idSite in order. It's very handy.

comment:3 Changed 5 years ago by aaabbb2

I feel that this patch also satisfies bug #401 because, in away, it allows the user to select which siteId is chosen by default by opening the piwik installation via a hostname. They *could* make a local entry in their "hosts" file to make "" point to their piwik installation, thereby allowing them to "choose" which siteId comes up by default in their own installation.

comment:4 Changed 5 years ago by vipsoft (robocoder)

  • Sensitive unset


  • #401 contemplates Admin UI changes
  • investigate using cached web site attributes (instead of a db query)
  • would the Public Suffix List simplify the domain matching algorithm?
  • investigate interaction of the Login controller which does a redirect using the Referer, e.g.,

comment:5 Changed 5 years ago by matt (mattab)

aaabbb2 , your patch sounds interesting. However such logic should be in a plugin. We can add a hook that would make it easy for plugins to change the list of website IDs.
For example, we could add a new hook:

Index: core/Controller.php
--- core/Controller.php	(revision 1383)
+++ core/Controller.php	(working copy)
@@ -285,11 +285,20 @@
 	function redirectToIndex($moduleToRedirect, $actionToRedirect)
-		$sitesId = Piwik_SitesManager_API::getSitesIdWithAtLeastViewAccess();
-		if(!empty($sitesId))
+		$defaultWebsiteToLoad = false;
+		Piwik_PostEvent( 'Controller.selectDefaultWebsiteToLoad', $defaultWebsiteToLoad );
+		if(empty($siteIdToLoadByDefault))
-			$firstSiteId = $sitesId[0];
-			$firstSite = new Piwik_Site($firstSiteId);
+    		$sitesId = Piwik_SitesManager_API::getSitesIdWithAtLeastViewAccess();
+    		if(isset($sitesId[0])) 
+    		{
+    		    $defaultWebsiteToLoad = $sitesId[0];
+    		}
+		}
+		if(!empty($defaultWebsiteToLoad))
+		{
+			$firstSite = new Piwik_Site($defaultWebsiteToLoad);
 			if ($firstSite->getCreationDate()->isToday()) 
 				$defaultDate = 'today';

comment:6 Changed 5 years ago by matt (mattab)

  • Summary changed from Automatically select idSite from HTTP_HOST when logging in to Add a hook to allow plugins to define the website ID to load by default when going to stats

comment:7 Changed 5 years ago by aaabbb2

I would suggest a small change to help with naming consistency.

The function Piwik_SitesManager_API::getSitesIdWithAtLeastViewAccess() clearly returns site IDs, but "Piwik_PostEvent( 'Controller.selectDefaultWebsiteToLoad', $defaultWebsiteToLoad )" might suggest that $defaultWebsiteToLoad would be an entire record or object. I would propose using consistent "SiteId" instead of "Website" in the hooks.

Piwik_PostEvent('Controller.selectDefaultSiteIdForIndex', $defaultSiteIdToLoad);

The function Piwik_SitesManager_API::getSitesIdWithAtLeastViewAccess() only returns an array of site IDs, so the original patch had to perform an extra query to retrieve all the information about the sites with view access. It would be nice if the core "redirectToIndex" function loaded all the site records and passed those to the "selectDefaultSiteIdForIndex" or "selectDefaultWebsiteToLoad" so the plugin did not have to decide which sites are in question. The plugin should only concentrate on an algorithm for selecting the best from a list of possible choices ("WithAtLeastViewAccess").

So, perhaps a hook that looks like this:

Piwik_PostEvent('Controller.selectDefaultSiteIdForIndex', $siteObjectsWithViewAccess, $defaultSiteIdToLoad);

comment:8 Changed 5 years ago by matt (mattab)

  • Milestone changed from 2- DigitalVibes to 2 - Piwik 0.5

comment:9 Changed 4 years ago by vipsoft (robocoder)

  • Milestone changed from 1 - Piwik 0.5 to 1 - Piwik 0.5.1

comment:10 Changed 4 years ago by matt (mattab)

  • Resolution set to fixed
  • Status changed from new to closed

(In [1899]) Fixes #908
aaabbb2, I didn't pass the full array of websites info as this has performance impact (by default, Piwik doesn't load this data).

Note: See TracTickets for help on using tickets.