|
53 | 53 | var platformText = "platform"; |
54 | 54 | var platformVersionText = "platformVersion"; |
55 | 55 | var docsUrl = "https://docs.simpleanalytics.com"; |
| 56 | + var allowParams; |
56 | 57 | var isBotAgent = |
57 | 58 | /(bot|spider|crawl)/i.test(userAgent) && !/(cubot)/i.test(userAgent); |
58 | 59 | /** if screen **/ |
|
120 | 121 | return !!value === value; |
121 | 122 | }; |
122 | 123 |
|
123 | | - var getParams = function (regex) { |
| 124 | + var getParams = function (regex, returnArray) { |
124 | 125 | // From the search we grab the utm_source and ref and save only that |
125 | 126 | var matches = loc.search.match( |
126 | 127 | new RegExp("[?&](" + regex + ")=([^?&]+)", "gi") |
127 | 128 | ); |
128 | 129 | var match = matches |
129 | 130 | ? matches.map(function (m) { |
130 | | - return m.split("=")[1]; |
| 131 | + return m.split(/[?&=]/).slice(-2); |
131 | 132 | }) |
132 | 133 | : []; |
133 | | - if (match && match[0]) return match[0]; |
| 134 | + |
| 135 | + if (match[0]) return returnArray ? match[0] : match[0][1]; |
134 | 136 | }; |
135 | 137 |
|
136 | 138 | /** if ignorepages **/ |
|
166 | 168 | var sendData = function (data, callback) { |
167 | 169 | data = assign(payload, page, data); |
168 | 170 |
|
| 171 | + if (allowParams) |
| 172 | + data.params = stringify( |
| 173 | + allowParams |
| 174 | + .map(function (param) { |
| 175 | + var params = getParams(param, true); |
| 176 | + if (!params) return; |
| 177 | + return { key: params[0], value: params[1] }; |
| 178 | + }) |
| 179 | + .filter(Boolean) |
| 180 | + ); |
| 181 | + |
169 | 182 | var image = new Image(); |
170 | 183 | /** if events **/ |
171 | 184 | if (callback) { |
|
283 | 296 | ); |
284 | 297 | /** endif **/ |
285 | 298 |
|
| 299 | + /** if (or ignorepages allowparams) **/ |
| 300 | + var convertCommaSeparatedToArray = function (csv) { |
| 301 | + return Array.isArray(csv) |
| 302 | + ? csv |
| 303 | + : isString(csv) && csv.length |
| 304 | + ? csv.split(/, ?/) |
| 305 | + : []; |
| 306 | + }; |
| 307 | + /** endif **/ |
| 308 | + |
286 | 309 | /** if events **/ |
287 | 310 | // Event function name |
288 | 311 | var functionName = |
|
291 | 314 |
|
292 | 315 | /** if ignorepages **/ |
293 | 316 | // Customers can ignore certain pages |
294 | | - var ignorePagesRaw = |
295 | | - overwriteOptions.ignorePages || attr(scriptElement, "ignore-pages"); |
296 | | - |
297 | | - // Make sure ignore pages is an array |
298 | | - var ignorePages = Array.isArray(ignorePagesRaw) |
299 | | - ? ignorePagesRaw |
300 | | - : isString(ignorePagesRaw) && ignorePagesRaw.length |
301 | | - ? ignorePagesRaw.split(/, ?/) |
302 | | - : []; |
| 317 | + var ignorePages = convertCommaSeparatedToArray( |
| 318 | + overwriteOptions.ignorePages || attr(scriptElement, "ignore-pages") |
| 319 | + ); |
303 | 320 | /** endif **/ |
304 | 321 |
|
305 | | - // Customers can ignore certain pages |
306 | | - var ignorePagesRaw = |
307 | | - overwriteOptions.ignorePages || attr(scriptElement, "ignore-pages"); |
| 322 | + /** if allowparams **/ |
| 323 | + // Customers can allow params |
| 324 | + allowParams = convertCommaSeparatedToArray( |
| 325 | + overwriteOptions.allowParams || attr(scriptElement, "allow-params") |
| 326 | + ); |
| 327 | + /** endif **/ |
| 328 | + |
| 329 | + // By default we allow source, medium in the URLs. With strictUtm enabled |
| 330 | + // we only allow it with the utm_ prefix: utm_source, utm_medium, ... |
| 331 | + var strictUtm = |
| 332 | + overwriteOptions.strictUtm || attr(scriptElement, "strict-utm"); |
308 | 333 |
|
309 | 334 | ///////////////////// |
310 | 335 | // PAYLOAD FOR BOTH PAGE VIEWS AND EVENTS |
|
411 | 436 | .replace(/^https?:\/\/((m|l|w{2,3}([0-9]+)?)\.)?([^?#]+)(.*)$/, "$4") |
412 | 437 | .replace(/^([^/]+)$/, "$1") || undefinedVar; |
413 | 438 |
|
414 | | - // The prefix utm_ is optional |
415 | | - var utmRegexPrefix = "(utm_)?"; |
| 439 | + // The prefix utm_ is optional with strictUtm disabled |
| 440 | + var utmRegexPrefix = "(utm_)" + (strictUtm ? "" : "?"); |
416 | 441 | var source = { |
417 | | - source: getParams(utmRegexPrefix + "source|ref"), |
| 442 | + source: getParams(utmRegexPrefix + "source" + (strictUtm ? "" : "|ref")), |
418 | 443 | medium: getParams(utmRegexPrefix + "medium"), |
419 | 444 | campaign: getParams(utmRegexPrefix + "campaign"), |
420 | 445 | term: getParams(utmRegexPrefix + "term"), |
|
0 commit comments