diff options
author | Marcin Czołnowski <marcin@czolnowski.net> | 2014-10-03 17:05:52 +0400 |
---|---|---|
committer | Marcin Czołnowski <marcin@czolnowski.net> | 2014-10-03 17:05:52 +0400 |
commit | 20d509aac3fcb97efb8c34f3e0c8594f5f16d28f (patch) | |
tree | 494fe7148316b905ecf775801f1ec83a4eb8db8d | |
parent | 1a7dbc5ff6ab987338fa644f1c4c7a07602c10f6 (diff) |
Extend tracking with all changes.
-rw-r--r-- | core/Piwik.php | 11 | ||||
-rw-r--r-- | plugins/Morpheus/templates/javascriptCode.tpl | 4 | ||||
-rw-r--r-- | tests/PHPUnit/Integration/Core/PiwikTest.php | 93 |
3 files changed, 104 insertions, 4 deletions
diff --git a/core/Piwik.php b/core/Piwik.php index 16f50f387f..13124728f9 100644 --- a/core/Piwik.php +++ b/core/Piwik.php @@ -142,6 +142,7 @@ class Piwik // Build optional parameters to be added to text $options = ''; + $optionsBeforeTrackerUrl = ''; if ($groupPageTitlesByDomain) { $options .= ' _paq.push(["setDocumentTitle", document.domain + "/" + document.title]);' . PHP_EOL; } @@ -187,7 +188,9 @@ class Piwik $codeImpl = array( 'idSite' => $idSite, 'piwikUrl' => Common::sanitizeInputValue($piwikUrl), - 'options' => $options + 'options' => $options, + 'optionsBeforeTrackerUrl' => $optionsBeforeTrackerUrl, + 'protocol' => '//' ); $parameters = compact('mergeSubdomains', 'groupPageTitlesByDomain', 'mergeAliasUrls', 'visitorCustomVariables', 'pageCustomVariables', 'customCampaignNameQueryParam', 'customCampaignKeywordParam', @@ -205,6 +208,10 @@ class Piwik * - **piwikUrl**: The tracker URL to use. * - **options**: A string of JavaScript code that customises * the JavaScript tracker. + * - **optionsBeforeTrackerUrl**: A string of Javascript code that customises + * the JavaScript tracker inside of anonymous function before + * adding setTrackerUrl into paq. + * - **protocol**: Piwik url protocol. * * The **httpsPiwikUrl** element can be set if the HTTPS * domain is different from the normal domain. @@ -212,7 +219,7 @@ class Piwik */ self::postEvent('Piwik.getJavascriptCode', array(&$codeImpl, $parameters)); - $setTrackerUrl = 'var u="//{$piwikUrl}/";'; + $setTrackerUrl = 'var u="' . $codeImpl['protocol'] . '{$piwikUrl}/";'; if (!empty($codeImpl['httpsPiwikUrl'])) { $setTrackerUrl = 'var u=((document.location.protocol === "https:") ? "https://{$httpsPiwikUrl}/" : "http://{$piwikUrl}/");'; diff --git a/plugins/Morpheus/templates/javascriptCode.tpl b/plugins/Morpheus/templates/javascriptCode.tpl index 344e6d0311..125259b807 100644 --- a/plugins/Morpheus/templates/javascriptCode.tpl +++ b/plugins/Morpheus/templates/javascriptCode.tpl @@ -5,11 +5,11 @@ _paq.push(['enableLinkTracking']); (function() { {$setTrackerUrl} - _paq.push(['setTrackerUrl', u+'piwik.php']); + {$optionsBeforeTrackerUrl}_paq.push(['setTrackerUrl', u+'piwik.php']); _paq.push(['setSiteId', {$idSite}]); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s); })(); </script> -<noscript><p><img src="//{$piwikUrl}/piwik.php?idsite={$idSite}" style="border:0;" alt="" /></p></noscript> +<noscript><p><img src="{$protocol}{$piwikUrl}/piwik.php?idsite={$idSite}" style="border:0;" alt="" /></p></noscript> <!-- End Piwik Code --> diff --git a/tests/PHPUnit/Integration/Core/PiwikTest.php b/tests/PHPUnit/Integration/Core/PiwikTest.php index 3ec353b2f2..23692355d5 100644 --- a/tests/PHPUnit/Integration/Core/PiwikTest.php +++ b/tests/PHPUnit/Integration/Core/PiwikTest.php @@ -67,6 +67,99 @@ class Core_PiwikTest extends DatabaseTestCase } /** + * Tests the generated JS code with protocol overwrite + * @group Core + */ + public function testJavascriptTrackingCode_withAllOptionsAndProtocolOverwrite() + { + Piwik::addAction('Piwik.getJavascriptCode', function (&$codeImpl) { + $codeImpl['protocol'] = 'https://'; + }); + + $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 = "<!-- Piwik --> +<script type="text/javascript"> + var _paq = _paq || []; + _paq.push([\"setDocumentTitle\", document.domain + \"/\" + document.title]); + // you can set up to 5 custom variables for each visitor + _paq.push([\"setCustomVariable\", 1, \"name\", \"value\", \"visit\"]); + _paq.push([\"setCustomVariable\", 2, \"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\", 1, \"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="https://localhost/piwik/"; + _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.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s); + })(); +</script> +<noscript><p><img src="https://localhost/piwik/piwik.php?idsite=1" style="border:0;" alt="" /></p></noscript> +<!-- End Piwik Code --> +"; + + $this->assertEquals($expected, $jsTag); + } + + /** + * Tests the generated JS code with options before tracker url + * @group Core + */ + public function testJavascriptTrackingCode_withAllOptionsAndOptionsBeforeTrackerUrl() + { + Piwik::addAction('Piwik.getJavascriptCode', function (&$codeImpl) { + $codeImpl['optionsBeforeTrackerUrl'] .= "_paq.push(['setAPIUrl', 'http://localhost/statistics']);\n "; + }); + + $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 = "<!-- Piwik --> +<script type="text/javascript"> + var _paq = _paq || []; + _paq.push([\"setDocumentTitle\", document.domain + \"/\" + document.title]); + // you can set up to 5 custom variables for each visitor + _paq.push([\"setCustomVariable\", 1, \"name\", \"value\", \"visit\"]); + _paq.push([\"setCustomVariable\", 2, \"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\", 1, \"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="//localhost/piwik/"; + _paq.push(['setAPIUrl', 'http://localhost/statistics']); + _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.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s); + })(); +</script> +<noscript><p><img src="//localhost/piwik/piwik.php?idsite=1" style="border:0;" alt="" /></p></noscript> +<!-- End Piwik Code --> +"; + + $this->assertEquals($expected, $jsTag); + } + + /** * Dataprovider for testIsNumericValid */ public function getValidNumeric() |