Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not possible to create multiple Piwik tracker instances having different API urls #42

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 41 additions & 21 deletions PiwikTracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class PiwikTracker
* PiwikTracker::$URL = 'http://yourwebsite.org/piwik/';
*
* @var string
* @deprecated use setTrackerUrl instead
*/
static public $URL = '';

Expand Down Expand Up @@ -123,6 +124,7 @@ public function __construct($idSite, $apiUrl = '')
if (!empty($apiUrl)) {
self::$URL = $apiUrl;
}
$this->trackerUrl = $apiUrl;

// Life of the visitor cookie (in sec)
$this->configVisitorCookieTimeout = 33955200; // 13 months (365 + 28 days)
Expand Down Expand Up @@ -158,11 +160,21 @@ public function __construct($idSite, $apiUrl = '')
$this->sendImageResponse = true;

$this->visitorCustomVar = $this->getCustomVariablesFromCookie();

$this->outgoingTrackerCookies = array();
$this->incomingTrackerCookies = array();
}

/**
* Sets (overwrites) the Matomo base URL, for example http://example.org/matomo/.
*
* @param string $trackerUrl
*/
public function setTrackerUrl($trackerUrl)
{
$this->trackerUrl = $trackerUrl;
}

/**
* By default, Piwik expects utf-8 encoded values, for example
* for the page URL parameter values, Page Title, etc.
Expand Down Expand Up @@ -1121,7 +1133,7 @@ public function getUrlTrackAction($actionUrl, $actionType)
*
* Allowed only for Admin/Super User, must be used along with setTokenAuth()
* @see setTokenAuth()
* @param string $dateTime Date with the format 'Y-m-d H:i:s', or a UNIX timestamp.
* @param string $dateTime Date with the format 'Y-m-d H:i:s', or a UNIX timestamp.
* If the datetime is older than one day (default value for tracking_requests_require_authentication_when_custom_timestamp_newer_than), then you must call setTokenAuth() with a valid Admin/Super user token.
* @return $this
*/
Expand Down Expand Up @@ -1581,7 +1593,7 @@ protected function sendRequest($url, $method = 'GET', $data = null, $force = fal
$options[CURLOPT_COOKIE] = http_build_query($this->outgoingTrackerCookies);
$this->outgoingTrackerCookies = array();
}

$ch = curl_init();
curl_setopt_array($ch, $options);
ob_start();
Expand All @@ -1592,7 +1604,7 @@ protected function sendRequest($url, $method = 'GET', $data = null, $force = fal
if (!empty($response)) {
list($header, $content) = explode("\r\n\r\n", $response, $limitCount = 2);
}

$this->parseIncomingCookies(explode("\r\n", $header));

} elseif (function_exists('stream_context_create')) {
Expand All @@ -1619,11 +1631,11 @@ protected function sendRequest($url, $method = 'GET', $data = null, $force = fal
$stream_options['http']['header'] .= 'Cookie: ' . http_build_query($this->outgoingTrackerCookies) . "\r\n";
$this->outgoingTrackerCookies = array();
}

$ctx = stream_context_create($stream_options);
$response = file_get_contents($url, 0, $ctx);
$content = $response;

$this->parseIncomingCookies($http_response_header);
}

