1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
/*!
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
/**
* Controller for javascript tracking code generator
*/
(function () {
// gets the list of custom variables entered by the user in a custom variable section
function getCustomVariables(customVars) {
var result = [];
angular.forEach(customVars, function (customVar) {
result.push([customVar.name, customVar.value]);
});
return result;
};
// quickly gets the host + port from a url
function getHostNameFromUrl(url) {
var element = $('<a></a>')[0];
element.href = url;
return element.hostname;
};
angular.module('piwikApp').controller('JsTrackingCodeController', JsTrackingCodeController);
JsTrackingCodeController.$inject = ['$scope', 'piwikApi'];
function JsTrackingCodeController($scope, piwikApi) {
this.showAdvanced = false;
this.isLoading = false;
this.customVars = [];
this.siteUrls = {};
this.maxCustomVariables = parseInt(angular.element('[name=numMaxCustomVariables]').val(), 10);
this.canAddMoreCustomVariables = this.maxCustomVariables && this.maxCustomVariables > 0;
// get preloaded server-side data necessary for code generation
var piwikHost = window.location.host,
piwikPath = location.pathname.substring(0, location.pathname.lastIndexOf('/')),
self = this;
// queries Piwik for needed site info for one site
var getSiteData = function (idSite, sectionSelect, callback) {
// if data is already loaded, don't do an AJAX request
if (self.siteUrls[idSite]) {
callback();
return;
}
// disable section
self.isLoading = true;
piwikApi.fetch({
module: 'API',
method: 'SitesManager.getSiteUrlsFromId',
idSite: idSite,
filter_limit: '-1'
}).then(function (data) {
self.siteUrls[idSite] = data || [];
// re-enable controls
self.isLoading = false;
callback();
});
};
// function that generates JS code
var generateJsCodeAjax = null;
var generateJsCode = function (trackingCodeChangedManually) {
// get params used to generate JS code
var params = {
piwikUrl: piwikHost + piwikPath,
groupPageTitlesByDomain: self.groupByDomain ? 1 : 0,
mergeSubdomains: self.trackAllSubdomains ? 1 : 0,
mergeAliasUrls: self.trackAllAliases ? 1 : 0,
visitorCustomVariables: self.trackCustomVars ? getCustomVariables(self.customVars) : 0,
customCampaignNameQueryParam: null,
customCampaignKeywordParam: null,
doNotTrack: self.doNotTrack ? 1 : 0,
disableCookies: self.disableCookies ? 1 : 0,
trackNoScript: self.trackNoScript ? 1: 0
};
if (self.useCustomCampaignParams) {
params.customCampaignNameQueryParam = self.customCampaignName;
params.customCampaignKeywordParam = self.customCampaignKeyword;
}
if (generateJsCodeAjax) {
generateJsCodeAjax.abort();
}
generateJsCodeAjax = piwikApi.post({
module: 'API',
format: 'json',
method: 'SitesManager.getJavascriptTag',
idSite: self.site.id
}, params).then(function (response) {
generateJsCodeAjax = null;
self.trackingCode = response.value;
if(trackingCodeChangedManually) {
var jsCodeTextarea = $('#javascript-text .codeblock');
jsCodeTextarea.effect("highlight", {}, 1500);
}
});
return generateJsCodeAjax;
};
this.addCustomVar = function () {
if (this.canAddMoreCustomVariables) {
this.customVars.push({name: '', value: ''});
}
this.canAddMoreCustomVariables = this.maxCustomVariables > this.customVars.length;
};
this.addCustomVar();
this.updateTrackingCode = function () {
generateJsCode(true);
};
this.changeSite = function (trackingCodeChangedManually) {
$('.current-site-name').html(self.site.name);
getSiteData(this.site.id, '#js-code-options', function () {
var siteHost = getHostNameFromUrl(self.siteUrls[self.site.id][0]);
$('.current-site-host').text(siteHost);
var defaultAliasUrl = 'x.' + siteHost;
$('.current-site-alias').text(self.siteUrls[self.site.id][1] || defaultAliasUrl);
generateJsCode(true);
});
};
if (this.site && this.site.id) {
this.changeSite(false);
}
}
})();
|