analytics-client
Advanced tools
Comparing version 1.4.0-getQueryString-changes-f19973daf1f7e8eccaa33d5a03b5d70ae5fe8a0a to 1.4.0-new-declareClient-function-15d28c71e2f9479aa6c0ee67757b884c0a7e2d38
@@ -8,5 +8,5 @@ # Change Log | ||
# v1.4.0 | ||
## (2021-02-26) | ||
## (2021-03-03) | ||
* Changed getQueryString to validate domains [Ezequiel Boehler] | ||
* Created new declareClient function un url-params [Ezequiel Boehler] | ||
@@ -13,0 +13,0 @@ # v1.3.0 |
{ | ||
"name": "analytics-client", | ||
"version": "1.4.0-getQueryString-changes-f19973daf1f7e8eccaa33d5a03b5d70ae5fe8a0a", | ||
"version": "1.4.0-new-declareClient-function-15d28c71e2f9479aa6c0ee67757b884c0a7e2d38", | ||
"description": "Convenient builders to compose analytics tools", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -19,4 +19,4 @@ import { Client } from './client'; | ||
getSessionIdQueryString(): string; | ||
getQueryString(destinationUrl?: URL, currentUrl?: URL): string; | ||
getQueryString(): string; | ||
isOptOutRequested(): boolean; | ||
} |
@@ -66,2 +66,6 @@ "use strict"; | ||
} | ||
if (!this.client && client.sessionId() === this.sessionId) { | ||
this.client = client; | ||
return; | ||
} | ||
var newDeviceId = this.setDeviceIds(null, client.deviceId()); | ||
@@ -103,29 +107,6 @@ if (newDeviceId != null) { | ||
}; | ||
AnalyticsUrlParams.prototype.getQueryString = function (destinationUrl, currentUrl) { | ||
var regex = /([a-zA-Z0-9-]+)(\.[a-zA-Z]{2,3})?(\.[a-zA-Z]+$)/g; | ||
var actualDomainMatch; | ||
if (currentUrl) { | ||
actualDomainMatch = currentUrl.hostname.match(regex); | ||
} | ||
else if (typeof window !== undefined) { | ||
actualDomainMatch = window.location.hostname.match(regex); | ||
} | ||
else { | ||
actualDomainMatch = null; | ||
} | ||
var destinationDomainMatch = destinationUrl | ||
? destinationUrl.hostname.match(regex) | ||
: null; | ||
var actualDomain = actualDomainMatch | ||
? actualDomainMatch.toString() | ||
: null; | ||
var destinationDomain = destinationDomainMatch | ||
? destinationDomainMatch.toString() | ||
: null; | ||
if (!destinationDomain || actualDomain !== destinationDomain) { | ||
return [this.getDeviceIdsQueryString(), this.getSessionIdQueryString()] | ||
.filter(function (x) { return x; }) | ||
.join('&'); | ||
} | ||
return ''; | ||
AnalyticsUrlParams.prototype.getQueryString = function () { | ||
return [this.getDeviceIdsQueryString(), this.getSessionIdQueryString()] | ||
.filter(function (x) { return x; }) | ||
.join('&'); | ||
}; | ||
@@ -132,0 +113,0 @@ AnalyticsUrlParams.prototype.isOptOutRequested = function () { |
@@ -70,13 +70,2 @@ "use strict"; | ||
}); | ||
test('parsing and matching destination and actual URL to regex', function () { | ||
var urlParams = new url_params_1.AnalyticsUrlParams(); | ||
expect(urlParams.getQueryString()).toBe(''); | ||
urlParams.consumeUrlParameters('s_id=123&other=value'); | ||
expect(urlParams.getQueryString()).toBe('s_id=123'); | ||
urlParams.consumeUrlParameters('d_id=d1&other=value'); | ||
expect(urlParams.getQueryString()).toBe('d_id=d1&s_id=123'); | ||
expect(urlParams.getQueryString(new URL('https://test.domain.io'), new URL('https://domain.io'))).toBe(''); | ||
expect(urlParams.getQueryString(new URL('https://test.domain.io'), new URL('https://otherdomain.com'))).toBe('d_id=d1&s_id=123'); | ||
expect(urlParams.getQueryString(new URL('https://test.domain.edge.io'), new URL('https://domain2.edge.io'))).toBe(''); | ||
}); | ||
var clientMock = function () { | ||
@@ -83,0 +72,0 @@ return ({ |
{ | ||
"name": "analytics-client", | ||
"version": "1.4.0-getQueryString-changes-f19973daf1f7e8eccaa33d5a03b5d70ae5fe8a0a", | ||
"version": "1.4.0-new-declareClient-function-15d28c71e2f9479aa6c0ee67757b884c0a7e2d38", | ||
"description": "Convenient builders to compose analytics tools", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -43,2 +43,5 @@ Analytics client | ||
}) | ||
// A newly created client is passed to the `AnalyticsUrlParams` to make it fully functional. | ||
urlParamsHandler.declareClient(client); | ||
``` | ||
@@ -45,0 +48,0 @@ |
@@ -106,2 +106,8 @@ import * as Cookies from 'js-cookie'; | ||
} | ||
if (!this.client && client.sessionId() === this.sessionId) { | ||
this.client = client; | ||
return; | ||
} | ||
const newDeviceId = this.setDeviceIds(null, client.deviceId()); | ||
@@ -167,33 +173,6 @@ if (newDeviceId != null) { | ||
*/ | ||
getQueryString(destinationUrl?: URL, currentUrl?: URL): string { | ||
// this regex is based on the assumption that we wont be using TLDs longer than 3 characters. If we do, it will break | ||
// the logic and take that longer TLD as the main domain, for example hub.balena.edge.io -> edge.io | ||
const regex = /([a-zA-Z0-9-]+)(\.[a-zA-Z]{2,3})?(\.[a-zA-Z]+$)/g; | ||
let actualDomainMatch; | ||
if (currentUrl) { | ||
actualDomainMatch = currentUrl.hostname.match(regex); | ||
} else if (typeof window !== undefined) { | ||
actualDomainMatch = window.location.hostname.match(regex); | ||
} else { | ||
actualDomainMatch = null; | ||
} | ||
const destinationDomainMatch = destinationUrl | ||
? destinationUrl.hostname.match(regex) | ||
: null; | ||
const actualDomain = actualDomainMatch | ||
? actualDomainMatch.toString() | ||
: null; | ||
const destinationDomain = destinationDomainMatch | ||
? destinationDomainMatch.toString() | ||
: null; | ||
if (!destinationDomain || actualDomain !== destinationDomain) { | ||
return [this.getDeviceIdsQueryString(), this.getSessionIdQueryString()] | ||
.filter(x => x) | ||
.join('&'); | ||
} | ||
return ''; | ||
getQueryString(): string { | ||
return [this.getDeviceIdsQueryString(), this.getSessionIdQueryString()] | ||
.filter(x => x) | ||
.join('&'); | ||
} | ||
@@ -200,0 +179,0 @@ |
@@ -105,39 +105,2 @@ import { Client, createNoopClient } from '../src/client'; | ||
test('parsing and matching destination and actual URL to regex', () => { | ||
const urlParams = new AnalyticsUrlParams(); | ||
expect(urlParams.getQueryString()).toBe(''); | ||
urlParams.consumeUrlParameters('s_id=123&other=value'); | ||
expect(urlParams.getQueryString()).toBe('s_id=123'); | ||
// Case when not passing any destination or actual URL | ||
urlParams.consumeUrlParameters('d_id=d1&other=value'); | ||
expect(urlParams.getQueryString()).toBe('d_id=d1&s_id=123'); | ||
// Case when passing matching destination and actual URL while a d_id and s_id exist. | ||
expect( | ||
urlParams.getQueryString( | ||
new URL('https://test.domain.io'), | ||
new URL('https://domain.io'), | ||
), | ||
).toBe(''); | ||
// Case when passing none matching destination and actual URL while a d_id and s_id exist. | ||
expect( | ||
urlParams.getQueryString( | ||
new URL('https://test.domain.io'), | ||
new URL('https://otherdomain.com'), | ||
), | ||
).toBe('d_id=d1&s_id=123'); | ||
// Case when passing destination URL that violates the TLD regex assumption. Here it will result in matching URL "edge.io", when in fact | ||
// It should be treated as different URLs and return the d_id and s_id params | ||
expect( | ||
urlParams.getQueryString( | ||
new URL('https://test.domain.edge.io'), | ||
new URL('https://domain2.edge.io'), | ||
), | ||
).toBe(''); | ||
}); | ||
interface AnalyticsMock { | ||
@@ -144,0 +107,0 @@ setDeviceIdParams: string | null; |
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
172
301952
2400