analytics-client
Advanced tools
Comparing version 1.4.0-getQueryString-changes-f19973daf1f7e8eccaa33d5a03b5d70ae5fe8a0a to 1.4.0-new-declareClient-function-f8afc0f0c179fd831ae4d1af70a70298610d5849
@@ -8,5 +8,5 @@ # Change Log | ||
# v1.4.0 | ||
## (2021-02-26) | ||
## (2021-03-01) | ||
* 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-f8afc0f0c179fd831ae4d1af70a70298610d5849", | ||
"description": "Convenient builders to compose analytics tools", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -14,2 +14,3 @@ import { Client } from './client'; | ||
setClient(client: Client): void; | ||
declareClient(client: Client): void; | ||
allDeviceIds(): string[]; | ||
@@ -20,4 +21,4 @@ getPassedDeviceId(): string | undefined; | ||
getSessionIdQueryString(): string; | ||
getQueryString(destinationUrl?: URL, currentUrl?: URL): string; | ||
getQueryString(): string; | ||
isOptOutRequested(): boolean; | ||
} |
@@ -73,2 +73,5 @@ "use strict"; | ||
}; | ||
AnalyticsUrlParams.prototype.declareClient = function (client) { | ||
this.client = client; | ||
}; | ||
AnalyticsUrlParams.prototype.allDeviceIds = function () { | ||
@@ -103,29 +106,6 @@ var currentId = this.client != null ? this.client.deviceId() : 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 +112,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-f8afc0f0c179fd831ae4d1af70a70298610d5849", | ||
"description": "Convenient builders to compose analytics tools", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -43,2 +43,5 @@ Analytics client | ||
}) | ||
//And at last, we pass this newly created client to the AnalyticsUrlParams since its needed for some functionalities | ||
urlParamsHandler.declareClient(client); | ||
``` | ||
@@ -45,0 +48,0 @@ |
@@ -115,2 +115,11 @@ import * as Cookies from 'js-cookie'; | ||
/** | ||
* | ||
* @param client | ||
* Sets the class client value to whatever client object is passed on the argument. | ||
*/ | ||
declareClient(client: Client) { | ||
this.client = client; | ||
} | ||
/** | ||
* @return all anonymous device IDs that can be passed to other sites | ||
@@ -167,33 +176,6 @@ */ | ||
*/ | ||
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 +182,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
301990
2404