diff options
author | diosmosis <benakamoorthi@fastmail.fm> | 2014-04-03 03:54:06 +0400 |
---|---|---|
committer | diosmosis <benakamoorthi@fastmail.fm> | 2014-04-03 03:54:44 +0400 |
commit | 9f687ad2eaea1c02b087175b12c3946ee4694c60 (patch) | |
tree | 32faa400ad347d5fb43f578c9685186f3ac275b0 /plugins | |
parent | 98bc277a08af76e7f498b37b13dada76be727e8f (diff) |
Remove stray newlines in javascript tracking code output, use server side tracking code generation for JS code & for image tracking code, add new SitesManager.getImageTrackingCode API method for server side image tracking code generation, and fix bug where goals for initially selected site in image tracking code generator were not loaded.
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/CoreAdminHome/javascripts/jsTrackingGenerator.js | 244 | ||||
-rw-r--r-- | plugins/SitesManager/API.php | 53 | ||||
-rw-r--r-- | plugins/Zeitgeist/templates/javascriptCode.tpl | 1 |
3 files changed, 120 insertions, 178 deletions
diff --git a/plugins/CoreAdminHome/javascripts/jsTrackingGenerator.js b/plugins/CoreAdminHome/javascripts/jsTrackingGenerator.js index d5a9f7363f..707a3eef94 100644 --- a/plugins/CoreAdminHome/javascripts/jsTrackingGenerator.js +++ b/plugins/CoreAdminHome/javascripts/jsTrackingGenerator.js @@ -12,46 +12,9 @@ exports = require('piwik/Tracking'); /** - * Singleton class used to trigger events on. Plugins can bind to these events - * to customize the tracking code shown to users. + * This class is deprecated. Use server-side events instead. * - * The following events are triggered: - * - * - customizeJavaScriptParams: function (eventObject, params) - * - * Triggered before tracking JavaScript is generated. This event can be used to - * customize how the generated code looks, without having to parse JavaScript. - * - * params is an object containing the data used to create the JavaScript tracking - * code. - * - * - customizeJavaScript: function (eventObject, eventData) - * - * Triggered after tracking JavaScript is generated. This event can be used to - * customize how the generated code looks, if it cannot be done with the - * customizeJavaScriptParams event. - * - * eventData is an object with one element, 'code', which holds the JavaScript - * tracking code. - * - * - customizeTrackerLinkParams: function (eventObject, params) - * - * Triggered before the tracking link is generated. This event can be used to - * customize how the generated link looks, without having to parse the HTML - * link code. - * - * params is an object containing the data used to create the tracking link. - * - * - customizeTrackerLink: function (eventObject, eventData) - * - * Triggered after the tracking link is generated. This event can be used to - * customize how the generated link looks, if the customization cannot be done - * with the customizeTrackerLinkParams event. - * - * eventData is an object with one element, 'code', which holds the tracking - * link HTML. - * - * @constructor + * @deprecated */ var TrackingCodeGenerator = function () { // empty @@ -185,150 +148,81 @@ }; // function that generates JS code - var generateJsCode = function () { - // get params used to generate JS code - var params = { - idSite: $('#js-tracker-website').attr('siteid'), - groupPageTitlesByDomain: $('#javascript-tracking-group-by-domain').is(':checked'), - mergeSubdomains: $('#javascript-tracking-all-subdomains').is(':checked'), - mergeAliasUrls: $('#javascript-tracking-all-aliases').is(':checked'), - visitorCustomVariables: getCustomVariables('javascript-tracking-visitor-cv'), - pageCustomVariables: getCustomVariables('javascript-tracking-page-cv'), - customCampaignNameQueryParam: null, - customCampaignKeywordParam: null, - doNotTrack: $('#javascript-tracking-do-not-track').is(':checked'), - piwikHost: piwikHost, - piwikPath: piwikPath - }; - - if ($('#custom-campaign-query-params-check').is(':checked')) { - params.customCampaignNameQueryParam = $('#custom-campaign-name-query-param').val(); - params.customCampaignKeywordParam = $('#custom-campaign-keyword-query-param').val(); - } - - // allow plugins to modify data used to generate tracking code - $(TrackingCodeGeneratorSingleton).trigger('customizeJavaScriptParams', params); - - var idSite = params.idSite; - - // generate JS - // changes made to this code should be mirrored in core/Piwik.php function getJavascriptCode() - var result = '<!-- Piwik -->\n\ -<script type="text/javascript">\n\ - var _paq = _paq || [];\n'; - - if (params.groupPageTitlesByDomain) { - result += ' _paq.push(["setDocumentTitle", document.domain + "/" + document.title]);\n'; - } - - if (params.mergeSubdomains) { - var mainHostAllSub = '*.' + getHostNameFromUrl(siteUrls[idSite][0]); - result += ' _paq.push(["setCookieDomain", ' + JSON.stringify(mainHostAllSub) + ']);\n'; - } - - if (params.mergeAliasUrls) { - var siteHosts = []; - for (var i = 0; i != siteUrls[idSite].length; ++i) { - siteHosts[i] = '*.' + getHostNameFromUrl(siteUrls[idSite][i]); + var generateJsCodeAjax = null, + generateJsCode = function () { + // get params used to generate JS code + var params = { + piwikUrl: piwikHost + piwikPath, + groupPageTitlesByDomain: $('#javascript-tracking-group-by-domain').is(':checked') ? 1 : 0, + mergeSubdomains: $('#javascript-tracking-all-subdomains').is(':checked') ? 1 : 0, + mergeAliasUrls: $('#javascript-tracking-all-aliases').is(':checked') ? 1 : 0, + visitorCustomVariables: getCustomVariables('javascript-tracking-visitor-cv'), + pageCustomVariables: getCustomVariables('javascript-tracking-page-cv'), + customCampaignNameQueryParam: null, + customCampaignKeywordParam: null, + doNotTrack: $('#javascript-tracking-do-not-track').is(':checked') ? 1 : 0, + }; + + if ($('#custom-campaign-query-params-check').is(':checked')) { + params.customCampaignNameQueryParam = $('#custom-campaign-name-query-param').val(); + params.customCampaignKeywordParam = $('#custom-campaign-keyword-query-param').val(); } - result += ' _paq.push(["setDomains", ' + JSON.stringify(siteHosts) + ']);\n'; - } - - if (params.visitorCustomVariables.length) { - result += ' // you can set up to 5 custom variables for each visitor\n'; - result += getCustomVariableJS(params.visitorCustomVariables, 'visit'); - } - - if (params.pageCustomVariables.length) { - result += ' // you can set up to 5 custom variables for each action (page view, ' + - 'download, click, site search)\n'; - result += getCustomVariableJS(params.pageCustomVariables, 'page'); - } - if (params.customCampaignNameQueryParam) { - result += ' _paq.push(["setCampaignNameKey", ' + JSON.stringify(params.customCampaignNameQueryParam) + ']);\n'; - } - - if (params.customCampaignKeywordParam) { - result += ' _paq.push(["setCampaignKeywordKey", ' + JSON.stringify(params.customCampaignKeywordParam) + ']);\n'; - } - - if (params.doNotTrack) { - result += ' _paq.push(["setDoNotTrack", true]);\n'; - } - - result += ' _paq.push(["trackPageView"]);\n\ - _paq.push(["enableLinkTracking"]);\n\n\ - (function() {\n\ - var u=(("https:" == document.location.protocol) ? "https" : "http") + "://' + params.piwikHost + params.piwikPath + '/";\n\ - _paq.push(["setTrackerUrl", u+"piwik.php"]);\n\ - _paq.push(["setSiteId", ' + JSON.stringify(idSite) + ']);\n\ - var d=document, g=d.createElement("script"), s=d.getElementsByTagName("script")[0]; g.type="text/javascript";\n\ - g.defer=true; g.async=true; g.src=u+"piwik.js"; s.parentNode.insertBefore(g,s);\n\ - })();\n\ -</script>\n\ -'+'<!-- End Piwik Code -->'; - - // allow plugins to modify generated JavaScript - var eventData = {code: result, piwikHost: piwikHost }; - $(TrackingCodeGeneratorSingleton).trigger('customizeJavaScript', eventData); - result = eventData.code; - - $('#javascript-text').find('textarea').val(result) - }; - - // function that generates image tracker link - var generateImageTrackerLink = function () { - // get data used to generate the link - var generateDataParams = { - idSite: $('#image-tracker-website').attr('siteid'), - actionName: $('#image-tracker-action-name').val(), - piwikHost: piwikHost, - piwikPath: piwikPath - }; - - if ($('#image-tracking-goal-check').is(':checked')) { - generateDataParams.idGoal = $('#image-tracker-goal').val(); - if (generateDataParams.idGoal) { - generateDataParams.revenue = $('#image-tracker-advanced-options').find('.revenue').val(); + if (generateJsCodeAjax) { + generateJsCodeAjax.abort(); } - } - - // allow plugins to modify data used to generate tracking code - $(TrackingCodeGeneratorSingleton).trigger('customizeTrackerLinkParams', generateDataParams); - // generate link HTML - var params = { - idsite: generateDataParams.idSite, - rec: 1 + generateJsCodeAjax = new ajaxHelper(); + generateJsCodeAjax.addParams({ + module: 'API', + format: 'json', + method: 'SitesManager.getJavascriptTag', + idSite: $('#js-tracker-website').attr('siteid') + }, 'GET'); + generateJsCodeAjax.addParams(params, 'POST'); + generateJsCodeAjax.setCallback(function (response) { + generateJsCodeAjax = null; + + $('#javascript-text').find('textarea').val(response.value); + }); + generateJsCodeAjax.send(); }; - if (generateDataParams.actionName) { - params.action_name = generateDataParams.actionName; - } - - if (generateDataParams.idGoal) { - params.idGoal = generateDataParams.idGoal; - if (generateDataParams.revenue) { - params.revenue = generateDataParams.revenue; + // function that generates image tracker link + var generateImageTrackingAjax = null, + generateImageTrackerLink = function () { + // get data used to generate the link + var generateDataParams = { + piwikUrl: piwikHost + piwikPath, + actionName: $('#image-tracker-action-name').val(), + }; + + if ($('#image-tracking-goal-check').is(':checked')) { + generateDataParams.idGoal = $('#image-tracker-goal').val(); + if (generateDataParams.idGoal) { + generateDataParams.revenue = $('#image-tracker-advanced-options').find('.revenue').val(); + } } - } - - var piwikURL = ("https:" == document.location.protocol ? "https://" : "http://") + generateDataParams.piwikHost - + generateDataParams.piwikPath + '/piwik.php'; - var result = '<!-- Piwik Image Tracker -->\n\ -<img src="' + piwikURL + '?' + $.param(params) + '" style="border:0" alt="" />\n\ -<!-- End Piwik -->'; - - // allow plugins to modify tracking link code - var eventData = {code: result}; - $(TrackingCodeGeneratorSingleton).trigger('customizeTrackerLink', eventData); - result = eventData.code; + if (generateImageTrackingAjax) { + generateImageTrackingAjax.abort(); + } - result = result.replace(/[&]/g, "&"); - $('#image-tracking-text').find('textarea').val(result); - }; + generateImageTrackingAjax = new ajaxHelper(); + generateImageTrackingAjax.addParams({ + module: 'API', + format: 'json', + method: 'SitesManager.getImageTrackingCode', + idSite: $('#image-tracker-website').attr('siteid') + }, 'GET'); + generateImageTrackingAjax.addParams(generateDataParams, 'POST'); + generateImageTrackingAjax.setCallback(function (response) { + generateImageTrackingAjax = null; + + $('#image-tracking-text').find('textarea').val(response.value); + }); + generateImageTrackingAjax.send(); + }; // on image link tracker site change, change available goals $('#image-tracker-website').bind('change', function (e, site) { @@ -403,7 +297,7 @@ $('#js-tracker-website').attr('siteid'), '#js-code-options,#image-tracking-code-options', function () { - var imageTrackerSiteId = $('#image-tracker-website').find('.custom_select_main_link').attr('data-siteid'); + var imageTrackerSiteId = $('#image-tracker-website').attr('siteid'); resetGoalSelectItems(imageTrackerSiteId, 'image-tracker-goal'); generateJsCode(); diff --git a/plugins/SitesManager/API.php b/plugins/SitesManager/API.php index 6bbab8de7e..3baf42679e 100644 --- a/plugins/SitesManager/API.php +++ b/plugins/SitesManager/API.php @@ -24,6 +24,7 @@ use Piwik\TaskScheduler; use Piwik\Tracker\Cache; use Piwik\Url; use Piwik\UrlHelper; +use Piwik\ProxyHttp; /** * The SitesManager API gives you full control on Websites in Piwik (create, update and delete), and many methods to retrieve websites based on various attributes. @@ -71,7 +72,10 @@ class API extends \Piwik\Plugin\API * @internal param $ * @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) + public function getJavascriptTag($idSite, $piwikUrl = '', $mergeSubdomains = false, $groupPageTitlesByDomain = false, + $mergeAliasUrls = false, $visitorCustomVariables = false, $pageCustomVariables = false, + $customCampaignNameQueryParam = false, $customCampaignKeywordParam = false, + $doNotTrack = false) { Piwik::checkUserHasViewAccess($idSite); @@ -80,12 +84,57 @@ class API extends \Piwik\Plugin\API } $piwikUrl = Common::sanitizeInputValues($piwikUrl); - $htmlEncoded = Piwik::getJavascriptCode($idSite, $piwikUrl, $mergeSubdomains, $groupPageTitlesByDomain, $mergeAliasUrls, $visitorCustomVariables, $pageCustomVariables, $customCampaignNameQueryParam, $customCampaignKeywordParam, $doNotTrack); + $htmlEncoded = Piwik::getJavascriptCode($idSite, $piwikUrl, $mergeSubdomains, $groupPageTitlesByDomain, + $mergeAliasUrls, $visitorCustomVariables, $pageCustomVariables, + $customCampaignNameQueryParam, $customCampaignKeywordParam, + $doNotTrack); $htmlEncoded = str_replace(array('<br>', '<br />', '<br/>'), '', $htmlEncoded); return $htmlEncoded; } /** + * Returns image link tracking code for a given site with specified options. + * + * @param int $idSite The ID to generate tracking code for. + * @param string $piwikUrl The domain and URL path to the Piwik installation. + * @param int $idGoal An ID for a goal to trigger a conversion for. + * @param int $revenue The revenue of the goal conversion. Only used if $idGoal is supplied. + * @return string The HTML tracking code. + */ + public function getImageTrackingCode($idSite, $piwikUrl = '', $actionName = false, $idGoal = false, $revenue = false) + { + $urlParams = array('idSite' => $idSite, 'rec' => 1); + + if ($actionName !== false) { + $urlParams['action_name'] = urlencode(Common::unsanitizeInputValue($actionName)); + } + + if ($idGoal !== false) { + $urlParams['idGoal'] = $idGoal; + if ($revenue !== false) { + $urlParams['revenue'] = $revenue; + } + } + + /** + * Triggered when generating image link tracking code server side. Plugins can use + * this event to customise the image tracking code that is displayed to the + * user. + * + * @param string &$piwikHost The domain and URL path to the Piwik installation, eg, + * `'examplepiwik.com/path/to/piwik'`. + * @param array &$urlParams The query parameters used in the <img> element's src + * URL. See Piwik's image tracking docs for more info. + */ + Piwik::postEvent('SitesManager.getImageTrackingCode', array(&$piwikUrl, &$urlParams)); + + $piwikUrl = (ProxyHttp::isHttps() ? "https://" : "http://") . $piwikUrl . '/piwik.php'; + return "<!-- Piwik Image Tracker--> +<img src=\"$piwikUrl?" . Url::getQueryStringFromParameters($urlParams) . "\" style=\"border:0\" alt=\"\" /> +<!-- End Piwik -->"; + } + + /** * Returns all websites belonging to the specified group * @param string $group Group name * @return array of sites diff --git a/plugins/Zeitgeist/templates/javascriptCode.tpl b/plugins/Zeitgeist/templates/javascriptCode.tpl index 104e39cbbf..488f39d00c 100644 --- a/plugins/Zeitgeist/templates/javascriptCode.tpl +++ b/plugins/Zeitgeist/templates/javascriptCode.tpl @@ -10,7 +10,6 @@ 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); })(); - </script> <noscript><p><img src="http://{$piwikUrl}/piwik.php?idsite={$idSite}" style="border:0;" alt="" /></p></noscript> <!-- End Piwik Code --> |