analytics-client
Advanced tools
Comparing version 1.6.0 to 1.7.0-getQueryString-expect-relative-url-52e2284b7f6f44a1aa310470454cb3c515ad20a7
@@ -7,4 +7,9 @@ # Change Log | ||
# v1.7.0 | ||
## (2021-08-13) | ||
* Get getQueryString to expect relative URLs too [Ezequiel Boehler] | ||
# v1.6.0 | ||
## (2021-04-23) | ||
## (2021-04-22) | ||
@@ -11,0 +16,0 @@ * Added unsetParamsReferrerOnNewSession to client [Ezequiel Boehler] |
{ | ||
"name": "analytics-client", | ||
"version": "1.6.0", | ||
"version": "1.7.0-getQueryString-expect-relative-url-52e2284b7f6f44a1aa310470454cb3c515ad20a7", | ||
"description": "Convenient builders to compose analytics tools", | ||
@@ -31,2 +31,3 @@ "repository": { | ||
"@types/mixpanel-browser": "2.35.1", | ||
"@types/node": "^16.6.1", | ||
"husky": "^3.0.9", | ||
@@ -54,3 +55,6 @@ "jest": "^24.9.0", | ||
} | ||
}, | ||
"versionist": { | ||
"publishedAt": "2021-08-13T15:43:39.782Z" | ||
} | ||
} |
@@ -20,4 +20,4 @@ import { Client } from './client'; | ||
getSessionIdQueryString(): string; | ||
getQueryString(destinationUrl?: URL, currentUrl?: URL): string; | ||
getQueryString(destinationUrl?: URL | string, currentUrl?: URL): string; | ||
isOptOutRequested(): boolean; | ||
} |
@@ -112,3 +112,21 @@ "use strict"; | ||
AnalyticsUrlParams.prototype.getQueryString = function (destinationUrl, currentUrl) { | ||
var relativeRegex = /^(?!www\.|(?:http|ftp)s?:\/\/|[A-Za-z]:\\|\/\/).*/g; | ||
if (typeof destinationUrl === 'string' && | ||
destinationUrl.match(relativeRegex)) { | ||
return ''; | ||
} | ||
if (typeof destinationUrl === 'string') { | ||
try { | ||
destinationUrl = new URL(destinationUrl); | ||
} | ||
catch (err) { | ||
console.error(err); | ||
return ''; | ||
} | ||
} | ||
var regex = /([a-zA-Z0-9-]+)(\.[a-zA-Z]{2,3})?(\.[a-zA-Z]+$)/g; | ||
var destinationDomainMatch = destinationUrl | ||
? destinationUrl.hostname.match(regex) | ||
: null; | ||
var destinationDomain = destinationDomainMatch === null || destinationDomainMatch === void 0 ? void 0 : destinationDomainMatch[0]; | ||
var actualDomainMatch; | ||
@@ -124,11 +142,3 @@ if (currentUrl) { | ||
} | ||
var destinationDomainMatch = destinationUrl | ||
? destinationUrl.hostname.match(regex) | ||
: null; | ||
var actualDomain = actualDomainMatch | ||
? actualDomainMatch.toString() | ||
: null; | ||
var destinationDomain = destinationDomainMatch | ||
? destinationDomainMatch.toString() | ||
: null; | ||
var actualDomain = actualDomainMatch === null || actualDomainMatch === void 0 ? void 0 : actualDomainMatch[0]; | ||
if (!destinationDomain || actualDomain !== destinationDomain) { | ||
@@ -135,0 +145,0 @@ return [this.getDeviceIdsQueryString(), this.getSessionIdQueryString()] |
@@ -80,2 +80,5 @@ "use strict"; | ||
expect(urlParams.getQueryString(new URL('https://test.domain.edge.io'), new URL('https://domain2.edge.io'))).toBe(''); | ||
expect(urlParams.getQueryString('/etcher', new URL('https://domain2.edge.io'))).toBe(''); | ||
expect(urlParams.getQueryString('https://test.domain.io', new URL('https://domain.io'))).toBe(''); | ||
expect(urlParams.getQueryString('https://test.domain.io', new URL('https://otherdomain.com'))).toBe('d_id=d1&s_id=123'); | ||
}); | ||
@@ -82,0 +85,0 @@ var clientMock = function () { |
{ | ||
"name": "analytics-client", | ||
"version": "1.6.0", | ||
"version": "1.7.0-getQueryString-expect-relative-url-52e2284b7f6f44a1aa310470454cb3c515ad20a7", | ||
"description": "Convenient builders to compose analytics tools", | ||
@@ -31,2 +31,3 @@ "repository": { | ||
"@types/mixpanel-browser": "2.35.1", | ||
"@types/node": "^16.6.1", | ||
"husky": "^3.0.9", | ||
@@ -54,3 +55,6 @@ "jest": "^24.9.0", | ||
} | ||
}, | ||
"versionist": { | ||
"publishedAt": "2021-08-13T15:43:39.782Z" | ||
} | ||
} |
@@ -178,3 +178,21 @@ import * as Cookies from 'js-cookie'; | ||
*/ | ||
getQueryString(destinationUrl?: URL, currentUrl?: URL): string { | ||
getQueryString(destinationUrl?: URL | string, currentUrl?: URL): string { | ||
// we first check if destionationUrl is a relative URL string. If it is, we exit. | ||
const relativeRegex = /^(?!www\.|(?:http|ftp)s?:\/\/|[A-Za-z]:\\|\/\/).*/g; | ||
if ( | ||
typeof destinationUrl === 'string' && | ||
destinationUrl.match(relativeRegex) | ||
) { | ||
return ''; | ||
} | ||
if (typeof destinationUrl === 'string') { | ||
try { | ||
destinationUrl = new URL(destinationUrl); | ||
} catch (err) { | ||
console.error(err); | ||
return ''; | ||
} | ||
} | ||
// this regex is based on the assumption that we wont be using TLDs longer than 3 characters. If we do, it will break | ||
@@ -184,3 +202,8 @@ // the logic and take that longer TLD as the main domain, for example hub.balena.edge.io -> edge.io | ||
let actualDomainMatch; | ||
const destinationDomainMatch = destinationUrl | ||
? destinationUrl.hostname.match(regex) | ||
: null; | ||
const destinationDomain = destinationDomainMatch?.[0]; | ||
let actualDomainMatch: RegExpMatchArray | null; | ||
if (currentUrl) { | ||
@@ -194,13 +217,4 @@ actualDomainMatch = currentUrl.hostname.match(regex); | ||
const destinationDomainMatch = destinationUrl | ||
? destinationUrl.hostname.match(regex) | ||
: null; | ||
const actualDomain = actualDomainMatch?.[0]; | ||
const actualDomain = actualDomainMatch | ||
? actualDomainMatch.toString() | ||
: null; | ||
const destinationDomain = destinationDomainMatch | ||
? destinationDomainMatch.toString() | ||
: null; | ||
if (!destinationDomain || actualDomain !== destinationDomain) { | ||
@@ -207,0 +221,0 @@ return [this.getDeviceIdsQueryString(), this.getSessionIdQueryString()] |
@@ -140,2 +140,23 @@ import { Client, createNoopClient } from '../src/client'; | ||
).toBe(''); | ||
// Case when passing a relative URL as destinationUrl | ||
expect( | ||
urlParams.getQueryString('/etcher', new URL('https://domain2.edge.io')), | ||
).toBe(''); | ||
// Case when passing an absolute URL as a string for destinationUrl and no passing is expected | ||
expect( | ||
urlParams.getQueryString( | ||
'https://test.domain.io', | ||
new URL('https://domain.io'), | ||
), | ||
).toBe(''); | ||
// Case when passing an absolute URL as a string for destinationUrl and passing is expected | ||
expect( | ||
urlParams.getQueryString( | ||
'https://test.domain.io', | ||
new URL('https://otherdomain.com'), | ||
), | ||
).toBe('d_id=d1&s_id=123'); | ||
}); | ||
@@ -142,0 +163,0 @@ |
{ | ||
"compilerOptions": { | ||
"lib": [ | ||
"ES6","DOM" | ||
], | ||
"module": "commonjs", | ||
@@ -4,0 +7,0 @@ "target": "es5", |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
315884
2617
14
2