After we
brought our users to subdomains , we wanted to have detailed statistics for each subdomain separately, and also to be able to provide these statistics to the user. Of course, you can manually connect all subdomains to Google Analytics / Ya.Metrika, but when there are a lot of subdomains, this task starts to look unnecessarily difficult.
I wanted some kind of automated solution, and it was found in the form of a statistics system with open source
Piwik . This system can be installed on your server, add your site to it and use it. It provides functionality, mostly similar to GA and J.Metrika, + you can write your own plug-ins (for example, I developed a yesterday-today-forecast widget for myself, similar in functionality to the same block from Yandex.metrics).
First, for tests, the system kept statistics on 1 domain and 1 subdomain, and coped with it quite successfully, completely replacing Ya.metrika and GA for me. Accordingly, it was decided to use the Piwik API to implement automatic connection of statistics to user subdomains. The following was required:
- Create a user in the Piwik system
- Create a site in Pivik
- Give user rights to view site statistics
- Get the site id to set the counter
I will tell you a little about Piwik API. It works in this way: the whole Piwik consists of modules, and some modules provide API functions. Any developer can write a module and implement some API function in it, which can then be called.
The call of API functions is easiest to implement by calling to a specific address, like this:
example.com/piwik/index.php?module=API&method=UsersManager.getUsersLogin&format=PHP&prettyDisplay=true&token_auth=14e1b600b1fd579f47433b88e8d85291In this line,
example.com/piwik/index.php is the path to our installed pivik,
method = UsersManager.getUsersLogin is a module and function that we call,
format = PHP means that we need to get data in the form of a serialized array for use in PHP ,
prettyDisplay = true is a parameter that accepts an API function and
token_auth is our token, which we can get in our pivik on the user management page or on the page with a list of API functions.
')
Accordingly, the easiest way to access the Piwik API from PHP looks like this:
$url = 'http://example.com/piwik/index.php?module=API&method=UsersManager.getUsersLogin&format=PHP&prettyDisplay=true&token_auth=14e1b600b1fd579f47433b88e8d85291' $tmp = file_get_contents($url); $response = unserialize($tmp);
As a result, we get an array of $ response with all the data that Piwik returned to us.
We now return to our problem with user statistics. For all our tasks, the Piwik API has corresponding functions:
UsersManager.addUser to create a new user accepts username, password and user e-mail as parameters
SitesManager.addSite to create a site, takes in the form of parameters the name and url of the site, returns the id of the site in the system
UsersManager.setUserAccess for issuing permissions, accepts user login, site id and access rights as parameters (no-access / view / admin)
I didn’t want to manually execute file_get_contents and unserialize for each request, so I wrote a small wrapper class for Piwik API, in which I implemented methods for creating a user / site and issuing rights.
<?php Class Piwik_api { private $sAuthToken; private $sPiwikUrl; function __construct($sAuthToken, $sPiwikUrl) { $this->sPiwikUrl = $sPiwikUrl; $this->sAuthToken = $sAuthToken; } function execute($sMethodName, $arData, $sFormat = "PHP") { $url = $this->sPiwikUrl; $url .= "?module=API&method=" . $sMethodName; $url .= '&'.http_build_query($arData); $url .= "&format=$sFormat"; $url .= "&token_auth=" . $this->sAuthToken; $tmp = file_get_contents($url); $response = unserialize($tmp); return $response; } function addUser($sUserName, $sUserPassword, $sUserEmail) { return $this->execute('UsersManager.addUser', array("userLogin" => $sUserName, "password" => $sUserPassword, "email" => $sUserEmail )); } function addSite($sSiteName, $sUrl) { return $this->execute('SitesManager.addSite', array("siteName" => $sSiteName, "urls" => $sUrl )); } function setUserAccess($sUserLogin, $sAccess, $iSiteId) { return $this->execute('UsersManager.setUserAccess', array("userLogin" => $sUserLogin, "access" => $sAccess, "idSites" => $iSiteId )); } }
The addSite method returns a number - this is the site id in the pivik system.
Accordingly, using this class, the original problem is solved trivially: just simultaneously with the generation of a user subdomain, we create a user in the Piwik system, create a website and issue rights, and then the user already issues a link to Piwik and his login and password to view the statistics. Like that:
$oPiwikApi = new Piwik_api(PIWIK_AUTH_TOKEN, PIWIK_URL); $oPiwikApi->addUser($sLogin, $sPassword, $sEmail); $iPiwikSiteId = $oPiwikApi->addSite($sSiteName, $sUrl); $oPiwikApi->setUserAccess($sLogin, "view", $iPiwikSiteId);
There may be a question of confidence in the statistics - for completely untrusting users, we left the ability to manually connect the GA and Ya. Metrics, and Piwik was made the system for everyone by default.
After all these manipulations, users received statistics for their subdomains, completely similar in functionality to GA / Ya. Metric systems, including the entire conversion history, visitors, etc., and each user only sees statistics for his or her domain.
UPD:A review article about the Pivik himself is
already in Habré, this article is rather intended to show how easy and convenient it is to work with it. By the way, now I discovered that my problem was discussed
in the comments to that article.
The execute method is rewritten using http_build_query.