generate() public method

public generate ( integer $idSite, string $piwikUrl, boolean $mergeSubdomains = false, boolean $groupPageTitlesByDomain = false, boolean $mergeAliasUrls = false, array $visitorCustomVariables = null, array $pageCustomVariables = null, string $customCampaignNameQueryParam = null, string $customCampaignKeywordParam = null, boolean $doNotTrack = false, boolean $disableCookies = false, boolean $trackNoScript = false ) : string
$idSite integer
$piwikUrl string http://path/to/piwik/site/
$mergeSubdomains boolean
$groupPageTitlesByDomain boolean
$mergeAliasUrls boolean
$visitorCustomVariables array
$pageCustomVariables array
$customCampaignNameQueryParam string
$customCampaignKeywordParam string
$doNotTrack boolean
$disableCookies boolean
$trackNoScript boolean
return string Javascript code.
Ejemplo n.º 1
0
    public function testStringsAreEscaped()
    {
        $generator = new TrackerCodeGenerator();
        $jsTag = $generator->generate($idSite = 1, $piwikUrl = 'abc"def', $mergeSubdomains = true, $groupPageTitlesByDomain = true, $mergeAliasUrls = true, $visitorCustomVariables = array(array('abc"def', 'abc"def')), $pageCustomVariables = array(array('abc"def', 'abc"def')), $customCampaignNameQueryParam = 'abc"def', $customCampaignKeywordParam = 'abc"def');
        $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, "abc\\"def", "abc\\"def", "visit"]);
  // you can set up to 5 custom variables for each action (page view, download, click, site search)
  _paq.push(["setCustomVariable", 1, "abc\\"def", "abc\\"def", "page"]);
  _paq.push(["setCampaignNameKey", "abc\\"def"]);
  _paq.push(["setCampaignKeywordKey", "abc\\"def"]);
  _paq.push([\'trackPageView\']);
  _paq.push([\'enableLinkTracking\']);
  (function() {
    var u="//abc"def/";
    _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="//abc"def/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
  */
 public function testJavascriptTrackingCode_withAllOptionsAndOptionsBeforeTrackerUrl()
 {
     $generator = new TrackerCodeGenerator();
     Piwik::addAction('Piwik.getJavascriptCode', function (&$codeImpl) {
         $codeImpl['optionsBeforeTrackerUrl'] .= "_paq.push(['setAPIUrl', 'http://localhost/statistics']);\n    ";
     });
     $jsTag = $generator->generate($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 -->\n<script type="text/javascript">\n  var _paq = _paq || [];\n  _paq.push([\"setDocumentTitle\", document.domain + \"/\" + document.title]);\n  // you can set up to 5 custom variables for each visitor\n  _paq.push([\"setCustomVariable\", 1, \"name\", \"value\", \"visit\"]);\n  _paq.push([\"setCustomVariable\", 2, \"name 2\", \"value 2\", \"visit\"]);\n  // you can set up to 5 custom variables for each action (page view, download, click, site search)\n  _paq.push([\"setCustomVariable\", 1, \"page cvar\", \"page cvar value\", \"page\"]);\n  _paq.push([\"setCampaignNameKey\", \"campaignKey\"]);\n  _paq.push([\"setCampaignKeywordKey\", \"keywordKey\"]);\n  _paq.push([\"setDoNotTrack\", true]);\n  _paq.push(['trackPageView']);\n  _paq.push(['enableLinkTracking']);\n  (function() {\n    var u="//localhost/piwik/";\n    _paq.push(['setAPIUrl', 'http://localhost/statistics']);\n    _paq.push(['setTrackerUrl', u+'piwik.php']);\n    _paq.push(['setSiteId', 1]);\n    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];\n    g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);\n  })();\n</script>\n<noscript><p><img src="//localhost/piwik/piwik.php?idsite=1" style="border:0;" alt="" /></p></noscript>\n<!-- End Piwik Code -->\n";
     $this->assertEquals($expected, $jsTag);
 }
Ejemplo n.º 3
0
 /**
  * Returns the javascript tag for the given idSite.
  * This tag must be included on every page to be tracked by Piwik
  *
  * @param int $idSite
  * @param string $piwikUrl
  * @param bool $mergeSubdomains
  * @param bool $groupPageTitlesByDomain
  * @param bool $mergeAliasUrls
  * @param bool $visitorCustomVariables
  * @param bool $pageCustomVariables
  * @param bool $customCampaignNameQueryParam
  * @param bool $customCampaignKeywordParam
  * @param bool $doNotTrack
  * @param bool $disableCookies
  * @return string The Javascript tag ready to be included on the HTML pages
  */
 public function getJavascriptTag($idSite, $piwikUrl = '', $mergeSubdomains = false, $groupPageTitlesByDomain = false, $mergeAliasUrls = false, $visitorCustomVariables = false, $pageCustomVariables = false, $customCampaignNameQueryParam = false, $customCampaignKeywordParam = false, $doNotTrack = false, $disableCookies = false)
 {
     Piwik::checkUserHasViewAccess($idSite);
     if (empty($piwikUrl)) {
         $piwikUrl = SettingsPiwik::getPiwikUrl();
     }
     $piwikUrl = Common::sanitizeInputValues($piwikUrl);
     $javascriptGenerator = new Tracker\TrackerCodeGenerator();
     $htmlEncoded = $javascriptGenerator->generate($idSite, $piwikUrl, $mergeSubdomains, $groupPageTitlesByDomain, $mergeAliasUrls, $visitorCustomVariables, $pageCustomVariables, $customCampaignNameQueryParam, $customCampaignKeywordParam, $doNotTrack, $disableCookies);
     $htmlEncoded = str_replace(array('<br>', '<br />', '<br/>'), '', $htmlEncoded);
     return $htmlEncoded;
 }
Ejemplo n.º 4
0
 /**
  * Installation Step 7: Display JavaScript tracking code
  */
 public function trackingCode()
 {
     $this->checkPiwikIsNotInstalled();
     $view = new View('@Installation/trackingCode', $this->getInstallationSteps(), __FUNCTION__);
     $siteName = Common::unsanitizeInputValue($this->getParam('site_name'));
     $idSite = $this->getParam('site_idSite');
     // Load the Tracking code and help text from the SitesManager
     $viewTrackingHelp = new \Piwik\View('@SitesManager/_displayJavascriptCode');
     $viewTrackingHelp->displaySiteName = $siteName;
     $javascriptGenerator = new TrackerCodeGenerator();
     $viewTrackingHelp->jsTag = $javascriptGenerator->generate($idSite, Url::getCurrentUrlWithoutFileName());
     $viewTrackingHelp->idSite = $idSite;
     $viewTrackingHelp->piwikUrl = Url::getCurrentUrlWithoutFileName();
     $view->trackingHelp = $viewTrackingHelp->render();
     $view->displaySiteName = $siteName;
     $view->displayfirstWebsiteSetupSuccess = true;
     $view->showNextStep = true;
     return $view->render();
 }
Ejemplo n.º 5
0
 public function siteWithoutData()
 {
     $javascriptGenerator = new TrackerCodeGenerator();
     $piwikUrl = Url::getCurrentUrlWithoutFileName();
     return $this->renderTemplate('siteWithoutData', array('siteName' => $this->site->getName(), 'trackingHelp' => $this->renderTemplate('_displayJavascriptCode', array('displaySiteName' => Common::unsanitizeInputValue($this->site->getName()), 'jsTag' => $javascriptGenerator->generate($this->idSite, $piwikUrl), 'idSite' => $this->idSite, 'piwikUrl' => $piwikUrl))));
 }
Ejemplo n.º 6
0
 /**
  * Returns the javascript tag for the given idSite.
  * This tag must be included on every page to be tracked by Piwik
  *
  * @param int $idSite
  * @param string $piwikUrl
  * @param bool $mergeSubdomains
  * @param bool $groupPageTitlesByDomain
  * @param bool $mergeAliasUrls
  * @param bool $visitorCustomVariables
  * @param bool $pageCustomVariables
  * @param bool $customCampaignNameQueryParam
  * @param bool $customCampaignKeywordParam
  * @param bool $doNotTrack
  * @param bool $disableCookies
  * @return string The Javascript tag ready to be included on the HTML pages
  */
 public function getJavascriptTag($idSite, $piwikUrl = '', $mergeSubdomains = false, $groupPageTitlesByDomain = false, $mergeAliasUrls = false, $visitorCustomVariables = false, $pageCustomVariables = false, $customCampaignNameQueryParam = false, $customCampaignKeywordParam = false, $doNotTrack = false, $disableCookies = false)
 {
     Piwik::checkUserHasViewAccess($idSite);
     if (empty($piwikUrl)) {
         $piwikUrl = SettingsPiwik::getPiwikUrl();
     }
     // Revert the automatic encoding
     // TODO remove that when https://github.com/piwik/piwik/issues/4231 is fixed
     $piwikUrl = Common::unsanitizeInputValue($piwikUrl);
     $visitorCustomVariables = Common::unsanitizeInputValues($visitorCustomVariables);
     $pageCustomVariables = Common::unsanitizeInputValues($pageCustomVariables);
     $customCampaignNameQueryParam = Common::unsanitizeInputValue($customCampaignNameQueryParam);
     $customCampaignKeywordParam = Common::unsanitizeInputValue($customCampaignKeywordParam);
     $generator = new TrackerCodeGenerator();
     $code = $generator->generate($idSite, $piwikUrl, $mergeSubdomains, $groupPageTitlesByDomain, $mergeAliasUrls, $visitorCustomVariables, $pageCustomVariables, $customCampaignNameQueryParam, $customCampaignKeywordParam, $doNotTrack, $disableCookies);
     $code = str_replace(array('<br>', '<br />', '<br/>'), '', $code);
     return $code;
 }
Ejemplo n.º 7
0
 /**
  * Displays the admin UI page showing all tracking tags
  * @return string
  */
 function displayJavascriptCode()
 {
     $idSite = Common::getRequestVar('idSite');
     Piwik::checkUserHasViewAccess($idSite);
     $javascriptGenerator = new TrackerCodeGenerator();
     $jsTag = $javascriptGenerator->generate($idSite, SettingsPiwik::getPiwikUrl());
     $site = new Site($idSite);
     return $this->renderTemplate('displayJavascriptCode', array('idSite' => $idSite, 'displaySiteName' => $site->getName(), 'jsTag' => $jsTag));
 }
Ejemplo n.º 8
0
 public function siteWithoutData()
 {
     $javascriptGenerator = new TrackerCodeGenerator();
     $piwikUrl = Url::getCurrentUrlWithoutFileName();
     if (!$this->site) {
         throw new UnexpectedWebsiteFoundException('Invalid site ' . $this->idSite);
     }
     return $this->renderTemplate('siteWithoutData', array('siteName' => $this->site->getName(), 'idSite' => $this->site->getId(), 'trackingHelp' => $this->renderTemplate('_displayJavascriptCode', array('displaySiteName' => Common::unsanitizeInputValue($this->site->getName()), 'jsTag' => $javascriptGenerator->generate($this->idSite, $piwikUrl), 'idSite' => $this->idSite, 'piwikUrl' => $piwikUrl))));
 }