Expand All @@ -1646,21 +1658,29 @@ protected function getTimestamp()
*/
protected function getBaseUrl()
{
if (empty(self::$URL)) {
if (empty($this->trackerUrl) && !empty(self::$URL)) {
// for BC
$this->trackerUrl = self::$URL;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think $URL should override $this->trackerUrl? I'm thinking of the following situation:

$tracker = new PiwikTracker('http://mymatomo1.com/piwik.php'); // trackerUrl property set to mymatomo1.com
...
PiwikTracker::$URL = 'http://myothermatomo2.com/piwik.php';
$tracker->doTrackPageView(...); // trackerUrl overrides $URL so mymatomo1 is used instead of myothermatomo2.com

Actually I'm not too sure how to fix this... Maybe there needs to be two properties, $originalTrackerUrl set in the constructor and $trackerUrl set by the method. Where $trackerUrl overrides everything, but if not set, self::$URL is used? This isn't as simple as I thought it was...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'm not sure either. That's why I mentioned BC and being difficult. The problem is the next tracker creation new Tracker() would also call ::$URL = '...' and overwrite that... was thinking to remove self::$URL = ... in constructor but then we regress there. Not sure what is used more than the other... maybe we need to wait for Matomo 4 with this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose we could add an options parameter like this:

$tracker = new PiwikTracker($idSite, 'myurl', [ 'doNotUseGlobalUrl' => true ]);

And maybe issue a warning or notice when doNotUseGlobalUrl isn't set to true?

Then in Matomo 4 we get rid of the option. At least this way, the bug doesn't cause problems in the meantime.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't we need to set it to false by default re BC? Users wouldn't be aware they need to change this parameter when they update (even when we mention it in changelog it would go mainly unnoticed). But at least with the option there be a way to restore old behaviour. Let's see what @mattab thinks

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, would need to be false by default. I think this solution would mostly be for us not running into it, though if we could issue a warning of some kind w/o causing any issues, users might notice.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry but don't know what is the best solution here. But sounds good to throw a warning to help people notice their code might be mis-behaving.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tsteur any new thoughts on this conversation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't do anything before Matomo 4 I reckon

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll remove this from the current sprint.


// for BC
self::$URL = $this->trackerUrl;

if (empty($this->trackerUrl)) {
throw new Exception(
'You must first set the Piwik Tracker URL by calling
PiwikTracker::$URL = \'http://your-website.org/piwik/\';'
);
}
if (strpos(self::$URL, '/piwik.php') === false
&& strpos(self::$URL, '/proxy-piwik.php') === false
&& strpos(self::$URL, '/matomo.php') === false
&& strpos(self::$URL, '/proxy-matomo.php') === false
if (strpos($this->trackerUrl, '/piwik.php') === false
&& strpos($this->trackerUrl, '/proxy-piwik.php') === false
&& strpos($this->trackerUrl, '/matomo.php') === false
&& strpos($this->trackerUrl, '/proxy-matomo.php') === false
) {
self::$URL .= '/piwik.php';
$this->trackerUrl .= '/piwik.php';
}

return self::$URL;
return $this->trackerUrl;
}

/**
Expand Down Expand Up @@ -1883,9 +1903,9 @@ protected static function getCurrentQueryString()
protected static function getCurrentUrl()
{
return self::getCurrentScheme() . '://'
. self::getCurrentHost()
. self::getCurrentScriptName()
. self::getCurrentQueryString();
. self::getCurrentHost()
. self::getCurrentScriptName()
. self::getCurrentQueryString();
}

/**
Expand Down Expand Up @@ -1976,7 +1996,7 @@ public function setOutgoingTrackerCookie($name, $value)
$this->outgoingTrackerCookies[$name] = $value;
}
}

/**
* Gets a cookie which was set by the tracking server.
*
Expand All @@ -1989,7 +2009,7 @@ public function getIncomingTrackerCookie($name)
if (isset($this->incomingTrackerCookies[$name])) {
return $this->incomingTrackerCookies[$name];
}

return false;
}

Expand All @@ -2001,11 +2021,11 @@ public function getIncomingTrackerCookie($name)
protected function parseIncomingCookies($headers)
{
$this->incomingTrackerCookies = array();

if (!empty($headers)) {
$headerName = 'set-cookie:';
$headerNameLength = strlen($headerName);

foreach($headers as $header) {
if (strpos(strtolower($header), $headerName) !== 0) {
continue;
Expand All @@ -2017,7 +2037,7 @@ protected function parseIncomingCookies($headers)
}
parse_str($cookies, $this->incomingTrackerCookies);
}
}
}
}
}

Expand Down