analytics-client
Advanced tools
Comparing version 1.4.0-new-declareClient-function-4ed072f83e0083c60ef7d34fdf031cfe96b19840 to 1.4.0-new-declareClient-function-d255ef076e3c1ed3ccce6c0e545f132babb293db
@@ -8,5 +8,5 @@ # Change Log | ||
# v1.4.0 | ||
## (2021-03-03) | ||
## (2021-03-29) | ||
* Created new declareClient function un url-params [Ezequiel Boehler] | ||
* Update setClient() to work with new init method [Ezequiel Boehler] | ||
@@ -13,0 +13,0 @@ # v1.3.0 |
{ | ||
"name": "analytics-client", | ||
"version": "1.4.0-new-declareClient-function-4ed072f83e0083c60ef7d34fdf031cfe96b19840", | ||
"version": "1.4.0-new-declareClient-function-d255ef076e3c1ed3ccce6c0e545f132babb293db", | ||
"description": "Convenient builders to compose analytics tools", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -14,2 +14,3 @@ import { Client } from './client'; | ||
setClient(client: Client): void; | ||
getClient(): Client | undefined; | ||
allDeviceIds(): string[]; | ||
@@ -20,4 +21,4 @@ getPassedDeviceId(): string | undefined; | ||
getSessionIdQueryString(): string; | ||
getQueryString(): string; | ||
getQueryString(destinationUrl?: URL, currentUrl?: URL): string; | ||
isOptOutRequested(): boolean; | ||
} |
@@ -66,10 +66,11 @@ "use strict"; | ||
} | ||
if (!this.client && this.sessionId) { | ||
this.client = client; | ||
this.client.setSessionId(this.sessionId); | ||
return; | ||
if (this.getPassedDeviceId() && | ||
this.getPassedDeviceId() !== client.deviceId()) { | ||
var newDeviceId = this.setDeviceIds(null, client.deviceId()); | ||
if (newDeviceId != null) { | ||
client.setDeviceId(newDeviceId); | ||
} | ||
} | ||
var newDeviceId = this.setDeviceIds(null, client.deviceId()); | ||
if (newDeviceId != null) { | ||
client.setDeviceId(newDeviceId); | ||
if (this.sessionId && client.sessionId() !== this.sessionId) { | ||
client.setSessionId(this.sessionId); | ||
} | ||
@@ -79,2 +80,5 @@ this.sessionId = client.sessionId(); | ||
}; | ||
AnalyticsUrlParams.prototype.getClient = function () { | ||
return this.client; | ||
}; | ||
AnalyticsUrlParams.prototype.allDeviceIds = function () { | ||
@@ -109,6 +113,29 @@ var currentId = this.client != null ? this.client.deviceId() : null; | ||
}; | ||
AnalyticsUrlParams.prototype.getQueryString = function () { | ||
return [this.getDeviceIdsQueryString(), this.getSessionIdQueryString()] | ||
.filter(function (x) { return x; }) | ||
.join('&'); | ||
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 ''; | ||
}; | ||
@@ -115,0 +142,0 @@ AnalyticsUrlParams.prototype.isOptOutRequested = function () { |
@@ -70,2 +70,13 @@ "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 () { | ||
@@ -78,3 +89,6 @@ return ({ | ||
this.deviceIdRetrieved = true; | ||
return 'test_device_id'; | ||
if (!this.setDeviceIdParams) { | ||
return 'test_device_id'; | ||
} | ||
return this.setDeviceIdParams; | ||
}, | ||
@@ -166,8 +180,38 @@ sessionId: function () { | ||
expect(urlParams.isOptOutRequested()).toBeFalsy(); | ||
var passedDeviceId = urlParams.getPassedDeviceId(); | ||
var client = clientMock(); | ||
var initialDeviceId = client.deviceId(); | ||
if (passedDeviceId) { | ||
client.setDeviceId(passedDeviceId); | ||
} | ||
urlParams.setClient(client); | ||
expect(client.deviceIdRetrieved).toBeTruthy(); | ||
expect(client.setDeviceIdParams).toStrictEqual(initialDeviceId); | ||
var allDeviceIds = urlParams.allDeviceIds(); | ||
expect(client.setDeviceIdParams).toStrictEqual(null); | ||
expect(allDeviceIds).toEqual(['test_device_id']); | ||
expect(client.knownSessionId).toStrictEqual(123); | ||
}); | ||
test('set default client with passed sessionId', function () { | ||
var urlParams = new url_params_1.AnalyticsUrlParams(); | ||
urlParams.consumeUrlParameters('s_id=999&optOutAnalytics=false'); | ||
expect(urlParams.isOptOutRequested()).toBeFalsy(); | ||
var passedSessonId = urlParams.getSessionId(); | ||
var client = clientMock(); | ||
urlParams.setClient(client); | ||
expect(client.setDeviceIdParams).toStrictEqual(null); | ||
expect(client.knownSessionId).toStrictEqual(passedSessonId); | ||
}); | ||
test('set default client with passed deviceId', function () { | ||
var urlParams = new url_params_1.AnalyticsUrlParams(); | ||
urlParams.consumeUrlParameters('d_id=999,888,777'); | ||
var passedDeviceId = urlParams.getPassedDeviceId(); | ||
var client = clientMock(); | ||
if (passedDeviceId) { | ||
client.setDeviceId(passedDeviceId); | ||
} | ||
var allDeviceIds = urlParams.allDeviceIds(); | ||
urlParams.setClient(client); | ||
expect(client.setDeviceIdParams).toStrictEqual('999'); | ||
expect(allDeviceIds).toEqual(['999', '888', '777']); | ||
expect(client.knownSessionId).toStrictEqual(123); | ||
expect(urlParams.getClient()).toEqual(client); | ||
}); | ||
//# sourceMappingURL=url-params.test.js.map |
{ | ||
"name": "analytics-client", | ||
"version": "1.4.0-new-declareClient-function-4ed072f83e0083c60ef7d34fdf031cfe96b19840", | ||
"version": "1.4.0-new-declareClient-function-d255ef076e3c1ed3ccce6c0e545f132babb293db", | ||
"description": "Convenient builders to compose analytics tools", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -45,3 +45,3 @@ Analytics client | ||
// A newly created client is passed to the `AnalyticsUrlParams` to make it fully functional. | ||
urlParamsHandler.declareClient(client); | ||
urlParamsHandler.setClient(client); | ||
``` | ||
@@ -48,0 +48,0 @@ |
@@ -106,12 +106,13 @@ import * as Cookies from 'js-cookie'; | ||
} | ||
if (!this.client && this.sessionId) { | ||
this.client = client; | ||
this.client.setSessionId(this.sessionId); | ||
return; | ||
if ( | ||
this.getPassedDeviceId() && | ||
this.getPassedDeviceId() !== client.deviceId() | ||
) { | ||
const newDeviceId = this.setDeviceIds(null, client.deviceId()); | ||
if (newDeviceId != null) { | ||
client.setDeviceId(newDeviceId); | ||
} | ||
} | ||
const newDeviceId = this.setDeviceIds(null, client.deviceId()); | ||
if (newDeviceId != null) { | ||
client.setDeviceId(newDeviceId); | ||
if (this.sessionId && client.sessionId() !== this.sessionId) { | ||
client.setSessionId(this.sessionId); | ||
} | ||
@@ -122,2 +123,6 @@ this.sessionId = client.sessionId(); | ||
getClient() { | ||
return this.client; | ||
} | ||
/** | ||
@@ -175,6 +180,33 @@ * @return all anonymous device IDs that can be passed to other sites | ||
*/ | ||
getQueryString(): string { | ||
return [this.getDeviceIdsQueryString(), this.getSessionIdQueryString()] | ||
.filter(x => x) | ||
.join('&'); | ||
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 ''; | ||
} | ||
@@ -181,0 +213,0 @@ |
@@ -105,2 +105,39 @@ 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 { | ||
@@ -120,3 +157,6 @@ setDeviceIdParams: string | null; | ||
this.deviceIdRetrieved = true; | ||
return 'test_device_id'; | ||
if (!this.setDeviceIdParams) { | ||
return 'test_device_id'; | ||
} | ||
return this.setDeviceIdParams; | ||
}, | ||
@@ -230,7 +270,43 @@ sessionId() { | ||
const passedDeviceId = urlParams.getPassedDeviceId(); | ||
const client = clientMock(); | ||
const initialDeviceId = client.deviceId(); | ||
if (passedDeviceId) { | ||
client.setDeviceId(passedDeviceId); | ||
} | ||
urlParams.setClient(client); | ||
expect(client.deviceIdRetrieved).toBeTruthy(); | ||
expect(client.setDeviceIdParams).toStrictEqual(initialDeviceId); | ||
const allDeviceIds = urlParams.allDeviceIds(); | ||
expect(client.setDeviceIdParams).toStrictEqual(null); | ||
expect(allDeviceIds).toEqual(['test_device_id']); | ||
expect(client.knownSessionId).toStrictEqual(123); | ||
}); | ||
test('set default client with passed sessionId', () => { | ||
const urlParams = new AnalyticsUrlParams(); | ||
urlParams.consumeUrlParameters('s_id=999&optOutAnalytics=false'); | ||
expect(urlParams.isOptOutRequested()).toBeFalsy(); | ||
const passedSessonId = urlParams.getSessionId(); | ||
const client = clientMock(); | ||
urlParams.setClient(client); | ||
expect(client.setDeviceIdParams).toStrictEqual(null); | ||
expect(client.knownSessionId).toStrictEqual(passedSessonId); | ||
}); | ||
test('set default client with passed deviceId', () => { | ||
const urlParams = new AnalyticsUrlParams(); | ||
urlParams.consumeUrlParameters('d_id=999,888,777'); | ||
const passedDeviceId = urlParams.getPassedDeviceId(); | ||
const client = clientMock(); | ||
if (passedDeviceId) { | ||
client.setDeviceId(passedDeviceId); | ||
} | ||
const allDeviceIds = urlParams.allDeviceIds(); | ||
urlParams.setClient(client); | ||
expect(client.setDeviceIdParams).toStrictEqual('999'); | ||
expect(allDeviceIds).toEqual(['999', '888', '777']); | ||
expect(client.knownSessionId).toStrictEqual(123); | ||
expect(urlParams.getClient()).toEqual(client); | ||
}); |
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
312440
2569