Skip to content

Commit

Permalink
Refs #4247 Adding unit test + fixing "visit" custom variable scope
Browse files Browse the repository at this point in the history
  • Loading branch information
mattab committed Oct 28, 2013
1 parent de56af3 commit df4fc40
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 32 deletions.
47 changes: 29 additions & 18 deletions core/Piwik.php
Expand Up @@ -134,33 +134,20 @@ static public function getJavascriptCode($idSite, $piwikUrl, $mergeSubdomains =
$piwikUrl = @$matches[2];
$jsCode = str_replace('{$idSite}', $idSite, $jsCode);
$jsCode = str_replace('{$piwikUrl}', Common::sanitizeInputValue($piwikUrl), $jsCode);

// Build optional parameters to be added to text
$options = '';
if ($groupPageTitlesByDomain) {
$options .= PHP_EOL . ' _paq.push(["setDocumentTitle", document.domain + "/" + document.title]);' . PHP_EOL;
$options .= ' _paq.push(["setDocumentTitle", document.domain + "/" + document.title]);' . PHP_EOL;
}
if ($mergeSubdomains || $mergeAliasUrls) {
// Both flags need URL data
$site_urls = APISitesManager::getInstance()->getSiteUrlsFromId( $idSite );
// We need to parse_url to isolate hosts
$site_hosts = array ();
foreach ($site_urls as $site_url) {
$referrerParsed = parse_url($site_url);
array_push($site_hosts, $referrerParsed['host']);
}
}
if ($mergeSubdomains) {
$options .= PHP_EOL . ' _paq.push(["setCookieDomain", "*.' . $site_hosts[0] . '"]);' . PHP_EOL;
}
if ($mergeAliasUrls) {
$urls = '["*.'.implode('","*.',$site_hosts).'"]';
$options .= ' _paq.push(["setDomains", '.$urls.']);' . PHP_EOL;
$options .= self::getJavascriptTagOptions($idSite, $mergeSubdomains, $mergeAliasUrls);
}
if ($visitorCustomVariables) {
$options .= '// you can set up to 5 custom variables for each visitor' . PHP_EOL;
$options .= ' // you can set up to 5 custom variables for each visitor' . PHP_EOL;
$index = 0;
foreach ($visitorCustomVariables as $visitorCustomVariable) {
$options .= ' _paq.push(["setCustomVariable", '.$index++.', "'.$visitorCustomVariable[0].'", "'.$visitorCustomVariable[1].'", "visitor"]);' . PHP_EOL;
$options .= ' _paq.push(["setCustomVariable", '.$index++.', "'.$visitorCustomVariable[0].'", "'.$visitorCustomVariable[1].'", "visit"]);' . PHP_EOL;
}
}
if ($pageCustomVariables) {
Expand Down Expand Up @@ -768,4 +755,28 @@ public static function translateException($message, $args = array())
return $message;
}
}

protected static function getJavascriptTagOptions($idSite, $mergeSubdomains, $mergeAliasUrls)
{
try {
$websiteUrls = APISitesManager::getInstance()->getSiteUrlsFromId($idSite);
} catch (\Exception $e) {
return '';
}
// We need to parse_url to isolate hosts
$websiteHosts = array();
foreach ($websiteUrls as $site_url) {
$referrerParsed = parse_url($site_url);
$websiteHosts[] = $referrerParsed['host'];
}
$options = '';
if ($mergeSubdomains && !empty($websiteHosts)) {
$options .= PHP_EOL . ' _paq.push(["setCookieDomain", "*.' . $websiteHosts[0] . '"]);' . PHP_EOL;
}
if ($mergeAliasUrls && !empty($websiteHosts)) {
$urls = '["*.' . implode('","*.', $websiteHosts) . '"]';
$options .= ' _paq.push(["setDomains", ' . $urls . ']);' . PHP_EOL;
}
return $options;
}
}
2 changes: 1 addition & 1 deletion plugins/Zeitgeist/templates/javascriptCode.tpl
@@ -1,5 +1,5 @@
<!-- Piwik -->
<script type="text/javascript">
<script type="text/javascript">
var _paq = _paq || [];
{$options}
_paq.push(['trackPageView']);
Expand Down
69 changes: 56 additions & 13 deletions tests/PHPUnit/Core/PiwikTest.php
Expand Up @@ -14,6 +14,49 @@

