Skip to content

Commit ac3e26b

Browse files
author
Matthieu Aubry
committed
Merge pull request matomo-org#15 from piwik/customparameters
Added possibility to set any custom tracking parameters for 3rd party plugins
2 parents fcbbc83 + d4e25a1 commit ac3e26b

1 file changed

Lines changed: 34 additions & 1 deletion

File tree

PiwikTracker.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public function __construct($idSite, $apiUrl = '')
8989
$this->forcedNewVisit = false;
9090
$this->generationTime = false;
9191
$this->pageCustomVar = false;
92+
$this->customParameters = array();
9293
$this->customData = false;
9394
$this->hasCookies = false;
9495
$this->token_auth = false;
@@ -308,6 +309,28 @@ public function clearCustomVariables()
308309
$this->eventCustomVar = array();
309310
}
310311

312+
/**
313+
* Sets a custom tracking parameter. This is useful if you need to send any tracking parameters for a 3rd party
314+
* plugin that is not shipped with Piwik itself. Please note that custom parameters are cleared after each
315+
* tracking request.
316+
*
317+
* @param string $trackingApiParameter The name of the tracking API parameter, eg 'dimension1'
318+
* @param string $value Tracking parameter value that shall be sent for this tracking parameter.
319+
* @throws Exception
320+
*/
321+
public function setCustomTrackingParameter($trackingApiParameter, $value)
322+
{
323+
$this->customParameters[$trackingApiParameter] = $value;
324+
}
325+
326+
/**
327+
* Clear / reset all previously set custom tracking parameters.
328+
*/
329+
public function clearCustomTrackingParameters()
330+
{
331+
$this->customParameters = array();
332+
}
333+
311334
/**
312335
* Sets the current visitor ID to a random new one.
313336
*/
@@ -1396,6 +1419,7 @@ protected function sendRequest($url, $method = 'GET', $data = null, $force = fal
13961419

13971420
// Clear custom variables so they don't get copied over to other users in the bulk request
13981421
$this->clearCustomVariables();
1422+
$this->clearCustomTrackingParameters();
13991423
$this->userAgent = false;
14001424
$this->acceptLanguage = false;
14011425

@@ -1505,6 +1529,13 @@ protected function getRequest($idSite)
15051529
{
15061530
$this->setFirstPartyCookies();
15071531

1532+
$customFields = '';
1533+
if (!empty($this->customParameters)) {
1534+
foreach ($this->customParameters as $parameter => $value) {
1535+
$customFields .= '&' . urlencode($parameter) . '=' . urlencode($value);
1536+
}
1537+
}
1538+
15081539
$url = $this->getBaseUrl() .
15091540
'?idsite=' . $idSite .
15101541
'&rec=1' .
@@ -1568,6 +1599,7 @@ protected function getRequest($idSite)
15681599
(!empty($this->city) ? '&city=' . urlencode($this->city) : '') .
15691600
(!empty($this->lat) ? '&lat=' . urlencode($this->lat) : '') .
15701601
(!empty($this->long) ? '&long=' . urlencode($this->long) : '') .
1602+
$customFields .
15711603
(!$this->sendImageResponse ? '&send_image=0' : '') .
15721604

15731605
// DEBUG
@@ -1577,6 +1609,7 @@ protected function getRequest($idSite)
15771609
// Reset page level custom variables after this page view
15781610
$this->pageCustomVar = array();
15791611
$this->eventCustomVar = array();
1612+
$this->clearCustomTrackingParameters();
15801613

15811614
// force new visit only once, user must call again setForceNewVisit()
15821615
$this->forcedNewVisit = false;
@@ -1811,4 +1844,4 @@ function Piwik_getUrlTrackGoal($idSite, $idGoal, $revenue = 0.0)
18111844
$tracker = new PiwikTracker($idSite);
18121845

18131846
return $tracker->getUrlTrackGoal($idGoal, $revenue);
1814-
}
1847+
}

0 commit comments

Comments
 (0)