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/CoreAdminHome/javascripts | |
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/CoreAdminHome/javascripts')
-rw-r--r-- | plugins/CoreAdminHome/javascripts/jsTrackingGenerator.js | 244 |
1 files changed, 69 insertions, 175 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(); |