class PiwikTest extends DatabaseTestCase
{
/**
* Tests the generated JS code
* @group Core
*/
public function testJavascriptTrackingCode_withAllOptions()
{
$jsTag = Piwik::getJavascriptCode($idSite = 1, $piwikUrl = 'http://localhost/piwik',
$mergeSubdomains = true, $groupPageTitlesByDomain = true, $mergeAliasUrls = true,
$visitorCustomVariables = array( array("name", "value"), array("name 2", "value 2") ),
$pageCustomVariables = array( array("page cvar", "page cvar value") ),
$customCampaignNameQueryParam = "campaignKey", $customCampaignKeywordParam = "keywordKey",
$doNotTrack = true);

$expected = "&lt;!-- Piwik --&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
var _paq = _paq || [];
_paq.push([\"setDocumentTitle\", document.domain + \"/\" + document.title]);
// you can set up to 5 custom variables for each visitor
_paq.push([\"setCustomVariable\", 0, \"name\", \"value\", \"visit\"]);
_paq.push([\"setCustomVariable\", 1, \"name 2\", \"value 2\", \"visit\"]);
// you can set up to 5 custom variables for each action (page view, download, click, site search)
_paq.push([\"setCustomVariable\", 0, \"page cvar\", \"page cvar value\", \"page\"]);
_paq.push([\"setCampaignNameKey\", \"campaignKey\"]);
_paq.push([\"setCampaignKeywordKey\", \"keywordKey\"]);
_paq.push([\"setDoNotTrack\", true]);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u=((&quot;https:&quot; == document.location.protocol) ? &quot;https&quot; : &quot;http&quot;) + &quot;://localhost/piwik&quot;;
_paq.push(['setTrackerUrl', u+'piwik.php']);
_paq.push(['setSiteId', 1]);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript';
g.defer=true; g.async=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
})();
&lt;/script&gt;
&lt;noscript&gt;&lt;p&gt;&lt;img src=&quot;http://localhost/piwikpiwik.php?idsite=1&quot; style=&quot;border:0;&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;&lt;/noscript&gt;
&lt;!-- End Piwik Code --&gt;
";

$this->assertEquals($jsTag, $expected);
}

/**
* Dataprovider for testIsNumericValid
*/
Expand All @@ -34,7 +77,7 @@ public function getValidNumeric()

/**
* @group Core
*
*
* @dataProvider getValidNumeric
*/
public function testIsNumericValid($toTest)
Expand All @@ -58,7 +101,7 @@ public function getInvalidNumeric()

/**
* @group Core
*
*
* @dataProvider getInvalidNumeric
*/
public function testIsNumericNotValid($toTest)
Expand Down Expand Up @@ -96,26 +139,26 @@ public function getPrettyTimeFromSecondsData()
array((86400 * (365.25 + 10)), array('1 years 10 days', '9006:00:00')),
array(1.342, array('1.34s', '00:00:01.34')),
array(.342, array('0.34s', '00:00:00.34')),
array(.02, array('0.02s', '00:00:00.02')),
array(.02, array('0.02s', '00:00:00.02')),
array(.002, array('0.002s', '00:00:00')),
array(1.002, array('1s', '00:00:01')),
array(1.02, array('1.02s', '00:00:01.02')),
array(1.2, array('1.2s', '00:00:01.20')),
array(122.1, array('2 min 2.1s', '00:02:02.10'))
array(1.002, array('1s', '00:00:01')),
array(1.02, array('1.02s', '00:00:01.02')),
array(1.2, array('1.2s', '00:00:01.20')),
array(122.1, array('2 min 2.1s', '00:02:02.10'))
);
}

/**
* @group Core
*
*
* @dataProvider getPrettyTimeFromSecondsData
*/
public function testGetPrettyTimeFromSeconds($seconds, $expected)
{
if (($seconds * 100) > PHP_INT_MAX) {
$this->markTestSkipped("Will not pass on 32-bit machine.");
}

Translate::loadEnglishTranslation();

$sentenceExpected = str_replace(' ', '&nbsp;', $expected[0]);
Expand Down Expand Up @@ -153,7 +196,7 @@ public function getInvalidLoginStringData()

/**
* @group Core
*
*
* @dataProvider getInvalidLoginStringData
*/
public function testCheckInvalidLoginString($toTest)
Expand Down Expand Up @@ -184,7 +227,7 @@ public function getValidLoginStringData()

/**
* @group Core
*
*
* @dataProvider getValidLoginStringData
*/
public function testCheckValidLoginString($toTest)
Expand All @@ -209,7 +252,7 @@ public function getGetPrettyValueTestCases()

/**
* @group Core
*
*
* @dataProvider getGetPrettyValueTestCases
*/
public function testGetPrettyValue($columnName, $value, $expected)
Expand Down Expand Up @@ -246,7 +289,7 @@ public function getIsAssociativeArrayTestCases()

/**
* @group Core
*
*
* @dataProvider getIsAssociativeArrayTestCases
*/
public function testIsAssociativeArray($array, $expected)
Expand Down

0 comments on commit df4fc40

Please sign in to comment.